List of usage examples for java.util.concurrent CountDownLatch await
public boolean await(long timeout, TimeUnit unit) throws InterruptedException
From source file:com.amazonaws.eclipse.core.accounts.profiles.SdkCredentialsFileContentMonitorTest.java
@Test public void testFileChangedCallback() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); SdkCredentialsFileContentMonitor monitor = new SdkCredentialsFileContentMonitor(targetFile, MONITOR_POLLING_INTERVAL_MILLIS, new FileAlterationListenerAdaptor() { @Override// w w w .j av a2s . c o m public void onFileChange(final File changedFile) { latch.countDown(); } }); monitor.setDebugMode(true); monitor.start(); touch(targetFile); long waitTime = MONITOR_POLLING_INTERVAL_MILLIS * 2; Assert.assertTrue("File monitor callback not invoked after waiting for " + waitTime + " ms.", latch.await(waitTime, TimeUnit.MILLISECONDS)); }
From source file:com.vmware.photon.controller.client.resource.DeploymentApiTest.java
@Test public void testGetHostsAsync() throws IOException, InterruptedException { Vm vm1 = new Vm(); vm1.setId("vm1"); Vm vm2 = new Vm(); vm2.setId("vm2"); final ResourceList<Vm> vmList = new ResourceList<>(Arrays.asList(vm1, vm2)); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(vmList); setupMocks(serializedTask, HttpStatus.SC_OK); DeploymentApi deploymentApi = new DeploymentApi(restClient); final CountDownLatch latch = new CountDownLatch(1); deploymentApi.getAllDeploymentVmsAsync("foo", new FutureCallback<ResourceList<Vm>>() { @Override//from www. j av a2 s . com public void onSuccess(ResourceList<Vm> result) { assertEquals(result.getItems(), vmList.getItems()); latch.countDown(); } @Override public void onFailure(Throwable t) { fail(t.toString()); latch.countDown(); } }); assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true)); }
From source file:com.gooddata.http.client.GoodDataHttpClientIntegrationTest.java
License:asdf
@Test public void shouldRefreshTTConcurrent() throws Exception { mock401OnProjects();// ww w. ja v a 2s .c o m // this serves to block second thread, until the first one gets 401 on projects, which causes TT refresh // the test aims to test the second thread is not cycling on 401 and cumulating wrong TT headers final Semaphore semaphore = new Semaphore(1); requestOnPath(GDC_PROJECTS_PATH, "TT1").respondUsing(new Responder() { boolean first = true; @Override public StubResponse nextResponse(Request request) { if (first) { first = false; return StubResponse.builder().status(200).body(BODY_PROJECTS, CHARSET) .header(CONTENT_HEADER, CONTENT_TYPE_JSON_UTF).build(); } else { semaphore.release(); return StubResponse.builder().status(401).body(BODY_401, CHARSET) .header(CONTENT_HEADER, CONTENT_TYPE_JSON_UTF) .header(WWW_AUTHENTICATE_HEADER, GOODDATA_REALM + " " + TT_COOKIE) .delay(5, TimeUnit.SECONDS).build(); } } }); mock200OnProjects("TT2"); mock401OnPath(GDC_PROJECTS2_PATH, "TT1"); mock200OnPath(GDC_PROJECTS2_PATH, "TT2"); mock401OnToken(); respond200OnToken(mock200OnToken("TT1").thenRespond(), "TT2"); mockLogin(); final HttpClient client = createGoodDataClient(jadlerLogin, jadlerPassword, jadlerHost); // one get at the beginning causing successful login performGet(client, jadlerHost, GDC_PROJECTS_PATH, 200); // to be able to finish when both threads finished final CountDownLatch countDown = new CountDownLatch(2); final ExecutorService executor = Executors.newFixedThreadPool(2); semaphore.acquire(); // will be released in jadler executor.submit(new PerformGetWithCountDown(client, GDC_PROJECTS_PATH, countDown)); semaphore.acquire(); // causes waiting executor.submit(new PerformGetWithCountDown(client, GDC_PROJECTS2_PATH, countDown)); countDown.await(10, TimeUnit.SECONDS); verifyThatRequest().havingMethodEqualTo("GET").havingPathEqualTo(GDC_TOKEN_PATH) .havingHeaderEqualTo(SST_HEADER, "SST") // if received more than twice, it means the second thread didn't wait, while the first was refreshing TT .receivedTimes(2); verifyThatRequest().havingMethodEqualTo("GET").havingPathEqualTo(GDC_PROJECTS2_PATH) .havingHeaderEqualTo(TT_HEADER, "TT1") // the second thread should try only once with expired TT1 .receivedOnce(); verifyThatRequest().havingMethodEqualTo("GET").havingPathEqualTo(GDC_PROJECTS2_PATH) .havingHeaderEqualTo(TT_HEADER, "TT1").havingHeaderEqualTo(TT_HEADER, "TT2") // the second thread should not set more than one X-GDC-AuthTT header .receivedNever(); }
From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessReadWriteLock.java
@Test public void testThatDowngradingRespectsThreads() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try {/* w w w .ja v a2s .c o m*/ client.start(); final InterProcessReadWriteLock lock = new InterProcessReadWriteLock(client, "/lock"); ExecutorService t1 = Executors.newSingleThreadExecutor(); ExecutorService t2 = Executors.newSingleThreadExecutor(); final CountDownLatch latch = new CountDownLatch(1); Future<Object> f1 = t1.submit(new Callable<Object>() { @Override public Object call() throws Exception { lock.writeLock().acquire(); latch.countDown(); return null; } }); Future<Object> f2 = t2.submit(new Callable<Object>() { @Override public Object call() throws Exception { Assert.assertTrue(latch.await(10, TimeUnit.SECONDS)); Assert.assertFalse(lock.readLock().acquire(5, TimeUnit.SECONDS)); return null; } }); f1.get(); f2.get(); } finally { IOUtils.closeQuietly(client); } }
From source file:com.vmware.photon.controller.api.client.resource.DisksApiTest.java
@Test public void testDeleteAsync() throws IOException, InterruptedException { final Task responseTask = new Task(); responseTask.setId("12345"); responseTask.setState("QUEUED"); responseTask.setQueuedTime(Date.from(Instant.now())); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(responseTask); setupMocks(serializedTask, HttpStatus.SC_CREATED); DisksApi disksApi = new DisksApi(restClient); final CountDownLatch latch = new CountDownLatch(1); disksApi.deleteAsync("persistentDisk", new FutureCallback<Task>() { @Override/*from ww w.ja va2s . com*/ public void onSuccess(@Nullable Task result) { assertEquals(result, responseTask); latch.countDown(); } @Override public void onFailure(Throwable t) { fail(t.toString()); latch.countDown(); } }); assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true)); ; }
From source file:com.vmware.photon.controller.api.client.resource.DisksRestApiTest.java
@Test public void testDeleteAsync() throws IOException, InterruptedException { final Task responseTask = new Task(); responseTask.setId("12345"); responseTask.setState("QUEUED"); responseTask.setQueuedTime(Date.from(Instant.now())); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(responseTask); setupMocks(serializedTask, HttpStatus.SC_CREATED); DisksApi disksApi = new DisksRestApi(restClient); final CountDownLatch latch = new CountDownLatch(1); disksApi.deleteAsync("persistentDisk", new FutureCallback<Task>() { @Override//from w w w. j a v a 2 s. c om public void onSuccess(@Nullable Task result) { assertEquals(result, responseTask); latch.countDown(); } @Override public void onFailure(Throwable t) { fail(t.toString()); latch.countDown(); } }); assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true)); ; }
From source file:com.vmware.photon.controller.api.client.resource.ImagesApiTest.java
@Test public void testDeleteImageAsync() throws IOException, InterruptedException { final Task responseTask = new Task(); responseTask.setId("12345"); responseTask.setState("QUEUED"); responseTask.setQueuedTime(Date.from(Instant.now())); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(responseTask); setupMocks(serializedTask, HttpStatus.SC_CREATED); ImagesApi imagesApi = new ImagesApi(this.restClient); final CountDownLatch latch = new CountDownLatch(1); imagesApi.deleteAsync("foo", new FutureCallback<Task>() { @Override//from w ww. j a v a2 s .com public void onSuccess(@Nullable Task result) { assertEquals(result, responseTask); latch.countDown(); } @Override public void onFailure(Throwable t) { fail(t.toString()); latch.countDown(); } }); assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true)); }
From source file:com.vmware.photon.controller.api.client.resource.ImagesRestApiTest.java
@Test public void testDeleteImageAsync() throws IOException, InterruptedException { final Task responseTask = new Task(); responseTask.setId("12345"); responseTask.setState("QUEUED"); responseTask.setQueuedTime(Date.from(Instant.now())); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(responseTask); setupMocks(serializedTask, HttpStatus.SC_CREATED); ImagesApi imagesApi = new ImagesRestApi(this.restClient); final CountDownLatch latch = new CountDownLatch(1); imagesApi.deleteAsync("foo", new FutureCallback<Task>() { @Override/*from w w w . j ava2 s. c o m*/ public void onSuccess(@Nullable Task result) { assertEquals(result, responseTask); latch.countDown(); } @Override public void onFailure(Throwable t) { fail(t.toString()); latch.countDown(); } }); assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true)); }
From source file:com.vmware.photon.controller.api.client.resource.ImagesApiTest.java
@Test public void testGetImageAsync() throws IOException, InterruptedException { final Image image = new Image(); image.setId("image1"); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(image); setupMocks(serializedTask, HttpStatus.SC_OK); ImagesApi imagesApi = new ImagesApi(this.restClient); final CountDownLatch latch = new CountDownLatch(1); imagesApi.getImageAsync(image.getId(), new FutureCallback<Image>() { @Override//from w ww .ja v a2 s . co m public void onSuccess(@Nullable Image result) { assertEquals(result, image); latch.countDown(); } @Override public void onFailure(Throwable t) { fail(t.toString()); latch.countDown(); } }); assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true)); }
From source file:com.vmware.photon.controller.api.client.resource.ImagesRestApiTest.java
@Test public void testGetImageAsync() throws IOException, InterruptedException { final Image image = new Image(); image.setId("image1"); ObjectMapper mapper = new ObjectMapper(); String serializedTask = mapper.writeValueAsString(image); setupMocks(serializedTask, HttpStatus.SC_OK); ImagesApi imagesApi = new ImagesRestApi(this.restClient); final CountDownLatch latch = new CountDownLatch(1); imagesApi.getImageAsync(image.getId(), new FutureCallback<Image>() { @Override//from www . j a va 2 s.c o m public void onSuccess(@Nullable Image result) { assertEquals(result, image); latch.countDown(); } @Override public void onFailure(Throwable t) { fail(t.toString()); latch.countDown(); } }); assertThat(latch.await(COUNTDOWNLATCH_AWAIT_TIMEOUT, TimeUnit.SECONDS), is(true)); }