@Override public synchronized void onFailure(Throwable t) { assertFalse(called); assertThat(t).isInstanceOf(CancellationException.class); called = true; } };
public void testCallUninterruptiblyWithTimeout_badCallableWithEnoughTime() throws Exception { try { service.callUninterruptiblyWithTimeout(BAD_CALLABLE, ENOUGH_MS, MILLISECONDS); fail("no exception thrown"); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(SampleException.class); } }
public void testCallWithTimeout_badCallableWithEnoughTime() throws Exception { try { service.callWithTimeout(BAD_CALLABLE, ENOUGH_MS, MILLISECONDS); fail("no exception thrown"); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(SampleException.class); } }
public void testTransform_rejectionPropagatesToOutput() throws Exception { SettableFuture<Foo> input = SettableFuture.create(); Function<Foo, Foo> identity = identity(); ListenableFuture<Foo> transformed = transform(input, identity, REJECTING_EXECUTOR); input.set(new Foo()); try { getDone(transformed); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); } }
public void testCatching_rejectionPropagatesToOutput() throws Exception { SettableFuture<String> input = SettableFuture.create(); ListenableFuture<String> transformed = catching(input, Throwable.class, constant("foo"), REJECTING_EXECUTOR); input.setException(new Exception()); try { getDone(transformed); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); } }
public void testGetCheckedTimed_TimeoutException() { SettableFuture<String> future = SettableFuture.create(); try { getChecked(future, TwoArgConstructorException.class, 0, SECONDS); fail(); } catch (TwoArgConstructorException expected) { assertThat(expected).hasCauseThat().isInstanceOf(TimeoutException.class); } }
public void testTransform_getThrowsError() throws Exception { ListenableFuture<Object> input = UncheckedThrowingFuture.throwingError(new MyError()); ListenableFuture<Object> output = transform(input, identity(), directExecutor()); try { getDone(output); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyError.class); } }
public void testTransformAsync_rejectionPropagatesToOutput() throws Exception { SettableFuture<Foo> input = SettableFuture.create(); AsyncFunction<Foo, Foo> asyncIdentity = asyncIdentity(); ListenableFuture<Foo> transformed = transformAsync(input, asyncIdentity, REJECTING_EXECUTOR); input.set(new Foo()); try { getDone(transformed); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class); } }
public void testRunWithTimeout_badRunnableWithEnoughTime() throws Exception { try { service.runWithTimeout(BAD_RUNNABLE, ENOUGH_MS, MILLISECONDS); fail("no exception thrown"); } catch (UncheckedExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(SampleRuntimeException.class); } }
public void testRunUninterruptiblyWithTimeout_badRunnableWithEnoughTime() throws Exception { try { service.runUninterruptiblyWithTimeout(BAD_RUNNABLE, ENOUGH_MS, MILLISECONDS); fail("no exception thrown"); } catch (UncheckedExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(SampleRuntimeException.class); } }
public void testTransform_getThrowsRuntimeException() throws Exception { ListenableFuture<Object> input = UncheckedThrowingFuture.throwingRuntimeException(new MyRuntimeException()); ListenableFuture<Object> output = transform(input, identity(), directExecutor()); try { getDone(output); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyRuntimeException.class); } }
/** A single non-error failure is not logged because it is reported via the output future. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_exception() throws Exception { try { getDone(allAsList(immediateFailedFuture(new MyException()))); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyException.class); assertEquals( "Nothing should be logged", 0, aggregateFutureLogHandler.getStoredLogRecords().size()); } }
/** Ensure that errors are always logged. */ @SuppressWarnings("unchecked") public void testSuccessfulAsList_logging_error() throws Exception { assertEquals( newArrayList((Object) null), getDone(successfulAsList(immediateFailedFuture(new MyError())))); List<LogRecord> logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // errors are always logged assertThat(logged.get(0).getThrown()).isInstanceOf(MyError.class); }
public void testNewDataInput_readFullyAndThenSome() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); byte[] actual = new byte[bytes.length * 2]; try { in.readFully(actual); fail("expected exception"); } catch (IllegalStateException ex) { assertThat(ex).hasCauseThat().isInstanceOf(EOFException.class); } }
/** * This test reproduces the bug in canonicalizeWildcardType() when the type variable is * recursively bounded. */ public void testRecursiveWildcardSubtypeBug() throws Exception { try { new RecursiveTypeBoundBugExample<>().testAllDeclarations(); fail(); } catch (Exception e) { assertThat(e).hasCauseThat().isInstanceOf(AssertionError.class); } }
public void testTransformAsync_getThrowsRuntimeException() throws Exception { ListenableFuture<Object> input = UncheckedThrowingFuture.throwingRuntimeException(new MyRuntimeException()); ListenableFuture<Object> output = transformAsync(input, asyncIdentity(), directExecutor()); try { getDone(output); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyRuntimeException.class); } }
public void testTransformAsync_getThrowsError() throws Exception { ListenableFuture<Object> input = UncheckedThrowingFuture.throwingError(new MyError()); ListenableFuture<Object> output = transformAsync(input, asyncIdentity(), directExecutor()); try { getDone(output); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyError.class); } }
public void testInvokeSubscriberMethod_exceptionWrapping() throws Throwable { Method method = getTestSubscriberMethod("exceptionThrowingMethod"); Subscriber subscriber = Subscriber.create(bus, this, method); try { subscriber.invokeSubscriberMethod(FIXTURE_ARGUMENT); fail("Subscribers whose methods throw must throw InvocationTargetException"); } catch (InvocationTargetException expected) { assertThat(expected).hasCauseThat().isInstanceOf(IntentionalException.class); } }
public void testNewDataInput_readByte() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); for (int i = 0; i < bytes.length; i++) { assertEquals(bytes[i], in.readByte()); } try { in.readByte(); fail("expected exception"); } catch (IllegalStateException ex) { assertThat(ex).hasCauseThat().isInstanceOf(EOFException.class); } }
public void testNewDataInput_readUnsignedByte() { ByteArrayDataInput in = ByteStreams.newDataInput(bytes); for (int i = 0; i < bytes.length; i++) { assertEquals(bytes[i], in.readUnsignedByte()); } try { in.readUnsignedByte(); fail("expected exception"); } catch (IllegalStateException ex) { assertThat(ex).hasCauseThat().isInstanceOf(EOFException.class); } }