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.
 
 
 

98 lines
2.7 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using DotNetCore.CAP.Abstractions;
  4. using DotNetCore.CAP.Internal;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Xunit;
  7. namespace DotNetCore.CAP.Test
  8. {
  9. public class ConsumerServiceSelectorTest
  10. {
  11. private IServiceProvider _provider;
  12. public ConsumerServiceSelectorTest()
  13. {
  14. var services = new ServiceCollection();
  15. //services.AddSingleton<IConsumerServiceSelector, DefaultConsumerServiceSelector>();
  16. services.AddScoped<IFooTest, CandidatesFooTest>();
  17. services.AddScoped<IBarTest, CandidatesBarTest>();
  18. services.AddLogging();
  19. services.AddCap(x => { });
  20. _provider = services.BuildServiceProvider();
  21. }
  22. [Fact]
  23. public void CanFindAllConsumerService()
  24. {
  25. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  26. var candidates = selector.SelectCandidates();
  27. Assert.Equal(2, candidates.Count);
  28. }
  29. [Fact]
  30. public void CanFindSpecifiedTopic()
  31. {
  32. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  33. var candidates = selector.SelectCandidates();
  34. var bestCandidates = selector.SelectBestCandidate("Candidates.Foo", candidates);
  35. Assert.NotNull(bestCandidates);
  36. Assert.NotNull(bestCandidates.MethodInfo);
  37. Assert.Equal(typeof(Task), bestCandidates.MethodInfo.ReturnType);
  38. }
  39. }
  40. public class CandidatesTopic : TopicAttribute
  41. {
  42. public CandidatesTopic(string topicName) : base(topicName)
  43. {
  44. }
  45. }
  46. public interface IFooTest
  47. {
  48. }
  49. public interface IBarTest
  50. {
  51. }
  52. public class CandidatesFooTest : IFooTest, ICapSubscribe
  53. {
  54. [CandidatesTopic("Candidates.Foo")]
  55. public Task GetFoo()
  56. {
  57. Console.WriteLine("GetFoo() method has bee excuted.");
  58. return Task.CompletedTask;
  59. }
  60. [CandidatesTopic("Candidates.Foo2")]
  61. public void GetFoo2()
  62. {
  63. Console.WriteLine("GetFoo2() method has bee excuted.");
  64. }
  65. }
  66. public class CandidatesBarTest : IBarTest
  67. {
  68. [CandidatesTopic("Candidates.Bar")]
  69. public Task GetBar()
  70. {
  71. Console.WriteLine("GetBar() method has bee excuted.");
  72. return Task.CompletedTask;
  73. }
  74. [CandidatesTopic("Candidates.Bar2")]
  75. public void GetBar2()
  76. {
  77. Console.WriteLine("GetBar2() method has bee excuted.");
  78. }
  79. public void GetBar3()
  80. {
  81. Console.WriteLine("GetBar3() method has bee excuted.");
  82. }
  83. }
  84. }