public void AwaitReturnsImmediatelyIfCountIsZero()
        {
            CountDownLatch l = new CountDownLatch(1);
            l.CountDown();

            ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
                {
                    Assert.IsTrue(l.Count == 0);
                    l.Await();

                });
            ThreadManager.JoinAndVerify();
        }
        public void TestListenerTransactionalFails()
        {
            this.transactional = true;
            var latch = new CountDownLatch(this.messageCount);
            this.container = this.CreateContainer(new TxTestListener(latch, true, this));
            for (var i = 0; i < this.txSize; i++)
            {
                this.template.ConvertAndSend(this.queue.Name, i + "foo");
            }

            var timeout = Math.Min(1 + this.messageCount / (4 * this.concurrentConsumers), 30);
            logger.Debug("Waiting for messages with timeout = " + timeout + " (s)");
            var waited = latch.Await(new TimeSpan(0, 0, 0, timeout));
            Assert.True(waited, "Timed out waiting for message");
            Assert.Null(this.template.ReceiveAndConvert(this.queue.Name));
        }
 public void TestBlockingReceiveWithTimeout()
 {
     QueueChannel channel = new QueueChannel();
     AtomicBoolean receiveInterrupted = new AtomicBoolean(false);
     CountDownLatch latch = new CountDownLatch(1);
     Thread t = new Thread(new ThreadStart(delegate
                                               {
                                                   IMessage message = channel.Receive(new TimeSpan(10000));
                                                   receiveInterrupted.Value = true;
                                                   Assert.IsTrue(message == null);
                                                   latch.CountDown();
                                               }));
     t.Start();
     //Assert.IsFalse(receiveInterrupted.Value);
     t.Interrupt();
     latch.Await();
     Assert.IsTrue(receiveInterrupted.Value);
 }
        public void AwaitReturnsAfterCountDownToZeroButNotBefore()
        {
            CountDownLatch l = new CountDownLatch(2);

            ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
                    {
                        Assert.IsTrue(l.Count > 0);
                        l.Await();
                        Assert.IsTrue(l.Count == 0);

                    });
            Assert.AreEqual(l.Count, 2);

            Thread.Sleep(Delays.Short);
            l.CountDown();
            Assert.AreEqual(l.Count, 1);
            l.CountDown();
            Assert.AreEqual(l.Count, 0);
            ThreadManager.JoinAndVerify();
        }
 public void TestMultipleMessagesWithResponseCorrelator()
 {
     IApplicationContext context = TestUtils.GetContext(@"Gateway\gatewayWithResponseCorrelator.xml");
     const int numRequests = 500;
     ITestService service = (ITestService) context.GetObject("proxy");
     string[] results = new string[numRequests];
     CountDownLatch latch = new CountDownLatch(numRequests);
     IExecutor executor = Executors.NewFixedThreadPool(numRequests);
     for (int i = 0; i < numRequests; i++)
     {
         int count = i;
         executor.Execute(delegate
                              {
                                  // add some randomness to the ordering of requests
                                  try
                                  {
                                      Thread.Sleep(new Random().Next(100));
                                  }
                                  catch (ThreadInterruptedException e)
                                  {
                                      // ignore
                                  }
                                  results[count] = service.RequestReply("test-" + count);
                                  latch.CountDown();
                              });
     }
     latch.Await(TimeSpan.FromSeconds(10));
     for (int i = 0; i < numRequests; i++)
     {
         Assert.That(results[i], Is.EqualTo("test-" + i + "!!!"));
     }
     TestChannelInterceptor interceptor = (TestChannelInterceptor) context.GetObject("interceptor");
     Assert.That(interceptor.SentCount, Is.EqualTo(numRequests));
     Assert.That(interceptor.ReceivedCount, Is.EqualTo(numRequests));
 }
 public void TestBlockingSendWithNoTimeout()
 {
     QueueChannel channel = new QueueChannel(1);
     bool result1 = channel.Send(new Message<string>("test-1"));
     Assert.IsTrue(result1);
     AtomicBoolean SendInterrupted = new AtomicBoolean(false);
     CountDownLatch latch = new CountDownLatch(1);
     Thread t = new Thread(new ThreadStart(delegate
                                               {
                                                   channel.Send(new Message<string>("test-2"));
                                                   SendInterrupted.Value = true;
                                                   latch.CountDown();
                                               }));
     t.Start();
     //Assert.IsFalse(SendInterrupted.Value);
     t.Interrupt();
     latch.Await();
     Assert.IsTrue(SendInterrupted.Value);
 }
        public void TestSimpleSendAndReceive()
        {
            AtomicBoolean messageReceived = new AtomicBoolean(false);
            CountDownLatch latch = new CountDownLatch(1);
            QueueChannel channel = new QueueChannel();
            new Thread(new ThreadStart(delegate
                                           {
                                               IMessage message = channel.Receive();
                                               if (message != null)
                                               {
                                                   messageReceived.Value = true;
                                                   latch.CountDown();
                                               }
                                           })).Start();

            Assert.That(messageReceived.Value, Is.False);
            channel.Send(new Message<string>("testing"));
            latch.Await(new TimeSpan(0, 0, 0, 0, 25));
            Assert.That(messageReceived.Value, Is.True);
        }
        public void TestImmediateReceive()
        {
            AtomicBoolean messageReceived = new AtomicBoolean(false);
            QueueChannel channel = new QueueChannel();
            CountDownLatch latch1 = new CountDownLatch(1);
            CountDownLatch latch2 = new CountDownLatch(1);
            IExecutor singleThreadExecutor = Executors.NewSingleThreadExecutor();

            singleThreadExecutor.Execute(
                delegate
                    {
                        IMessage message = channel.Receive(TimeSpan.Zero);
                        if (message != null)
                        {
                            messageReceived.Value = true;
                        }
                        latch1.CountDown();
                    });

            latch1.Await();
            singleThreadExecutor.Execute(
                delegate { channel.Send(new Message<string>("testing")); });

            Assert.IsFalse(messageReceived.Value);
            singleThreadExecutor.Execute(
                delegate
                    {
                        IMessage message = channel.Receive(TimeSpan.Zero);
                        if (message != null)
                        {
                            messageReceived.Value = true;
                        }
                        latch2.CountDown();
                    });

            latch2.Await();
            Assert.IsNotNull(messageReceived.Value);
        }
        public void TimedAwaitTimesOutIfNotCountedDownBeforeTimeout()
        {
            CountDownLatch l = new CountDownLatch(1);
            ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
                    {
                        Assert.IsTrue(l.Count > 0);
                        Assert.IsFalse(l.Await(Delays.Short));
                        Assert.IsTrue(l.Count > 0);

                    });
            Assert.AreEqual(l.Count, 1);
            ThreadManager.JoinAndVerify();
            Assert.AreEqual(l.Count, 1);
        }
        public void TimedAwaitReturnsImmediatelyIfCountIsZero()
        {
            CountDownLatch l = new CountDownLatch(1);
            l.CountDown();

            ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
                {
                    Assert.IsTrue(l.Count == 0);
                    Assert.IsTrue(l.Await(Delays.Medium));
                    Assert.IsTrue(l.Await(TimeSpan.Zero));
                });
            ThreadManager.JoinAndVerify(Delays.Short);
        }
        public void TimedAwaitReturnsFalseOnNonPositiveWaitTime()
        {
            CountDownLatch l = new CountDownLatch(1);

            ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
                {
                    Assert.IsTrue(l.Count == 1);
                    Assert.IsFalse(l.Await(TimeSpan.Zero));
                });
            ThreadManager.JoinAndVerify(Delays.Short);
        }
 public void TimedAwaitChokesIfInterruptedBeforeCountedDown()
 {
     CountDownLatch l = new CountDownLatch(1);
     Thread t = ThreadManager.StartAndAssertRegistered(
         "T1",
         delegate
             {
                 Assert.IsTrue(l.Count > 0);
                 Assert.Throws<ThreadInterruptedException>(()=>l.Await(Delays.Medium));
             });
     Thread.Sleep(Delays.Short);
     Assert.AreEqual(l.Count, 1);
     t.Interrupt();
     ThreadManager.JoinAndVerify();
 }
 public void sendWithReturnAddress()
 {
     IList<string> replies = new List<string>(3);
     CountDownLatch latch = new CountDownLatch(3);
     IMessageChannel replyChannel = new SendWithReturnAddressChannel(replies, latch);
     MessageChannelTemplate template = new MessageChannelTemplate();
     IMessage message1 = MessageBuilder.WithPayload("test1").SetReplyChannel(replyChannel).Build();
     IMessage message2 = MessageBuilder.WithPayload("test2").SetReplyChannel(replyChannel).Build();
     IMessage message3 = MessageBuilder.WithPayload("test3").SetReplyChannel(replyChannel).Build();
     template.Send(message1, requestChannel);
     template.Send(message2, requestChannel);
     template.Send(message3, requestChannel);
     latch.Await(TimeSpan.FromMilliseconds(2000));
     Assert.That(latch.Count, Is.EqualTo(0));
     Assert.IsTrue(replies.Contains("TEST1"));
     Assert.IsTrue(replies.Contains("TEST2"));
     Assert.IsTrue(replies.Contains("TEST3"));
 }
 public void TestListenerTransactionalSunnyDay()
 {
     transactional = true;
     CountDownLatch latch = new CountDownLatch(messageCount);
     container = CreateContainer(new TxTestListener(latch, false, this));
     for (int i = 0; i < messageCount; i++)
     {
         template.ConvertAndSend(queue.Name, i + "foo");
     }
     int timeout = Math.Min(1 + messageCount / (4 * concurrentConsumers), 30);
     logger.Debug("Waiting for messages with timeout = " + timeout + " (s)");
     var waited = latch.Await(new TimeSpan(0, 0, 0, timeout));
     Assert.True(waited, "Timed out waiting for message");
     Assert.Null(template.ReceiveAndConvert(queue.Name));
 }
        /// <summary>
        /// Does the sunny day test.
        /// </summary>
        /// <param name="latch">The latch.</param>
        /// <param name="listener">The listener.</param>
        /// <remarks></remarks>
        private void DoSunnyDayTest(CountDownLatch latch, object listener)
        {
            this.container = this.CreateContainer(listener);
            for (var i = 0; i < this.messageCount; i++)
            {
                this.template.ConvertAndSend(this.queue.Name, i + "foo");
            }

            var waited = latch.Await(new TimeSpan(0, 0, 0, Math.Max(2, this.messageCount / 40)));
            Assert.True(waited, "Timed out waiting for message");
            Assert.Null(this.template.ReceiveAndConvert(this.queue.Name));
        }
        /// <summary>
        /// Does the listener with exception test.
        /// </summary>
        /// <param name="latch">The latch.</param>
        /// <param name="listener">The listener.</param>
        /// <remarks></remarks>
        private void DoListenerWithExceptionTest(CountDownLatch latch, object listener)
        {
            this.container = this.CreateContainer(listener);
            if (this.acknowledgeMode.TransactionAllowed())
            {
                // Should only need one message if it is going to fail
                for (var i = 0; i < this.concurrentConsumers; i++)
                {
                    this.template.ConvertAndSend(this.queue.Name, i + "foo");
                }
            }
            else
            {
                for (var i = 0; i < this.messageCount; i++)
                {
                    this.template.ConvertAndSend(this.queue.Name, i + "foo");
                }
            }

            try
            {
                var waited = latch.Await(new TimeSpan(0, 0, 0, (5 + Math.Max(1, this.messageCount / 20))));
                Assert.True(waited, "Timed out waiting for message");
            }
            finally
            {
                // Wait for broker communication to finish before trying to stop
                // container
                Thread.Sleep(300);
                this.container.Shutdown();
                Thread.Sleep(300);
            }

            if (this.acknowledgeMode.TransactionAllowed())
            {
                Assert.NotNull(this.template.ReceiveAndConvert(this.queue.Name));
            }
            else
            {
                Assert.Null(this.template.ReceiveAndConvert(this.queue.Name));
            }
        }