public void Can_called_many_times()
        {
            // Arrange
            var consumer = new PriorityBurrowConsumer(Substitute.For<IModel>(), Substitute.For<IMessageHandler>(), Substitute.For<IRabbitWatcher>(), false, 1);
            consumer.Init(Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>(), new CompositeSubscription(), 1, "sem");

            // Action
            consumer.Dispose();
            consumer.Dispose();
        }
Example #2
0
        public void Can_called_many_times()
        {
            // Arrange
            var waitHandler = new AutoResetEvent(false);
            var queue = Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>();
            queue.When(x => x.Dequeue()).Do(callInfo => waitHandler.WaitOne());
            var consumer = new PriorityBurrowConsumer(Substitute.For<IModel>(), Substitute.For<IMessageHandler>(), Substitute.For<IRabbitWatcher>(), false, 1);
            consumer.Init(queue, new CompositeSubscription(), 1, "sem");
            consumer.Ready();

            // Action
            consumer.Dispose();
            consumer.Dispose();
            waitHandler.Set();
        }
        public void When_called_should_catch_all_exception()
        {
            // Arrange
            var blockTheThread = new AutoResetEvent(false);
            var waitForFirstDequeue = new AutoResetEvent(false);

            var channel = Substitute.For<IModel>();
            var msgHandler = Substitute.For<IMessageHandler>();
            msgHandler.When(x => x.HandleMessage(Arg.Any<IBasicConsumer>(), Arg.Any<BasicDeliverEventArgs>()))
                      .Do(callInfo => { throw new Exception(); });

            var queue = Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>();
            queue.When(x => x.Dequeue()).Do(callInfo => { waitForFirstDequeue.Set(); blockTheThread.WaitOne(); });

            var consumer = new PriorityBurrowConsumer(channel, msgHandler, Substitute.For<IRabbitWatcher>(), true, 1);

            var sub = Substitute.For<CompositeSubscription>();
            sub.AddSubscription(new Subscription(channel) { ConsumerTag = "Burrow" });
            consumer.Init(queue, sub, 1, Guid.NewGuid().ToString());
            consumer.Ready();


            // Action
            waitForFirstDequeue.WaitOne();
            consumer.HandleMessageDelivery(new BasicDeliverEventArgs
            {
                BasicProperties = Substitute.For<IBasicProperties>(),
                ConsumerTag = "Burrow"
            });

            // Assert
            msgHandler.Received().HandleError(Arg.Any<IBasicConsumer>(), Arg.Any<BasicDeliverEventArgs>(), Arg.Any<Exception>());
            consumer.Dispose();
            blockTheThread.Set();
        }
        public void When_called_should_execute_methods_on_message_handler()
        {
            // Arrange
            var channel = Substitute.For<IModel>();
            var handler = Substitute.For<IMessageHandler>();
            var queue = Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>();
            queue.When(x => x.Dequeue()).Do(callInfo => Thread.Sleep(100));

            var consumer = new PriorityBurrowConsumer(channel, handler, Substitute.For<IRabbitWatcher>(), true, 1);

            var sub = Substitute.For<CompositeSubscription>();
            sub.AddSubscription(new Subscription(channel) { ConsumerTag = "Burrow" });
            consumer.Init(queue, sub, 1, Guid.NewGuid().ToString());
            consumer.Ready();


            // Action
            consumer.HandleMessageDelivery(new BasicDeliverEventArgs
            {
                BasicProperties = Substitute.For<IBasicProperties>()
            });

            // Assert
            handler.Received(1).HandleMessage(consumer, Arg.Any<BasicDeliverEventArgs>());
            consumer.Dispose();
        }
