Ejemplo n.º 1
0
        protected void TestInboxConcurrency(string workQueueUriFormat, string errorQueueUriFormat, int msToComplete,
											bool isTransactional)
        {
            const int threadCount = 1;

            var padlock = new object();
            var configuration = GetConfiguration(workQueueUriFormat, errorQueueUriFormat, threadCount, isTransactional);
            var module = new InboxConcurrencyModule();

            configuration.Modules.Add(module);

            using (var bus = new ServiceBus(configuration))
            {
                for (var i = 0; i < threadCount; i++)
                {
                    var message = bus.CreateTransportMessage(new ConcurrentCommand
                        {
                            MessageIndex = i
                        });

                    configuration.Inbox.WorkQueue.Enqueue(message.MessageId, configuration.Serializer.Serialize(message));
                }

                var idleThreads = new List<int>();

                bus.Events.ThreadWaiting += (sender, args) =>
                    {
                        lock (padlock)
                        {
                            if (idleThreads.Contains(Thread.CurrentThread.ManagedThreadId))
                            {
                                return;
                            }

                            idleThreads.Add(Thread.CurrentThread.ManagedThreadId);
                        }
                    };

                bus.Start();

                while (idleThreads.Count < threadCount)
                {
                    Thread.Sleep(30);
                }
            }

            AttemptDropQueues(workQueueUriFormat, errorQueueUriFormat);

            Assert.AreEqual(threadCount, module.OnAfterGetMessageCount,
                            string.Format("Got {0} messages but {1} were sent.", module.OnAfterGetMessageCount, threadCount));

            Assert.IsTrue(module.AllMessagesReceivedWithinTimespan(msToComplete),
                          "All dequeued messages have to be within {0} ms of first get message.", msToComplete);
        }
        public void Should_be_able_to_roll_back_any_database_and_queue_changes_when_an_exception_occurs_in_the_receive_pipeline()
        {
            var configuration = DefaultConfiguration();

            var inboxWorkQueue = configuration.QueueManager.GetQueue("msmq://./test-inbox-work");
            var inboxJournalQueue = configuration.QueueManager.GetQueue("msmq://./test-inbox-journal");
            var inboxErrorQueue = configuration.QueueManager.GetQueue("msmq://./test-error");

            configuration.Inbox =
                new InboxQueueConfiguration
                    {
                        WorkQueue = inboxWorkQueue,
                        JournalQueue = inboxJournalQueue,
                        ErrorQueue = inboxErrorQueue,
                        DurationToSleepWhenIdle = new[] {TimeSpan.FromMilliseconds(5)},
                        DurationToIgnoreOnFailure = new[] {TimeSpan.FromMilliseconds(5)},
                        MaximumFailureCount = 100,
                        ThreadCount = 1
                    };

            configuration.QueueManager.CreatePhysicalQueues(configuration, QueueCreationType.All);

            inboxWorkQueue.Purge();
            inboxJournalQueue.Purge();
            inboxErrorQueue.Purge();

            var module = new ReceivePipelineExceptionModule(inboxWorkQueue);

            configuration.Modules.Add(module);

            using (var bus = new ServiceBus(configuration))
            {
                var message = bus.CreateTransportMessage(new ReceivePipelineCommand());

                inboxWorkQueue.Enqueue(message.MessageId, configuration.Serializer.Serialize(message));

                Assert.AreEqual(1, inboxWorkQueue.Count());

                bus.Start();

                while (module.ShouldWait())
                {
                    Thread.Sleep(10);
                }
            }
        }
        protected void TestExceptionHandling(string workQueueUriFormat, string errorQueueUriFormat)
        {
            var configuration = DefaultConfiguration(true);

            var inboxWorkQueue = configuration.QueueManager.GetQueue(string.Format(workQueueUriFormat, "test-inbox-work"));
            var inboxErrorQueue = configuration.QueueManager.GetQueue(string.Format(errorQueueUriFormat, "test-error"));

            configuration.Inbox =
                new InboxQueueConfiguration
                    {
                        WorkQueue = inboxWorkQueue,
                        ErrorQueue = inboxErrorQueue,
                        DurationToSleepWhenIdle = new[] { TimeSpan.FromMilliseconds(5) },
                        DurationToIgnoreOnFailure = new[] { TimeSpan.FromMilliseconds(5) },
                        MaximumFailureCount = 100,
                        ThreadCount = 1
                    };

            inboxWorkQueue.Drop();
            inboxErrorQueue.Drop();

            configuration.QueueManager.CreatePhysicalQueues(configuration);

            var module = new ReceivePipelineExceptionModule(inboxWorkQueue);

            configuration.Modules.Add(module);

            using (var bus = new ServiceBus(configuration))
            {
                var message = bus.CreateTransportMessage(new ReceivePipelineCommand());

                inboxWorkQueue.Enqueue(message.MessageId, configuration.Serializer.Serialize(message));

                Assert.IsFalse(inboxWorkQueue.IsEmpty());

                bus.Start();

                while (module.ShouldWait())
                {
                    Thread.Sleep(10);
                }
            }
        }
