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.
 
 
 

69 line
2.6 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. var message = new Message(header, null);
  47. var context = new ConsumerContext(descriptor, message);
  48. var cancellationToken = new CancellationToken();
  49. var ret = await SubscribeInvoker.InvokeAsync(context, cancellationToken);
  50. Assert.Equal(cancellationToken, ret.Result);
  51. }
  52. }
  53. public class FakeSubscriberWithCancellation : ICapSubscribe
  54. {
  55. [CapSubscribe("fake.output.withcancellation")]
  56. public CancellationToken CancellationTokenInjected(CancellationToken cancellationToken)
  57. {
  58. return cancellationToken;
  59. }
  60. }
  61. }