diff --git a/test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs b/test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs new file mode 100644 index 0000000..5bdbac0 --- /dev/null +++ b/test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using DotNetCore.CAP.Abstractions; +using DotNetCore.CAP.Internal; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace DotNetCore.CAP.Test +{ + public class ConsumerServiceSelectorTest + { + + private IServiceProvider _provider; + + public ConsumerServiceSelectorTest() + { + var services = new ServiceCollection(); + //services.AddSingleton(); + services.AddScoped(); + services.AddScoped(); + services.AddLogging(); + services.AddCap(); + _provider = services.BuildServiceProvider(); + } + + [Fact] + public void CanFindAllConsumerService() + { + var selector = _provider.GetRequiredService(); + var candidates = selector.SelectCandidates(_provider); + + Assert.Equal(2, candidates.Count); + } + + [Fact] + public void CanFindSpecifiedTopic() + { + var selector = _provider.GetRequiredService(); + var candidates = selector.SelectCandidates(_provider); + var bestCandidates = selector.SelectBestCandidate("Candidates.Foo", candidates); + + Assert.NotNull(bestCandidates); + Assert.NotNull(bestCandidates.MethodInfo); + Assert.Equal(bestCandidates.MethodInfo.ReturnType, typeof(Task)); + } + } + + public class CandidatesTopic : TopicAttribute + { + public CandidatesTopic(string topicName) : base(topicName) + { + } + } + + public interface IFooTest { } + public interface IBarTest { } + + public class CandidatesFooTest : IFooTest, IConsumerService + { + [CandidatesTopic("Candidates.Foo")] + public Task GetFoo() + { + Console.WriteLine("GetFoo() method has bee excuted."); + return Task.CompletedTask; + } + + [CandidatesTopic("Candidates.Foo2")] + public void GetFoo2() + { + Console.WriteLine("GetFoo2() method has bee excuted."); + } + } + + public class CandidatesBarTest : IBarTest + { + [CandidatesTopic("Candidates.Bar")] + public Task GetBar() + { + Console.WriteLine("GetBar() method has bee excuted."); + return Task.CompletedTask; + } + + [CandidatesTopic("Candidates.Bar2")] + public void GetBar2() + { + Console.WriteLine("GetBar2() method has bee excuted."); + } + } + +}