1+ using System ;
12using System . Collections . Generic ;
3+ using System . Threading ;
24using System . Threading . Tasks ;
35using Microsoft . Extensions . DependencyInjection ;
46using Moq ;
57using RabbitMQ . Client . Core . DependencyInjection . Configuration ;
68using RabbitMQ . Client . Core . DependencyInjection . Services ;
79using RabbitMQ . Client . Core . DependencyInjection . Tests . Stubs ;
10+ using RabbitMQ . Client . Events ;
811using Xunit ;
912
1013namespace RabbitMQ . Client . Core . DependencyInjection . Tests . IntegrationTests
1114{
1215 public class QueueServiceTests
1316 {
17+ readonly TimeSpan _globalTestsTimeout = TimeSpan . FromSeconds ( 60 ) ;
18+
1419 [ Fact ]
1520 public async Task ShouldProperlyPublishAndConsumeMessages ( )
1621 {
@@ -36,20 +41,41 @@ public async Task ShouldProperlyPublishAndConsumeMessages()
3641 }
3742 }
3843 } ;
39- var serviceCollection = new ServiceCollection ( ) ;
44+
45+ var connectionFactoryMock = new Mock < RabbitMqConnectionFactory > { CallBase = true }
46+ . As < IRabbitMqConnectionFactory > ( ) ;
47+
48+ AsyncEventingBasicConsumer consumer = null ;
49+ connectionFactoryMock . Setup ( x => x . CreateConsumer ( It . IsAny < IModel > ( ) ) )
50+ . Returns < IModel > ( channel =>
51+ {
52+ consumer = new AsyncEventingBasicConsumer ( channel ) ;
53+ return consumer ;
54+ } ) ;
4055
4156 var callerMock = new Mock < IStubCaller > ( ) ;
42- serviceCollection . AddRabbitMqClient ( clientOptions )
57+
58+ var serviceCollection = new ServiceCollection ( ) ;
59+ serviceCollection
60+ . AddSingleton ( connectionFactoryMock . Object )
61+ . AddSingleton ( callerMock . Object )
62+ . AddRabbitMqClient ( clientOptions )
4363 . AddConsumptionExchange ( "exchange.name" , exchangeOptions )
44- . AddMessageHandlerTransient < StubMessageHandler > ( "routing.key" )
45- . AddSingleton ( callerMock . Object ) ;
64+ . AddMessageHandlerTransient < StubMessageHandler > ( "routing.key" ) ;
4665
4766 var serviceProvider = serviceCollection . BuildServiceProvider ( ) ;
48-
4967 var queueService = serviceProvider . GetRequiredService < IQueueService > ( ) ;
5068 queueService . StartConsuming ( ) ;
69+
70+ var resetEvent = new AutoResetEvent ( false ) ;
71+ consumer . Received += async ( sender , @event ) =>
72+ {
73+ resetEvent . Set ( ) ;
74+ } ;
5175
5276 await queueService . SendAsync ( "message" , "exchange.name" , "routing.key" ) ;
77+ resetEvent . WaitOne ( _globalTestsTimeout ) ;
78+ callerMock . Verify ( x => x . Call ( It . IsAny < string > ( ) ) , Times . Once ) ;
5379 }
5480 }
5581}
0 commit comments