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.
 
 
 

174 lines
5.4 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using DotNetCore.CAP.Abstractions;
  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, DefaultConsumerServiceSelector>();
  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. [InlineData("Asterisk.aaa.bbb")]
  57. public void CanNotFindAsteriskTopic(string topic)
  58. {
  59. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  60. var candidates = selector.SelectCandidates();
  61. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  62. Assert.Null(bestCandidates);
  63. }
  64. [Theory]
  65. [InlineData("Candidates.Pound.AAA")]
  66. [InlineData("Candidates.Pound.AAA.BBB")]
  67. [InlineData("AAA.Pound")]
  68. [InlineData("aaa.Pound")]
  69. [InlineData("aaa.bbb.Pound")]
  70. [InlineData("aaa.BBB.Pound")]
  71. public void CanFindPoundTopic(string topic)
  72. {
  73. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  74. var candidates = selector.SelectCandidates();
  75. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  76. Assert.NotNull(bestCandidates);
  77. }
  78. [Theory]
  79. [InlineData("Pound")]
  80. [InlineData("aaa.Pound.AAA.BBB")]
  81. [InlineData("Pound.AAA")]
  82. [InlineData("Pound.aaa")]
  83. [InlineData("AAA.Pound.aaa")]
  84. public void CanNotFindPoundTopic(string topic)
  85. {
  86. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  87. var candidates = selector.SelectCandidates();
  88. var bestCandidates = selector.SelectBestCandidate(topic, candidates);
  89. Assert.Null(bestCandidates);
  90. }
  91. }
  92. public class CandidatesTopic : TopicAttribute
  93. {
  94. public CandidatesTopic(string topicName) : base(topicName)
  95. {
  96. }
  97. }
  98. public interface IFooTest
  99. {
  100. }
  101. public interface IBarTest
  102. {
  103. }
  104. public class CandidatesFooTest : IFooTest, ICapSubscribe
  105. {
  106. [CandidatesTopic("Candidates.Foo")]
  107. public Task GetFoo()
  108. {
  109. Console.WriteLine("GetFoo() method has bee excuted.");
  110. return Task.CompletedTask;
  111. }
  112. [CandidatesTopic("Candidates.Foo2")]
  113. public void GetFoo2()
  114. {
  115. Console.WriteLine("GetFoo2() method has bee excuted.");
  116. }
  117. [CandidatesTopic("*.*.Asterisk")]
  118. [CandidatesTopic("*.Asterisk")]
  119. public void GetFooAsterisk()
  120. {
  121. Console.WriteLine("GetFoo2Asterisk() method has bee excuted.");
  122. }
  123. [CandidatesTopic("Candidates.Pound.#")]
  124. [CandidatesTopic("#.Pound")]
  125. public void GetFooPound()
  126. {
  127. Console.WriteLine("GetFoo2Pound() method has bee excuted.");
  128. }
  129. }
  130. public class CandidatesBarTest : IBarTest
  131. {
  132. [CandidatesTopic("Candidates.Bar")]
  133. public Task GetBar()
  134. {
  135. Console.WriteLine("GetBar() method has bee excuted.");
  136. return Task.CompletedTask;
  137. }
  138. [CandidatesTopic("Candidates.Bar2")]
  139. public void GetBar2()
  140. {
  141. Console.WriteLine("GetBar2() method has bee excuted.");
  142. }
  143. public void GetBar3()
  144. {
  145. Console.WriteLine("GetBar3() method has bee excuted.");
  146. }
  147. }
  148. }