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

ConsumerServiceSelectorTest.cs 7.7 KiB

7 лет назад
5 лет назад
7 лет назад
5 лет назад
7 лет назад
5 лет назад
5 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.AddScoped<IAbstractTest, CandidatesAbstractTest>();
  21. services.AddLogging();
  22. _provider = services.BuildServiceProvider();
  23. }
  24. [Fact]
  25. public void CanFindAllConsumerService()
  26. {
  27. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  28. var candidates = selector.SelectCandidates();
  29. Assert.Equal(10, candidates.Count);
  30. }
  31. [Theory]
  32. [InlineData("Candidates.Foo")]
  33. [InlineData("Candidates.Foo3")]
  34. [InlineData("Candidates.Foo4")]
  35. public void CanFindSpecifiedTopic(string topic)
  36. {
  37. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  38. var candidates = selector.SelectCandidates();
  39. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  40. Assert.NotNull(bestCandidates);
  41. Assert.NotNull(bestCandidates.MethodInfo);
  42. Assert.Equal(typeof(Task), bestCandidates.MethodInfo.ReturnType);
  43. }
  44. [Theory]
  45. [InlineData("Candidates.Abstract")]
  46. [InlineData("Candidates.Abstract2")]
  47. public void CanFindInheritedMethodsTopic(string topic)
  48. {
  49. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  50. var candidates = selector.SelectCandidates();
  51. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  52. Assert.NotNull(bestCandidates);
  53. Assert.NotNull(bestCandidates.MethodInfo);
  54. Assert.Equal(typeof(Task), bestCandidates.MethodInfo.ReturnType);
  55. }
  56. [Theory]
  57. [InlineData("Candidates.Asterisk")]
  58. [InlineData("candidates.Asterisk")]
  59. [InlineData("AAA.BBB.Asterisk")]
  60. [InlineData("aaa.bbb.Asterisk")]
  61. public void CanFindAsteriskTopic(string topic)
  62. {
  63. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  64. var candidates = selector.SelectCandidates();
  65. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  66. Assert.NotNull(bestCandidates);
  67. }
  68. [Theory]
  69. [InlineData("Candidates.Asterisk.AAA")]
  70. [InlineData("AAA.BBB.CCC.Asterisk")]
  71. [InlineData("aaa.BBB.ccc.Asterisk")]
  72. public void CanNotFindAsteriskTopic(string topic)
  73. {
  74. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  75. var candidates = selector.SelectCandidates();
  76. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  77. Assert.Null(bestCandidates);
  78. }
  79. [Theory]
  80. [InlineData("Asterisk.aaa.bbb")]
  81. public void CanNotFindAsteriskTopic2(string topic)
  82. {
  83. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  84. var candidates = selector.SelectCandidates();
  85. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  86. Assert.Null(bestCandidates);
  87. }
  88. [Theory]
  89. [InlineData("Candidates.Pound.AAA")]
  90. [InlineData("Candidates.Pound.AAA.BBB")]
  91. [InlineData("AAA.Pound")]
  92. [InlineData("aaa.Pound")]
  93. [InlineData("aaa.bbb.Pound")]
  94. [InlineData("aaa.BBB.Pound")]
  95. public void CanFindPoundTopic(string topic)
  96. {
  97. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  98. var candidates = selector.SelectCandidates();
  99. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  100. Assert.NotNull(bestCandidates);
  101. }
  102. [Theory]
  103. [InlineData("Pound")]
  104. [InlineData("Pound.AAA")]
  105. [InlineData("Pound.aaa")]
  106. [InlineData("AAA.Pound.aaa")]
  107. public void CanNotFindPoundTopic(string topic)
  108. {
  109. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  110. var candidates = selector.SelectCandidates();
  111. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  112. Assert.Null(bestCandidates);
  113. }
  114. }
  115. public class CandidatesTopic : TopicAttribute
  116. {
  117. public CandidatesTopic(string topicName, bool isPartial = false) : base(topicName, isPartial)
  118. {
  119. }
  120. }
  121. public interface IFooTest
  122. {
  123. }
  124. public interface IBarTest
  125. {
  126. }
  127. public interface IAbstractTest
  128. {
  129. }
  130. [CandidatesTopic("Candidates")]
  131. public class CandidatesFooTest : IFooTest, ICapSubscribe
  132. {
  133. [CandidatesTopic("Candidates.Foo")]
  134. public Task GetFoo()
  135. {
  136. Console.WriteLine("GetFoo() method has bee excuted.");
  137. return Task.CompletedTask;
  138. }
  139. [CandidatesTopic("Candidates.Foo2")]
  140. public void GetFoo2()
  141. {
  142. Console.WriteLine("GetFoo2() method has bee excuted.");
  143. }
  144. [CandidatesTopic("Foo3", isPartial: true)]
  145. public Task GetFoo3()
  146. {
  147. Console.WriteLine("GetFoo3() method has bee excuted.");
  148. return Task.CompletedTask;
  149. }
  150. [CandidatesTopic(".Foo4", isPartial: true)]
  151. public Task GetFoo4()
  152. {
  153. Console.WriteLine("GetFoo4() method has bee excuted.");
  154. return Task.CompletedTask;
  155. }
  156. [CandidatesTopic("*.*.Asterisk")]
  157. [CandidatesTopic("*.Asterisk")]
  158. public void GetFooAsterisk()
  159. {
  160. Console.WriteLine("GetFoo2Asterisk() method has bee excuted.");
  161. }
  162. [CandidatesTopic("Candidates.Pound.#")]
  163. [CandidatesTopic("#.Pound")]
  164. public void GetFooPound()
  165. {
  166. Console.WriteLine("GetFoo2Pound() method has bee excuted.");
  167. }
  168. }
  169. public class CandidatesBarTest : IBarTest
  170. {
  171. [CandidatesTopic("Candidates.Bar")]
  172. public Task GetBar()
  173. {
  174. Console.WriteLine("GetBar() method has bee excuted.");
  175. return Task.CompletedTask;
  176. }
  177. [CandidatesTopic("Candidates.Bar2")]
  178. public void GetBar2()
  179. {
  180. Console.WriteLine("GetBar2() method has bee excuted.");
  181. }
  182. public void GetBar3()
  183. {
  184. Console.WriteLine("GetBar3() method has bee excuted.");
  185. }
  186. }
  187. /// <summary>
  188. /// Test to verify if an inherited class also gets the subscribed methods.
  189. /// Abstract class doesn't have a subscribe topic, inherited class with a topic
  190. /// should also get the partial subscribed methods.
  191. /// </summary>
  192. public abstract class CandidatesAbstractBaseTest : ICapSubscribe, IAbstractTest
  193. {
  194. [CandidatesTopic("Candidates.Abstract")]
  195. public virtual Task GetAbstract()
  196. {
  197. Console.WriteLine("GetAbstract() method has been excuted.");
  198. return Task.CompletedTask;
  199. }
  200. [CandidatesTopic("Abstract2", isPartial: true)]
  201. public virtual Task GetAbstract2()
  202. {
  203. Console.WriteLine("GetAbstract2() method has been excuted.");
  204. return Task.CompletedTask;
  205. }
  206. }
  207. [CandidatesTopic("Candidates")]
  208. public class CandidatesAbstractTest : CandidatesAbstractBaseTest
  209. {
  210. }
  211. }