Ejemplo n.º 4
0
        private const int MillisecondsToDefer = 1000; // give the service bus enough time to start up

        #endregion Fields

        #region Methods

        protected void TestDeferredProcessing(string workQueueUriFormat, string deferredQueueUriFormat,
		                                      string errorQueueUriFormat, bool isTransactional)
        {
            var configuration = GetInboxConfiguration(workQueueUriFormat, deferredQueueUriFormat, errorQueueUriFormat, 1,
                                                      isTransactional);

            var module = new DeferredMessageModule();

            configuration.Modules.Add(module);

            using (var bus = new ServiceBus(configuration))
            {
                bus.Start();

                var message = bus.CreateTransportMessage(new SimpleCommand());

                message.IgnoreTillDate = DateTime.Now.AddMilliseconds(MillisecondsToDefer);
                message.RecipientInboxWorkQueueUri = configuration.Inbox.WorkQueue.Uri.ToString();

                configuration.Inbox.WorkQueue.Enqueue(message.MessageId, configuration.Serializer.Serialize(message));

                var timeout = DateTime.Now.AddMilliseconds(MillisecondsToDefer + 5000);

                // wait for the message to be returned from the deferred queue
                while ((!module.DeferredMessageReturned || !module.MessageHandled)
                       &&
                       timeout > DateTime.Now)
                {
                    Thread.Sleep(5);
                }

                Assert.IsTrue(module.DeferredMessageReturned, "Deferred message was never returned.");
                Assert.IsTrue(module.MessageHandled, "Deferred message was never handled.");

                Assert.IsTrue(configuration.Inbox.ErrorQueue.IsEmpty());
                Assert.IsNull(configuration.Inbox.DeferredQueue.GetMessage());
                Assert.IsNull(configuration.Inbox.WorkQueue.GetMessage());
            }

            AttemptDropQueues(workQueueUriFormat, errorQueueUriFormat);
        }
