### Question:
WaitingObserver implements ExecutionObserver { public boolean await(long timeoutInMillis) throws InterruptedException { if (timeoutInMillis > 0) { return latch.await(timeoutInMillis, TimeUnit.MILLISECONDS); } else { latch.await(); return true; } }  @Override void onResult(ExecutionResult executionResult); ExecutionResult getExecutionResult(); boolean await(long timeoutInMillis); boolean await(TimeConstraints timeConstraints);  }### Answer:
@Test public void awaitMillis_noTimeout() throws InterruptedException { WaitingObserver observer = new WaitingObserver(); notifyAfter(observer, 100); assertTrue(observer.await(0)); }
@Test public void awaitMillis_noTimeout_alreadyHappened() throws InterruptedException { WaitingObserver observer = new WaitingObserver(); notifyAfter(observer, 0); assertTrue(observer.await(0)); }
@Test public void awaitMillis_negativeTimeout() throws InterruptedException { WaitingObserver observer = new WaitingObserver(); notifyAfter(observer, 100); assertTrue(observer.await(-1)); }
@Test public void awaitMillis_withTimeout_Ok() throws InterruptedException { WaitingObserver observer = new WaitingObserver(); notifyAfter(observer, 100); assertTrue(observer.await(1000)); }
@Test public void awaitMillis_withTimeout_AlreadyHappened() throws InterruptedException { WaitingObserver observer = new WaitingObserver(); notifyAfter(observer, 0); assertTrue(observer.await(1000)); }
@Test public void awaitMillis_withTimeout_Fails() throws InterruptedException { WaitingObserver observer = new WaitingObserver(); notifyAfter(observer, 1000); assertFalse(observer.await(100)); }
@Test public void awaitTimeConstraints_noTimeout() throws InterruptedException { WaitingObserver observer = new WaitingObserver(); TimeConstraints tc = mock(TimeConstraints.class); when(tc.getTimeRemaining()).thenReturn(null); notifyAfter(observer, 100); assertTrue(observer.await(tc)); }
@Test public void awaitTimeConstraints_withTimeout_Ok() throws InterruptedException { WaitingObserver observer = new WaitingObserver(); TimeConstraints tc = mock(TimeConstraints.class); when(tc.getTimeRemaining()).thenReturn(1000L); notifyAfter(observer, 100); assertTrue(observer.await(tc)); }
@Test public void awaitTimeConstraints_withTimeout_AlreadyHappened() throws InterruptedException { WaitingObserver observer = new WaitingObserver(); TimeConstraints tc = mock(TimeConstraints.class); when(tc.getTimeRemaining()).thenReturn(1000L); notifyAfter(observer, 0); assertTrue(observer.await(tc)); }
@Test public void awaitTimeConstraints_withTimeout_Fails() throws InterruptedException { WaitingObserver observer = new WaitingObserver(); TimeConstraints tc = mock(TimeConstraints.class); when(tc.getTimeRemaining()).thenReturn(100L); notifyAfter(observer, 1000); assertFalse(observer.await(tc)); }
@Test public void awaitTimeConstraints_expiredBeforeCallButHappenedBeforeAwait() throws InterruptedException { WaitingObserver observer = new WaitingObserver(); TimeConstraints tc = mock(TimeConstraints.class); when(tc.getTimeRemaining()).thenReturn(-5L); notifyAfter(observer, 0); assertTrue(observer.await(tc)); }