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.

ConsumerServiceSelectorTest.cs 6.3 KiB

7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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(8, candidates.Count);
  29. }
  30. [Theory]
  31. [InlineData("Candidates.Foo")]
  32. [InlineData("Candidates.Foo3")]
  33. [InlineData("Candidates.Foo4")]
  34. public void CanFindSpecifiedTopic(string topic)
  35. {
  36. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  37. var candidates = selector.SelectCandidates();
  38. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  39. Assert.NotNull(bestCandidates);
  40. Assert.NotNull(bestCandidates.MethodInfo);
  41. Assert.Equal(typeof(Task), bestCandidates.MethodInfo.ReturnType);
  42. }
  43. [Theory]
  44. [InlineData("Candidates.Asterisk")]
  45. [InlineData("candidates.Asterisk")]
  46. [InlineData("AAA.BBB.Asterisk")]
  47. [InlineData("aaa.bbb.Asterisk")]
  48. public void CanFindAsteriskTopic(string topic)
  49. {
  50. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  51. var candidates = selector.SelectCandidates();
  52. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  53. Assert.NotNull(bestCandidates);
  54. }
  55. [Theory]
  56. [InlineData("Candidates.Asterisk.AAA")]
  57. [InlineData("AAA.BBB.CCC.Asterisk")]
  58. [InlineData("aaa.BBB.ccc.Asterisk")]
  59. public void CanNotFindAsteriskTopic(string topic)
  60. {
  61. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  62. var candidates = selector.SelectCandidates();
  63. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  64. Assert.Null(bestCandidates);
  65. }
  66. [Theory]
  67. [InlineData("Asterisk.aaa.bbb")]
  68. public void CanNotFindAsteriskTopic2(string topic)
  69. {
  70. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  71. var candidates = selector.SelectCandidates();
  72. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  73. Assert.Null(bestCandidates);
  74. }
  75. [Theory]
  76. [InlineData("Candidates.Pound.AAA")]
  77. [InlineData("Candidates.Pound.AAA.BBB")]
  78. [InlineData("AAA.Pound")]
  79. [InlineData("aaa.Pound")]
  80. [InlineData("aaa.bbb.Pound")]
  81. [InlineData("aaa.BBB.Pound")]
  82. public void CanFindPoundTopic(string topic)
  83. {
  84. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  85. var candidates = selector.SelectCandidates();
  86. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  87. Assert.NotNull(bestCandidates);
  88. }
  89. [Theory]
  90. [InlineData("Pound")]
  91. [InlineData("Pound.AAA")]
  92. [InlineData("Pound.aaa")]
  93. [InlineData("AAA.Pound.aaa")]
  94. public void CanNotFindPoundTopic(string topic)
  95. {
  96. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  97. var candidates = selector.SelectCandidates();
  98. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  99. Assert.Null(bestCandidates);
  100. }
  101. }
  102. public class CandidatesTopic : TopicAttribute
  103. {
  104. public CandidatesTopic(string topicName, bool isPartial = false) : base(topicName, isPartial)
  105. {
  106. }
  107. }
  108. public interface IFooTest
  109. {
  110. }
  111. public interface IBarTest
  112. {
  113. }
  114. [CandidatesTopic("Candidates")]
  115. public class CandidatesFooTest : IFooTest, ICapSubscribe
  116. {
  117. [CandidatesTopic("Candidates.Foo")]
  118. public Task GetFoo()
  119. {
  120. Console.WriteLine("GetFoo() method has bee excuted.");
  121. return Task.CompletedTask;
  122. }
  123. [CandidatesTopic("Candidates.Foo2")]
  124. public void GetFoo2()
  125. {
  126. Console.WriteLine("GetFoo2() method has bee excuted.");
  127. }
  128. [CandidatesTopic("Foo3", isPartial: true)]
  129. public Task GetFoo3()
  130. {
  131. Console.WriteLine("GetFoo3() method has bee excuted.");
  132. return Task.CompletedTask;
  133. }
  134. [CandidatesTopic(".Foo4", isPartial: true)]
  135. public Task GetFoo4()
  136. {
  137. Console.WriteLine("GetFoo4() method has bee excuted.");
  138. return Task.CompletedTask;
  139. }
  140. [CandidatesTopic("*.*.Asterisk")]
  141. [CandidatesTopic("*.Asterisk")]
  142. public void GetFooAsterisk()
  143. {
  144. Console.WriteLine("GetFoo2Asterisk() method has bee excuted.");
  145. }
  146. [CandidatesTopic("Candidates.Pound.#")]
  147. [CandidatesTopic("#.Pound")]
  148. public void GetFooPound()
  149. {
  150. Console.WriteLine("GetFoo2Pound() method has bee excuted.");
  151. }
  152. }
  153. public class CandidatesBarTest : IBarTest
  154. {
  155. [CandidatesTopic("Candidates.Bar")]
  156. public Task GetBar()
  157. {
  158. Console.WriteLine("GetBar() method has bee excuted.");
  159. return Task.CompletedTask;
  160. }
  161. [CandidatesTopic("Candidates.Bar2")]
  162. public void GetBar2()
  163. {
  164. Console.WriteLine("GetBar2() method has bee excuted.");
  165. }
  166. public void GetBar3()
  167. {
  168. Console.WriteLine("GetBar3() method has bee excuted.");
  169. }
  170. }
  171. }