Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

296 lignes
9.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using DotNetCore.CAP.Internal;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Xunit;
  9. namespace DotNetCore.CAP.Test
  10. {
  11. public class ConsumerServiceSelectorTest
  12. {
  13. private readonly IServiceProvider _provider;
  14. public ConsumerServiceSelectorTest()
  15. {
  16. var services = new ServiceCollection();
  17. services.AddOptions();
  18. services.PostConfigure<CapOptions>(x=>{});
  19. services.AddSingleton<IServiceCollection>(_ => services);
  20. services.AddSingleton<IConsumerServiceSelector, ConsumerServiceSelector>();
  21. services.AddScoped<IFooTest, CandidatesFooTest>();
  22. services.AddScoped<IBarTest, CandidatesBarTest>();
  23. services.AddScoped<IAbstractTest, CandidatesAbstractTest>();
  24. services.AddScoped<ICancellationTest, CancellationTokenTest>();
  25. services.AddLogging();
  26. _provider = services.BuildServiceProvider();
  27. }
  28. [Fact]
  29. public void CanFindAllConsumerService()
  30. {
  31. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  32. var candidates = selector.SelectCandidates();
  33. Assert.Equal(13, candidates.Count);
  34. }
  35. [Theory]
  36. [InlineData("Candidates.Foo")]
  37. [InlineData("Candidates.Foo3")]
  38. [InlineData("Candidates.Foo4")]
  39. public void CanFindSpecifiedTopic(string topic)
  40. {
  41. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  42. var candidates = selector.SelectCandidates();
  43. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  44. Assert.NotNull(bestCandidates);
  45. Assert.NotNull(bestCandidates.MethodInfo);
  46. Assert.Equal(typeof(Task), bestCandidates.MethodInfo.ReturnType);
  47. }
  48. [Theory]
  49. [InlineData("Candidates.Abstract")]
  50. [InlineData("Candidates.Abstract2")]
  51. public void CanFindInheritedMethodsTopic(string topic)
  52. {
  53. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  54. var candidates = selector.SelectCandidates();
  55. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  56. Assert.NotNull(bestCandidates);
  57. Assert.NotNull(bestCandidates.MethodInfo);
  58. Assert.Equal(typeof(Task), bestCandidates.MethodInfo.ReturnType);
  59. }
  60. [Theory]
  61. [InlineData("Candidates.Asterisk")]
  62. [InlineData("candidates.Asterisk")]
  63. [InlineData("AAA.BBB.Asterisk")]
  64. [InlineData("aaa.bbb.Asterisk")]
  65. public void CanFindAsteriskTopic(string topic)
  66. {
  67. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  68. var candidates = selector.SelectCandidates();
  69. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  70. Assert.NotNull(bestCandidates);
  71. }
  72. [Theory]
  73. [InlineData("Candidates.Asterisk.AAA")]
  74. [InlineData("AAA.BBB.CCC.Asterisk")]
  75. [InlineData("aaa.BBB.ccc.Asterisk")]
  76. public void CanNotFindAsteriskTopic(string topic)
  77. {
  78. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  79. var candidates = selector.SelectCandidates();
  80. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  81. Assert.Null(bestCandidates);
  82. }
  83. [Theory]
  84. [InlineData("Asterisk.aaa.bbb")]
  85. public void CanNotFindAsteriskTopic2(string topic)
  86. {
  87. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  88. var candidates = selector.SelectCandidates();
  89. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  90. Assert.Null(bestCandidates);
  91. }
  92. [Theory]
  93. [InlineData("Candidates.Pound.AAA")]
  94. [InlineData("Candidates.Pound.AAA.BBB")]
  95. [InlineData("AAA.Pound")]
  96. [InlineData("aaa.Pound")]
  97. [InlineData("aaa.bbb.Pound")]
  98. [InlineData("aaa.BBB.Pound")]
  99. public void CanFindPoundTopic(string topic)
  100. {
  101. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  102. var candidates = selector.SelectCandidates();
  103. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  104. Assert.NotNull(bestCandidates);
  105. }
  106. [Theory]
  107. [InlineData("Pound")]
  108. [InlineData("Pound.AAA")]
  109. [InlineData("Pound.aaa")]
  110. [InlineData("AAA.Pound.aaa")]
  111. public void CanNotFindPoundTopic(string topic)
  112. {
  113. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  114. var candidates = selector.SelectCandidates();
  115. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  116. Assert.Null(bestCandidates);
  117. }
  118. [Theory]
  119. [InlineData("CancellationToken.NoAdditionalParameters", 1, 1)]
  120. [InlineData("CancellationToken.OneAdditionalParameter", 2, 2)]
  121. [InlineData("CancellationToken.CommonArrangement", 3, 2)]
  122. public void CanFindTopicSuccessfully(string topic, int parameterCount, int fromCapCount)
  123. {
  124. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  125. var candidates = selector.SelectCandidates();
  126. var bestCandidate = selector.SelectBestCandidate(topic, candidates);
  127. Assert.NotEqual(0, candidates.Count);
  128. Assert.NotNull(bestCandidate);
  129. Assert.Equal(bestCandidate.Parameters.Count, parameterCount);
  130. Assert.Equal(fromCapCount, bestCandidate.Parameters.Count(x => x.IsFromCap));
  131. }
  132. }
  133. public class CandidatesTopic : TopicAttribute
  134. {
  135. public CandidatesTopic(string topicName, bool isPartial = false) : base(topicName, isPartial)
  136. {
  137. }
  138. }
  139. public interface IFooTest
  140. {
  141. }
  142. public interface IBarTest
  143. {
  144. }
  145. public interface IAbstractTest
  146. {
  147. }
  148. public interface ICancellationTest
  149. {
  150. }
  151. [CandidatesTopic("Candidates")]
  152. public class CandidatesFooTest : IFooTest, ICapSubscribe
  153. {
  154. [CandidatesTopic("Candidates.Foo")]
  155. public Task GetFoo()
  156. {
  157. Console.WriteLine("GetFoo() method has bee excuted.");
  158. return Task.CompletedTask;
  159. }
  160. [CandidatesTopic("Candidates.Foo2")]
  161. public void GetFoo2()
  162. {
  163. Console.WriteLine("GetFoo2() method has bee excuted.");
  164. }
  165. [CandidatesTopic("Foo3", isPartial: true)]
  166. public Task GetFoo3()
  167. {
  168. Console.WriteLine("GetFoo3() method has bee excuted.");
  169. return Task.CompletedTask;
  170. }
  171. [CandidatesTopic(".Foo4", isPartial: true)]
  172. public Task GetFoo4()
  173. {
  174. Console.WriteLine("GetFoo4() method has bee excuted.");
  175. return Task.CompletedTask;
  176. }
  177. [CandidatesTopic("*.*.Asterisk")]
  178. [CandidatesTopic("*.Asterisk")]
  179. public void GetFooAsterisk()
  180. {
  181. Console.WriteLine("GetFoo2Asterisk() method has bee excuted.");
  182. }
  183. [CandidatesTopic("Candidates.Pound.#")]
  184. [CandidatesTopic("#.Pound")]
  185. public void GetFooPound()
  186. {
  187. Console.WriteLine("GetFoo2Pound() method has bee excuted.");
  188. }
  189. }
  190. public class CandidatesBarTest : IBarTest
  191. {
  192. [CandidatesTopic("Candidates.Bar")]
  193. public Task GetBar()
  194. {
  195. Console.WriteLine("GetBar() method has bee excuted.");
  196. return Task.CompletedTask;
  197. }
  198. [CandidatesTopic("Candidates.Bar2")]
  199. public void GetBar2()
  200. {
  201. Console.WriteLine("GetBar2() method has bee excuted.");
  202. }
  203. public void GetBar3()
  204. {
  205. Console.WriteLine("GetBar3() method has bee excuted.");
  206. }
  207. }
  208. /// <summary>
  209. /// Test to verify if an inherited class also gets the subscribed methods.
  210. /// Abstract class doesn't have a subscribe topic, inherited class with a topic
  211. /// should also get the partial subscribed methods.
  212. /// </summary>
  213. public abstract class CandidatesAbstractBaseTest : ICapSubscribe, IAbstractTest
  214. {
  215. [CandidatesTopic("Candidates.Abstract")]
  216. public virtual Task GetAbstract()
  217. {
  218. Console.WriteLine("GetAbstract() method has been excuted.");
  219. return Task.CompletedTask;
  220. }
  221. [CandidatesTopic("Abstract2", isPartial: true)]
  222. public virtual Task GetAbstract2()
  223. {
  224. Console.WriteLine("GetAbstract2() method has been excuted.");
  225. return Task.CompletedTask;
  226. }
  227. }
  228. [CandidatesTopic("Candidates")]
  229. public class CandidatesAbstractTest : CandidatesAbstractBaseTest
  230. {
  231. }
  232. [CandidatesTopic("Candidates")]
  233. public class CancellationTokenTest : ICancellationTest, ICapSubscribe
  234. {
  235. [CandidatesTopic("CancellationToken.NoAdditionalParameters")]
  236. public Task NoAdditionalParameters(CancellationToken cancellationToken)
  237. {
  238. Console.WriteLine($"{nameof(NoAdditionalParameters)} method has been executed.");
  239. return Task.CompletedTask;
  240. }
  241. [CandidatesTopic("CancellationToken.OneAdditionalParameter")]
  242. public Task OneAdditionalParameter([FromCap] CapHeader headers, CancellationToken cancellationToken)
  243. {
  244. Console.WriteLine($"{nameof(OneAdditionalParameter)} method has been executed.");
  245. return Task.CompletedTask;
  246. }
  247. [CandidatesTopic("CancellationToken.CommonArrangement")]
  248. public Task CommonArrangement(DateTime date, [FromCap] IDictionary<string, string> headers, CancellationToken cancellationToken)
  249. {
  250. Console.WriteLine($"{nameof(CommonArrangement)} method has been executed.");
  251. return Task.CompletedTask;
  252. }
  253. }
  254. }