Example #5
0
        public void Should_start_a_thread_to_dequeue_on_priority_queue()
        {
            // Arrange
            var dequeueCount = new AutoResetEvent(false);
            var enqueueCount = new AutoResetEvent(false);
            var channel = Substitute.For<IModel>();
            
            var queue = Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>();
            queue.Dequeue().Returns(new GenericPriorityMessage<BasicDeliverEventArgs>(new BasicDeliverEventArgs(), 1));
            queue.When(x => x.Dequeue()).Do(callInfo => dequeueCount.Set());
            queue.When(x => x.Enqueue(Arg.Any<GenericPriorityMessage<BasicDeliverEventArgs>>()))
                 .Do(callInfo => enqueueCount.Set());

            var handler = Substitute.For<IMessageHandler>();
            handler.When(h => h.HandleMessage(Arg.Any<BasicDeliverEventArgs>()))
                   .Do(callInfo => handler.HandlingComplete += Raise.Event<MessageHandlingEvent>(new BasicDeliverEventArgs()));

            var consumer = new PriorityBurrowConsumer(channel, handler, Substitute.For<IRabbitWatcher>(), true, 1);

            var sub = Substitute.For<CompositeSubscription>();
            sub.AddSubscription(new Subscription(channel) { ConsumerTag = "Burrow" });
            consumer.Init(queue, sub, 1, Guid.NewGuid().ToString());

            // Action
            consumer.Ready();
            dequeueCount.WaitOne();
            consumer.PriorityQueue.Enqueue(new GenericPriorityMessage<BasicDeliverEventArgs>(new BasicDeliverEventArgs(), 1));
            enqueueCount.WaitOne();
            
            

            // Assert
            consumer.Dispose();
        }
        public void Should_ack_if_auto_ack()
        {
            // Arrange
            var waitHandler = new ManualResetEvent(false);
            var queue = Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>();
            queue.When(x => x.Dequeue()).Do(callInfo => waitHandler.WaitOne());

            var model = Substitute.For<IModel>();
            model.IsOpen.Returns(true);
            model.When(x => x.BasicAck(Arg.Any<ulong>(), Arg.Any<bool>())).Do(callInfo => waitHandler.Set());

            var handler = Substitute.For<IMessageHandler>();
            var subscription = new CompositeSubscription();
            subscription.AddSubscription(new Subscription(model) {ConsumerTag = "ConsumerTag"});

            var consumer = new PriorityBurrowConsumer(model, handler, Substitute.For<IRabbitWatcher>(), true, 1);
            consumer.Init(queue, subscription, 1, Guid.NewGuid().ToString());
            consumer.Ready();

            // Action
            handler.HandlingComplete += Raise.Event<MessageHandlingEvent>(new BasicDeliverEventArgs{ConsumerTag = "ConsumerTag"});
            waitHandler.WaitOne();

            // Assert
            model.Received(1).BasicAck(Arg.Any<ulong>(), false);
            consumer.Dispose();
        }
        public void Should_release_the_pool()
        {
            // Arrange
            var blockTheThread = new AutoResetEvent(false);
            var countdownEvent = new CountdownEvent(1);

            var queue = Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>();
            queue.When(x => x.Dequeue()).Do(callInfo => { countdownEvent.Signal(); blockTheThread.WaitOne(); });
            
            var consumer = new PriorityBurrowConsumer(Substitute.For<IModel>(), Substitute.For<IMessageHandler>(), Substitute.For<IRabbitWatcher>(), false, 1);
            consumer.Init(queue, Substitute.For<CompositeSubscription>(), 1, Guid.NewGuid().ToString());
            consumer.Ready();
            

            // Action
            countdownEvent.Wait();
            countdownEvent.Reset();
            blockTheThread.Set();
            consumer.MessageHandlerHandlingComplete(null);
            countdownEvent.Wait();
            // Assert
            
            queue.Received(2).Dequeue();
            consumer.Dispose();
            blockTheThread.Dispose();
        }
        public void Should_do_nothing_if_already_disposed()
        {
            // Arrange
            var model = Substitute.For<IModel>();
            var consumer = new PriorityBurrowConsumer(model, Substitute.For<IMessageHandler>(), Substitute.For<IRabbitWatcher>(), false, 1);
            consumer.Init(Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>(), new CompositeSubscription(), 1, "sem");

            // Action
            consumer.Dispose();
            consumer.DoAck(new BasicDeliverEventArgs());


            // Assert
            model.DidNotReceive().BasicAck(Arg.Any<ulong>(), Arg.Any<bool>());
        }
Example #9
0
        public void Should_do_nothing_if_already_disposed()
        {
            // Arrange
            var waitHandler = new ManualResetEvent(false);
            var model = Substitute.For<IModel>();
            var consumer = new PriorityBurrowConsumer(model, Substitute.For<IMessageHandler>(), Substitute.For<IRabbitWatcher>(), false, 1);
            var queue = Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>();
            queue.When(x => x.Dequeue()).Do(callInfo => waitHandler.WaitOne());
            consumer.Init(queue, new CompositeSubscription(), 1, "sem");
            consumer.Ready();

            // Action
            consumer.Dispose();
            consumer.DoAck(new BasicDeliverEventArgs());

            // Assert
            model.DidNotReceive().BasicAck(Arg.Any<ulong>(), Arg.Any<bool>());
            waitHandler.Set();
        }