Ejemplo n.º 5
0
        protected void TestInboxThroughput(string workQueueUriFormat, string errorQueueUriFormat, int timeoutMilliseconds,
										   int count, bool isTransactional)
        {
            const int threadCount = 1;
            var padlock = new object();
            var configuration = GetConfiguration(workQueueUriFormat, errorQueueUriFormat, threadCount, isTransactional);

            Console.WriteLine("Sending {0} messages to input queue '{1}'.", count, configuration.Inbox.WorkQueue.Uri);

            var sw = new Stopwatch();

            using (var bus = new ServiceBus(configuration))
            {
                for (var i = 0; i < 5; i++)
                {
                    var warmup = bus.CreateTransportMessage(new SimpleCommand("warmup"));

                    configuration.Inbox.WorkQueue.Enqueue(warmup.MessageId, configuration.Serializer.Serialize(warmup));
                }

                var idleThreads = new List<int>();

                bus.Events.ThreadWaiting += (sender, args) =>
                    {
                        lock (padlock)
                        {
                            if (idleThreads.Contains(Thread.CurrentThread.ManagedThreadId))
                            {
                                return;
                            }

                            idleThreads.Add(Thread.CurrentThread.ManagedThreadId);
                        }
                    };

                bus.Start();

                while (idleThreads.Count < threadCount)
                {
                    Thread.Sleep(25);
                }

                bus.Stop();

                sw.Start();

                for (var i = 0; i < count; i++)
                {
                    var message = bus.CreateTransportMessage(new SimpleCommand("command " + i));

                    configuration.Inbox.WorkQueue.Enqueue(message.MessageId, configuration.Serializer.Serialize(message));
                }

                sw.Stop();

                Console.WriteLine("Took {0} ms to send {1} messages.  Starting processing.", sw.ElapsedMilliseconds,
                                  count);

                idleThreads.Clear();
                bus.Start();

                sw.Reset();
                sw.Start();

                while (idleThreads.Count < threadCount)
                {
                    Thread.Sleep(25);
                }

                sw.Stop();
            }

            AttemptDropQueues(workQueueUriFormat, errorQueueUriFormat);

            var ms = sw.ElapsedMilliseconds;

            Console.WriteLine("Processed {0} messages in {1} ms", count, ms);

            Assert.IsTrue(ms < timeoutMilliseconds,
                          "Should be able to process at least {0} messages in {1} ms but it ook {2} ms.",
                          count, timeoutMilliseconds, ms);
        }
Ejemplo n.º 6
0
        protected void TestInboxError(string workQueueUriFormat, string errorQueueUriFormat, bool isTransactional)
        {
            var padlock = new object();
            var configuration = GetConfiguration(workQueueUriFormat, errorQueueUriFormat, 1, isTransactional);

            using (var bus = new ServiceBus(configuration))
            {
                var message = bus.CreateTransportMessage(new NoHandlerCommand());

                configuration.Inbox.WorkQueue.Enqueue(message.MessageId, configuration.Serializer.Serialize(message));

                var idleThreads = new List<int>();

                bus.Events.ThreadWaiting += (sender, args) =>
                    {
                        lock (padlock)
                        {
                            if (idleThreads.Contains(Thread.CurrentThread.ManagedThreadId))
                            {
                                return;
                            }

                            idleThreads.Add(Thread.CurrentThread.ManagedThreadId);
                        }
                    };

                bus.Start();

                while (idleThreads.Count < 1)
                {
                    Thread.Sleep(5);
                }

                Assert.NotNull(configuration.Inbox.ErrorQueue.GetMessage());
            }

            AttemptDropQueues(workQueueUriFormat, errorQueueUriFormat);
        }
Ejemplo n.º 7
0
        protected void TestIdempotenceProcessing(string workQueueUriFormat, string errorQueueUriFormat, bool isTransactional, bool enqueueUniqueMessages)
        {
            const int messageCount = 200;

            var configuration = GetInboxConfiguration(workQueueUriFormat, errorQueueUriFormat, 5, isTransactional);
            var padlock = new object();

            using (var bus = new ServiceBus(configuration))
            {
                if (enqueueUniqueMessages)
                {
                    for (int i = 0; i < messageCount; i++)
                    {
                        var message = bus.CreateTransportMessage(new IdempotenceCommand());

                        message.RecipientInboxWorkQueueUri = configuration.Inbox.WorkQueue.Uri.ToString();

                        configuration.Inbox.WorkQueue.Enqueue(message.MessageId, configuration.Serializer.Serialize(message));
                    }
                }
                else
                {
                    var message = bus.CreateTransportMessage(new IdempotenceCommand());

                    message.RecipientInboxWorkQueueUri = configuration.Inbox.WorkQueue.Uri.ToString();

                    for (int i = 0; i < messageCount; i++)
                    {
                        configuration.Inbox.WorkQueue.Enqueue(message.MessageId, configuration.Serializer.Serialize(message));
                    }
                }

                var idleThreads = new List<int>();

                bus.Events.ThreadWaiting += (sender, args) =>
                {
                    lock (padlock)
                    {
                        if (idleThreads.Contains(Thread.CurrentThread.ManagedThreadId))
                        {
                            return;
                        }

                        idleThreads.Add(Thread.CurrentThread.ManagedThreadId);
                    }
                };

                bus.Start();

                while (idleThreads.Count < 5)
                {
                    Thread.Sleep(5);
                }

                Assert.IsNull(configuration.Inbox.ErrorQueue.GetMessage());
                Assert.IsNull(configuration.Inbox.WorkQueue.GetMessage());

                if (enqueueUniqueMessages)
                {
                    Assert.AreEqual(messageCount, ((IdempotenceMessageHandlerFactory)bus.Configuration.MessageHandlerFactory).ProcessedCount);
                }
                else
                {
                    Assert.AreEqual(1, ((IdempotenceMessageHandlerFactory)bus.Configuration.MessageHandlerFactory).ProcessedCount);
                }
            }

            AttemptDropQueues(workQueueUriFormat, errorQueueUriFormat);
        }
