/** * @throws Exception If failed. */ @Test public void testFutureGet() throws Exception { GridFutureAdapter<String> fut0 = new GridFutureAdapter<>(); IgniteFutureImpl<String> fut = createFuture(fut0); assertFalse(fut.isDone()); U.sleep(100); fut0.onDone("test"); assertEquals("test", fut.get()); assertTrue(fut.isDone()); }
assertFalse(fut.isDone());
/** * @throws Exception If failed. */ @Test public void testAsyncListeners() throws Exception { GridFutureAdapter<String> fut0 = new GridFutureAdapter<>(); IgniteFutureImpl<String> fut = createFuture(fut0); final CountDownLatch latch1 = new CountDownLatch(1); IgniteInClosure<? super IgniteFuture<String>> lsnr1 = createAsyncListener(latch1, CUSTOM_THREAD_NAME, null); assertFalse(fut.isDone()); fut.listenAsync(lsnr1, customExec); U.sleep(100); assertEquals(1, latch1.getCount()); fut0.onDone("test"); assert latch1.await(1, TimeUnit.SECONDS) : latch1.getCount(); checkAsyncListener(fut); checkAsyncListener(createFuture(new GridFinishedFuture<>("test"))); }
/** * @throws Exception If failed. */ @Test public void testFutureException() throws Exception { GridFutureAdapter<String> fut0 = new GridFutureAdapter<>(); final IgniteFutureImpl<String> fut = createFuture(fut0); assertFalse(fut.isDone()); U.sleep(100); IgniteCheckedException err0 = new IgniteCheckedException("test error"); fut0.onDone(err0); Exception err = (Exception)GridTestUtils.assertThrows(log, new Callable<Void>() { @Override public Void call() throws Exception { fut.get(); return null; } }, expectedException(), "test error"); assertExpectedException(err, err0); assertTrue(fut.isDone()); err = (Exception)GridTestUtils.assertThrows(log, new Callable<Void>() { @Override public Void call() throws Exception { fut.get(); return null; } }, expectedException(), null); assertExpectedException(err, err0); }
/** * @param err Err. */ private void checkChainedOnErrorFinishedFuture(final Exception err) throws Exception { IgniteFutureImpl<String> fut = createFuture(new GridFinishedFuture<String>(err)); // Chain callback will be invoked in specific executor. IgniteFuture<Integer> chained1 = fut.chainAsync(new C1<IgniteFuture<String>, Integer>() { @Override public Integer apply(IgniteFuture<String> fut) { assertEquals(CUSTOM_THREAD_NAME, Thread.currentThread().getName()); try { fut.get(); fail(); } catch (IgniteException e) { assertExpectedException(e, err); throw e; } return -1; } }, customExec); assertExceptionThrown(err, chained1); assertExceptionThrown(err, fut); assertTrue(chained1.isDone()); assertTrue(fut.isDone()); }
/** * @throws Exception If failed. */ @Test public void testChain() throws Exception { GridFutureAdapter<String> fut0 = new GridFutureAdapter<>(); IgniteFutureImpl<String> fut = createFuture(fut0); IgniteFuture<Integer> chained = fut.chain(new C1<IgniteFuture<String>, Integer>() { @Override public Integer apply(IgniteFuture<String> fut) { return Integer.valueOf(fut.get()); } }); assertFalse(chained.isDone()); U.sleep(100); final AtomicInteger lsnrCnt = new AtomicInteger(); chained.listen(new CI1<IgniteFuture<Integer>>() { @Override public void apply(IgniteFuture<Integer> fut) { assertEquals(10, (int)fut.get()); lsnrCnt.incrementAndGet(); } }); fut0.onDone("10"); assertTrue(chained.isDone()); assertEquals(10, (int)chained.get()); assertEquals(1, lsnrCnt.get()); assertTrue(fut.isDone()); assertEquals("10", fut.get()); }