Esempio n. 1
0
        public async Task ConsumerTriggersMessageReceiver()
        {
            var channelMock      = new Mock <IModel>();
            var properties       = new Mock <IBasicProperties>();
            var simulatedPayload = new ReadOnlyMemory <byte>(new byte[] { 1, 1, 1 });
            AsyncEventingBasicConsumer?basicConsumer = null;  // Once the channelMock registers a consumer, we'll fill it in here.

            using var hosted       = new HostedRabbitMqService(_options.Object, _logger.Object, _connectionService.Object, _messageReceiver.Object);
            using var cancellation = new CancellationTokenSource();

            _connectionService.Setup(c => c.GetChannel()).Returns(channelMock.Object);

            // in reality an extension method with a simpler signature is called, but Moq won't allow us to simulate that directly. So we expand the called extension method here.
            channelMock.Setup(c => c.BasicConsume(QueueName, It.IsAny <bool>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <IDictionary <string, object> >(), It.IsAny <AsyncEventingBasicConsumer>()))
            .Callback <string, bool, string, bool, bool, IDictionary <string, object>, IBasicConsumer>((name, autoAck, consumerTag, noLocal, exclusive, arguments, consumer) =>
            {
                // Consumer here is an actual RabbitMQ consumer waiting for our model to fire stuff.
                basicConsumer = consumer as AsyncEventingBasicConsumer;
            });
            _messageReceiver.Setup(m => m.HandleIncoming(typeof(DummyEvent), simulatedPayload)).Returns(Task.CompletedTask).Verifiable("Message receiver was not called!");

            await hosted.StartAsync(cancellation.Token);

            basicConsumer?.HandleBasicDeliver(string.Empty, 420, false, string.Empty, QueueName, properties.Object, simulatedPayload);

            channelMock.Verify();
            _messageReceiver.Verify();
            await hosted.StopAsync(cancellation.Token);
        }
Esempio n. 2
0
        public async Task DeclaresQueuesOnStartup()
        {
            var channelMock = new Mock <IModel>();

            using var hosted       = new HostedRabbitMqService(_options.Object, _logger.Object, _connectionService.Object, _messageReceiver.Object);
            using var cancellation = new CancellationTokenSource();

            _connectionService.Setup(c => c.GetChannel()).Returns(channelMock.Object);
            channelMock.Setup(channel => channel.QueueDeclare(QueueName, It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <Dictionary <string, object> >()))
            .Verifiable($"Queue {QueueName} did not get declared!");

            await hosted.StartAsync(cancellation.Token);

            channelMock.Verify();
            await hosted.StopAsync(cancellation.Token);
        }