Пример #1
0
        public void MessageCount_WhenCalled_WithExistentQueueName_ItWillReturnsInt_10()
        {
            var dateTimeProvider     = new Mock <IDateTimeProvider>();
            var logger               = new Mock <ILogger>();
            var configurationManager = new Mock <IConfigurationManager>();

            configurationManager.Setup(x => x.Get <string>("QueueUri")).Returns("amqp://*****:*****@localhost:5672");

            var model = new Mock <IModel>();

            model.Setup(x => x.MessageCount("Test")).Returns(10);
            var connection = new Mock <IConnection>();

            connection.Setup(x => x.CreateModel()).Returns(model.Object);
            var connectionManager = new Mock <IRabbitMqConnectionManager>();

            connectionManager.Setup(x => x.Connection).Returns(connection.Object);

            var queueName = "Test";

            IQueueWrapper rabbitMqWrapper = new RabbitMqWrapper(dateTimeProvider.Object, logger.Object, configurationManager.Object, connectionManager.Object, _metrics);

            rabbitMqWrapper.Start();

            uint result = rabbitMqWrapper.MessageCount(queueName);

            Assert.AreEqual(10u, result);
        }
Пример #2
0
        public void Stop_WhenCalledAfterStart_CallsModelDispose()
        {
            var dateTimeProvider     = new Mock <IDateTimeProvider>();
            var logger               = new Mock <ILogger>();
            var configurationManager = new Mock <IConfigurationManager>();

            configurationManager.Setup(x => x.Get <string>("QueueUri")).Returns("amqp://*****:*****@localhost:5672");
            var model      = new Mock <IModel>();
            var connection = new Mock <IConnection>();

            connection.Setup(x => x.CreateModel()).Returns(model.Object);

            var connectionFactory = new Mock <IConnectionFactory>();

            connectionFactory.Setup(x => x.Uri).Returns(new Uri("amqp://*****:*****@localhost:5672"));
            connectionFactory.Setup(x => x.CreateConnection()).Returns(connection.Object);

            var mqConnectionManager = new RabbitMqConnectionManager(connectionFactory.Object, configurationManager.Object);

            var rabbitMqWrapper = new RabbitMqWrapper(dateTimeProvider.Object, logger.Object, configurationManager.Object, mqConnectionManager, _metrics);

            rabbitMqWrapper.Start();
            rabbitMqWrapper.Stop();

            model.Verify(x => x.Dispose(), Times.Once);
        }
Пример #3
0
        public void StopMessageConsumer_WhenCalledWithConsumerTag_CallsLoggerTrace()
        {
            var dateTimeProvider = new Mock <IDateTimeProvider>();
            var logger           = new Mock <ILogger>();

            logger.Setup(x => x.Trace(It.IsAny <string>()));
            var configurationManager = new Mock <IConfigurationManager>();

            configurationManager.Setup(x => x.Get <string>("QueueUri")).Returns("amqp://*****:*****@localhost:5672");

            var model      = new Mock <IModel>();
            var connection = new Mock <IConnection>();

            connection.Setup(x => x.CreateModel()).Returns(model.Object);
            var connectionManager = new Mock <IRabbitMqConnectionManager>();

            connectionManager.Setup(x => x.Connection).Returns(connection.Object);

            var rabbitMqWrapper = new RabbitMqWrapper(dateTimeProvider.Object, logger.Object, configurationManager.Object, connectionManager.Object, _metrics);

            rabbitMqWrapper.Start();
            rabbitMqWrapper.StopMessageConsumer("TestConsumerTag");

            logger.Verify(x => x.Trace(It.Is <string>(s => s == "RabbitMqWrapper.StopMessageConsumer"),
                                       It.Is <LogItem>(li => li.Key == "Event" && li.Value() == "Message handler unsubscribed from queue"),
                                       It.Is <LogItem>(li => li.Key == "QueueName"),
                                       It.Is <LogItem>(li => li.Key == "ConsumerTag" && li.Value() == "TestConsumerTag")), Times.Once);
        }
Пример #4
0
        public void PublishMessage_WhenCalledWithDefaultTopic_CallsBasicPublish()
        {
            var dateTimeProvider     = new Mock <IDateTimeProvider>();
            var logger               = new Mock <ILogger>();
            var configurationManager = new Mock <IConfigurationManager>();

            configurationManager.Setup(x => x.Get <string>("QueueUri")).Returns("amqp://*****:*****@localhost:5672");

            var model      = new Mock <IModel>();
            var connection = new Mock <IConnection>();

            connection.Setup(x => x.CreateModel()).Returns(model.Object);
            var connectionManager = new Mock <IRabbitMqConnectionManager>();

            connectionManager.Setup(x => x.Connection).Returns(connection.Object);

            var rabbitMqWrapper = new RabbitMqWrapper(dateTimeProvider.Object, logger.Object, configurationManager.Object, connectionManager.Object, _metrics);

            var parameters = new Message <object, object> {
                Uuid           = Guid.NewGuid(),
                Source         = "test",
                LastModifiedBy = "test",
                Method         = "InvokeTest",
                Need           = new object()
            };

            rabbitMqWrapper.Start();
            rabbitMqWrapper.PublishMessage(parameters);

            model.Verify(x => x.BasicPublish(It.IsAny <string>(), "A.*", It.IsAny <bool>(), It.IsAny <IBasicProperties>(), It.IsAny <byte[]>()), Times.Once);
        }
Пример #5
0
        public void StartMessageConsumer_WhenCalledAfterStart_CallsLoggerTrace()
        {
            var dateTimeProvider     = new Mock <IDateTimeProvider>();
            var logger               = new Mock <ILogger>();
            var configurationManager = new Mock <IConfigurationManager>();

            configurationManager.Setup(x => x.Get <string>("QueueUri")).Returns("amqp://*****:*****@localhost:5672");

            var model      = new Mock <IModel>();
            var connection = new Mock <IConnection>();

            connection.Setup(x => x.CreateModel()).Returns(model.Object);
            var connectionManager = new Mock <IRabbitMqConnectionManager>();

            connectionManager.Setup(x => x.Connection).Returns(connection.Object);

            var rabbitMqWrapper = new RabbitMqWrapper(dateTimeProvider.Object, logger.Object, configurationManager.Object, connectionManager.Object, _metrics);

            rabbitMqWrapper.Start();
            rabbitMqWrapper.StartMessageConsumer(new Mock <AsyncEventHandler <BasicDeliverEventArgs> >().Object);

            logger.Verify(x => x.Trace(It.Is <string>(s => s == "RabbitMqWrapper.StartMessageConsumer"),
                                       It.Is <LogItem>(li => li.Key == "Event" && li.Value() == "Message handler subscribed to queue"),
                                       It.Is <LogItem>(li => li.Key == "QueueName"),
                                       It.Is <LogItem>(li => li.Key == "ConsumerTag"),
                                       It.Is <LogItem>(li => li.Key == "HandlerMethod" && li.Value() == "Invoke")), Times.Once);
        }
Пример #6
0
        public void Stop_WhenConnectionCloseRaisesNonEndOfStreamException_DoesBubbleException()
        {
            var dateTimeProvider     = new Mock <IDateTimeProvider>();
            var logger               = new Mock <ILogger>();
            var configurationManager = new Mock <IConfigurationManager>();

            configurationManager.Setup(x => x.Get <string>("QueueUri")).Returns("amqp://*****:*****@localhost:5672");
            var model      = new Mock <IModel>();
            var connection = new Mock <IConnection>();

            connection.Setup(x => x.CreateModel()).Returns(model.Object);
            connection.Setup(x => x.Dispose()).Throws <Exception>();

            var connectionFactory = new Mock <IConnectionFactory>();

            connectionFactory.Setup(x => x.Uri).Returns(new Uri("amqp://*****:*****@localhost:5672"));
            connectionFactory.Setup(x => x.CreateConnection()).Returns(connection.Object);

            var rabbitMqConnectionManager = new RabbitMqConnectionManager(connectionFactory.Object, configurationManager.Object);

            var rabbitMqWrapper = new RabbitMqWrapper(dateTimeProvider.Object, logger.Object, configurationManager.Object, rabbitMqConnectionManager, _metrics);

            rabbitMqWrapper.Start();
            rabbitMqWrapper.Stop();
            rabbitMqConnectionManager.Dispose();
        }
        public void Start_should_call_BasicQos_on_model_if_MaximumConcurrency_config_is_set()
        {
            _mockConfigurationManager.Setup(m => m.Get <ushort>("MaximumConcurrency", 0)).Returns(25);
            _sut = CreateSut();

            _sut.Start();

            _mockModel.Verify(m => m.BasicQos(0, 25, false), Times.Once);
        }
