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.
 
 
 

152 regels
4.5 KiB

  1. using System;
  2. using System.Reflection;
  3. using System.Threading.Tasks;
  4. using DotNetCore.CAP.Abstractions;
  5. using DotNetCore.CAP.Infrastructure;
  6. using DotNetCore.CAP.Messages;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.Logging;
  9. using Microsoft.Extensions.Logging.Abstractions;
  10. using Moq;
  11. using Xunit;
  12. namespace DotNetCore.CAP.Test
  13. {
  14. public class ConsumerInvokerTest
  15. {
  16. private ILoggerFactory _loggerFactory;
  17. private Mock<IMessagePacker> _mockMessagePacker;
  18. private Mock<IModelBinderFactory> _mockModelBinderFactory;
  19. private MessageContext _messageContext;
  20. public ConsumerInvokerTest()
  21. {
  22. _loggerFactory = new NullLoggerFactory();
  23. _mockMessagePacker = new Mock<IMessagePacker>();
  24. _mockModelBinderFactory = new Mock<IModelBinderFactory>();
  25. }
  26. private Internal.DefaultConsumerInvoker InitDefaultConsumerInvoker(IServiceProvider provider)
  27. {
  28. var invoker = new Internal.DefaultConsumerInvoker(
  29. _loggerFactory,
  30. provider,
  31. _mockMessagePacker.Object,
  32. _mockModelBinderFactory.Object);
  33. var message = new CapReceivedMessage
  34. {
  35. Id = SnowflakeId.Default().NextId(),
  36. Name = "test",
  37. Content = DateTime.Now.ToString(),
  38. StatusName = StatusName.Scheduled,
  39. Group = "Group.Test"
  40. };
  41. _mockMessagePacker
  42. .Setup(x => x.UnPack(It.IsAny<string>()))
  43. .Returns(new CapMessageDto(message.Content));
  44. _messageContext = new MessageContext
  45. {
  46. Group = message.Group,
  47. Name = message.Name,
  48. Content = Helper.ToJson(message)
  49. };
  50. return invoker;
  51. }
  52. [Fact]
  53. public async Task CanInvokeServiceTest()
  54. {
  55. var services = new ServiceCollection();
  56. services.AddSingleton<ITestService, TestService2>();
  57. services.AddSingleton<ITestService, TestService>();
  58. var provider = services.BuildServiceProvider();
  59. var invoker = InitDefaultConsumerInvoker(provider);
  60. var descriptor = new ConsumerExecutorDescriptor
  61. {
  62. ServiceTypeInfo = typeof(ITestService).GetTypeInfo(),
  63. ImplTypeInfo = typeof(TestService).GetTypeInfo(),
  64. MethodInfo = typeof(TestService).GetMethod("Index")
  65. };
  66. descriptor.Attribute = descriptor.MethodInfo.GetCustomAttribute<TopicAttribute>(true);
  67. var context = new Internal.ConsumerContext(descriptor, _messageContext);
  68. var result = await invoker.InvokeAsync(context);
  69. Assert.NotNull(result);
  70. Assert.NotNull(result.Result);
  71. Assert.Equal("test", result.Result.ToString());
  72. }
  73. [Fact]
  74. public async Task CanInvokeControllerTest()
  75. {
  76. var services = new ServiceCollection();
  77. var provider = services.BuildServiceProvider();
  78. var invoker = InitDefaultConsumerInvoker(provider);
  79. var descriptor = new ConsumerExecutorDescriptor
  80. {
  81. ServiceTypeInfo = typeof(HomeController).GetTypeInfo(),
  82. ImplTypeInfo = typeof(HomeController).GetTypeInfo(),
  83. MethodInfo = typeof(HomeController).GetMethod("Index")
  84. };
  85. descriptor.Attribute = descriptor.MethodInfo.GetCustomAttribute<TopicAttribute>(true);
  86. var context = new Internal.ConsumerContext(descriptor, _messageContext);
  87. var result = await invoker.InvokeAsync(context);
  88. Assert.NotNull(result);
  89. Assert.NotNull(result.Result);
  90. Assert.Equal("test", result.Result.ToString());
  91. }
  92. }
  93. public class HomeController
  94. {
  95. [CapSubscribe("test")]
  96. public string Index()
  97. {
  98. return "test";
  99. }
  100. }
  101. public interface ITestService { }
  102. public class TestService : ITestService, ICapSubscribe
  103. {
  104. [CapSubscribe("test")]
  105. public string Index()
  106. {
  107. return "test";
  108. }
  109. }
  110. public class TestService2 : ITestService
  111. {
  112. [CapSubscribe("test")]
  113. public string Index()
  114. {
  115. return "test2";
  116. }
  117. }
  118. public class CapSubscribeAttribute : TopicAttribute
  119. {
  120. public CapSubscribeAttribute(string name) : base(name)
  121. {
  122. }
  123. }
  124. }