Package java.util.concurrent

Examples of java.util.concurrent.CountDownLatch


      managers[0].stop();
     
      int THREADS = 10;
      threadPool = Executors.newFixedThreadPool(THREADS);
     
      CountDownLatch startingGun = new CountDownLatch(THREADS + 1);
      CountDownLatch finishedSignal = new CountDownLatch(THREADS);
      ConcurrentRequestHandler concurrentHandler = new ConcurrentRequestHandler();
      Valve pipelineHead = SessionTestUtil.setupPipeline(managers[1], concurrentHandler);
      Loader[] loaders = new Loader[THREADS];
     
      for (int i = 0; i < loaders.length; i++)
      {
         loaders[i] = new Loader(pipelineHead, concurrentHandler, managers[1], id1, attrs.keySet(), startingGun, finishedSignal);
         threadPool.execute(loaders[i]);
      }
     
      startingGun.countDown();
     
      assertTrue("loaders completed on time", finishedSignal.await(45, TimeUnit.SECONDS));    
     
      for (int i = 0; i < loaders.length; i++)
      {        
         assertNotNull("got checked attributes for " + i, loaders[i].checkedAttributes);
         assertTrue("checked 'count' attribute for " + i, loaders[i].checkedAttributes.containsKey("count"));
View Full Code Here


   @Test
   public void testOneway() throws Exception
   {
      HttpClient client = new HttpClient();
      {
         latch = new CountDownLatch(1);
         PutMethod method = new PutMethod("http://localhost:9091?oneway=true");
         method.setRequestEntity(new StringRequestEntity("content",
               "text/plain", null));
         long start = System.currentTimeMillis();
         int status = client.executeMethod(method);
View Full Code Here

   @Test
   public void testAsynch() throws Exception
   {
      HttpClient client = new HttpClient();
      {
         latch = new CountDownLatch(1);
         PostMethod method = new PostMethod("http://localhost:9091?asynch=true");
         method.setRequestEntity(new StringRequestEntity("content",
               "text/plain", null));
         long start = System.currentTimeMillis();
         int status = client.executeMethod(method);
         @SuppressWarnings("unused")
         long end = System.currentTimeMillis() - start;
         Assert.assertEquals(HttpServletResponse.SC_ACCEPTED, status);
         String jobUrl = method.getResponseHeader(HttpHeaders.LOCATION)
               .getValue();
         System.out.println("JOB: " + jobUrl);
         GetMethod get = new GetMethod(jobUrl);
         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_ACCEPTED, status);
         Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));
         // there's a lag between when the latch completes and the executor
         // registers the completion of the call
         String existingQueryString = get.getQueryString();
         get.setQueryString((existingQueryString == null ? "" : "&") + "wait=1000");
         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_OK, status);
         Assert.assertEquals(get.getResponseBodyAsString(), "content");

         // test its still there
         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_OK, status);
         Assert.assertEquals(get.getResponseBodyAsString(), "content");

         // delete and test delete
         DeleteMethod delete = new DeleteMethod(jobUrl);
         status = client.executeMethod(delete);
         Assert.assertEquals(HttpServletResponse.SC_NO_CONTENT, status);

         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_GONE, status);

         method.releaseConnection();
      }

      {
         dispatcher.setMaxCacheSize(1);
         latch = new CountDownLatch(1);
         PostMethod method = new PostMethod("http://localhost:9091?asynch=true");
         method.setRequestEntity(new StringRequestEntity("content",
               "text/plain", null));
         int status = client.executeMethod(method);
         Assert.assertEquals(HttpServletResponse.SC_ACCEPTED, status);
         String jobUrl1 = method.getResponseHeader(HttpHeaders.LOCATION)
               .getValue();
         Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));

         latch = new CountDownLatch(1);
         method.setRequestEntity(new StringRequestEntity("content",
               "text/plain", null));
         status = client.executeMethod(method);
         Assert.assertEquals(HttpServletResponse.SC_ACCEPTED, status);
         String jobUrl2 = method.getResponseHeader(HttpHeaders.LOCATION)
               .getValue();
         Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));

         Assert.assertTrue(!jobUrl1.equals(jobUrl2));

         GetMethod get = new GetMethod(jobUrl1);
         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_GONE, status);

         // test its still there
         get = new GetMethod(jobUrl2);
         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_OK, status);
         Assert.assertEquals(get.getResponseBodyAsString(), "content");

         // delete and test delete
         DeleteMethod delete = new DeleteMethod(jobUrl2);
         status = client.executeMethod(delete);
         Assert.assertEquals(HttpServletResponse.SC_NO_CONTENT, status);

         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_GONE, status);

         method.releaseConnection();
      }
      // test readAndRemove
      {
         dispatcher.setMaxCacheSize(10);
         latch = new CountDownLatch(1);
         PostMethod method = new PostMethod("http://localhost:9091?asynch=true");
         method.setRequestEntity(new StringRequestEntity("content",
               "text/plain", null));
         int status = client.executeMethod(method);
         Assert.assertEquals(HttpServletResponse.SC_ACCEPTED, status);
View Full Code Here

   protected CountDownLatch shutdownLatch;
   protected Thread backgroundThread;

   public void runInBackground()
   {
      shutdownLatch = new CountDownLatch(1);
      try
      {
         init();
      }
      catch (IOException e)
View Full Code Here

      return 0;
   }

   public void init() throws IOException
   {
      shutdownLatch = new CountDownLatch(1);
      acceptor = createAcceptor();

      if (expiredIn > 0)
      {
         ssclThread = new Thread(serverThreads, new Runnable()
View Full Code Here

   @Test
   public void testOneway() throws Exception
   {
      HttpClient client = new HttpClient();
      {
         latch = new CountDownLatch(1);
         PutMethod method = createPutMethod("?oneway=true");
         method.setRequestEntity(new StringRequestEntity("content", "text/plain", null));
         long start = System.currentTimeMillis();
         int status = client.executeMethod(method);
         long end = System.currentTimeMillis() - start;
View Full Code Here

   @Test
   public void testAsynch() throws Exception
   {
      HttpClient client = new HttpClient();
      {
         latch = new CountDownLatch(1);
         PostMethod method = createPostMethod("?asynch=true");
         method.setRequestEntity(new StringRequestEntity("content", "text/plain", null));
         long start = System.currentTimeMillis();
         int status = client.executeMethod(method);
         @SuppressWarnings("unused")
         long end = System.currentTimeMillis() - start;
         Assert.assertEquals(HttpServletResponse.SC_ACCEPTED, status);
         String jobUrl = method.getResponseHeader(HttpHeaders.LOCATION).getValue();
         System.out.println("JOB: " + jobUrl);
         GetMethod get = new GetMethod(jobUrl);
         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_ACCEPTED, status);
         Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));
         // there's a lag between when the latch completes and the executor
         // registers the completion of the call
         String existingQueryString = get.getQueryString();
         get.setQueryString((existingQueryString == null ? "" : "&") + "wait=1000");
         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_OK, status);
         Assert.assertEquals(get.getResponseBodyAsString(), "content");

         // test its still there
         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_OK, status);
         Assert.assertEquals(get.getResponseBodyAsString(), "content");

         // delete and test delete
         DeleteMethod delete = new DeleteMethod(jobUrl);
         status = client.executeMethod(delete);
         Assert.assertEquals(HttpServletResponse.SC_NO_CONTENT, status);

         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_GONE, status);

         method.releaseConnection();
      }

      {
         dispatcher.setMaxCacheSize(1);
         latch = new CountDownLatch(1);
         PostMethod method = createPostMethod("?asynch=true");
         method.setRequestEntity(new StringRequestEntity("content", "text/plain", null));
         int status = client.executeMethod(method);
         Assert.assertEquals(HttpServletResponse.SC_ACCEPTED, status);
         String jobUrl1 = method.getResponseHeader(HttpHeaders.LOCATION).getValue();
         Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));

         latch = new CountDownLatch(1);
         method.setRequestEntity(new StringRequestEntity("content", "text/plain", null));
         status = client.executeMethod(method);
         Assert.assertEquals(HttpServletResponse.SC_ACCEPTED, status);
         String jobUrl2 = method.getResponseHeader(HttpHeaders.LOCATION).getValue();
         Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));

         Assert.assertTrue(!jobUrl1.equals(jobUrl2));

         GetMethod get = new GetMethod(jobUrl1);
         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_GONE, status);

         // test its still there
         get = new GetMethod(jobUrl2);
         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_OK, status);
         Assert.assertEquals(get.getResponseBodyAsString(), "content");

         // delete and test delete
         DeleteMethod delete = new DeleteMethod(jobUrl2);
         status = client.executeMethod(delete);
         Assert.assertEquals(HttpServletResponse.SC_NO_CONTENT, status);

         status = client.executeMethod(get);
         Assert.assertEquals(HttpServletResponse.SC_GONE, status);

         method.releaseConnection();
      }
      // test readAndRemove
      {
         dispatcher.setMaxCacheSize(10);
         latch = new CountDownLatch(1);
         PostMethod method = createPostMethod("?asynch=true");
         method.setRequestEntity(new StringRequestEntity("content", "text/plain", null));
         int status = client.executeMethod(method);
         Assert.assertEquals(HttpServletResponse.SC_ACCEPTED, status);
         String jobUrl2 = method.getResponseHeader(HttpHeaders.LOCATION).getValue();
View Full Code Here

public class ThreadedTaskControllerTest extends TestCase {
  public void testThreadedTaskController() throws TimeoutException, InterruptedException {
    DefaultUserPreferences preferences = new DefaultUserPreferences();
    ViewFactory viewFactory = new SwingViewFactory();
    // 1. Create a very simple short task that simply counts down latch
    final CountDownLatch shortTaskLatch = new CountDownLatch(1);
    Callable<Void> shortTask = new Callable<Void>() {
        public Void call() throws Exception {
          shortTaskLatch.countDown();
          return null;
        }
      };
    // Create an exception handler that fails test if it was called
    ThreadedTaskController.ExceptionHandler noExceptionHandler =
        new ThreadedTaskController.ExceptionHandler() {
          public void handleException(Exception ex) {
            fail("Exception handler shouldn't be called");
          }
        };
    // Add a listener that fails test if a window is displayed
    PropertyChangeListener activeWindowListener = new PropertyChangeListener() {
          public void propertyChange(PropertyChangeEvent ev) {
            fail("No window should be displayed for short task");
          }
        };
    KeyboardFocusManager.getCurrentKeyboardFocusManager().
        addPropertyChangeListener("activeWindow", activeWindowListener);
    // Check that a simple short task is correctly executed with no exception
    // and doesn't create any visible dialog at screen
    new ThreadedTaskController(shortTask, "Message", noExceptionHandler, preferences, viewFactory).executeTask(null);
    shortTaskLatch.await(1000, TimeUnit.MILLISECONDS);
    assertEquals("Simple task wasn't executed", 0, shortTaskLatch.getCount());
    KeyboardFocusManager.getCurrentKeyboardFocusManager().
        removePropertyChangeListener("activeWindow", activeWindowListener);
   
    // 2. Create a longer task
    final CountDownLatch longTaskLatch = new CountDownLatch(2);
    Callable<Void> longTask = new Callable<Void>() {
        public Void call() throws Exception {
          Thread.sleep(1000);
          longTaskLatch.countDown();
          return null;
        }
      };
    // Add a listener that counts down latch once a waiting dialog is displayed
    activeWindowListener = new PropertyChangeListener() {
          public void propertyChange(PropertyChangeEvent ev) {
            longTaskLatch.countDown();
          }
        };
    KeyboardFocusManager.getCurrentKeyboardFocusManager().
        addPropertyChangeListener("activeWindow", activeWindowListener);
    // Check that a long task creates a visible dialog at screen
    new ThreadedTaskController(longTask, "Message", noExceptionHandler, preferences, viewFactory).executeTask(null);
    longTaskLatch.await(1500, TimeUnit.MILLISECONDS);
    assertEquals("Long task wasn't executed with a waiting dialog", 0, longTaskLatch.getCount());
    KeyboardFocusManager.getCurrentKeyboardFocusManager().
        removePropertyChangeListener("activeWindow", activeWindowListener);
   
    // 3. Create a long task that we will cancel 
    final CountDownLatch cancelledTaskLatch = new CountDownLatch(1);
    Callable<Void> cancelledTask = new Callable<Void>() {
        public Void call() throws Exception {
          try {
            Thread.sleep(1000);
          } catch (InterruptedException ex) {
            cancelledTaskLatch.countDown();
          }
          return null;
        }
      };
    // Add a listener that closes the waiting dialog
    activeWindowListener = new PropertyChangeListener() {
          public void propertyChange(PropertyChangeEvent ev) {
            final Window activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
            if (activeWindow != null) {
              try {
                ((JButton)TestUtilities.findComponent(activeWindow, JButton.class)).doClick();
              } catch (ComponentSearchException ex) {
                fail("No button in waiting dialog");
              }
            }
          }
        };
    KeyboardFocusManager.getCurrentKeyboardFocusManager().
        addPropertyChangeListener("activeWindow", activeWindowListener);
    // Check that a long task creates a visible dialog at screen
    new ThreadedTaskController(cancelledTask, "Message", noExceptionHandler, preferences, viewFactory).executeTask(null);
    cancelledTaskLatch.await(1500, TimeUnit.MILLISECONDS);
    assertEquals("Task wasn't cancelled", 0, cancelledTaskLatch.getCount());
    KeyboardFocusManager.getCurrentKeyboardFocusManager().
        removePropertyChangeListener("activeWindow", activeWindowListener);

    // 4. Create a task that fails 
    final CountDownLatch failingTaskLatch = new CountDownLatch(2);
    Callable<Void> failingTask = new Callable<Void>() {
        public Void call() throws Exception {
          failingTaskLatch.countDown();
          throw new Exception();
        }
      };
    // Create an exception handler that counts down latch when it's called
    ThreadedTaskController.ExceptionHandler exceptionHandler =
        new ThreadedTaskController.ExceptionHandler() {
          public void handleException(Exception ex) {
            failingTaskLatch.countDown();
          }
        };
    // Check that a long task creates a visible dialog at screen
    new ThreadedTaskController(failingTask, "Message", exceptionHandler, preferences, viewFactory).executeTask(null);
    failingTaskLatch.await(1000, TimeUnit.MILLISECONDS);
    assertEquals("Exception in task wasn't handled", 0, failingTaskLatch.getCount());
  }
View Full Code Here

    CountDownLatch latch;
    int            count;

    public CountUpDownLatch() {
        latch      = new CountDownLatch(1);
    }
View Full Code Here

    }

    public void countUp() {

        if (latch.getCount() == 0) {
            latch = new CountDownLatch(1);
        }

        count++;
    }
View Full Code Here

TOP

Related Classes of java.util.concurrent.CountDownLatch

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.