Ejemplo n.º 8
0
        public void TestResourceUsage(string queueUriFormat, bool isTransactional)
        {
            const int threadCount = 5;

            var configuration = GetConfiguration(queueUriFormat, isTransactional, threadCount);

            var cpuCounter = new PerformanceCounterValue(new PerformanceCounter
                {
                    CategoryName = "Processor",
                    CounterName = "% Processor Time",
                    InstanceName = "_Total"
                });

            var padlock = new object();
            var idleThreads = new List<int>();
            var startDate = DateTime.Now;
            var endDate = startDate.AddSeconds(10);
            var iteration = 0;
            float cpuUsageLimit;
            float cpuMaximumUsage = 0f;

            cpuCounter.NextValue();
            Thread.Sleep(1000);
            cpuUsageLimit = cpuCounter.NextValue() + 25F;

            using (var bus = new ServiceBus(configuration).Start())
            {
                bus.Events.ThreadWaiting += (sender, args) =>
                {
                    lock (padlock)
                    {
                        if (idleThreads.Contains(Thread.CurrentThread.ManagedThreadId))
                        {
                            return;
                        }

                        idleThreads.Add(Thread.CurrentThread.ManagedThreadId);
                    }
                };

                while (DateTime.Now < endDate)
                {
                    iteration++;

                    for (var i = 0; i < 5; i++)
                    {
                        var message = bus.CreateTransportMessage(new SimpleCommand("[resource testing]"),
                                                                c => c.WithRecipient(configuration.Inbox.WorkQueue));

                        configuration.Inbox.WorkQueue.Enqueue(message.MessageId, configuration.Serializer.Serialize(message));
                    }

                    idleThreads.Clear();

                    Console.WriteLine("[checking usage] : iteration = {0}", iteration);

                    while (idleThreads.Count < threadCount)
                    {
                        var cpuUsage = cpuCounter.NextValue();

                        if (cpuUsage > cpuMaximumUsage)
                        {
                            cpuMaximumUsage = cpuUsage;
                        }

                        Assert.IsTrue(cpuUsage < cpuUsageLimit,
                                      string.Format("[EXCEEDED] : cpu usage = {0} / limit = {1}", cpuUsage, cpuUsageLimit));

                        Thread.Sleep(25);
                    }
                }

                Console.WriteLine("[done] : started = '{0}' / end = '{1}'", startDate, endDate);
                Console.WriteLine("[CPU] : maximum usage = {0} / cpu usage limit = {1}", cpuMaximumUsage, cpuUsageLimit);
            }
        }
Ejemplo n.º 9
0
        protected void TestInboxError(string queueSchemeAndHost, bool useJournal)
        {
            var configuration = GetTestInboxConfiguration(queueSchemeAndHost, useJournal, 1);

            using (var bus = new ServiceBus(configuration))
            {
                var message = bus.CreateTransportMessage(new NoHandlerCommand());

                configuration.Inbox.WorkQueue.Enqueue(message.MessageId, configuration.Serializer.Serialize(message));

                var working = true;

                bus.Events.ThreadWaiting += (sender, args) => { working = false; };

                bus.Start();

                while (working)
                {
                    Thread.Sleep(5);
                }
            }

            Assert.NotNull(configuration.Inbox.ErrorQueue.Dequeue());
        }
