Package org.hornetq.utils

Examples of org.hornetq.utils.ReusableLatch$CountSync


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

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

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

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

      latch.await();
   }
View Full Code Here


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

      latch.countUp(); // 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.await(5000))
               {
                  ReusableLatchTest.log.error("Latch timed out");
               }
            }
            catch (Exception e)
            {
               ReusableLatchTest.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.countUp();
               }
            }
            catch (Exception e)
            {
               ReusableLatchTest.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.countDown();
               }
            }
            catch (Exception e)
            {
               ReusableLatchTest.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.countDown();

      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 ReusableLatch latch = new ReusableLatch(5);
      for (int i = 0 ; i < 5; i++)
      {
         latch.countDown();
      }
     
      latch.countUp();

      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.await(1000))
               {
                  ReusableLatchTest.log.error("Latch timed out!", new Exception("trace"));
               }
            }
            catch (Exception e)
            {
               ReusableLatchTest.log.error(e);
               this.e = e;
            }
            waiting = false;
         }
      }

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

      t.readyLatch.await();

      Assert.assertEquals(true, t.waiting);

      latch.countDown();

      t.join();

      Assert.assertEquals(false, t.waiting);

      Assert.assertNull(t.e);

      latch.countUp();

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

      t.readyLatch.await();

      Assert.assertEquals(true, t.waiting);

      latch.countDown();

      t.join();

      Assert.assertEquals(false, t.waiting);

      Assert.assertNull(t.e);

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

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

      latch.countDown();

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

   }
View Full Code Here

      setup(2, 60 * 1024, false);

      SimpleIDGenerator idGen = new SimpleIDGenerator(1000);

      final ReusableLatch reusableLatchDone = new ReusableLatch();
      reusableLatchDone.countUp();
      final ReusableLatch reusableLatchWait = new ReusableLatch();
      reusableLatchWait.countUp();

      journal = new JournalImpl(fileSize, minFiles, 0, 0, fileFactory, filePrefix, fileExtension, maxAIO)
      {

         @Override
         public void onCompactDone()
         {
            reusableLatchDone.countDown();
            System.out.println("Waiting on Compact");
            try
            {
               reusableLatchWait.await();
            }
            catch (InterruptedException e)
            {
               e.printStackTrace();
            }
            System.out.println("Done");
         }
      };

      journal.setAutoReclaim(false);

      startJournal();
      load();

      long consumerTX = idGen.generateID();

      long firstID = idGen.generateID();

      long appendTX = idGen.generateID();

      long addedRecord = idGen.generateID();

      Thread tCompact = new Thread()
      {
         @Override
         public void run()
         {
            try
            {
               journal.compact();
            }
            catch (Exception e)
            {
               e.printStackTrace();
            }
         }
      };

      tCompact.start();

      reusableLatchDone.await();

      addTx(consumerTX, firstID);

      addTx(appendTX, addedRecord);

      commit(appendTX);

      updateTx(consumerTX, addedRecord);

      commit(consumerTX);

      delete(addedRecord);

      reusableLatchWait.countDown();

      tCompact.join();

      journal.forceMoveNextFile();

View Full Code Here

   public void testDeleteWhileCleanup() throws Exception
   {

      setup(2, 60 * 1024, false);

      final ReusableLatch reusableLatchDone = new ReusableLatch();
      reusableLatchDone.countUp();
      final ReusableLatch reusableLatchWait = new ReusableLatch();
      reusableLatchWait.countUp();

      journal = new JournalImpl(fileSize, minFiles, 0, 0, fileFactory, filePrefix, fileExtension, maxAIO)
      {

         @Override
         public void onCompactDone()
         {
            reusableLatchDone.countDown();
            System.out.println("Waiting on Compact");
            try
            {
               reusableLatchWait.await();
            }
            catch (InterruptedException e)
            {
               e.printStackTrace();
            }
            System.out.println("Done");
         }
      };

      journal.setAutoReclaim(false);

      startJournal();
      load();

      Thread tCompact = new Thread()
      {
         @Override
         public void run()
         {
            try
            {
               journal.cleanUp(journal.getDataFiles()[0]);
            }
            catch (Exception e)
            {
               e.printStackTrace();
            }
         }
      };

      for (int i = 0; i < 100; i++)
      {
         add(i);
      }

      journal.forceMoveNextFile();

      for (int i = 10; i < 90; i++)
      {
         delete(i);
      }

      tCompact.start();

      reusableLatchDone.await();

      // Delete part of the live records while cleanup still working
      for (int i = 1; i < 5; i++)
      {
         delete(i);
      }

      reusableLatchWait.countDown();

      tCompact.join();

      // Delete part of the live records after cleanup is done
      for (int i = 5; i < 10; i++)
View Full Code Here

      setup(2, 60 * 1024, false);

      SimpleIDGenerator idGen = new SimpleIDGenerator(1000);

      final ReusableLatch reusableLatchDone = new ReusableLatch();
      reusableLatchDone.countUp();
      final ReusableLatch reusableLatchWait = new ReusableLatch();
      reusableLatchWait.countUp();

      journal = new JournalImpl(fileSize, minFiles, 0, 0, fileFactory, filePrefix, fileExtension, maxAIO)
      {

         @Override
         public void onCompactDone()
         {
            reusableLatchDone.countDown();
            System.out.println("Waiting on Compact");
            try
            {
               reusableLatchWait.await();
            }
            catch (InterruptedException e)
            {
               e.printStackTrace();
            }
            System.out.println("Done");
         }
      };

      journal.setAutoReclaim(false);

      startJournal();
      load();

      Thread tCompact = new Thread()
      {
         @Override
         public void run()
         {
            try
            {
               journal.compact();
            }
            catch (Exception e)
            {
               e.printStackTrace();
            }
         }
      };

      long appendTX = idGen.generateID();
      long appendOne = idGen.generateID();
      long appendTwo = idGen.generateID();

      long updateTX = idGen.generateID();

      addTx(appendTX, appendOne);

      tCompact.start();
      reusableLatchDone.await();

      addTx(appendTX, appendTwo);

      commit(appendTX);

      updateTx(updateTX, appendOne);
      updateTx(updateTX, appendTwo);

      commit(updateTX);
      // delete(appendTwo);

      reusableLatchWait.countDown();
      tCompact.join();

      journal.compact();

      stopJournal();
View Full Code Here

   {
      super.setUp();

      tCompact = null;

      startedCompactingLatch = new ReusableLatch(1);

      releaseCompactingLatch = new ReusableLatch(1);

      File file = new File(getTestDir());

      deleteDirectory(file);
View Full Code Here

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

  
   public void testLatchWithParameterizedDown() throws Exception
   {
      ReusableLatch latch = new ReusableLatch(1000);
     
      latch.countDown(5000);
     
      assertTrue(latch.await(1000));
     
     
      assertEquals(0, latch.getCount());
   }
View Full Code Here

      assertEquals(0, latch.getCount());
   }
  
   public void testLatchOnSingleThread() throws Exception
   {
      ReusableLatch latch = new ReusableLatch();

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

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

      latch.await();
   }
View Full Code Here

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

      latch.countUp(); // 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.await(5000))
               {
                  ReusableLatchTest.log.error("Latch timed out");
               }
            }
            catch (Exception e)
            {
               ReusableLatchTest.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.countUp();
               }
            }
            catch (Exception e)
            {
               ReusableLatchTest.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.countDown();
               }
            }
            catch (Exception e)
            {
               ReusableLatchTest.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.countDown();

      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

TOP

Related Classes of org.hornetq.utils.ReusableLatch$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.