Examples of CountDownLatch


Examples of java.util.concurrent.CountDownLatch

  //ning === http://github.com/ning/async-http-client
  @Test
  public void doSimpleAsyncRequestTestWithNing() throws IOException, InterruptedException {
    int iterations = 100;
    final CountDownLatch latch = new CountDownLatch(iterations);
    AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
    for (int i = 1; i <= iterations; i++) {

      asyncHttpClient.prepareGet("http://localhost:" + PORT + "/").
      execute(new AsyncCompletionHandler<com.ning.http.client.Response>(){

        @Override
        public com.ning.http.client.Response onCompleted(com.ning.http.client.Response response) throws Exception{
          String body = response.getResponseBody();
          assertEquals(expectedPayload, body);
          {
            List<String> expectedHeaders = Arrays.asList(new String[] {"Server", "Date", "Content-Length", "Etag", "Connection"});
            assertEquals(200, response.getStatusCode());
            assertEquals(expectedHeaders.size(), response.getHeaders().size());
            for (String header : expectedHeaders) {
              assertTrue(response.getHeader(header) != null);
            }
            assertEquals(expectedPayload.length()+"", response.getHeader("Content-Length"));
          }
          latch.countDown();
          return response;
        }

        @Override
        public void onThrowable(Throwable t){
          assertTrue(false);
        }

      });
    }
    latch.await(15 * 1000, TimeUnit.MILLISECONDS);
    assertEquals(0, latch.getCount());
  }
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

  }
 
  @Test
  public void smallHttpPostBodyTest() throws ClientProtocolException, IOException, InterruptedException {
    final String body = "Roger Schildmeijer";
    final CountDownLatch latch = new CountDownLatch(1);
    AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
    asyncHttpClient.preparePost("http://localhost:" + PORT + "/echo").setBody(body).
    execute(new AsyncCompletionHandler<Response>(){

      @Override
      public Response onCompleted(Response response) throws Exception{
        assertNotNull(response);
        assertEquals(200, response.getStatusCode());
        assertEquals("OK", response.getStatusText());
        assertEquals(5, response.getHeaders().size());
        String payLoad = response.getResponseBody();
        assertEquals(body, payLoad);
        latch.countDown();
        return response;
      }

      @Override
      public void onThrowable(Throwable t) { }
    });

    latch.await();
    assertTrue(latch.getCount() == 0);
  }
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

    String body = "Roger Schildmeijer: 0\n";
    for (int i = 1; i <= 1000; i++) {
      body += "Roger Schildmeijer: " + i + "\n";
    }
    final String expectedBody = body;
    final CountDownLatch latch = new CountDownLatch(1);
    AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
    asyncHttpClient.preparePost("http://localhost:" + PORT + "/echo").setBody(body).
    execute(new AsyncCompletionHandler<Response>(){

      @Override
      public Response onCompleted(Response response) throws Exception{
        assertNotNull(response);
        assertEquals(200, response.getStatusCode());
        assertEquals("OK", response.getStatusText());
        assertEquals(5, response.getHeaders().size());
        String payLoad = response.getResponseBody();
        assertEquals(expectedBody, payLoad);
        latch.countDown();
        return response;
      }

      @Override
      public void onThrowable(Throwable t) { }
    });

    latch.await();
    assertTrue(latch.getCount() == 0);
  }
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

  @Test
  public void multipleStartStopCombinations() throws InterruptedException {
    final HttpServer server = new HttpServer(new Application(Maps.<String, RequestHandler>newHashMap()));
   
    final int n = 10;
    final CountDownLatch latch = new CountDownLatch(n);
    for (int i = 0; i < n; i++) {
      IOLoop.INSTANCE.addCallback(new AsyncCallback() { public void onCallback() { server.listen(PORT+1); }});
      IOLoop.INSTANCE.addCallback(new AsyncCallback() { public void onCallback() { server.stop(); latch.countDown(); }});
    }
    latch.await(5, TimeUnit.SECONDS);
    assertEquals(0, latch.getCount());
  }
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

  }
 
  @Test
  public void connectToUnresolvableAddressUsingAsynchronousHttpClient() throws InterruptedException {
    final String unresolvableAddress = "http://ttasfdqwertyuiop.se./start";
    final CountDownLatch latch = new CountDownLatch(1);
    final AsynchronousHttpClient client = new AsynchronousHttpClient();
    final AsyncCallback runByIOLoop = new AsyncCallback() {

      public void onCallback() {
        client.fetch(unresolvableAddress, new AsyncResult<org.deftserver.web.http.client.HttpResponse>() {

          public void onSuccess(org.deftserver.web.http.client.HttpResponse result) { client.close(); }

          public void onFailure(Throwable caught) {
            if (caught instanceof UnresolvedAddressException) latch.countDown();
            client.close();
          }
        });
      }
    };
    IOLoop.INSTANCE.addCallback(runByIOLoop);
   
    latch.await(30, TimeUnit.SECONDS);
    assertEquals(0, latch.getCount());
  }
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

  }

  @Test
  public void connectToUnconnectableAddressUsingAsynchronousHttpClient() throws InterruptedException {
    final String unconnectableAddress = "http://localhost:8039/start";
    final CountDownLatch latch = new CountDownLatch(1);
    final AsynchronousHttpClient client = new AsynchronousHttpClient();
    final AsyncCallback runByIOLoop = new AsyncCallback() {

      public void onCallback() {
        client.fetch(unconnectableAddress, new AsyncResult<org.deftserver.web.http.client.HttpResponse>() {

          public void onSuccess(org.deftserver.web.http.client.HttpResponse result) { client.close(); }

          public void onFailure(Throwable caught) {
            if (caught instanceof ConnectException) latch.countDown();
            client.close();
          }
        });
      }
    };
    IOLoop.INSTANCE.addCallback(runByIOLoop);
   
    latch.await(30, TimeUnit.SECONDS);
    assertEquals(0, latch.getCount());
  }
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

  }
 
  @Test
  public void multipleAsynchronousHttpClientTest() throws InterruptedException {
    for (int i = 0; i < 100; i++) {
      final CountDownLatch latch = new CountDownLatch(1);
      final String url = "http://localhost:" + PORT + "/";
      final AsynchronousHttpClient http = new AsynchronousHttpClient();
      final String[] result = {"BODY_PLACEHOLDER", "STATUSCODE_PLACEHOLDER"};
      final AsyncResult<org.deftserver.web.http.client.HttpResponse> cb =
        new AsyncResult<org.deftserver.web.http.client.HttpResponse>() {

        public void onSuccess(org.deftserver.web.http.client.HttpResponse response) {
          result[0] = response.getBody();
          result[1] = response.getStatusLine();
          latch.countDown();
        }

        public void onFailure(Throwable ignore) { }
      };
      // make sure that the http.fetch(..) is invoked from the ioloop thread
      IOLoop.INSTANCE.addCallback(new AsyncCallback() { public void onCallback() { http.fetch(url, cb); }});
      latch.await(5, TimeUnit.SECONDS);
      assertEquals(0, latch.getCount());
      assertEquals("hello test", result[0]);
      assertEquals("HTTP/1.1 200 OK", result[1]);
    }
  }
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

    }
  }
 
  @Test
  public void AsynchronousHttpClientConnectionFailedTest() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final String url = "http://localhost:" + (PORT+1) + "/";
    final AsynchronousHttpClient http = new AsynchronousHttpClient();
    final AsyncResult<org.deftserver.web.http.client.HttpResponse> cb =
      new AsyncResult<org.deftserver.web.http.client.HttpResponse>() {

      public void onSuccess(org.deftserver.web.http.client.HttpResponse response) { }

      public void onFailure(Throwable e) { if (e instanceof ConnectException) latch.countDown(); }
    };
    // make sure that the http.fetch(..) is invoked from the ioloop thread
    IOLoop.INSTANCE.addCallback(new AsyncCallback() { public void onCallback() { http.fetch(url, cb); }});
    latch.await(5, TimeUnit.SECONDS);
    assertEquals(0, latch.getCount());
  }
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

   public void testThreads() throws Exception
   {
      Thread workers[] = new Thread[20];
      final List<Exception> exceptions = new LinkedList<Exception>();
      final int loops = 100;
      final CountDownLatch latch = new CountDownLatch(1);

      for (int i = 0; i < workers.length; i++)
      {
         workers[i] = new Thread()
         {
            public void run()
            {
               try
               {
                  latch.await();
               }
               catch (InterruptedException e)
               {
               }

               for (int j = 0; j < loops; j++)
               {
                  try
                  {
                     cache.put(fqn, "key", "value");
                  }
                  catch (Exception e)
                  {
                     log.error("Exception doing put in loop " + j, e);
                     exceptions.add(new Exception("Caused on thread " + getName() + " in loop " + j + " when doing a put()", e));
                  }

                  try
                  {
                     cache.remove(fqn, "key");
                  }
                  catch (Exception e)
                  {
                     log.error("Exception doing remove in loop " + j, e);
                     exceptions.add(new Exception("Caused on thread " + getName() + " in loop " + j + " when doing a remove()", e));
                  }

                  try
                  {
                     cache.get(fqn, "key");
                  }
                  catch (Exception e)
                  {
                     log.error("Exception doing get in loop " + j, e);
                     exceptions.add(new Exception("Caused on thread " + getName() + " in loop " + j + " when doing a get()", e));
                  }
               }
            }
         };

         workers[i].start();
      }

      latch.countDown();

      for (Thread t : workers)
         t.join();

      for (Exception e : exceptions)
View Full Code Here

Examples of java.util.concurrent.CountDownLatch

      assertTrue(cl2.exists(fqn("/a/b/d")));
      assertTrue(cl2.exists(fqn("/e")));
      assertTrue(cl2.exists(fqn("/e/f/g")));

      WaitForPushSingletonStoreCacheLoader scl2 = (WaitForPushSingletonStoreCacheLoader) cache2.getCacheLoaderManager().getCacheLoader();
      CountDownLatch startPushLatch = new CountDownLatch(1);
      scl2.setStartPushLatch(startPushLatch);
     
      ViewChangeListener viewChangeListener = new ViewChangeListener(cache2);
      stopCache1(false);
      cache2.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
      cache2.getInvocationContext().getOptionOverrides().setSuppressPersistence(true);
      cache2.put(fqn("/e/i"), "i-key", "i-value");
      startPushLatch.countDown();
      viewChangeListener.waitForViewChange(60, TimeUnit.SECONDS);

      waitForPushStateCompletion(scl2.getPushStateFuture());

      assertTrue(cl2.exists(fqn("/a")));
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.