Package EDU.oswego.cs.dl.util.concurrent

Examples of EDU.oswego.cs.dl.util.concurrent.Latch


     */
    public void doTestAsynchRecoverWithAutoAck() throws JMSException, InterruptedException {
       
        final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final String errorMessage[] = new String[]{null};
        final Latch doneLatch = new Latch();
       
        MessageConsumer consumer = session.createConsumer(dest);       
       
        MessageProducer producer = session.createProducer(dest);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        producer.send(session.createTextMessage("First"));
        producer.send(session.createTextMessage("Second"));
       
        consumer.setMessageListener(new MessageListener(){
            int counter;
            public void onMessage(Message msg) {
                counter++;
                try {
                    TextMessage message = (TextMessage)msg;
                    switch( counter ) {
                      case 1:
                            assertEquals("First", message.getText());
                            assertFalse(message.getJMSRedelivered());
                            break;                       
                      case 2:
                          // This should rollback the delivery of this message.. and re-deliver.
                            assertEquals("Second", message.getText());
                            assertFalse(message.getJMSRedelivered());
                            session.recover();
                            break;
                           
                      case 3:
                            assertEquals("Second", message.getText());
                            assertTrue(message.getJMSRedelivered());                           
                          doneLatch.release();
                          break;
                         
                      default:
                          errorMessage[0]="Got too many messages: "+counter;
                          doneLatch.release();
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                  errorMessage[0]="Got exception: "+e;
                  doneLatch.release();
                }
            }
        });
        connection.start();
       
        if( doneLatch.attempt(1000*5000) ) {
            if( errorMessage[0]!=null ) {
                fail(errorMessage[0]);
            }           
        } else {
            fail("Timeout waiting for async message delivery to complete.");
View Full Code Here


                // 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];
            Latch latch = new Latch();
            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.release();
            // wait for all workers to complete
            for (int j = 0; j < t.length; j++) {
                t[j].join();
            }
        }
View Full Code Here

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

      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
            {
               Transaction tx = startTransaction();

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

               // notify the reading thread
               readerCanRead.release();
              
               readerDone.acquire();
              
               tx.commit();
            }
            catch (AssertionFailedError e)
            {
               writerError = e;
            }
            catch(Throwable t)
            {
               t.printStackTrace();
               writerFailed = true;
            }
            finally
            {
               System.out.println("writer thread exits");
               readerCanRead.release();
               writerDone.release();
            }
         }
      }, "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.acquire();

         // 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
      {              
         System.out.println("reader thread exits");
         readerDone.release();
      }

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

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

      // If any assertion failed, throw on the AssertionFailedError
     
View Full Code Here

   }


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

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

      // start a first thread and a transaction

      Thread firstThread = new Thread(new Runnable()
      {
         public void run()
         {
            try
            {
               Transaction tx = startTransaction();

               System.out.println("Writing /a/1");

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

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

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

               System.out.println("rolling back");

               tx.rollback();

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

               // notify the reading thread
               secondCanRead.release();
            }
            catch (AssertionFailedError e)
            {
               writerError = e;
            }
            catch(Throwable t)
            {
               t.printStackTrace();
               writerFailed = true;
            }
            finally
            {
               System.out.println("first thread exits");
               secondCanWrite.release();
               secondCanRead.release();
               firstDone.release();
            }
         }
      }, "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.acquire();

               System.out.println("writing a2");

               // create a second child under parent
               Fqn a2 = new Fqn(PARENT, "2");
               try
               {
                  cache.put(a2, KEY, VALUE);
               }
               catch (TimeoutException good)
               {
                  // first thread locked us out of parent
                  System.out.println("Prevented from writing a2 -- " +
                                     good.getLocalizedMessage());
                  return;
               }

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

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

               // I should still see the value I put
               assertEquals("Known issue JBCACHE-407 -- write lock not acquired on " +
                            "creation of an empty node", VALUE, cache.get(a2, KEY));
            }
            catch (AssertionFailedError e)
            {
               readerError = e;
            }
            catch(Throwable t)
            {
               t.printStackTrace();
               readerFailed = true;
            }
            finally
            {
               System.out.println("second thread exits");
               firstCanRollback.release();
               secondDone.release();
            }
         }
      }, "SECOND");
      secondThread.start();

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

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

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

      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
            {
               Transaction tx = startTransaction();

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

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

               readerDone.acquire();

               tx.commit();
            }
            catch (AssertionFailedError e)
            {
               writerError = e;
            }
            catch (Throwable t)
            {
               t.printStackTrace();
               writerFailed = true;
            }
            finally
            {
               System.out.println("writer thread exits");
               readerCanRead.release();
               writerDone.release();
            }
         }
      }, "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.acquire();

         // 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
      {
         System.out.println("reader thread exits");
         readerDone.release();
      }

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

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

      // If any assertion failed, throw on the AssertionFailedError

