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.
 
 
 

62 lines
2.1 KiB

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