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.
 
 
 

100 lines
2.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using DotNetCore.CAP.Abstractions;
  6. using DotNetCore.CAP.Internal;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Xunit;
  9. namespace DotNetCore.CAP.Test
  10. {
  11. public class ConsumerServiceSelectorTest
  12. {
  13. private IServiceProvider _provider;
  14. public ConsumerServiceSelectorTest()
  15. {
  16. var services = new ServiceCollection();
  17. //services.AddSingleton<IConsumerServiceSelector, DefaultConsumerServiceSelector>();
  18. services.AddScoped<IFooTest, CandidatesFooTest>();
  19. services.AddScoped<IBarTest, CandidatesBarTest>();
  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(_provider);
  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(_provider);
  36. var bestCandidates = selector.SelectBestCandidate("Candidates.Foo", candidates);
  37. Assert.NotNull(bestCandidates);
  38. Assert.NotNull(bestCandidates.MethodInfo);
  39. Assert.Equal(bestCandidates.MethodInfo.ReturnType, typeof(Task));
  40. }
  41. }
  42. public class CandidatesTopic : TopicAttribute
  43. {
  44. public CandidatesTopic(string topicName) : base(topicName)
  45. {
  46. }
  47. }
  48. public interface IFooTest
  49. {
  50. }
  51. public interface IBarTest
  52. {
  53. }
  54. public class CandidatesFooTest : IFooTest, ICapSubscribe
  55. {
  56. [CandidatesTopic("Candidates.Foo")]
  57. public Task GetFoo()
  58. {
  59. Console.WriteLine("GetFoo() method has bee excuted.");
  60. return Task.CompletedTask;
  61. }
  62. [CandidatesTopic("Candidates.Foo2")]
  63. public void GetFoo2()
  64. {
  65. Console.WriteLine("GetFoo2() method has bee excuted.");
  66. }
  67. }
  68. public class CandidatesBarTest : IBarTest
  69. {
  70. [CandidatesTopic("Candidates.Bar")]
  71. public Task GetBar()
  72. {
  73. Console.WriteLine("GetBar() method has bee excuted.");
  74. return Task.CompletedTask;
  75. }
  76. [CandidatesTopic("Candidates.Bar2")]
  77. public void GetBar2()
  78. {
  79. Console.WriteLine("GetBar2() method has bee excuted.");
  80. }
  81. public void GetBar3()
  82. {
  83. Console.WriteLine("GetBar3() method has bee excuted.");
  84. }
  85. }
  86. }