@Override public void onSuccess(DelayedMap.AsyncClient client) { try { getBeforeFuture = client.getValueSlowly(200, TimeUnit.MILLISECONDS, "testKey"); putFuture = client.putValueSlowly(400, TimeUnit.MILLISECONDS, "testKey", "testValue"); getAfterFuture = client.getValueSlowly(600, TimeUnit.MILLISECONDS, "testKey"); } catch (Throwable t) { onFailure(t); } latch.countDown(); }
@Test public void testAsyncLateListener() throws Exception { ListenableFuture<String> getFuture; final CountDownLatch latch = new CountDownLatch(1); try (DelayedMap.AsyncClient client = createClient(DelayedMap.AsyncClient.class, syncServer).get()) { getFuture = client.getValueSlowly(250, TimeUnit.MILLISECONDS, "testKey"); // Sleep first, to test the case where the callback is executed immediately Thread.sleep(500); Futures.addCallback( getFuture, new FutureCallback<String>() { @Override public void onSuccess(String result) { latch.countDown(); } @Override public void onFailure(Throwable t) { fail("Async call to getValueSlowly failed", t); } }); // Because of the sleep above, the latch should already be open // (because the callback is added on this thread and the default callback // executor runs callbacks on the same thread, this shouldn't even be a race). assertTrue(latch.await(0, TimeUnit.MILLISECONDS)); } }
@Test public void testAsyncEarlyListener() throws Exception { ListenableFuture<String> getFuture; final CountDownLatch latch = new CountDownLatch(1); try (DelayedMap.AsyncClient client = createClient(DelayedMap.AsyncClient.class, syncServer).get()) { getFuture = client.getValueSlowly(500, TimeUnit.MILLISECONDS, "testKey"); // Add callback immediately, to test case where it should be fired later when the // results are ready Futures.addCallback( getFuture, new FutureCallback<String>() { @Override public void onSuccess(String result) { latch.countDown(); } @Override public void onFailure(Throwable t) { fail("Async call to getValueSlowly failed", t); } }); latch.await(); } }
@Test public void testAsyncClient() throws Exception { ListenableFuture<String> getBeforeFuture; ListenableFuture<String> getAfterFuture; ListenableFuture<Void> putFuture; try (DelayedMap.AsyncClient client = createClient(DelayedMap.AsyncClient.class, syncServer).get()) { getBeforeFuture = client.getValueSlowly(200, TimeUnit.MILLISECONDS, "testKey"); putFuture = client.putValueSlowly(400, TimeUnit.MILLISECONDS, "testKey", "testValue"); getAfterFuture = client.getValueSlowly(600, TimeUnit.MILLISECONDS, "testKey"); assertEquals(Uninterruptibles.getUninterruptibly(getBeforeFuture), "default"); assertEquals(Uninterruptibles.getUninterruptibly(getAfterFuture), "testValue"); Uninterruptibles.getUninterruptibly(putFuture); } }
@Test public void testAsyncOutOfOrder() throws Exception { ListenableFuture<String> getFuture; ListenableFuture<Void> putFuture; try (DelayedMap.AsyncClient client = createClient(DelayedMap.AsyncClient.class, syncServer).get()) { getFuture = client.getValueSlowly(500, TimeUnit.MILLISECONDS, "testKey"); putFuture = client.putValueSlowly(250, TimeUnit.MILLISECONDS, "testKey", "testValue"); assertEquals(getFuture.get(1, TimeUnit.SECONDS), "testValue"); putFuture.get(1, TimeUnit.SECONDS); } }
// Call a method that will sleep for longer than the channel timeout, and expect a // TimeoutException (wrapped in a TTransportException) @Test public void testAsyncTimeout() throws Exception { try (DelayedMap.AsyncClient client = createClient(DelayedMap.AsyncClient.class, syncServer).get()) { ListenableFuture<String> getFuture = client.getValueSlowly(1500, TimeUnit.MILLISECONDS, "testKey"); try { getFuture.get(2000, TimeUnit.MILLISECONDS); fail("Call did not timeout as expected"); } catch (java.util.concurrent.TimeoutException e) { fail("Waited too long for channel timeout"); } catch (ExecutionException e) { checkTransportException(e.getCause(), ReadTimeoutException.class); } } }