// Plain callable (no special handling) public void testBasicCallable() throws Exception { TestCallable<String> tc = new TestCallable<String>("X"); assertEquals(0, tc.runs); Future<String> f = schedInline.submit(tc); assertEquals("X", f.get()); assertEquals(1, tc.runs); }
// Plain runnable with value (no special handling) public void testBasicRunnableWithValue() throws Exception { TestRunnable tr = new TestRunnable(); assertEquals(0, tr.runs); Future<String> f = schedInline.submit(tr, "X"); assertEquals("X", f.get()); assertEquals(1, tr.runs); }
// Verify you can return null from your callable public void testRetryableCallableReturningNull() throws Exception { TestRtryCallable<Object> tc = new TestRtryCallable<Object>(null, 1, 1); assertEquals(0, tc.runs); assertEquals(0, tc.retries); Future<Object> f = schedPooled.submit(tc); assertNull(f.get()); assertFalse(tc.gaveUp); assertEquals(2, tc.runs); assertEquals(1, tc.retries); }
// Verify you can still return an exception from your callable public void testRetryableCallableReturningExc() throws Exception { ExecutionException e = new ExecutionException(new Exception()); TestRtryCallable<ExecutionException> tc = new TestRtryCallable<ExecutionException>(e, 0, 1); assertEquals(0, tc.runs); assertEquals(0, tc.retries); Future<ExecutionException> f = schedPooled.submit(tc); assertSame(e, f.get()); assertFalse(tc.gaveUp); assertEquals(1, tc.runs); assertEquals(0, tc.retries); }
// A few retries and then success public void testRetryableCallable() throws Exception { TestRtryCallable<String> tc = new TestRtryCallable<String>("X", 2, 3); assertEquals(0, tc.runs); assertEquals(0, tc.retries); Future<String> f = schedPooled.submit(tc); assertEquals("X", f.get()); assertTrue(f.isDone()); assertFalse(f.isCancelled()); assertFalse(tc.gaveUp); assertEquals("X", tc.result); assertEquals(3, tc.runs); assertEquals(2, tc.retries); }
// A few retries and then success public void testCancellingRetryableCallable() throws Exception { TestRtryCallable<String> tc = new TestRtryCallable<String>("X", 2, 3); assertEquals(0, tc.runs); assertEquals(0, tc.retries); Future<String> f = schedPooled.submit(tc); f.cancel(true); try { Object o = f.get(); fail("expected cancellation, got " + o); } catch (CancellationException e) { // ok } }
// A few retries and a failure public void testFailingRetryableCallable() throws Exception { TestRtryCallable<String> tc = new TestRtryCallable<String>("X", 4, 3); assertEquals(0, tc.runs); assertEquals(0, tc.retries); Future<String> f = schedPooled.submit(tc); try { String s = f.get(); fail("Expected failure, got " + s); } catch (ExecutionException e) { assertEquals("Too many failures", e.getMessage()); assertEquals(4, ((CompositeExecutorException) e).getExceptions().size()); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); assertTrue("Hey, no also caused by", sw.toString().indexOf("Also caused by") > 0); assertSame(e, tc.result); } assertTrue(tc.gaveUp); assertEquals(4, tc.runs); assertEquals(3, tc.retries); }