Пример #8
0
        public void Start_WhenCalledWithoutQueueUri_RaisesException()
        {
            var dateTimeProvider     = new Mock <IDateTimeProvider>();
            var logger               = new Mock <ILogger>();
            var configurationManager = new Mock <IConfigurationManager>();

            configurationManager.Setup(x => x.Get <string>("QueueUri")).Returns((string)null);

            var connection        = new Mock <IConnection>();
            var connectionFactory = new Mock <IConnectionFactory>();

            connectionFactory.Setup(x => x.CreateConnection()).Returns(connection.Object);

            var rabbitMqConnectionManager = new RabbitMqConnectionManager(connectionFactory.Object, configurationManager.Object);

            var rabbitMqWrapper = new RabbitMqWrapper(dateTimeProvider.Object, logger.Object, configurationManager.Object, rabbitMqConnectionManager, _metrics);

            rabbitMqWrapper.Start();
        }
Пример #9
0
        public void PurgeQueue_WhenCalledAfterStart_CallsModelQueuePurge()
        {
            var dateTimeProvider     = new Mock <IDateTimeProvider>();
            var logger               = new Mock <ILogger>();
            var configurationManager = new Mock <IConfigurationManager>();

            configurationManager.Setup(x => x.Get <string>("QueueUri")).Returns("amqp://*****:*****@localhost:5672");
            var model      = new Mock <IModel>();
            var connection = new Mock <IConnection>();

            connection.Setup(x => x.CreateModel()).Returns(model.Object);
            var connectionManager = new Mock <IRabbitMqConnectionManager>();

            connectionManager.Setup(x => x.Connection).Returns(connection.Object);

            var rabbitMqWrapper = new RabbitMqWrapper(dateTimeProvider.Object, logger.Object, configurationManager.Object, connectionManager.Object, _metrics);

            rabbitMqWrapper.Start();
            rabbitMqWrapper.PurgeQueue();

            model.Verify(x => x.QueuePurge(It.IsAny <string>()), Times.Once);
        }
Пример #10
0
        public void StopMessageConsumer_WhenCalledWithNoConsumerTag_DoesNotCallLoggerTrace()
        {
            var dateTimeProvider     = new Mock <IDateTimeProvider>();
            var logger               = new Mock <ILogger>();
            var configurationManager = new Mock <IConfigurationManager>();

            configurationManager.Setup(x => x.Get <string>("QueueUri")).Returns("amqp://*****:*****@localhost:5672");

            var model      = new Mock <IModel>();
            var connection = new Mock <IConnection>();

            connection.Setup(x => x.CreateModel()).Returns(model.Object);
            var connectionManager = new Mock <IRabbitMqConnectionManager>();

            connectionManager.Setup(x => x.Connection).Returns(connection.Object);

            var rabbitMqWrapper = new RabbitMqWrapper(dateTimeProvider.Object, logger.Object, configurationManager.Object, connectionManager.Object, _metrics);

            rabbitMqWrapper.Start();
            rabbitMqWrapper.StopMessageConsumer(null);

            logger.Verify(x => x.Trace(It.IsAny <string>()), Times.Never);
        }
        public void Start_should_not_call_BasicQos_on_model_if_MaximumConcurrency_config_is_not_set()
        {
            _sut.Start();

            _mockModel.Verify(m => m.BasicQos(It.IsAny <uint>(), It.IsAny <ushort>(), It.IsAny <bool>()), Times.Never);
        }