Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

183 строки
5.7 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using DotNetCore.CAP.Internal;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Xunit;
  6. namespace DotNetCore.CAP.Test
  7. {
  8. public class ConsumerServiceSelectorTest
  9. {
  10. private readonly IServiceProvider _provider;
  11. public ConsumerServiceSelectorTest()
  12. {
  13. var services = new ServiceCollection();
  14. ServiceCollectionExtensions.ServiceCollection = services;
  15. services.AddOptions();
  16. services.PostConfigure<CapOptions>(x=>{});
  17. services.AddSingleton<IConsumerServiceSelector, ConsumerServiceSelector>();
  18. services.AddScoped<IFooTest, CandidatesFooTest>();
  19. services.AddScoped<IBarTest, CandidatesBarTest>();
  20. services.AddLogging();
  21. _provider = services.BuildServiceProvider();
  22. }
  23. [Fact]
  24. public void CanFindAllConsumerService()
  25. {
  26. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  27. var candidates = selector.SelectCandidates();
  28. Assert.Equal(6, candidates.Count);
  29. }
  30. [Fact]
  31. public void CanFindSpecifiedTopic()
  32. {
  33. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  34. var candidates = selector.SelectCandidates();
  35. var bestCandidates = selector.SelectBestCandidate("Candidates.Foo", candidates);
  36. Assert.NotNull(bestCandidates);
  37. Assert.NotNull(bestCandidates.MethodInfo);
  38. Assert.Equal(typeof(Task), bestCandidates.MethodInfo.ReturnType);
  39. }
  40. [Theory]
  41. [InlineData("Candidates.Asterisk")]
  42. [InlineData("candidates.Asterisk")]
  43. [InlineData("AAA.BBB.Asterisk")]
  44. [InlineData("aaa.bbb.Asterisk")]
  45. public void CanFindAsteriskTopic(string topic)
  46. {
  47. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  48. var candidates = selector.SelectCandidates();
  49. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  50. Assert.NotNull(bestCandidates);
  51. }
  52. [Theory]
  53. [InlineData("Candidates.Asterisk.AAA")]
  54. [InlineData("AAA.BBB.CCC.Asterisk")]
  55. [InlineData("aaa.BBB.ccc.Asterisk")]
  56. public void CanNotFindAsteriskTopic(string topic)
  57. {
  58. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  59. var candidates = selector.SelectCandidates();
  60. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  61. Assert.Null(bestCandidates);
  62. }
  63. [Theory]
  64. [InlineData("Asterisk.aaa.bbb")]
  65. public void CanNotFindAsteriskTopic2(string topic)
  66. {
  67. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  68. var candidates = selector.SelectCandidates();
  69. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  70. Assert.Null(bestCandidates);
  71. }
  72. [Theory]
  73. [InlineData("Candidates.Pound.AAA")]
  74. [InlineData("Candidates.Pound.AAA.BBB")]
  75. [InlineData("AAA.Pound")]
  76. [InlineData("aaa.Pound")]
  77. [InlineData("aaa.bbb.Pound")]
  78. [InlineData("aaa.BBB.Pound")]
  79. public void CanFindPoundTopic(string topic)
  80. {
  81. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  82. var candidates = selector.SelectCandidates();
  83. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  84. Assert.NotNull(bestCandidates);
  85. }
  86. [Theory]
  87. [InlineData("Pound")]
  88. [InlineData("Pound.AAA")]
  89. [InlineData("Pound.aaa")]
  90. [InlineData("AAA.Pound.aaa")]
  91. public void CanNotFindPoundTopic(string topic)
  92. {
  93. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  94. var candidates = selector.SelectCandidates();
  95. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  96. Assert.Null(bestCandidates);
  97. }
  98. }
  99. public class CandidatesTopic : TopicAttribute
  100. {
  101. public CandidatesTopic(string topicName) : base(topicName)
  102. {
  103. }
  104. }
  105. public interface IFooTest
  106. {
  107. }
  108. public interface IBarTest
  109. {
  110. }
  111. public class CandidatesFooTest : IFooTest, ICapSubscribe
  112. {
  113. [CandidatesTopic("Candidates.Foo")]
  114. public Task GetFoo()
  115. {
  116. Console.WriteLine("GetFoo() method has bee excuted.");
  117. return Task.CompletedTask;
  118. }
  119. [CandidatesTopic("Candidates.Foo2")]
  120. public void GetFoo2()
  121. {
  122. Console.WriteLine("GetFoo2() method has bee excuted.");
  123. }
  124. [CandidatesTopic("*.*.Asterisk")]
  125. [CandidatesTopic("*.Asterisk")]
  126. public void GetFooAsterisk()
  127. {
  128. Console.WriteLine("GetFoo2Asterisk() method has bee excuted.");
  129. }
  130. [CandidatesTopic("Candidates.Pound.#")]
  131. [CandidatesTopic("#.Pound")]
  132. public void GetFooPound()
  133. {
  134. Console.WriteLine("GetFoo2Pound() method has bee excuted.");
  135. }
  136. }
  137. public class CandidatesBarTest : IBarTest
  138. {
  139. [CandidatesTopic("Candidates.Bar")]
  140. public Task GetBar()
  141. {
  142. Console.WriteLine("GetBar() method has bee excuted.");
  143. return Task.CompletedTask;
  144. }
  145. [CandidatesTopic("Candidates.Bar2")]
  146. public void GetBar2()
  147. {
  148. Console.WriteLine("GetBar2() method has bee excuted.");
  149. }
  150. public void GetBar3()
  151. {
  152. Console.WriteLine("GetBar3() method has bee excuted.");
  153. }
  154. }
  155. }