コード例 #1
0
            public void BlockOnSendBlockedIfReachedBoundedCapacity()
            {
                var send1     = new ManualResetEvent(false);
                var send2     = new ManualResetEvent(false);
                var processor = new FakeMessageProcessor <Object>();
                var bus       = new OptimisticMessageSender <Object>(processor, 1);

                Assert.Equal(1, bus.BoundedCapacity);

                Task.Run(() =>
                {
                    //NOTE: Once message is received for processing, the message is dequeued and thus we actually need
                    //      one more message to confirm blocking behavior (i.e., one in queue and one being processed).
                    bus.Send(new Message <Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
                    bus.Send(new Message <Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
                    send1.Set();
                });

                Task.Run(() =>
                {
                    send1.WaitOne();
                    bus.Send(new Message <Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
                    send2.Set();
                });

                processor.WaitForMessage();

                Assert.True(send1.WaitOne(TimeSpan.FromMilliseconds(100)));
                Assert.False(send2.WaitOne(TimeSpan.FromMilliseconds(100)));

                processor.ProcessNextMessage();
                processor.WaitForMessage();

                Assert.True(send2.WaitOne(TimeSpan.FromMilliseconds(100)));
            }
コード例 #2
0
            public void BlockOnSendBlockedIfReachedBoundedCapacity()
            {
                var send1 = new ManualResetEvent(false);
                var send2 = new ManualResetEvent(false);
                var processor = new FakeMessageProcessor<Object>();
                var bus = new OptimisticMessageSender<Object>(processor, 1);

                Assert.Equal(1, bus.BoundedCapacity);

                Task.Run(() =>
                {
                    //NOTE: Once message is received for processing, the message is dequeued and thus we actually need
                    //      one more message to confirm blocking behavior (i.e., one in queue and one being processed).
                    bus.Send(new Message<Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
                    bus.Send(new Message<Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
                    send1.Set();
                });

                Task.Run(() =>
                {
                    send1.WaitOne();
                    bus.Send(new Message<Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
                    send2.Set();
                });

                processor.WaitForMessage();

                Assert.True(send1.WaitOne(TimeSpan.FromMilliseconds(100)));
                Assert.False(send2.WaitOne(TimeSpan.FromMilliseconds(100)));

                processor.ProcessNextMessage();
                processor.WaitForMessage();

                Assert.True(send2.WaitOne(TimeSpan.FromMilliseconds(100)));
            }
コード例 #3
0
            public void WillMoveMessagesToIntermediateProcessingQueue()
            {
                var message = testQueue.SendMessage("Test Message");

                using (new MessageReceiver <String>(TestMessageQueue.Path, new BinarySerializer(), messageProcessor))
                    using (var processingQueue = TestMessageQueue.Create("processing"))
                    {
                        messageProcessor.WaitForMessage();

                        Assert.NotNull(processingQueue.PeekById(message.Id));

                        messageProcessor.ProcessNextMessage();
                    }
            }
コード例 #4
0
            public void WillWaitForMessageDrain()
            {
                var processor = new FakeMessageProcessor <Object>();
                var bus       = new OptimisticMessageSender <Object>(processor);
                var messages  = new[]
                {
                    new Message <Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()),
                    new Message <Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()),
                    new Message <Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object())
                };

                foreach (var message in messages)
                {
                    bus.Send(message);
                }

                Task.Run(() =>
                {
                    foreach (var message in messages)
                    {
                        processor.WaitForMessage();
                        processor.ProcessNextMessage();
                    }
                });

                bus.Dispose();

                Assert.Equal(0, bus.Count);
            }
コード例 #5
0
            public void WillIgnorePosionMessageExceptions()
            {
                var processor = new FakeMessageProcessor <Object>();
                var bus       = new OptimisticMessageSender <Object>(processor);
                var ex        = new InvalidOperationException();

                bus.Send(new Message <Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
                processor.WaitForMessage();
                processor.ThrowException(ex);
                processor.ProcessNextMessage();
                bus.Dispose();
            }
コード例 #6
0
            public void WillRemoveFromQueueOnceMessageProcessed()
            {
                var processor = new FakeMessageProcessor <Object>();
                var bus       = new OptimisticMessageSender <Object>(processor, 1);
                var attempt   = 0;

                bus.Send(new Message <Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
                processor.WaitForMessage();
                processor.ProcessNextMessage();

                while (bus.Count > 0 && attempt < 10)
                {
                    Thread.Sleep(100);
                }

                Assert.Equal(0, bus.Count);
            }
コード例 #7
0
            public void WillRemoveFromQueueOnceMessageProcessed()
            {
                var processor = new FakeMessageProcessor<Object>();
                var bus = new OptimisticMessageSender<Object>(processor, 1);
                var attempt = 0;

                bus.Send(new Message<Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
                processor.WaitForMessage();
                processor.ProcessNextMessage();

                while (bus.Count > 0 && attempt < 10) Thread.Sleep(100);

                Assert.Equal(0, bus.Count);
            }
コード例 #8
0
            public void WillIgnorePosionMessageExceptions()
            {
                var processor = new FakeMessageProcessor<Object>();
                var bus = new OptimisticMessageSender<Object>(processor);
                var ex = new InvalidOperationException();

                bus.Send(new Message<Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()));
                processor.WaitForMessage();
                processor.ThrowException(ex);
                processor.ProcessNextMessage();
                bus.Dispose();
            }
コード例 #9
0
            public void WillWaitForMessageDrain()
            {
                var processor = new FakeMessageProcessor<Object>();
                var bus = new OptimisticMessageSender<Object>(processor);
                var messages = new[]
                {
                    new Message<Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()),
                    new Message<Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object()),
                    new Message<Object>(Guid.NewGuid(), HeaderCollection.Empty, new Object())
                };

                foreach (var message in messages)
                    bus.Send(message);

                Task.Run(() =>
                {
                    foreach (var message in messages)
                    {
                        processor.WaitForMessage();
                        processor.ProcessNextMessage();
                    }
                });

                bus.Dispose();

                Assert.Equal(0, bus.Count);
            }