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.
 
 
 

97 lines
2.7 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(2, 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. }
  39. public class CandidatesTopic : TopicAttribute
  40. {
  41. public CandidatesTopic(string topicName) : base(topicName)
  42. {
  43. }
  44. }
  45. public interface IFooTest
  46. {
  47. }
  48. public interface IBarTest
  49. {
  50. }
  51. public class CandidatesFooTest : IFooTest, ICapSubscribe
  52. {
  53. [CandidatesTopic("Candidates.Foo")]
  54. public Task GetFoo()
  55. {
  56. Console.WriteLine("GetFoo() method has bee excuted.");
  57. return Task.CompletedTask;
  58. }
  59. [CandidatesTopic("Candidates.Foo2")]
  60. public void GetFoo2()
  61. {
  62. Console.WriteLine("GetFoo2() method has bee excuted.");
  63. }
  64. }
  65. public class CandidatesBarTest : IBarTest
  66. {
  67. [CandidatesTopic("Candidates.Bar")]
  68. public Task GetBar()
  69. {
  70. Console.WriteLine("GetBar() method has bee excuted.");
  71. return Task.CompletedTask;
  72. }
  73. [CandidatesTopic("Candidates.Bar2")]
  74. public void GetBar2()
  75. {
  76. Console.WriteLine("GetBar2() method has bee excuted.");
  77. }
  78. public void GetBar3()
  79. {
  80. Console.WriteLine("GetBar3() method has bee excuted.");
  81. }
  82. }
  83. }