AddTestListener() public method

public AddTestListener ( ) : void
return void
        public void Failure_Queue_Receives_Messages_When_Retries_Maxed()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime <IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);
                tester.OnlyRetryOnce();

                string message            = "Publish this message";
                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(3);

                tester.AddTestListener();
                tester.AddTestSubscription(ci, new PassThroughMessageFilter());

                tester.WaitForDeliveriesAndFailures(1, 3, TimeSpan.FromSeconds(10), () =>
                {
                    serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                });

                Assert.AreEqual(0, ci.PublishedCount);

                MessageDelivery delivery = serviceBusRuntime.ServiceLocator.GetInstance <QueuedDeliveryCore>().FailureQueue.Dequeue(TimeSpan.FromSeconds(1));
                Assert.IsNotNull(delivery);

                tester.VerifyQueuesEmpty();
                Assert.AreEqual(3, ((IEnumerable <string>)delivery.Context[MessageDelivery.Exceptions]).Count());
            }
        }
        public void MessageFilter_Properly_Excludes_Messages()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime <IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);

                string message = "Publish this message";

                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(1);

                tester.OnlyRetryOnce();

                tester.AddTestListener();
                tester.AddTestSubscription(ci, new BooleanMessageFilter(false));


                try
                {
                    tester.WaitForDeliveries(2, TimeSpan.FromSeconds(5), () =>
                    {
                        serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                    });
                }
                catch (TimeoutException) // should timeout while waiting
                {
                }

                Assert.AreEqual(0, ci.PublishedCount);

                tester.VerifyQueuesEmpty();
            }
        }
        public void MessageFilter_Properly_Includes_Messages()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime <IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);
                tester.OnlyRetryOnce();

                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(0);

                tester.AddTestListener();
                tester.AddTestSubscription(ci, new BooleanMessageFilter(true));

                string message = "Publish this message";

                tester.WaitForDeliveries(2, TimeSpan.FromMinutes(1), () =>
                {
                    serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                });

                Assert.AreEqual(1, ci.PublishedCount);
                Assert.AreEqual(message, ci.PublishedMessages[0]);

                tester.VerifyQueuesEmpty();
            }
        }
        public void Retry_Queue_Receives_Initial_Failures()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime <IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);

                string message            = "Publish this message";
                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(1);

                tester.OnlyRetryOnce();
                tester.AddTestListener();

                tester.AddTestSubscription(ci, new PassThroughMessageFilter());

                bool failFirst     = false;
                bool deliverSecond = false;

                tester.StartAndStop(() =>
                {
                    CountdownLatch latch = new CountdownLatch(2 + 1);

                    serviceBusRuntime.MessageDelivered += (o, mdea) =>
                    {
                        int tick; if ((tick = latch.Tick()) == 0)
                        {
                            deliverSecond = true;
                        }
                        Console.WriteLine("Tick deliver " + tick);
                    };
                    serviceBusRuntime.MessageDeliveryFailed += (o, mdfea) =>
                    {
                        int tick; if ((tick = latch.Tick()) == 1)
                        {
                            failFirst = true;
                        }
                        Console.WriteLine("Tick fail " + tick);
                    };

                    serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));

                    // Wait for delivery
                    latch.Handle.WaitOne(TimeSpan.FromMinutes(1), false); // give it a minute
                });

                Assert.AreEqual(true, failFirst);
                Assert.AreEqual(true, deliverSecond);

                Assert.AreEqual(1, ci.PublishedCount);
                Assert.AreEqual(message, ci.PublishedMessages[0]);


                tester.VerifyQueuesEmpty();
            }
        }
        public void Can_Deliver_Many_Messages_With_Failures()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime <IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);
                tester.OnlyRetryOnce();

                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(0);
                ci.SetFailInterval(10);

                QueuedDeliveryCore qc = serviceBusRuntime.ServiceLocator.GetInstance <QueuedDeliveryCore>();
                qc.RetryDelay = 1;

                tester.AddTestListener();
                tester.AddTestSubscription(ci, new PassThroughMessageFilter());

                int messageCount = 1000;

                DateTime start = DateTime.Now;


                tester.WaitForDeliveries(messageCount * 2, TimeSpan.FromMinutes(1), () =>
                {
                    for (int i = 0; i < messageCount; i++)
                    {
                        string message = i.ToString();
                        serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                    }
                });


                bool[] results = new bool[messageCount];
                // Wait for delivery
                DateTime end = DateTime.Now;
                System.Diagnostics.Trace.TraceInformation("Time to deliver " + messageCount + " = " + (end - start));

                for (int i = 0; i < ci.PublishedCount; i++)
                {
                    results[Convert.ToInt32(ci.PublishedMessages[i])] = true;
                }

                for (int i = 0; i < messageCount; i++)
                {
                    Assert.IsTrue(results[i]);
                }

                Assert.AreEqual(messageCount, ci.PublishedCount);
                tester.VerifyQueuesEmpty();
            }
        }
        public void Can_Deliver_Many_Messages()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime <IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);

                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(0);

                tester.AddTestListener();
                tester.AddTestSubscription(ci, new PassThroughMessageFilter());

                int messageCount = 1000;


                for (int i = 0; i < messageCount; i++)
                {
                    string message = i.ToString();
                    serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                }

                DateTime start = DateTime.Now;

                tester.WaitForDeliveries(messageCount * 2, TimeSpan.FromMinutes(1), () =>
                {
                });


                bool[] results = new bool[messageCount];

                DateTime end = DateTime.Now;

                System.Diagnostics.Trace.TraceInformation("Time to deliver messages " + messageCount + " = " + (end - start));

                Assert.AreEqual(messageCount, ci.PublishedCount);

                for (int i = 0; i < ci.PublishedCount; i++)
                {
                    int j = Convert.ToInt32(ci.PublishedMessages[i]);
                    results[j] = true;
                }

                for (int i = 0; i < messageCount; i++)
                {
                    Assert.IsTrue(results[i]);
                }

                tester.VerifyQueuesEmpty();
            }
        }
        public void Dispatcher_Receives_Messages()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime <IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);

                string message            = "Publish this message";
                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(0);

                tester.AddTestListener();
                tester.AddTestSubscription(ci, new PassThroughMessageFilter());

                tester.WaitForDeliveries(2, TimeSpan.FromSeconds(50), () =>
                {
                    serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                });

                Assert.AreEqual(1, ci.PublishedCount);
                Assert.AreEqual(message, ci.PublishedMessages[0]);

                tester.VerifyQueuesEmpty();
            }
        }
        public void MessageFilter_Properly_Includes_Messages()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime<IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);
                tester.OnlyRetryOnce();

                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(0);

                tester.AddTestListener();
                tester.AddTestSubscription(ci, new BooleanMessageFilter(true));

                string message = "Publish this message";

                tester.WaitForDeliveries(2, TimeSpan.FromMinutes(1), ()=>
                {
                    serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                });

                Assert.AreEqual(1, ci.PublishedCount);
                Assert.AreEqual(message, ci.PublishedMessages[0]);

                tester.VerifyQueuesEmpty();
            }
        }
        public void Retry_Queue_Receives_Initial_Failures()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime<IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);

                string message = "Publish this message";
                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(1);

                tester.OnlyRetryOnce();
                tester.AddTestListener();

                tester.AddTestSubscription(ci, new PassThroughMessageFilter());

                bool failFirst = false;
                bool deliverSecond = false;

                tester.StartAndStop(() =>
                {
                    CountdownLatch latch = new CountdownLatch(2+1);

                    serviceBusRuntime.MessageDelivered += (o, mdea) =>
                    {
                        int tick; if ((tick = latch.Tick()) == 0) deliverSecond = true; Console.WriteLine("Tick deliver " + tick);
                    };
                    serviceBusRuntime.MessageDeliveryFailed += (o, mdfea) =>
                    {
                        int tick; if ((tick = latch.Tick()) == 1) failFirst = true; Console.WriteLine("Tick fail " + tick);
                    };

                    serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));

                    // Wait for delivery
                    latch.Handle.WaitOne(TimeSpan.FromMinutes(1), false); // give it a minute

                });

                Assert.AreEqual(true, failFirst);
                Assert.AreEqual(true, deliverSecond);

                Assert.AreEqual(1, ci.PublishedCount);
                Assert.AreEqual(message, ci.PublishedMessages[0]);

                tester.VerifyQueuesEmpty();
            }
        }
        public void MessageFilter_Properly_Excludes_Messages()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime<IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);

                string message = "Publish this message";

                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(1);

                tester.OnlyRetryOnce();

                tester.AddTestListener();
                tester.AddTestSubscription(ci, new BooleanMessageFilter(false));

                try
                {
                    tester.WaitForDeliveries(2, TimeSpan.FromSeconds(5), () =>
                    {
                        serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                    });
                }
                catch (TimeoutException) // should timeout while waiting
                {
                }

                Assert.AreEqual(0, ci.PublishedCount);

                tester.VerifyQueuesEmpty();
            }
        }
        public void Failure_Queue_Receives_Messages_When_Retries_Maxed()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime<IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);
                tester.OnlyRetryOnce();

                string message = "Publish this message";
                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(3);

                tester.AddTestListener();
                tester.AddTestSubscription(ci, new PassThroughMessageFilter());

                tester.WaitForDeliveriesAndFailures(1, 3, TimeSpan.FromSeconds(10), () =>
                {
                    serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                });

                Assert.AreEqual(0, ci.PublishedCount);

                MessageDelivery delivery = serviceBusRuntime.ServiceLocator.GetInstance<QueuedDeliveryCore>().FailureQueue.Dequeue(TimeSpan.FromSeconds(1));
                Assert.IsNotNull(delivery);

                tester.VerifyQueuesEmpty();
                Assert.AreEqual(3, ((IEnumerable<string>)delivery.Context[MessageDelivery.Exceptions]).Count());
            }
        }
        public void Dispatcher_Receives_Messages()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime<IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);

                string message = "Publish this message";
                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(0);

                tester.AddTestListener();
                tester.AddTestSubscription(ci, new PassThroughMessageFilter());

                tester.WaitForDeliveries(2, TimeSpan.FromSeconds(50), () =>
                {
                    serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                });

                Assert.AreEqual(1, ci.PublishedCount);
                Assert.AreEqual(message, ci.PublishedMessages[0]);

                tester.VerifyQueuesEmpty();
            }
        }
        public void Can_Deliver_Many_Messages_With_Failures()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime<IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);
                tester.OnlyRetryOnce();

                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(0);
                ci.SetFailInterval(10);

                QueuedDeliveryCore qc = serviceBusRuntime.ServiceLocator.GetInstance<QueuedDeliveryCore>();
                qc.RetryDelay = 1;

                tester.AddTestListener();
                tester.AddTestSubscription(ci, new PassThroughMessageFilter());

                int messageCount = 1000;

                DateTime start = DateTime.Now;

                tester.WaitForDeliveries(messageCount*2, TimeSpan.FromMinutes(1), () =>
                {
                    for (int i = 0; i < messageCount; i++)
                    {
                        string message = i.ToString();
                        serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                    }
                });

                bool[] results = new bool[messageCount];
                // Wait for delivery
                DateTime end = DateTime.Now;
                System.Diagnostics.Trace.TraceInformation("Time to deliver " + messageCount + " = " + (end - start));

                for (int i = 0; i < ci.PublishedCount; i++)
                {
                    results[Convert.ToInt32(ci.PublishedMessages[i])] = true;
                }

                for (int i = 0; i < messageCount; i++)
                {
                    Assert.IsTrue(results[i]);
                }

                Assert.AreEqual(messageCount, ci.PublishedCount);
                tester.VerifyQueuesEmpty();
            }
        }
        public void Can_Deliver_Many_Messages()
        {
            using (var serviceBusRuntime = Create.MsmqRuntime<IContract>())
            {
                ServiceBusTest tester = new ServiceBusTest(serviceBusRuntime);

                ContractImplementation ci = new ContractImplementation();
                ci.SetFailCount(0);

                tester.AddTestListener();
                tester.AddTestSubscription(ci, new PassThroughMessageFilter());

                int messageCount = 1000;

                for (int i = 0; i < messageCount; i++)
                {
                    string message = i.ToString();
                    serviceBusRuntime.PublishOneWay(new PublishRequest(typeof(IContract), "PublishThis", message));
                }

                DateTime start = DateTime.Now;

                tester.WaitForDeliveries(messageCount*2, TimeSpan.FromMinutes(1), () =>
                {

                });

                bool[] results = new bool[messageCount];

                DateTime end = DateTime.Now;

                System.Diagnostics.Trace.TraceInformation("Time to deliver messages "+messageCount+" = "+(end - start));

                Assert.AreEqual(messageCount, ci.PublishedCount);

                for(int i = 0; i < ci.PublishedCount; i++)
                {
                    int j = Convert.ToInt32(ci.PublishedMessages[i]);
                    results[j] = true;
                }

                for (int i = 0; i < messageCount; i++)
                {
                    Assert.IsTrue(results[i]);
                }

                tester.VerifyQueuesEmpty();
            }
        }