Example #1
0
        public void CountDownDecrementsCountWhenPositiveAndHasNoEffectWhenZero()
        {
            CountDownLatch l = new CountDownLatch(1);

            Assert.AreEqual(1, l.Count);
            l.CountDown();
            Assert.AreEqual(0, l.Count);
            l.CountDown();
            Assert.AreEqual(0, l.Count);
        }
Example #2
0
        public void ToStringIndicatesCurrentCount()
        {
            CountDownLatch s  = new CountDownLatch(2);
            String         us = s.ToString();

            Assert.IsTrue(us.IndexOf("Count = 2") >= 0);
            s.CountDown();
            String s1 = s.ToString();

            Assert.IsTrue(s1.IndexOf("Count = 1") >= 0);
            s.CountDown();
            String s2 = s.ToString();

            Assert.IsTrue(s2.IndexOf("Count = 0") >= 0);
        }
Example #3
0
        public void CountReturnsInitialCountAndDecreasesAftercountDown()
        {
            CountDownLatch l = new CountDownLatch(2);

            Assert.AreEqual(2, l.Count);
            l.CountDown();
            Assert.AreEqual(1, l.Count);
        }
Example #4
0
        public void TimedAwaitReturnsAfterCountDownToZero()
        {
            CountDownLatch l = new CountDownLatch(2);

            ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
            {
                Assert.IsTrue(l.Count > 0);
                Assert.IsTrue(l.Await(Delays.Small));
            });
            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 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 AwaitReturnsImmediatelyIfCountIsZero()
        {
            CountDownLatch l = new CountDownLatch(1);
            l.CountDown();

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

                });
            ThreadManager.JoinAndVerify();
        }
Example #7
0
        public void AwaitReturnsImmediatelyIfCountIsZero()
        {
            CountDownLatch l = new CountDownLatch(1);

            l.CountDown();

            ThreadManager.StartAndAssertRegistered(
                "T1",
                delegate
            {
                Assert.IsTrue(l.Count == 0);
                l.Await();
            });
            ThreadManager.JoinAndVerify();
        }
Example #8
0
        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 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 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 CountReturnsInitialCountAndDecreasesAftercountDown()
 {
     CountDownLatch l = new CountDownLatch(2);
     Assert.AreEqual(2, l.Count);
     l.CountDown();
     Assert.AreEqual(1, l.Count);
 }
 public void CountDownDecrementsCountWhenPositiveAndHasNoEffectWhenZero()
 {
     CountDownLatch l = new CountDownLatch(1);
     Assert.AreEqual(1, l.Count);
     l.CountDown();
     Assert.AreEqual(0, l.Count);
     l.CountDown();
     Assert.AreEqual(0, l.Count);
 }
        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 ToStringIndicatesCurrentCount()
 {
     CountDownLatch s = new CountDownLatch(2);
     String us = s.ToString();
     Assert.IsTrue(us.IndexOf("Count = 2") >= 0);
     s.CountDown();
     String s1 = s.ToString();
     Assert.IsTrue(s1.IndexOf("Count = 1") >= 0);
     s.CountDown();
     String s2 = s.ToString();
     Assert.IsTrue(s2.IndexOf("Count = 0") >= 0);
 }