Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ConsumerServiceSelectorTest.cs 2.7 KiB

7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }