Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

153 řádky
4.9 KiB

  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. }