Example #10
0
        public void Should_catch_IOException()
        {
            // Arrange
            var model = Substitute.For<IModel>();
            model.When(x => x.BasicAck(Arg.Any<ulong>(), Arg.Any<bool>())).Do(callInfo => { throw new IOException(); });
            var watcher = Substitute.For<IRabbitWatcher>();
            var sub = Substitute.For<CompositeSubscription>();
            sub.AddSubscription(new Subscription(model) { ConsumerTag = "Burrow" });

            var consumer = new PriorityBurrowConsumer(model, Substitute.For<IMessageHandler>(), watcher, false, 1);
            consumer.Init(Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>(), sub, 1, "sem");

            // Action

            consumer.DoAck(new BasicDeliverEventArgs { ConsumerTag = "Burrow" });


            // Assert
            watcher.Received().WarnFormat(Arg.Any<string>(), Arg.Any<object[]>());
            consumer.Dispose();
        }
Example #11
0
        public void Should_close_queue_and_end_other_thread()
        {
            // Arrange
            var channel = Substitute.For<IModel>();

            var queue = Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>();
            queue.When(x => x.Dequeue()).Do(callInfo => Thread.Sleep(100));

            var consumer = new PriorityBurrowConsumer(channel, Substitute.For<IMessageHandler>(), Substitute.For<IRabbitWatcher>(), true, 1);

            var sub = Substitute.For<CompositeSubscription>();
            sub.AddSubscription(new Subscription(channel) { ConsumerTag = "Burrow" });
            consumer.Init(queue, sub, 1, Guid.NewGuid().ToString());
            consumer.Ready();


            // Action
            consumer.OnCancel();

            // Assert
            consumer.Dispose();
        }
        public void Should_put_msg_to_queue()
        {
            // Arrange
            var channel = Substitute.For<IModel>();
            var handler = Substitute.For<IMessageHandler>();
            var queue = Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>();
            queue.When(x => x.Dequeue()).Do(callInfo => Thread.Sleep(100));

            var consumer = new PriorityBurrowConsumer(channel, handler, Substitute.For<IRabbitWatcher>(), true, 10);
            consumer.ConsumerTag = "ConsumerTag";
            var sub = Substitute.For<CompositeSubscription>();
            sub.AddSubscription(new Subscription(channel) { ConsumerTag = "ConsumerTag" });
            consumer.Init(queue, sub, 1, Guid.NewGuid().ToString());
            consumer.Ready();


            // Action
            consumer.HandleBasicDeliver("",0, false,"", "", null, null);

            // Assert
            queue.Received(1).Enqueue(Arg.Any<GenericPriorityMessage<BasicDeliverEventArgs>>());
            consumer.Dispose();
        }
Example #13
0
        public void Should_catch_EndOfStreamException()
        {
            // Arrange
            var count = new CountdownEvent(2);
            var channel = Substitute.For<IModel>();
            var watcher = Substitute.For<IRabbitWatcher>();
            var queue = Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>();
            queue.When(x => x.Dequeue()).Do(callInfo => {
                count.Signal();
                throw new EndOfStreamException();                
            });
            var consumer = new PriorityBurrowConsumer(channel, Substitute.For<IMessageHandler>(), watcher, true, 2);

            var sub = Substitute.For<CompositeSubscription>();
            sub.AddSubscription(new Subscription(channel) { ConsumerTag = "Burrow" });
            consumer.Init(queue, sub, 1, Guid.NewGuid().ToString());
            

            // Action
            consumer.Ready();
            count.Wait();

            // Assert
            consumer.Dispose();
        }
Example #14
0
        public void Should_catch_ThreadInterruptedException()
        {
            // Arrange
            var count = new AutoResetEvent(false);
            var channel = Substitute.For<IModel>();
            var watcher = Substitute.For<IRabbitWatcher>();
            watcher.When(w => w.WarnFormat(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<uint>(), Arg.Any<string>(), Arg.Any<string>())).Do(c => count.Set());
            var queue = Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>();
            queue.When(x => x.Dequeue()).Do(callInfo =>
            {
                throw new ThreadInterruptedException();
            });
            var consumer = new PriorityBurrowConsumer(channel, Substitute.For<IMessageHandler>(), watcher, true, 2);

            var sub = Substitute.For<CompositeSubscription>();
            sub.AddSubscription(new Subscription(channel) { ConsumerTag = "Burrow" });
            consumer.Init(queue, sub, 1, Guid.NewGuid().ToString());


            // Action
            consumer.Ready();
            count.WaitOne(2000);

            // Assert
            consumer.Dispose();
        }