Examples of CountDownLatch


Examples of java.util.concurrent.CountDownLatch

   }

   public void testAvoidConcurrentStatePush() throws Exception
   {
      final ExecutorService executor = Executors.newFixedThreadPool(2);
      final CountDownLatch pushStateCanFinish = new CountDownLatch(1);
      final CountDownLatch secondActiveStatusChangerCanStart = new CountDownLatch(1);
      final MockSingletonStoreCacheLoader mscl = new MockSingletonStoreCacheLoader(pushStateCanFinish, secondActiveStatusChangerCanStart, new SingletonStoreDefaultConfig());

      Future f1 = executor.submit(createActiveStatusChanger(mscl));
      secondActiveStatusChangerCanStart.await();

      Future f2 = executor.submit(createActiveStatusChanger(mscl));

      f1.get();
      f2.get();
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

      assertEquals(1, mscl.getNumberCreatedTasks());
   }

   public void testPushStateTimedOut() throws Exception
   {
      final CountDownLatch pushStateCanFinish = new CountDownLatch(1);
      SingletonStoreDefaultConfig ssdc = new SingletonStoreDefaultConfig();
      ssdc.setPushStateWhenCoordinatorTimeout(1000);
      final MockSingletonStoreCacheLoader mscl = new MockSingletonStoreCacheLoader(pushStateCanFinish, null, ssdc);

      Future f = Executors.newSingleThreadExecutor().submit(createActiveStatusChanger(mscl));
      pushStateCanFinish.await(2000, TimeUnit.MILLISECONDS);
      pushStateCanFinish.countDown();

      try
      {
         f.get();
         fail("Should have timed out");
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

            // terminate the test on the first failed worker
            throw mcl_exception;
         }
         // start several worker threads to work with the same set of FQN's
         Worker[] t = new Worker[NUM_WORKERS];
         CountDownLatch latch = new CountDownLatch(1);
         for (int j = 0; j < t.length; j++)
         {
            t[j] = new Worker(latch, NUM_FQNS_PER_RUN * i,
                  NUM_FQNS_PER_RUN, modificationsPerTx);
            t[j].start();
         }
         // fire the workers to start processing
         latch.countDown();
         // wait for all workers to complete
         for (Worker worker : t)
         {
            worker.join();
         }
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

      threadSafetyTest(false);
   }

   protected void threadSafetyTest(final boolean singleFqn) throws Exception
   {
      final CountDownLatch latch = new CountDownLatch(1);
      final Fqn fqn = Fqn.fromString("/a/b/c");
      final List<Fqn> fqns = new ArrayList<Fqn>(30);
      final Random r = new Random();
      if (!singleFqn)
      {
         for (int i = 0; i < 30; i++)
         {
            Fqn f = Fqn.fromString("/a/b/c/" + i);
            fqns.add(f);
            cache2.put(f, "k", "v");
            cache1.evict(f);
         }
      }
      else
      {
         cache2.put(fqn, "k", "v");
         cache1.evict(fqn);
      }
      final int loops = 25; // was 300
      final Set<Exception> exceptions = new CopyOnWriteArraySet<Exception>();

      Thread evictor = new Thread("Evictor")
      {
         public void run()
         {
            try
            {
               latch.await();
               for (int i = 0; i < loops; i++)
               {
                  Fqn f = singleFqn ? fqn : fqns.get(r.nextInt(fqns.size()));
                  cache1.evict(f);
                  TestingUtil.sleepRandom(50);
               }
            }
            catch (TimeoutException te)
            {
               // doesn't matter if we hit these on occasion
            }
            catch (Exception e)
            {
               exceptions.add(e);
            }
         }
      };

      evictor.start();

      Thread writer = new Thread("Writer")
      {
         public void run()
         {
            try
            {
               latch.await();
               for (int i = 0; i < loops; i++)
               {
                  Fqn f = singleFqn ? fqn : fqns.get(r.nextInt(fqns.size()));
                  cache2.put(f, "k", "v");
                  TestingUtil.sleepRandom(50);
               }
            }
            catch (Exception e)
            {
               exceptions.add(e);
            }
         }
      };

      writer.start();


      Thread reader1 = new Thread("Reader-1")
      {
         public void run()
         {
            try
            {
               latch.await();
               for (int i = 0; i < loops; i++)
               {
                  loader1.get(singleFqn ? fqn : fqns.get(r.nextInt(fqns.size())));
                  TestingUtil.sleepRandom(50);
               }
            }
            catch (Exception e)
            {
               exceptions.add(e);
            }
         }
      };
      reader1.start();

      Thread reader2 = new Thread("Reader-2")
      {
         public void run()
         {
            try
            {
               latch.await();
               for (int i = 0; i < loops; i++)
               {
                  loader1.getChildrenNames(singleFqn ? fqn.getParent() : fqns.get(r.nextInt(fqns.size())).getParent());
                  TestingUtil.sleepRandom(50);
               }
            }
            catch (Exception e)
            {
               exceptions.add(e);
            }
         }
      };
      reader2.start();

      Thread reader3 = new Thread("Reader-3")
      {
         public void run()
         {
            try
            {
               latch.await();
               for (int i = 0; i < loops; i++)
               {
                  loader1.getChildrenNames(singleFqn ? fqn : fqns.get(r.nextInt(fqns.size())));
                  TestingUtil.sleepRandom(50);
               }
            }
            catch (Exception e)
            {
               exceptions.add(e);
            }
         }
      };
      reader3.start();

      latch.countDown();
      reader1.join();
      reader2.join();
      reader3.join();
      evictor.join();
      writer.join();
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

      int totalInvocations = numThreads * numLoopsPerThread;

      assert totalInvocations % COUNT == 0 : "NumThreads and NumLoopsPerThread must multiply to be a multiple of COUNT";

      final CountDownLatch latch = new CountDownLatch(1);

      // mock the RPCManager used in the cache
      RPCManager mockRpcManager = EasyMock.createStrictMock(RPCManager.class);
      injectRpcManager(mockRpcManager);

      // expect basic cluster related calls
      expect(mockRpcManager.getMembers()).andReturn(originalRpcManager.getMembers()).anyTimes();
      expect(mockRpcManager.callRemoteMethods((Vector<Address>) anyObject(), (ReplicableCommand) anyObject(), anyBoolean(), anyLong(), anyBoolean())).andReturn(Collections.emptyList()).anyTimes();
      replay(mockRpcManager);

      Thread[] threads = new Thread[numThreads];

      for (int i = 0; i < numThreads; i++)
      {
         threads[i] = new Thread()
         {
            public void run()
            {
               try
               {
                  latch.await();
               }
               catch (InterruptedException e)
               {
                  // do nothing
               }
               for (int j = 0; j < numLoopsPerThread; j++)
               {
                  cache.put("/a/b/c/" + getName() + "/" + j, "k", "v");
               }
            }
         };
         threads[i].start();
      }

      // start the threads
      latch.countDown();

      // wait for threads to join
      for (Thread t : threads) t.join();

      // now test results
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

    *
    * @throws Exception
    */
   public void testNodeRemoved() throws Exception
   {
      final CountDownLatch readerCanRead = new CountDownLatch(1);
      final CountDownLatch readerDone = new CountDownLatch(1);
      final CountDownLatch writerDone = new CountDownLatch(1);

      cache.put(FQN, KEY, VALUE);
      assertEquals(VALUE, cache.get(FQN, KEY));

      // start a writer thread and a transaction

      Thread writerThread = new Thread(new Runnable()
      {
         public void run()
         {
            try
            {
               TransactionManager tx = startTransaction();

               // change VALUE in a transaction
               cache.removeNode(FQN);

               // notify the reading thread
               readerCanRead.countDown();

               readerDone.await();

               tx.commit();
            }
            catch (AssertionError e)
            {
               writerError = e;
            }
            catch (Throwable t)
            {
               t.printStackTrace();
               writerFailed = true;
            }
            finally
            {
               readerCanRead.countDown();
               writerDone.countDown();
            }
         }
      }, "WRITER");
      writerThread.start();

      try
      {
         // wait until the writer thread changes the value in a transaction,
         // but it did not yet commit or roll back.
         readerCanRead.await();

         // I shouldn't be able to see the "dirty" value
         assertEquals("2nd thread cannot see uncommitted changes", VALUE, cache.get(FQN, KEY));
      }
      catch (TimeoutException good)
      {
         // ignore; means worked as it should
      }
      finally
      {
         readerDone.countDown();
      }

      // wait for the writer to finish
      writerDone.await();

      assertNull("Node was removed", cache.getNode(FQN));

      // If any assertion failed, throw on the AssertionFailedError

View Full Code Here

Examples of java.util.concurrent.CountDownLatch

      cache = null;
   }

   public void testNodeCreationRollback() throws Exception
   {
      final CountDownLatch secondCanWrite = new CountDownLatch(1);
      final CountDownLatch secondCanRead = new CountDownLatch(1);
      final CountDownLatch secondDone = new CountDownLatch(1);
      final CountDownLatch firstCanRollback = new CountDownLatch(1);
      final CountDownLatch firstDone = new CountDownLatch(1);

      final Fqn PARENT = Fqn.fromString("/a");

      // start a first thread and a transaction

      Thread firstThread = new Thread(new Runnable()
      {
         public void run()
         {
            try
            {
               TransactionManager tm = startTransaction();

               // Create an empty parent node and a node with data
               Fqn a1 = Fqn.fromRelativeElements(PARENT, "1");
               cache.put(a1, KEY, VALUE);

               // notify the second thread it can write
               secondCanWrite.countDown();

               // wait until the second thread writes and allows me to rollback or until I timeout
               firstCanRollback.await(3000, TimeUnit.MILLISECONDS);

               tm.rollback();

               assertNull("a1 empty", cache.get(a1, KEY));

               // notify the reading thread
               secondCanRead.countDown();
            }
            catch (AssertionError e)
            {
               writerError = e;
            }
            catch (Throwable t)
            {
               t.printStackTrace();
               writerFailed = true;
            }
            finally
            {
               secondCanWrite.countDown();
               secondCanRead.countDown();
               firstDone.countDown();
            }
         }
      }, "FIRST");
      firstThread.start();

      // start a second thread; no transaction is necessary here

      Thread secondThread = new Thread(new Runnable()
      {
         public void run()
         {
            try
            {
               // wait until the first thread has created PARENT and a child
               secondCanWrite.await();

               // create a second child under parent
               Fqn a2 = Fqn.fromRelativeElements(PARENT, "2");
               try
               {
                  cache.put(a2, KEY, VALUE);
               }
               catch (TimeoutException good)
               {
                  // first thread locked us out of parent
                  return;
               }

               // let the first thread know it can rollback
               firstCanRollback.countDown();

               // wait until the first thread rolls back.
               secondCanRead.await();

               // I should still see the value I put
               assertEquals("write lock not acquired on " + "creation of an empty node", VALUE, cache.get(a2, KEY));
            }
            catch (AssertionError e)
            {
               readerError = e;
            }
            catch (Throwable t)
            {
               t.printStackTrace();
               readerFailed = true;
            }
            finally
            {
               firstCanRollback.countDown();
               secondDone.countDown();
            }
         }
      }, "SECOND");
      secondThread.start();

      // wait for both threads to finish
      secondDone.await();
      firstDone.await();

      // If any assertion failed, throw on the AssertionFailedError
      if (readerError != null)
      {
         throw readerError;
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

   }

   //todo mmarkus add a parameter here to user grater values for different mvn profiles
   protected void threadSafetyTest(final boolean singleFqn) throws Exception
   {
      final CountDownLatch latch = new CountDownLatch(1);
      final Fqn fqn = Fqn.fromString("/a/b/c");
      final List<Fqn> fqns = new ArrayList<Fqn>(30);
      final Random r = new Random();
      if (!singleFqn)
      {
         for (int i = 0; i < 30; i++)
         {
            Fqn f = Fqn.fromString("/a/b/c/" + i);
            fqns.add(f);
            loader.put(f, "k", "v");
         }
      }
      else
      {
         loader.put(fqn, "k", "v");
      }
      final int loops = 100;
      final Set<Exception> exceptions = new CopyOnWriteArraySet<Exception>();

      Thread remover1 = new Thread("Remover-1")
      {
         public void run()
         {
            try
            {
               latch.await();
               for (int i = 0; i < loops; i++)
               {
                  loader.remove(singleFqn ? fqn : fqns.get(r.nextInt(fqns.size())));
               }
            }
            catch (Exception e)
            {
               exceptions.add(e);
            }
         }
      };

      remover1.start();

      Thread remover2 = new Thread("Remover-2")
      {
         public void run()
         {
            try
            {
               latch.await();
               for (int i = 0; i < loops; i++)
               {
                  loader.remove(singleFqn ? fqn : fqns.get(r.nextInt(fqns.size())), "k");
               }
            }
            catch (Exception e)
            {
               exceptions.add(e);
            }
         }
      };

      remover2.start();


      Thread reader1 = new Thread("Reader-1")
      {
         public void run()
         {
            try
            {
               latch.await();
               for (int i = 0; i < loops; i++)
               {
                  loader.get(singleFqn ? fqn : fqns.get(r.nextInt(fqns.size())));
               }
            }
            catch (Exception e)
            {
               exceptions.add(e);
            }
         }
      };
      reader1.start();

      Thread reader2 = new Thread("Reader-2")
      {
         public void run()
         {
            try
            {
               latch.await();
               for (int i = 0; i < loops; i++)
               {
                  loader.getChildrenNames(singleFqn ? fqn : fqns.get(r.nextInt(fqns.size())));
               }
            }
            catch (Exception e)
            {
               exceptions.add(e);
            }
         }
      };
      reader2.start();


      Thread writer1 = new Thread("Writer-1")
      {
         public void run()
         {
            try
            {
               latch.await();
               for (int i = 0; i < loops; i++)
               {
                  loader.put(singleFqn ? fqn : fqns.get(r.nextInt(fqns.size())), "k", "v");
               }
            }
            catch (Exception e)
            {
               exceptions.add(e);
            }
         }
      };
      writer1.start();

      Thread writer2 = new Thread("Writer-2")
      {
         public void run()
         {
            try
            {
               latch.await();
               for (int i = 0; i < loops; i++)
               {
                  loader.put(singleFqn ? fqn : fqns.get(r.nextInt(fqns.size())), new HashMap<Object, Object>());
               }
            }
            catch (Exception e)
            {
               exceptions.add(e);
            }
         }
      };
      writer2.start();


      latch.countDown();
      reader1.join();
      reader2.join();
      remover1.join();
      remover2.join();
      writer1.join();
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

    *
    * @throws Exception
    */
   public void testNodeRemoved() throws Exception
   {
      final CountDownLatch readerCanRead = new CountDownLatch(1);
      final CountDownLatch readerDone = new CountDownLatch(1);
      final CountDownLatch writerDone = new CountDownLatch(1);

      cache.put(FQN, KEY, VALUE);
      assertEquals(VALUE, cache.get(FQN, KEY));

      // start a writer thread and a transaction

      Thread writerThread = new Thread(new Runnable()
      {
         public void run()
         {
            try
            {
               TransactionManager tm = startTransaction();

               // change VALUE in a transaction
               cache.removeNode(FQN);

               // notify the reading thread
               readerCanRead.countDown();

               readerDone.await();

               tm.commit();
            }
            catch (AssertionError e)
            {
               writerError = e;
            }
            catch (Throwable t)
            {
               t.printStackTrace();
               writerFailed = true;
            }
            finally
            {
               readerCanRead.countDown();
               writerDone.countDown();
            }
         }
      }, "WRITER");
      writerThread.start();

      try
      {
         // wait until the writer thread changes the value in a transaction,
         // but it did not yet commit or roll back.
         readerCanRead.await();

         // I shouldn't be able to see the "dirty" value
         assertEquals("2nd thread cannot see uncommitted changes", VALUE, cache.get(FQN, KEY));
      }
      catch (TimeoutException t)
      {
         // ignore, this is good
      }
      finally
      {
         readerDone.countDown();
      }

      // wait for the writer to finish
      writerDone.await();

      assertNull("Node was not removed", cache.getNode(FQN));

      // If any assertion failed, throw on the AssertionFailedError

View Full Code Here

Examples of java.util.concurrent.CountDownLatch

   {
      Cache<String, String> c = null;
      try
      {
         c = new UnitTestCacheFactory<String, String>().createCache(getClass());
         CountDownLatch latch = new CountDownLatch(2);
         AbstractListener syncListener = new Listener(latch);
         AbstractListener asyncListener = new AsyncListener(latch);
         c.addCacheListener(syncListener);
         c.addCacheListener(asyncListener);
         c.put("/a", "k", "v");
         latch.await();
         assert syncListener.caller == Thread.currentThread();
         assert asyncListener.caller != Thread.currentThread();
      }
      finally
      {
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.