Package org.hornetq.utils

Examples of org.hornetq.utils.VariableLatch$CountSync


*/
public class VariableLatchTest extends UnitTestCase
{
   public void testTimeout() throws Exception
   {
      VariableLatch latch = new VariableLatch();

      latch.up();

      long start = System.currentTimeMillis();
      Assert.assertFalse(latch.waitCompletion(1000));
      long end = System.currentTimeMillis();

      Assert.assertTrue("Timeout didn't work correctly", end - start >= 1000 && end - start < 2000);
   }
View Full Code Here


{
   private static final Logger log = Logger.getLogger(VariableLatchTest.class);

   public void testLatchOnSingleThread() throws Exception
   {
      VariableLatch latch = new VariableLatch();

      for (int i = 1; i <= 100; i++)
      {
         latch.up();
         Assert.assertEquals(i, latch.getCount());
      }

      for (int i = 100; i > 0; i--)
      {
         Assert.assertEquals(i, latch.getCount());
         latch.down();
         Assert.assertEquals(i - 1, latch.getCount());
      }

      latch.waitCompletion();
   }
View Full Code Here

    *
    * @throws Exception
    */
   public void testLatchOnMultiThread() throws Exception
   {
      final VariableLatch latch = new VariableLatch();

      latch.up(); // We hold at least one, so ThreadWaits won't go away

      final int numberOfThreads = 100;
      final int numberOfAdds = 100;

      class ThreadWait extends Thread
      {
         private volatile boolean waiting = true;

         @Override
         public void run()
         {
            try
            {
               if (!latch.waitCompletion(5000))
               {
                  VariableLatchTest.log.error("Latch timed out");
               }
            }
            catch (Exception e)
            {
               VariableLatchTest.log.error(e);
            }
            waiting = false;
         }
      }

      class ThreadAdd extends Thread
      {
         private final CountDownLatch latchReady;

         private final CountDownLatch latchStart;

         ThreadAdd(final CountDownLatch latchReady, final CountDownLatch latchStart)
         {
            this.latchReady = latchReady;
            this.latchStart = latchStart;
         }

         @Override
         public void run()
         {
            try
            {
               latchReady.countDown();
               // Everybody should start at the same time, to worse concurrency
               // effects
               latchStart.await();
               for (int i = 0; i < numberOfAdds; i++)
               {
                  latch.up();
               }
            }
            catch (Exception e)
            {
               VariableLatchTest.log.error(e.getMessage(), e);
            }
         }
      }

      CountDownLatch latchReady = new CountDownLatch(numberOfThreads);
      CountDownLatch latchStart = new CountDownLatch(1);

      ThreadAdd[] threadAdds = new ThreadAdd[numberOfThreads];
      ThreadWait waits[] = new ThreadWait[numberOfThreads];

      for (int i = 0; i < numberOfThreads; i++)
      {
         threadAdds[i] = new ThreadAdd(latchReady, latchStart);
         threadAdds[i].start();
         waits[i] = new ThreadWait();
         waits[i].start();
      }

      latchReady.await();
      latchStart.countDown();

      for (int i = 0; i < numberOfThreads; i++)
      {
         threadAdds[i].join();
      }

      for (int i = 0; i < numberOfThreads; i++)
      {
         Assert.assertTrue(waits[i].waiting);
      }

      Assert.assertEquals(numberOfThreads * numberOfAdds + 1, latch.getCount());

      class ThreadDown extends Thread
      {
         private final CountDownLatch latchReady;

         private final CountDownLatch latchStart;

         ThreadDown(final CountDownLatch latchReady, final CountDownLatch latchStart)
         {
            this.latchReady = latchReady;
            this.latchStart = latchStart;
         }

         @Override
         public void run()
         {
            try
            {
               latchReady.countDown();
               // Everybody should start at the same time, to worse concurrency
               // effects
               latchStart.await();
               for (int i = 0; i < numberOfAdds; i++)
               {
                  latch.down();
               }
            }
            catch (Exception e)
            {
               VariableLatchTest.log.error(e.getMessage(), e);
            }
         }
      }

      latchReady = new CountDownLatch(numberOfThreads);
      latchStart = new CountDownLatch(1);

      ThreadDown down[] = new ThreadDown[numberOfThreads];

      for (int i = 0; i < numberOfThreads; i++)
      {
         down[i] = new ThreadDown(latchReady, latchStart);
         down[i].start();
      }

      latchReady.await();
      latchStart.countDown();

      for (int i = 0; i < numberOfThreads; i++)
      {
         down[i].join();
      }

      Assert.assertEquals(1, latch.getCount());

      for (int i = 0; i < numberOfThreads; i++)
      {
         Assert.assertTrue(waits[i].waiting);
      }

      latch.down();

      for (int i = 0; i < numberOfThreads; i++)
      {
         waits[i].join();
      }

      Assert.assertEquals(0, latch.getCount());

      for (int i = 0; i < numberOfThreads; i++)
      {
         Assert.assertFalse(waits[i].waiting);
      }
View Full Code Here

      }
   }

   public void testReuseLatch() throws Exception
   {
      final VariableLatch latch = new VariableLatch();
      latch.up();

      class ThreadWait extends Thread
      {
         private volatile boolean waiting = false;

         private volatile Exception e;

         private final CountDownLatch readyLatch = new CountDownLatch(1);

         @Override
         public void run()
         {
            waiting = true;
            readyLatch.countDown();
            try
            {
               if (!latch.waitCompletion(1000))
               {
                  VariableLatchTest.log.error("Latch timed out!", new Exception("trace"));
               }
            }
            catch (Exception e)
            {
               VariableLatchTest.log.error(e);
               this.e = e;
            }
            waiting = false;
         }
      }

      ThreadWait t = new ThreadWait();
      t.start();

      t.readyLatch.await();

      Assert.assertEquals(true, t.waiting);

      latch.down();

      t.join();

      Assert.assertEquals(false, t.waiting);

      Assert.assertNull(t.e);

      latch.up();

      t = new ThreadWait();
      t.start();

      t.readyLatch.await();

      Assert.assertEquals(true, t.waiting);

      latch.down();

      t.join();

      Assert.assertEquals(false, t.waiting);

      Assert.assertNull(t.e);

      Assert.assertTrue(latch.waitCompletion(1000));

      Assert.assertEquals(0, latch.getCount());

      latch.down();

      Assert.assertEquals(0, latch.getCount());

   }
View Full Code Here

TOP

Related Classes of org.hornetq.utils.VariableLatch$CountSync

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.