1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Threading . Tasks ;
4+ using Microsoft . Extensions . Logging ;
5+ using Moq ;
6+ using RabbitMQ . Client . Core . DependencyInjection . Configuration ;
7+ using RabbitMQ . Client . Core . DependencyInjection . Models ;
8+ using RabbitMQ . Client . Core . DependencyInjection . Services ;
9+ using RabbitMQ . Client . Events ;
10+ using Xunit ;
11+
12+ namespace RabbitMQ . Client . Core . DependencyInjection . Tests . UnitTests
13+ {
14+ public class QueueServiceConsumerTests
15+ {
16+ [ Theory ]
17+ [ InlineData ( 1 ) ]
18+ [ InlineData ( 5 ) ]
19+ [ InlineData ( 10 ) ]
20+ [ InlineData ( 15 ) ]
21+ [ InlineData ( 20 ) ]
22+ [ InlineData ( 25 ) ]
23+ public async Task ShouldProperlyConsumeMessages ( int numberOfMessages )
24+ {
25+ var channelMock = new Mock < IModel > ( ) ;
26+ var connectionMock = new Mock < IConnection > ( ) ;
27+ connectionMock . Setup ( x => x . CreateModel ( ) )
28+ . Returns ( channelMock . Object ) ;
29+
30+ var connectionFactoryMock = new Mock < IRabbitMqConnectionFactory > ( ) ;
31+ connectionFactoryMock . Setup ( x => x . CreateRabbitMqConnection ( It . IsAny < RabbitMqClientOptions > ( ) ) )
32+ . Returns ( connectionMock . Object ) ;
33+
34+ var consumer = new AsyncEventingBasicConsumer ( channelMock . Object ) ;
35+ connectionFactoryMock . Setup ( x => x . CreateConsumer ( It . IsAny < IModel > ( ) ) )
36+ . Returns ( consumer ) ;
37+
38+ var messageHandlingServiceMock = new Mock < IMessageHandlingService > ( ) ;
39+ var queueService = CreateService ( connectionFactoryMock . Object , messageHandlingServiceMock . Object ) ;
40+
41+ await consumer . HandleBasicDeliver (
42+ "1" ,
43+ 0 ,
44+ false ,
45+ "exchange" ,
46+ "routing,key" ,
47+ null ,
48+ new ReadOnlyMemory < byte > ( ) ) ;
49+ messageHandlingServiceMock . Verify ( x => x . HandleMessageReceivingEvent ( It . IsAny < BasicDeliverEventArgs > ( ) , It . IsAny < IQueueService > ( ) ) , Times . Never ) ;
50+
51+ queueService . StartConsuming ( ) ;
52+
53+ for ( var i = 1 ; i <= numberOfMessages ; i ++ )
54+ {
55+ await consumer . HandleBasicDeliver (
56+ "1" ,
57+ ( ulong ) numberOfMessages ,
58+ false ,
59+ "exchange" ,
60+ "routing,key" ,
61+ null ,
62+ new ReadOnlyMemory < byte > ( ) ) ;
63+ }
64+
65+ messageHandlingServiceMock . Verify ( x => x . HandleMessageReceivingEvent ( It . IsAny < BasicDeliverEventArgs > ( ) , It . IsAny < IQueueService > ( ) ) , Times . Exactly ( numberOfMessages ) ) ;
66+ }
67+
68+ IConsumingService CreateService (
69+ IRabbitMqConnectionFactory connectionFactory ,
70+ IMessageHandlingService messageHandlingService )
71+ {
72+ var guid = Guid . NewGuid ( ) ;
73+ var connectionOptionContainer = new RabbitMqConnectionOptionsContainer
74+ {
75+ Guid = guid ,
76+ Options = new RabbitMqConnectionOptions
77+ {
78+ ConsumerOptions = new RabbitMqClientOptions ( )
79+ }
80+ } ;
81+ var loggerMock = new Mock < ILogger < QueueService > > ( ) ;
82+ return new QueueService (
83+ guid ,
84+ connectionFactory ,
85+ new List < RabbitMqConnectionOptionsContainer > { connectionOptionContainer } ,
86+ messageHandlingService ,
87+ new List < RabbitMqExchange > ( ) ,
88+ loggerMock . Object ) ;
89+ }
90+ }
91+ }
0 commit comments