public async Task ReceiverHandlerWithException()
        {
            var model = new Mock <IModel>();

            var(messageProvider, _, connectionFactory) = RabbitMockFactory.Get(model);
            var queueConsumerRabbitMQ = new QueueConsumerAsyncRabbitMQ(connectionFactory.Object, messageProvider.Object);

            queueConsumerRabbitMQ.Subscribe += (x) =>
            {
                throw new Exception();
            };

            Exception exception = null;

            queueConsumerRabbitMQ.Error += (obj, ex) =>
            {
                exception = ex.Exception;
            };

            var _consumer = (AsyncEventingBasicConsumer)typeof(QueueConsumerAsyncRabbitMQ).GetField("_consumer", BindingFlags.NonPublic | BindingFlags.Instance)
                            .GetValue(queueConsumerRabbitMQ);
            var @event = (AsyncEventHandler <BasicDeliverEventArgs>) typeof(AsyncEventingBasicConsumer).GetField("Received", BindingFlags.NonPublic | BindingFlags.Instance)
                         .GetValue(_consumer);
            await @event.Invoke(new EventingBasicConsumer(model.Object), new BasicDeliverEventArgs());

            Assert.NotNull(exception);
            model.Verify(x => x.BasicAck(It.IsAny <ulong>(), It.IsAny <bool>()));
        }
Exemplo n.º 2
0
 public void DependencyQueues()
 {
     var(appLogger, messageProvider, _, connectionFactory) = RabbitMockFactory.Get();
     Assert.Throws <ArgumentNullException>("queues", () =>
     {
         new QueueProviderRabbitMQ(appLogger.Object, messageProvider.Object, connectionFactory.Object);
     });
 }
Exemplo n.º 3
0
 public void DependencyConnectionFactory()
 {
     var(messageProvider, _, connectionFactory) = RabbitMockFactory.Get();
     Assert.Throws <ArgumentNullException>("factory", () =>
     {
         new QueueProviderRabbitMQ(messageProvider.Object, null);
     });
 }
Exemplo n.º 4
0
 public void DependencyMessageProvider()
 {
     var(messageProvider, _, connectionFactory) = RabbitMockFactory.Get();
     Assert.Throws <ArgumentNullException>("msgProvider", () =>
     {
         new QueueProviderRabbitMQ(null, connectionFactory.Object);
     });
 }
Exemplo n.º 5
0
 public void DependencyConnectionFactory()
 {
     var(messageProvider, queueProviderSettings, connectionFactory) = RabbitMockFactory.Get();
     Assert.Throws <ArgumentNullException>("factory", () =>
     {
         new QueueWatcherRabbitMQ(messageProvider.Object, (RabbitMQ.Client.IConnectionFactory)null);
     });
 }
 public void DependencyMessageProvider()
 {
     var(_, messageProvider, _, connectionFactory) = RabbitMockFactory.Get();
     Assert.Throws <ArgumentNullException>("provider", () =>
     {
         new QueueConsumerAsyncRabbitMQ(connectionFactory.Object, null);
     });
 }
Exemplo n.º 7
0
        public void GetQueueMessageCountShouldThrowsExceptionIfQueueNotExists()
        {
            var model = new Mock <IModel>();

            var(appLogger, messageProvider, _, connectionFactory) = RabbitMockFactory.Get(model);
            var queueProviderRabbitMQ = new QueueProviderRabbitMQ(appLogger.Object, messageProvider.Object, connectionFactory.Object, true, "queue1");

            Assert.Throws <Exception>(() => queueProviderRabbitMQ.GetQueueMessageCount("queue2"));
        }
Exemplo n.º 8
0
        public void DependencySubscribe()
        {
            var(messageProvider, queueProviderSettings, connectionFactory) = RabbitMockFactory.Get();
            var queueWatcherRabbitMQ = new QueueWatcherRabbitMQ(messageProvider.Object, queueProviderSettings.Object);

            Assert.Throws <ArgumentNullException>("Subscribe", () =>
            {
                queueWatcherRabbitMQ.StartWatch("queue1");
            });
        }
