25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConsumerServiceSelectorTest.cs 2.7 KiB

7 년 전
7 년 전
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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(_provider);
  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(_provider);
  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. }