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.
 
 
 

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