Read: 1 Read: 2 Exception in thread "main" java.util.concurrent.TimeoutException at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:228) at java.util.concurrent.FutureTask.get(FutureTask.java:91) at test.InputStreamWithTimeoutTest.main(InputStreamWithTimeoutTest.java:74)
@Override public T get() throws InterruptedException, ExecutionException { futureTask.run(); return futureTask.get(); }
@Override public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { futureTask.run(); return futureTask.get(timeout, unit); }
/** * Wait for the test task, returning the exception thrown by the test if the * test failed, an exception indicating a timeout if the test timed out, or * {@code null} if the test passed. */ private Throwable getResult(FutureTask<Throwable> task, Thread thread) { try { if (timeout > 0) { return task.get(timeout, timeUnit); } else { return task.get(); } } catch (InterruptedException e) { return e; // caller will re-throw; no need to call Thread.interrupt() } catch (ExecutionException e) { // test failed; have caller re-throw the exception thrown by the test return e.getCause(); } catch (TimeoutException e) { return createTimeoutException(thread); } }
@Implementation protected Result get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return future.get(timeout, unit); }
@Implementation protected Result get() throws InterruptedException, ExecutionException { return future.get(); }
@Override public JobExecutionResult get() throws InterruptedException { try { return super.get(); } catch (ExecutionException ee) { return JobExecutionResult.createFailureResult(ee.getCause()); } }
@Override public JobExecutionResult get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException { try { return super.get(timeout, unit); } catch (ExecutionException ee) { return JobExecutionResult.createFailureResult(ee.getCause()); } } }
private static void runNInterruptsTest( int times, SettableFuture<String> future, FutureTask<Boolean> interruptReporter) throws InterruptedException, ExecutionException, TimeoutException { Thread waitingThread = new Thread(interruptReporter); waitingThread.start(); for (int i = 0; i < times; i++) { waitingThread.interrupt(); } future.set(RESULT); assertEquals(times > 0, (boolean) interruptReporter.get(20, SECONDS)); }
@Override public V get() throws InterruptedException, ExecutionException { while (!isDone()) { scheduler.advanceToNextPostedRunnable(); } return super.get(); } }
private static Set<String> getAllWorkDagIds(List<FutureTask<Set<String>>> tasks) throws ExecutionException, InterruptedException { Set<String> allWorkDagIds = Sets.newHashSet(); for (FutureTask<Set<String>> task : tasks) { allWorkDagIds.addAll(task.get()); } return allWorkDagIds; }
@Test public void defaultExecutor() throws Exception { ApplicationContext context = new AnnotationConfigApplicationContext(ExecutorConfig.class); ExecutorService executor = context.getBean("executor", ExecutorService.class); FutureTask<String> task = new FutureTask<>(new Callable<String>() { @Override public String call() throws Exception { return "foo"; } }); executor.execute(task); assertEquals("foo", task.get()); }
private TryResult getResult(FutureTask<TryResult> ret) { TryResult o; try { o = ret.get(); } catch (Exception e) { e.printStackTrace(); return TryResult.createError(e.getMessage()); } if (ret.isCancelled()) return TryResult.createError("Cancelled"); return o; }
/** * Confirms that the test code triggers {@link InterruptedException} in a standard {@link Future}. */ public void testMakeUninterruptible_plainFutureSanityCheck() throws Exception { SettableFuture<String> future = SettableFuture.create(); FutureTask<Boolean> wasInterrupted = untimedInterruptReporter(future, true); Thread waitingThread = new Thread(wasInterrupted); waitingThread.start(); waitingThread.interrupt(); try { wasInterrupted.get(); fail(); } catch (ExecutionException expected) { assertTrue( expected.getCause().toString(), expected.getCause() instanceof InterruptedException); } }
@Override public void run() { while (true) { if (isInterrupted() || taskComplete) { return; } if (task.isDone()) { try { setResult(task.get()); } catch (InterruptedException | ExecutionException e) { setResult(new LottieResult<T>(e)); } taskComplete = true; stopTaskObserverIfNeeded(); } } } };
/** * Wait for the test task, returning the exception thrown by the test if the * test failed, an exception indicating a timeout if the test timed out, or * {@code null} if the test passed. */ private Throwable getResult(FutureTask<Throwable> task, Thread thread) { try { if (timeout > 0) { return task.get(timeout, timeUnit); } else { return task.get(); } } catch (InterruptedException e) { return e; // caller will re-throw; no need to call Thread.interrupt() } catch (ExecutionException e) { // test failed; have caller re-throw the exception thrown by the test return e.getCause(); } catch (TimeoutException e) { return createTimeoutException(thread); } }
/** * This is a basic test of the heartbeat scheduler logic. It steps through the execution of a * single dummy executor. */ @Test public void serialHeartbeatThread() throws Exception { FutureTask<Void> task = new FutureTask<>(new DummyHeartbeatTestCallable()); Thread thread = new Thread(task); thread.start(); thread.join(); task.get(); }
@Test public void testConcurrentUnpin() throws Exception { SimpleWindowPartitionCache<Integer, Object> cache = SimpleWindowPartitionCache.<Integer, Object>newBuilder() .maximumSize(1) .build(key -> new Object()); cache.pinAndGet(1); FutureTask<Boolean> ft1 = new FutureTask<>(() -> cache.unpin(1)); FutureTask<Boolean> ft2 = new FutureTask<>(() -> cache.unpin(1)); Thread t1 = new Thread(ft1); Thread t2 = new Thread(ft2); t1.start(); t2.start(); t1.join(); t2.join(); Assert.assertTrue(ft1.get() || ft2.get()); Assert.assertFalse(ft1.get() && ft2.get()); }
@Override protected void runTest() throws Throwable { final Runnable runChosenTest = new Runnable() { @Override public void run() { runChosenTest(); } }; final FutureTask<Void> task = new FutureTask<>(runChosenTest, null); startThread( new Runnable() { @Override public void run() { task.run(); } }); awaitUninterruptibly(doingCallLatch); long hangDelayMillis = (expectedOutcome == Outcome.HANG) ? EXPECTED_HANG_DELAY_MILLIS : UNEXPECTED_HANG_DELAY_MILLIS; boolean hung = !awaitUninterruptibly(callCompletedLatch, hangDelayMillis, TimeUnit.MILLISECONDS); if (hung) { assertEquals(expectedOutcome, Outcome.HANG); } else { assertNull(task.get(UNEXPECTED_HANG_DELAY_MILLIS, TimeUnit.MILLISECONDS)); } }
@Test public void defaultExecutor() throws Exception { ThreadPoolTaskExecutor executor = this.context.getBean("default", ThreadPoolTaskExecutor.class); assertEquals(1, getCorePoolSize(executor)); assertEquals(Integer.MAX_VALUE, getMaxPoolSize(executor)); assertEquals(Integer.MAX_VALUE, getQueueCapacity(executor)); assertEquals(60, getKeepAliveSeconds(executor)); assertEquals(false, getAllowCoreThreadTimeOut(executor)); FutureTask<String> task = new FutureTask<>(new Callable<String>() { @Override public String call() throws Exception { return "foo"; } }); executor.execute(task); assertEquals("foo", task.get()); }