Exemplo n.º 9
0
        public void InitWithDelay()
        {
            var model = new Mock <IModel>();

            var(appLogger, messageProvider, _, connectionFactory) = RabbitMockFactory.Get(model);
            var connection            = new Mock <IConnection>();
            var queueProviderRabbitMQ = new QueueProviderRabbitMQ(appLogger.Object, messageProvider.Object, connectionFactory.Object, true, "queue1");

            model.Verify(x => x.QueueBind("queue1", "bgTeam.direct.delay", "queue1", null));
            model.Verify(x => x.ExchangeDeclare("bgTeam.direct.delay", "x-delayed-message", true, false, It.IsAny <IDictionary <string, object> >()));
        }
Exemplo n.º 10
0
        public void InitWithoutDelay()
        {
            var model = new Mock <IModel>();

            var(appLogger, messageProvider, _, connectionFactory) = RabbitMockFactory.Get(model);
            var connection            = new Mock <IConnection>();
            var queueProviderRabbitMQ = new QueueProviderRabbitMQ(appLogger.Object, messageProvider.Object, connectionFactory.Object, false, "queue1");

            model.Verify(x => x.QueueBind("queue1", "bgTeam.direct", "queue1", null));
            model.Verify(x => x.ExchangeDeclare("bgTeam.direct", "direct", true, false, null));
        }
Exemplo n.º 11
0
        public void GetQueueMessageCount()
        {
            var model = new Mock <IModel>();

            model.Setup(x => x.MessageCount("queue1"))
            .Returns(10);
            var(appLogger, messageProvider, _, connectionFactory) = RabbitMockFactory.Get(model);
            var queueProviderRabbitMQ = new QueueProviderRabbitMQ(appLogger.Object, messageProvider.Object, connectionFactory.Object, true, "queue1");
            var count = queueProviderRabbitMQ.GetQueueMessageCount("queue1");

            Assert.Equal((uint)10, count);
        }
Exemplo n.º 12
0
        public void PushMessage()
        {
            var model = new Mock <IModel>();

            var(appLogger, messageProvider, _, connectionFactory) = RabbitMockFactory.Get(model);
            var queueProviderRabbitMQ = new QueueProviderRabbitMQ(appLogger.Object, messageProvider.Object, connectionFactory.Object, true, "queue1");

            queueProviderRabbitMQ.PushMessage(new QueueMessageDefault()
            {
                Body = "hi"
            });
            model.Verify(x => x.BasicPublish("bgTeam.direct.delay", "queue1", false, It.IsAny <IBasicProperties>(), It.IsAny <ReadOnlyMemory <byte> >()));
        }
Exemplo n.º 13
0
        public void PushMessageInQueues()
        {
            var model = new Mock <IModel>();

            model.Setup(x => x.QueueDeclare("queue2", It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>(), null))
            .Returns(new QueueDeclareOk("queue2", 12, 2));
            var(messageProvider, _, connectionFactory) = RabbitMockFactory.Get(model);
            var queueProviderRabbitMQ = new QueueProviderRabbitMQ(messageProvider.Object, connectionFactory.Object, true, "queue1");

            queueProviderRabbitMQ.PushMessage(new QueueMessageDefault()
            {
                Body = "hi"
            }, "queue1", "queue2");
            model.Verify(x => x.BasicPublish("bgTeam.direct.delay", "queue1", false, It.IsAny <IBasicProperties>(), It.IsAny <ReadOnlyMemory <byte> >()));
            model.Verify(x => x.BasicPublish("bgTeam.direct.delay", "queue2", false, It.IsAny <IBasicProperties>(), It.IsAny <ReadOnlyMemory <byte> >()));
        }
        public async Task ShutdownHandler()
        {
            var model = new Mock <IModel>();

            var(messageProvider, _, connectionFactory) = RabbitMockFactory.Get(model);
            var queueConsumerRabbitMQ = new QueueConsumerAsyncRabbitMQ(connectionFactory.Object, messageProvider.Object);

            queueConsumerRabbitMQ.Subscribe += (x) =>
            {
                return(Task.FromResult(new QueueMessageDefault("Hi")));
            };

            var _consumer = (AsyncEventingBasicConsumer)typeof(QueueConsumerAsyncRabbitMQ).GetField("_consumer", BindingFlags.NonPublic | BindingFlags.Instance)
                            .GetValue(queueConsumerRabbitMQ);
            var @event = (AsyncEventHandler <ShutdownEventArgs>) typeof(AsyncEventingBasicConsumer).GetField("Shutdown", BindingFlags.NonPublic | BindingFlags.Instance)
                         .GetValue(_consumer);
            await @event.Invoke(new EventingBasicConsumer(model.Object), new ShutdownEventArgs(ShutdownInitiator.Application, 0, ""));
        }
