Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

110 wiersze
3.7 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using DotNetCore.CAP.Internal;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Xunit;
  6. namespace DotNetCore.CAP.Test
  7. {
  8. public class CustomConsumerSubscribeTest
  9. {
  10. private const string TopicNamePrefix = "topic";
  11. private const string GroupNamePrefix = "group";
  12. private readonly IServiceProvider _provider;
  13. public CustomConsumerSubscribeTest()
  14. {
  15. var services = new ServiceCollection();
  16. // Declare subscribe interface and attribute when configuring services.
  17. services.AddSingleton<IConsumerServiceSelector, GenericConsumerServiceSelector<IMySubscribe, MySubscribeAttribute>>();
  18. services.AddTransient<IMySubscribe, CustomInterfaceTypesClass>();
  19. services.AddLogging();
  20. services.AddCap(x =>
  21. {
  22. x.TopicNamePrefix = TopicNamePrefix;
  23. x.GroupNamePrefix = GroupNamePrefix;
  24. });
  25. _provider = services.BuildServiceProvider();
  26. }
  27. [Fact]
  28. public void CanFindAllConsumerService()
  29. {
  30. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  31. var candidates = selector.SelectCandidates();
  32. Assert.Equal(3, candidates.Count);
  33. }
  34. [Fact]
  35. public void CanFindSpecifiedTopic()
  36. {
  37. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  38. var candidates = selector.SelectCandidates();
  39. var bestCandidates = selector.SelectBestCandidate($"{TopicNamePrefix}.Candidates.Foo", candidates);
  40. Assert.NotNull(bestCandidates);
  41. Assert.NotNull(bestCandidates.MethodInfo);
  42. Assert.StartsWith(GroupNamePrefix, bestCandidates.Attribute.Group);
  43. Assert.StartsWith(TopicNamePrefix, bestCandidates.TopicName);
  44. Assert.Equal(typeof(Task), bestCandidates.MethodInfo.ReturnType);
  45. }
  46. [Fact]
  47. public void CanFindTopicWithParameters()
  48. {
  49. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  50. var candidates = selector.SelectCandidates();
  51. var bestCandidates = selector.SelectBestCandidate($"{TopicNamePrefix}.Candidates.Foo3", candidates);
  52. Assert.NotNull(bestCandidates);
  53. Assert.NotNull(bestCandidates.Parameters);
  54. Assert.StartsWith(GroupNamePrefix, bestCandidates.Attribute.Group);
  55. Assert.StartsWith(TopicNamePrefix, bestCandidates.TopicName);
  56. Assert.Equal(typeof(Task), bestCandidates.MethodInfo.ReturnType);
  57. }
  58. }
  59. public interface IMySubscribe { }
  60. public class MySubscribeAttribute : Attribute, INamedGroup
  61. {
  62. public MySubscribeAttribute(string name)
  63. {
  64. Name = name;
  65. }
  66. public string Name { get; }
  67. public string Group { get; set; }
  68. }
  69. public class CustomInterfaceTypesClass : IMySubscribe
  70. {
  71. [MySubscribe("Candidates.Foo")]
  72. public Task GetFoo()
  73. {
  74. Console.WriteLine("GetFoo() method has been executed.");
  75. return Task.CompletedTask;
  76. }
  77. [MySubscribe("Candidates.Foo2")]
  78. public void GetFoo2()
  79. {
  80. Console.WriteLine("GetFoo2() method has been executed.");
  81. }
  82. [MySubscribe("Candidates.Foo3")]
  83. public Task GetFoo3(string message)
  84. {
  85. Console.WriteLine($"GetFoo3() received message {message}.");
  86. return Task.CompletedTask;
  87. }
  88. public void DistracterMethod()
  89. {
  90. Console.WriteLine("DistracterMethod() method has been executed.");
  91. }
  92. }
  93. }