Ejemplo n.º 10
0
        protected void TestInboxThroughput(string queueSchemeAndHost, int count,
                                           int timeoutMilliseconds, bool useJournal)
        {
            var configuration = GetTestInboxConfiguration(queueSchemeAndHost, useJournal, 1);

            Console.WriteLine("Sending {0} messages to input queue '{1}'.", count, configuration.Inbox.WorkQueue.Uri);

            var sw = new Stopwatch();

            using (var bus = new ServiceBus(configuration))
            {
                for (var i = 0; i < 5; i++)
                {
                    var warmup = bus.CreateTransportMessage(new SimpleCommand("warmup"));

                    configuration.Inbox.WorkQueue.Enqueue(warmup.MessageId, configuration.Serializer.Serialize(warmup));
                }

                var working = true;

                bus.Events.ThreadWaiting += (sender, args) => { working = false; };

                bus.Start();

                while (working)
                {
                    Thread.Sleep(25);
                }

                bus.Stop();

                sw.Start();

                for (var i = 0; i < count; i++)
                {
                    var message = bus.CreateTransportMessage(new SimpleCommand("command " + i));

                    configuration.Inbox.WorkQueue.Enqueue(message.MessageId, configuration.Serializer.Serialize(message));
                }

                sw.Stop();

                Console.WriteLine("Took {0} ms to send {1} messages.  Starting processing.", sw.ElapsedMilliseconds,
                                  count);

                bus.Start();

                sw.Reset();
                sw.Start();

                working = true;

                while (working)
                {
                    Thread.Sleep(25);
                }

                sw.Stop();
            }

            var ms = sw.ElapsedMilliseconds;

            Console.WriteLine("Processed {0} messages in {1} ms", count, ms);

            Assert.IsTrue(ms < timeoutMilliseconds,
                          "Should be able to process at least {0} messages in {1} ms but it ook {2} ms.",
                          count, timeoutMilliseconds, ms);
        }
Ejemplo n.º 11
0
        protected void TestInboxConcurrency(string queueSchemeAndHost, bool useJournal, int msToComplete)
        {
            const int COUNT = 10;

            var padlock = new object();
            var configuration = GetTestInboxConfiguration(queueSchemeAndHost, useJournal, COUNT);
            var afterDequeueDate = new List<DateTime>();
            var offsetDate = DateTime.MinValue;

            using (var bus = new ServiceBus(configuration))
            {
                for (var i = 0; i < COUNT; i++)
                {
                    var message = bus.CreateTransportMessage(new ConcurrentCommand
                        {
                            MessageIndex = i
                        });

                    configuration.Inbox.WorkQueue.Enqueue(message.MessageId, configuration.Serializer.Serialize(message));
                }

                var idleCount = 0;

                bus.Events.ThreadWaiting += (sender, args) =>
                    {
                        lock (padlock)
                        {
                            idleCount++;
                        }
                    };

                bus.Events.AfterDequeueStream += (sender, args) =>
                    {
                        lock (padlock)
                        {
                            if (offsetDate == DateTime.MinValue)
                            {
                                offsetDate = DateTime.Now;

                                Console.WriteLine("Offset date: {0}", offsetDate.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                            }

                            var dateTime = DateTime.Now;

                            afterDequeueDate.Add(dateTime);

                            Console.WriteLine("Dequeued date: {0}", dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                        }
                    };

                bus.Start();

                while (idleCount < COUNT)
                {
                    Thread.Sleep(50);
                }
            }

            Assert.AreEqual(COUNT, afterDequeueDate.Count,
                            string.Format("Dequeued {0} messages but {1} were sent.", afterDequeueDate.Count, COUNT));

            foreach (var dateTime in afterDequeueDate)
            {
                Assert.IsTrue(dateTime.Subtract(offsetDate) < TimeSpan.FromMilliseconds(msToComplete),
                              "All dequeued messages have to be within {0} ms of first dequeue.", msToComplete);
            }
        }