@Test(expected = TimeoutException.class) public void testTimeoutReached() throws TimeoutException, InterruptedException, ExecutionException { TimeoutTask<String> timoutTask = new TimeoutTask<String>(TEST_TASK); Callable<String> callable = new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(20000); return OK; } }; timoutTask.run(callable, 10, false); }
@Test public void testResultOK() throws TimeoutException, InterruptedException, ExecutionException { TimeoutTask<String> timoutTask = new TimeoutTask<String>(TEST_TASK); Callable<String> callable = new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(10); return OK; } }; FutureTask<String> futureTask = timoutTask.run(callable, 20, false); Object result = futureTask.get(); Assert.assertEquals(OK, result); }
@Test public void testTimeoutReached2() throws TimeoutException, InterruptedException, ExecutionException { TimeoutTask<String> timoutTask = new TimeoutTask<String>(TEST_TASK); Callable<String> callable = new Callable<String>() { @Override public String call() throws Exception { while (!Thread.interrupted()) {} return OK; } }; try { FutureTask<String> futureTask = timoutTask.run(callable, 10, true); System.out.println(futureTask.get()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }