public void testTrustedGetFailure_Completed() { SettableFuture<String> future = SettableFuture.create(); future.set("261"); assertThat(future.tryInternalFastPathGetFailure()).isNull(); }
public void testTrustedGetFailure_CanceledNoCause() { SettableFuture<String> future = SettableFuture.create(); future.cancel(false); assertThat(future.tryInternalFastPathGetFailure()).isNull(); }
public void testGetFailure_Failed() { AbstractFuture<String> future = new AbstractFuture<String>() {}; final Throwable failure = new Throwable(); future.setException(failure); assertThat(future.tryInternalFastPathGetFailure()).isNull(); }
public void testGetFailure_CanceledNoCause() { AbstractFuture<String> future = new AbstractFuture<String>() {}; future.cancel(false); assertThat(future.tryInternalFastPathGetFailure()).isNull(); }
public void testGetFailure_Completed() { AbstractFuture<String> future = new AbstractFuture<String>() {}; future.set("261"); assertThat(future.tryInternalFastPathGetFailure()).isNull(); }
public void testTrustedGetFailure_NotCompleted() { SettableFuture<String> future = SettableFuture.create(); assertThat(future.isDone()).isFalse(); assertThat(future.tryInternalFastPathGetFailure()).isNull(); }
public void testGetFailure_NotCompleted() { AbstractFuture<String> future = new AbstractFuture<String>() {}; assertThat(future.isDone()).isFalse(); assertThat(future.tryInternalFastPathGetFailure()).isNull(); }
@Override public synchronized void terminated(State from) { assertEquals(from, Iterables.getLast(stateHistory, State.NEW)); stateHistory.add(State.TERMINATED); assertEquals(State.TERMINATED, service.state()); if (from == State.NEW) { try { service.awaitRunning(); fail(); } catch (IllegalStateException expected) { assertThat(expected).hasCauseThat().isNull(); assertThat(expected) .hasMessageThat() .isEqualTo("Expected the service " + service + " to be RUNNING, but was TERMINATED"); } } completionLatch.countDown(); }
@Override public synchronized void stopping(State from) { assertEquals(from, Iterables.getLast(stateHistory)); stateHistory.add(State.STOPPING); if (from == State.STARTING) { try { service.awaitRunning(); fail(); } catch (IllegalStateException expected) { assertThat(expected).hasCauseThat().isNull(); assertThat(expected) .hasMessageThat() .isEqualTo("Expected the service " + service + " to be RUNNING, but was STOPPING"); } } assertNotSame(from, service.state()); }
@Test public void shouldBeAbleToBeUsedFromDifferentThread() { final CountDownLatch sync = new CountDownLatch(1); final Throwable[] error = {null}; new Thread() { @Override public void run() { try { executeQuery("select * from table_name"); } catch (Throwable e) { e.printStackTrace(); error[0] = e; } finally { sync.countDown(); } } } .start(); try { sync.await(); } catch (InterruptedException e) { throw new RuntimeException(e); } assertThat(error[0]).isNull(); }
private void verifyOk(ApiFuture<?> result) { Throwable error = null; try { result.get(FLUSH_PERIOD.plus(DELAY_BUFFER).toMillis(), TimeUnit.MILLISECONDS); } catch (ExecutionException e) { error = e.getCause(); } catch (Throwable t) { error = t; } assertThat(error).isNull(); }
@GwtIncompatible public void testImmediateCancelledFutureStack() throws Exception { ListenableFuture<String> future = CallerClass1.makeImmediateCancelledFuture(); assertTrue(future.isCancelled()); try { CallerClass2.get(future); fail(); } catch (CancellationException expected) { // There should be two CancellationException chained together. The outer one should have the // stack trace of where the get() call was made, and the inner should have the stack trace of // where the immediateCancelledFuture() call was made. List<StackTraceElement> stackTrace = ImmutableList.copyOf(expected.getStackTrace()); assertFalse(Iterables.any(stackTrace, hasClassName(CallerClass1.class))); assertTrue(Iterables.any(stackTrace, hasClassName(CallerClass2.class))); // See AbstractFutureCancellationCauseTest for how to set causes. assertThat(expected.getCause()).isNull(); } }
@Test public void testConnectResponseBeforeTimeout() throws Exception { long startTimeMs = SystemClock.elapsedRealtime(); final ConditionVariable startCondition = buildUrlRequestStartedCondition(); final CountDownLatch openLatch = new CountDownLatch(1); AtomicReference<Exception> exceptionOnTestThread = new AtomicReference<>(); new Thread() { @Override public void run() { try { dataSourceUnderTest.open(testDataSpec); } catch (HttpDataSourceException e) { exceptionOnTestThread.set(e); } finally { openLatch.countDown(); } } }.start(); startCondition.block(); // We should still be trying to open. assertNotCountedDown(openLatch); // We should still be trying to open as we approach the timeout. SystemClock.setCurrentTimeMillis(startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1); assertNotCountedDown(openLatch); // The response arrives just in time. dataSourceUnderTest.urlRequestCallback.onResponseStarted(mockUrlRequest, testUrlResponseInfo); openLatch.await(); assertThat(exceptionOnTestThread.get()).isNull(); }
@Test public void testOneToManyAuto() throws InterruptedException { MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true); ReframingResponseObserver<String, String> middleware = new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(2)); ServerStreamingStashCallable<String, String> innerCallable = new ServerStreamingStashCallable<>(ImmutableList.of("a", "b")); innerCallable.call("request", middleware); Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a-b"); Truth.assertThat(outerObserver.isDone()).isTrue(); Truth.assertThat(outerObserver.getFinalError()).isNull(); }
public void testCancel_notDoneNoInterrupt() throws Exception { InterruptibleFuture future = new InterruptibleFuture(); assertTrue(future.cancel(false)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); assertFalse(future.wasInterrupted()); assertFalse(future.interruptTaskWasCalled); try { future.get(); fail("Expected CancellationException"); } catch (CancellationException e) { // See AbstractFutureCancellationCauseTest for how to set causes assertThat(e).hasCauseThat().isNull(); } }
public void testCancel_notDoneInterrupt() throws Exception { InterruptibleFuture future = new InterruptibleFuture(); assertTrue(future.cancel(true)); assertTrue(future.isCancelled()); assertTrue(future.isDone()); assertTrue(future.wasInterrupted()); assertTrue(future.interruptTaskWasCalled); try { future.get(); fail("Expected CancellationException"); } catch (CancellationException e) { // See AbstractFutureCancellationCauseTest for how to set causes assertThat(e).hasCauseThat().isNull(); } }
@Test public void testOneToMany() throws InterruptedException { MockResponseObserver<String> outerObserver = new MockResponseObserver<>(false); ReframingResponseObserver<String, String> middleware = new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(2)); ServerStreamingStashCallable<String, String> innerCallable = new ServerStreamingStashCallable<>(ImmutableList.of("a", "b")); innerCallable.call("request", middleware); Preconditions.checkState(outerObserver.popNextResponse() == null); outerObserver.getController().request(1); Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a-b"); Truth.assertThat(outerObserver.isDone()).isTrue(); Truth.assertThat(outerObserver.getFinalError()).isNull(); }
@Test public void readRowRetryCodesMustMatch() { EnhancedBigtableStubSettings.Builder builder = EnhancedBigtableStubSettings.newBuilder() .setProjectId("my-project") .setInstanceId("my-instance"); builder.readRowsSettings().setRetryableCodes(Code.DEADLINE_EXCEEDED); builder.readRowSettings().setRetryableCodes(Code.ABORTED); Exception actualError = null; try { builder.build(); } catch (Exception e) { actualError = e; } assertThat(actualError).isNotNull(); builder.readRowSettings().setRetryableCodes(Code.DEADLINE_EXCEEDED); actualError = null; try { builder.build(); } catch (Exception e) { actualError = e; } assertThat(actualError).isNull(); }
@Test public void response() { Response<String> response = Response.success("Hi"); Result<String> result = Result.response(response); assertThat(result.isError()).isFalse(); assertThat(result.error()).isNull(); assertThat(result.response()).isSameAs(response); }