public void Should_block_the_adding_thread_if_queue_is_full() { // Arrange var test = true; var waitFirstEnqueue = new AutoResetEvent(false); var queue = new InMemoryPriorityQueue<GenericPriorityMessage<string>>(2, new PriorityComparer<GenericPriorityMessage<string>>()); // Action Task.Factory.StartNew(() => { while (test) { queue.Enqueue(new GenericPriorityMessage<string>("", 1)); waitFirstEnqueue.Set(); } //Block }); waitFirstEnqueue.WaitOne(); queue.Dequeue(); queue.Dequeue(); // Assert test = false; queue.Close(); }
public void Should_block_the_adding_thread_if_queue_is_full() { // Arrange var test = true; var waitFirstEnqueue = new AutoResetEvent(false); var queue = new InMemoryPriorityQueue <GenericPriorityMessage <string> >(2, new PriorityComparer <GenericPriorityMessage <string> >()); // Action Task.Factory.StartNew(() => { while (test) { queue.Enqueue(new GenericPriorityMessage <string>("", 1)); waitFirstEnqueue.Set(); } //Block }); waitFirstEnqueue.WaitOne(); queue.Dequeue(); queue.Dequeue(); // Assert test = false; queue.Close(); }
public void Should_throw_exception_if_queue_is_closed() { // Arrange var queue = new InMemoryPriorityQueue<GenericPriorityMessage<string>>(2, new PriorityComparer<GenericPriorityMessage<string>>()); // Action queue.Close(); queue.Enqueue(new GenericPriorityMessage<string>("", 1)); }
public void Should_throw_exception_if_queue_is_closed() { // Arrange var queue = new InMemoryPriorityQueue <GenericPriorityMessage <string> >(2, new PriorityComparer <GenericPriorityMessage <string> >()); // Action queue.Close(); queue.Enqueue(new GenericPriorityMessage <string>("", 1)); }
public void Should_add_item_to_queue_if_not_reach_match_size() { // Arrange var queue = new InMemoryPriorityQueue<GenericPriorityMessage<string>>(2, new PriorityComparer<GenericPriorityMessage<string>>()); // Action queue.Enqueue(new GenericPriorityMessage<string>("", 1)); // Assert Assert.AreEqual(1, queue.Count); }
public void Should_add_item_to_queue_if_not_reach_match_size() { // Arrange var queue = new InMemoryPriorityQueue <GenericPriorityMessage <string> >(2, new PriorityComparer <GenericPriorityMessage <string> >()); // Action queue.Enqueue(new GenericPriorityMessage <string>("", 1)); // Assert Assert.AreEqual(1, queue.Count); }
public void Should_delete_all_items_match_predicate() { // Arrange var queue = new InMemoryPriorityQueue<GenericPriorityMessage<string>>(20, new PriorityComparer<GenericPriorityMessage<string>>()); queue.Enqueue(new GenericPriorityMessage<string>("", 1)); queue.Enqueue(new GenericPriorityMessage<string>("", 1)); queue.Enqueue(new GenericPriorityMessage<string>("", 1)); queue.Enqueue(new GenericPriorityMessage<string>("", 1)); queue.Enqueue(new GenericPriorityMessage<string>("VanTheShark", 1)); // Action queue.DeleteAll(msg => msg.Message == ""); // Assert Assert.AreEqual(1, queue.Count); }
public void Should_delete_all_existing_msgs_that_have_same_priority() { // Arrange var channel = Substitute.For<IModel>(); var consumer = new PriorityBurrowConsumer(channel, Substitute.For<IMessageHandler>(), Substitute.For<IRabbitWatcher>(), true, 1); var queue = new InMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>(5, new PriorityComparer<GenericPriorityMessage<BasicDeliverEventArgs>>()); queue.Enqueue(new GenericPriorityMessage<BasicDeliverEventArgs>(new BasicDeliverEventArgs(), 2)); queue.Enqueue(new GenericPriorityMessage<BasicDeliverEventArgs>(new BasicDeliverEventArgs(), 2)); queue.Enqueue(new GenericPriorityMessage<BasicDeliverEventArgs>(new BasicDeliverEventArgs(), 2)); queue.Enqueue(new GenericPriorityMessage<BasicDeliverEventArgs>(new BasicDeliverEventArgs(), 2)); // Action consumer.Init(queue, new CompositeSubscription(), 2, "sem"); // Assert Assert.AreEqual(0, queue.Count); }
public void Should_delete_all_items_match_predicate() { // Arrange var queue = new InMemoryPriorityQueue <GenericPriorityMessage <string> >(20, new PriorityComparer <GenericPriorityMessage <string> >()); queue.Enqueue(new GenericPriorityMessage <string>("", 1)); queue.Enqueue(new GenericPriorityMessage <string>("", 1)); queue.Enqueue(new GenericPriorityMessage <string>("", 1)); queue.Enqueue(new GenericPriorityMessage <string>("", 1)); queue.Enqueue(new GenericPriorityMessage <string>("VanTheShark", 1)); // Action queue.DeleteAll(msg => msg.Message == ""); // Assert Assert.AreEqual(1, queue.Count); }
public void Should_delete_all_existing_msgs_that_have_same_priority() { // Arrange var channel = Substitute.For <IModel>(); var consumer = new PriorityBurrowConsumer(channel, Substitute.For <IMessageHandler>(), Substitute.For <IRabbitWatcher>(), true, 1); var queue = new InMemoryPriorityQueue <GenericPriorityMessage <BasicDeliverEventArgs> >(5, new PriorityComparer <GenericPriorityMessage <BasicDeliverEventArgs> >()); queue.Enqueue(new GenericPriorityMessage <BasicDeliverEventArgs>(new BasicDeliverEventArgs(), 2)); queue.Enqueue(new GenericPriorityMessage <BasicDeliverEventArgs>(new BasicDeliverEventArgs(), 2)); queue.Enqueue(new GenericPriorityMessage <BasicDeliverEventArgs>(new BasicDeliverEventArgs(), 2)); queue.Enqueue(new GenericPriorityMessage <BasicDeliverEventArgs>(new BasicDeliverEventArgs(), 2)); // Action consumer.Init(queue, new CompositeSubscription(), 2, "sem"); // Assert Assert.AreEqual(0, queue.Count); }
public void When_called_should_dispose_the_thread_if_the_message_handler_throws_exception() { var waitHandler = new ManualResetEvent(false); var watcher = Substitute.For <IRabbitWatcher>(); watcher.When(x => x.Error(Arg.Any <BadMessageHandlerException>())).Do(callInfo => waitHandler.Set()); var channel = Substitute.For <IModel>(); channel.IsOpen.Returns(true); var msgHandler = Substitute.For <IMessageHandler>(); msgHandler.When(x => x.HandleMessage(Arg.Any <BasicDeliverEventArgs>())).Do(callInfo => { throw new Exception("Bad excepton"); }); var queue = new InMemoryPriorityQueue <GenericPriorityMessage <BasicDeliverEventArgs> >(10, new PriorityComparer <GenericPriorityMessage <BasicDeliverEventArgs> >()); var sub = Substitute.For <CompositeSubscription>(); sub.AddSubscription(new Subscription(channel) { ConsumerTag = "Burrow" }); var consumer = new PriorityBurrowConsumer(channel, msgHandler, watcher, true, 1); consumer.Init(queue, sub, 1, Guid.NewGuid().ToString()); consumer.Ready(); // Action queue.Enqueue(new GenericPriorityMessage <BasicDeliverEventArgs>(new BasicDeliverEventArgs(), 1)); waitHandler.WaitOne(); // Assert watcher.Received(1).Error(Arg.Any <BadMessageHandlerException>()); }
public void When_called_should_dispose_the_thread_if_the_message_handler_throws_exception() { var waitHandler = new ManualResetEvent(false); var watcher = Substitute.For<IRabbitWatcher>(); watcher.When(x => x.Error(Arg.Any<BadMessageHandlerException>())).Do(callInfo => waitHandler.Set()); var channel = Substitute.For<IModel>(); channel.IsOpen.Returns(true); var msgHandler = Substitute.For<IMessageHandler>(); msgHandler.When(x => x.HandleMessage(Arg.Any<BasicDeliverEventArgs>())).Do(callInfo => { throw new Exception("Bad excepton"); }); var queue = new InMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>(10, new PriorityComparer<GenericPriorityMessage<BasicDeliverEventArgs>>()); var sub = Substitute.For<CompositeSubscription>(); sub.AddSubscription(new Subscription(channel) { ConsumerTag = "Burrow" }); var consumer = new PriorityBurrowConsumer(channel, msgHandler, watcher, true, 1); consumer.Init(queue, sub, 1, Guid.NewGuid().ToString()); consumer.Ready(); // Action queue.Enqueue(new GenericPriorityMessage<BasicDeliverEventArgs>(new BasicDeliverEventArgs(), 1)); waitHandler.WaitOne(); // Assert watcher.Received(1).Error(Arg.Any<BadMessageHandlerException>()); }