Exemplo n.º 15
0
        public void StartWatch()
        {
            var(messageProvider, queueProviderSettings, connectionFactory) = RabbitMockFactory.Get();
            var queueWatcherRabbitMQ = new QueueWatcherRabbitMQ(messageProvider.Object, connectionFactory.Object);

            var           @event  = new ManualResetEvent(false);
            IQueueMessage message = null;

            queueWatcherRabbitMQ.Subscribe += (IQueueMessage mess) => Task.Run(() =>
            {
                message = mess;
                @event.Set();
            });

            Task.Factory.StartNew(() => queueWatcherRabbitMQ.StartWatch("queue1"));
            @event.WaitOne();

            Assert.NotNull(message);
            Assert.Equal("Hi", message.Body);
        }
        public async Task ReceiverHandler()
        {
            var model = new Mock <IModel>();

            var(messageProvider, _, connectionFactory) = RabbitMockFactory.Get(model);
            var queueConsumerRabbitMQ = new QueueConsumerAsyncRabbitMQ(connectionFactory.Object, messageProvider.Object);

            queueConsumerRabbitMQ.Subscribe += (x) =>
            {
                return(Task.FromResult(new QueueMessageDefault("Hi")));
            };

            // Get AsyncEventingBasicConsumer from private field
            var _consumer = (AsyncEventingBasicConsumer)typeof(QueueConsumerAsyncRabbitMQ).GetField("_consumer", BindingFlags.NonPublic | BindingFlags.Instance)
                            .GetValue(queueConsumerRabbitMQ);

            // Raise event in AsyncEventingBasicConsumer
            var @event = (AsyncEventHandler <BasicDeliverEventArgs>) typeof(AsyncEventingBasicConsumer).GetField("Received", BindingFlags.NonPublic | BindingFlags.Instance)
                         .GetValue(_consumer);
            await @event.Invoke(new EventingBasicConsumer(model.Object), new BasicDeliverEventArgs());

            model.Verify(x => x.BasicAck(It.IsAny <ulong>(), It.IsAny <bool>()));
        }
Exemplo n.º 17
0
        public void DisposeShouldCloseChannel()
        {
            var model  = new Mock <IModel>();
            var @event = new ManualResetEvent(false);

            model.Setup(x => x.MessageCount("queue1"))
            .Returns(() =>
            {
                @event.WaitOne();
                return(10);
            });

            var(appLogger, messageProvider, _, connectionFactory) = RabbitMockFactory.Get(model);

            var queueProviderRabbitMQ = new QueueProviderRabbitMQ(appLogger.Object, messageProvider.Object, connectionFactory.Object, true, "queue1");
            var task = Task.Factory.StartNew(() => queueProviderRabbitMQ.GetQueueMessageCount("queue1"));

            queueProviderRabbitMQ.Dispose();
            model.Verify(x => x.Close());
            model.Verify(x => x.Dispose());

            Assert.Throws <AggregateException>(() => task.Wait());
            @event.Set();
        }
Exemplo n.º 18
0
 public void DependencyQueueProviderSettings()
 {
     var(appLogger, messageProvider, queueProviderSettings, connectionFactory) = RabbitMockFactory.Get();
     Assert.Throws <ArgumentNullException>("settings", () =>
     {
         new QueueWatcherRabbitMQ(appLogger.Object, messageProvider.Object, (IQueueProviderSettings)null);
     });
 }