View Full Code Here

    * should never read anything else than the original value (uncommitted values should be
    * isolated)
    */
   public void testReadCommitted() throws Exception
   {
      final Latch readerCanRead = new Latch();
      final Latch readerDone = new Latch();
      final Latch writerCanWrite = new Latch();
      final Latch writerCanRollback = new Latch();
      final Latch writerDone = new Latch();
     
      cache.put(FQN, KEY, VALUE);
      assertEquals(VALUE, cache.get(FQN, KEY));

      // start a reader thread; need a transaction so its initial
      // read lock is maintained while the writer does its work

      Thread readerThread = new Thread(new Runnable()
      {
         public void run()
         {
            Transaction tx = null;
            try
            {
               tx = startTransaction();
              
               // Read the value, thus acquiring a read lock on FQN
               assertEquals(VALUE, cache.get(FQN, KEY));
              
               writerCanWrite.release();
              
               // wait until the writer thread changes the value in a transaction, but it did not
               // yet commit or roll back.
               readerCanRead.acquire();

               try
               {
                  // I shouldn't be able to see the "dirty" value
                  assertEquals("thread w/ read lock " +
                               "can see subsequent uncommitted changes",
                                VALUE, cache.get(FQN, KEY));
               }
               catch (TimeoutException good)
               {
                  // this is what should happen due to writer's WL
               }
              
               // let the writer know it can rollback
               writerCanRollback.release();

               // I should still be able to see the "clean" value
               assertEquals(VALUE, cache.get(FQN, KEY));
            }
            catch (AssertionFailedError e)
            {
               readerError = e;
            }
            catch(Throwable t)
            {
               t.printStackTrace();
               readerFailed = true;
            }
            finally
            {              
               System.out.println("reader thread exits");
               if (tx != null)
                  try { tx.commit(); } catch (Exception e) {}
               writerCanWrite.release();
               writerCanRollback.release();
               readerDone.release();
            }
         }
      }, "READER");
      readerThread.start();


      // start a writer thread and a transaction

      Thread writerThread = new Thread(new Runnable()
      {
         public void run()
         {
            try
            {
               // wait until the reader thread reads and allows me to start
               writerCanWrite.attempt(3000);
              
               Transaction tx2 = startTransaction();

               // change VALUE in a transaction
               cache.put(FQN, KEY, "this-shouldnt-be-visible");

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

               // wait until the reader thread reads and allows me to rollback or until I timeout
               writerCanRollback.attempt(3000);

               System.out.println("rolling back");

               tx2.rollback();
            }
            catch (AssertionFailedError e)
            {
               writerError = e;
            }
            catch(Throwable t)
            {
               t.printStackTrace();
               writerFailed = true;
            }
            finally
            {
               System.out.println("writer thread exits");
               readerCanRead.release();
               writerDone.release();
            }
         }
      }, "WRITER");
      writerThread.start();

      // wait for both threads to finish
      readerDone.acquire();
      writerDone.acquire();

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

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

      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
            {
               Transaction tx = startTransaction();

               // change VALUE in a transaction
               cache.remove(PARENT_FQN);

               // notify the reading thread
               readerCanRead.release();
              
               readerDone.acquire();
              
               tx.commit();
            }
            catch (AssertionFailedError e)
            {
               writerError = e;
            }
            catch(Throwable t)
            {
               t.printStackTrace();
               writerFailed = true;
            }
            finally
            {
               System.out.println("writer thread exits");
               readerCanRead.release();
               writerDone.release();
            }
         }
      }, "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.acquire();

         // 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
      {              
         System.out.println("reader thread exits");
         readerDone.release();
      }

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

      assertNull("Node was removed", cache.get(FQN));
     
      // If any assertion failed, throw on the AssertionFailedError
     
View Full Code Here

    {
        int numCaches = 15;
        TreeCache[] caches = new TreeCache[numCaches];
        try
        {
            Latch latch = new Latch();
            CacheStarter[] starters = new CacheStarter[numCaches];

            for (int i=0; i<numCaches; i++)
            {
                caches[i] = createCache(1, new String(new char[]{(char) ('A' + i)}), false, false);
                starters[i] = new CacheStarter(latch, caches[i]);
                starters[i].start();
            }

            // now have the lot start simultaneously
            TestingUtil.sleepThread(500);
            latch.release();

            // allow a generous sleep time
            TestingUtil.blockUntilViewsReceived(caches, 180000);
            TestingUtil.sleepThread(1000 * numCaches);

 
View Full Code Here

   }

   public void testConcurrentReadAndRemove() throws Exception
   {
      final List exceptions = new LinkedList();
      final Latch readerLatch = new Latch();
      final Latch readerFinishedLatch = new Latch();
      final Fqn fqn = Fqn.fromString("/parent/child");

      cache.put(fqn, "k", "v");

      class Reader extends Thread
      {
         public void run()
         {
            try
            {
               cache.getTransactionManager().begin();
               cache.get(fqn, "k"); // read
               readerFinishedLatch.release();
               readerLatch.acquire(); // wait
               cache.getTransactionManager().commit();
            }
            catch (Exception e)
            {
               e.printStackTrace();
               exceptions.add(e);

            }
         }
      }

      Thread reader = new Reader();

      reader.start();
      readerFinishedLatch.acquire();
      cache.remove(fqn.getParent());
      assertFalse(cache.exists(fqn.getParent()));     
      readerLatch.release();
      reader.join();
View Full Code Here

   }
  
   public void testConcurrentPutReadAndRemove() throws Exception
   {
      final List exceptions = new LinkedList();
      final Latch readerLatch = new Latch();
      final Latch readerFinishedLatch = new Latch();
      final Fqn fqn = Fqn.fromString("/parent/child");

      cache.put(fqn, "k", "v");

      class Reader extends Thread
      {
         public void run()
         {
            try
            {
               cache.getTransactionManager().begin();
               cache.put(Fqn.ROOT, "x", "y"); // a dummy put to ensure that validation occurs
               cache.get(fqn, "k"); // read
               readerFinishedLatch.release();
               readerLatch.acquire(); // wait
               cache.getTransactionManager().commit();
            }
            catch (Exception e)
            {
               e.printStackTrace();
               exceptions.add(e);

            }
         }
      }

      Thread reader = new Reader();

      reader.start();
      readerFinishedLatch.acquire();
      cache.remove(fqn.getParent());
      assertFalse(cache.exists(fqn.getParent()));
      readerLatch.release();
      reader.join();
View Full Code Here

TOP

Related Classes of EDU.oswego.cs.dl.util.concurrent.Latch

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.