You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

73 regels
2.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using DotNetCore.CAP.Internal;
  7. using DotNetCore.CAP.Messages;
  8. using DotNetCore.CAP.Serialization;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Xunit;
  11. namespace DotNetCore.CAP.Test
  12. {
  13. public class SubscribeInvokerWithCancellation
  14. {
  15. private readonly IServiceProvider _serviceProvider;
  16. public SubscribeInvokerWithCancellation()
  17. {
  18. var serviceCollection = new ServiceCollection();
  19. serviceCollection.AddLogging();
  20. serviceCollection.AddSingleton<IBootstrapper, Bootstrapper>();
  21. serviceCollection.AddSingleton<ISerializer, JsonUtf8Serializer>();
  22. serviceCollection.AddSingleton<ISubscribeInvoker, SubscribeInvoker>();
  23. _serviceProvider = serviceCollection.BuildServiceProvider();
  24. }
  25. private ISubscribeInvoker SubscribeInvoker => _serviceProvider.GetService<ISubscribeInvoker>();
  26. [Fact]
  27. public async Task InvokeTest()
  28. {
  29. var descriptor = new ConsumerExecutorDescriptor()
  30. {
  31. Attribute = new CandidatesTopic("fake.output.withcancellation"),
  32. ServiceTypeInfo = typeof(FakeSubscriberWithCancellation).GetTypeInfo(),
  33. ImplTypeInfo = typeof(FakeSubscriberWithCancellation).GetTypeInfo(),
  34. MethodInfo = typeof(FakeSubscriberWithCancellation)
  35. .GetMethod(nameof(FakeSubscriberWithCancellation.CancellationTokenInjected)),
  36. Parameters = new List<ParameterDescriptor>
  37. {
  38. new ParameterDescriptor {
  39. ParameterType = typeof(CancellationToken),
  40. IsFromCap = true,
  41. Name = "cancellationToken"
  42. }
  43. }
  44. };
  45. var header = new Dictionary<string, string>()
  46. {
  47. [Headers.MessageId] = SnowflakeId.Default().NextId().ToString(),
  48. [Headers.MessageName] = "fake.output.withcancellation"
  49. };
  50. var message = new Message(header, null);
  51. var context = new ConsumerContext(descriptor, message);
  52. var cancellationToken = new CancellationToken();
  53. var ret = await SubscribeInvoker.InvokeAsync(context, cancellationToken);
  54. Assert.Equal(cancellationToken, ret.Result);
  55. }
  56. }
  57. public class FakeSubscriberWithCancellation : ICapSubscribe
  58. {
  59. [CapSubscribe("fake.output.withcancellation")]
  60. public CancellationToken CancellationTokenInjected(CancellationToken cancellationToken)
  61. {
  62. return cancellationToken;
  63. }
  64. }
  65. }