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.

CustomConsumerSubscribeTest.cs 4.9 KiB

5 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Threading.Tasks;
  6. using DotNetCore.CAP.Internal;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.Options;
  9. using Xunit;
  10. namespace DotNetCore.CAP.Test
  11. {
  12. public class CustomConsumerSubscribeTest
  13. {
  14. private readonly IServiceProvider _provider;
  15. public CustomConsumerSubscribeTest()
  16. {
  17. var services = new ServiceCollection();
  18. services.AddSingleton<IConsumerServiceSelector, MyConsumerServiceSelector>();
  19. services.AddTransient<IMySubscribe, CustomInterfaceTypesClass>();
  20. services.AddLogging();
  21. services.AddCap(x => { });
  22. _provider = services.BuildServiceProvider();
  23. }
  24. [Fact]
  25. public void CanFindAllConsumerService()
  26. {
  27. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  28. var candidates = selector.SelectCandidates();
  29. Assert.Equal(2, candidates.Count);
  30. }
  31. [Fact]
  32. public void CanFindSpecifiedTopic()
  33. {
  34. var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
  35. var candidates = selector.SelectCandidates();
  36. var bestCandidates = selector.SelectBestCandidate("Candidates.Foo", candidates);
  37. Assert.NotNull(bestCandidates);
  38. Assert.NotNull(bestCandidates.MethodInfo);
  39. Assert.Equal(typeof(Task), bestCandidates.MethodInfo.ReturnType);
  40. }
  41. }
  42. public class MyConsumerServiceSelector : ConsumerServiceSelector
  43. {
  44. private readonly CapOptions _capOptions;
  45. public MyConsumerServiceSelector(IServiceProvider serviceProvider)
  46. : base(serviceProvider)
  47. {
  48. _capOptions = serviceProvider.GetService<IOptions<CapOptions>>().Value;
  49. }
  50. protected override IEnumerable<ConsumerExecutorDescriptor> FindConsumersFromInterfaceTypes(IServiceProvider provider)
  51. {
  52. var executorDescriptorList = new List<ConsumerExecutorDescriptor>();
  53. using (var scoped = provider.CreateScope())
  54. {
  55. var scopedProvider = scoped.ServiceProvider;
  56. var consumerServices = scopedProvider.GetServices<IMySubscribe>();
  57. foreach (var service in consumerServices)
  58. {
  59. var typeInfo = service.GetType().GetTypeInfo();
  60. if (!typeof(IMySubscribe).GetTypeInfo().IsAssignableFrom(typeInfo))
  61. {
  62. continue;
  63. }
  64. executorDescriptorList.AddRange(GetMyDescription(typeInfo));
  65. }
  66. return executorDescriptorList;
  67. }
  68. }
  69. private IEnumerable<ConsumerExecutorDescriptor> GetMyDescription(TypeInfo typeInfo)
  70. {
  71. foreach (var method in typeInfo.DeclaredMethods)
  72. {
  73. var topicAttr = method.GetCustomAttributes<MySubscribeAttribute>(true);
  74. var topicAttributes = topicAttr as IList<MySubscribeAttribute> ?? topicAttr.ToList();
  75. if (!topicAttributes.Any())
  76. {
  77. continue;
  78. }
  79. foreach (var attr in topicAttributes)
  80. {
  81. if (attr.Group == null)
  82. {
  83. attr.Group = _capOptions.DefaultGroup + "." + _capOptions.Version;
  84. }
  85. else
  86. {
  87. attr.Group = attr.Group + "." + _capOptions.Version;
  88. }
  89. yield return new ConsumerExecutorDescriptor
  90. {
  91. Attribute = new CapSubscribeAttribute(attr.Name)
  92. {
  93. Group = attr.Group
  94. },
  95. MethodInfo = method,
  96. ImplTypeInfo = typeInfo
  97. };
  98. }
  99. }
  100. }
  101. }
  102. public interface IMySubscribe { }
  103. public class MySubscribeAttribute : Attribute
  104. {
  105. public MySubscribeAttribute(string name)
  106. {
  107. Name = name;
  108. }
  109. public string Name { get; }
  110. public string Group { get; set; }
  111. }
  112. public class CustomInterfaceTypesClass : IMySubscribe
  113. {
  114. [MySubscribe("Candidates.Foo")]
  115. public Task GetFoo()
  116. {
  117. Console.WriteLine("GetFoo() method has been excuted.");
  118. return Task.CompletedTask;
  119. }
  120. [MySubscribe("Candidates.Foo2")]
  121. public void GetFoo2()
  122. {
  123. Console.WriteLine("GetFoo2() method has been excuted.");
  124. }
  125. public void DistracterMethod()
  126. {
  127. Console.WriteLine("DistracterMethod() method has been excuted.");
  128. }
  129. }
  130. }