TimestampAllocationFailures { public RuntimeException responseTo(Throwable newFailure) { synchronized (this) { logNewFailure(newFailure); previousAllocationFailure = newFailure; } if (newFailure instanceof MultipleRunningTimestampServiceError) { encounteredMultipleRunningTimestamps = true; return wrapMultipleRunningTimestampServiceError(newFailure); } if (newFailure instanceof ServiceNotAvailableException) { return (ServiceNotAvailableException) newFailure; } return new SafeRuntimeException("Could not allocate more timestamps", newFailure); } @VisibleForTesting  TimestampAllocationFailures(Logger log);  TimestampAllocationFailures(); RuntimeException responseTo(Throwable newFailure); void verifyWeShouldIssueMoreTimestamps();  }
@Test public void shouldRethrowExceptions() { RuntimeException response = allocationFailures.responseTo(FAILURE); assertThat(response.getCause(), is(FAILURE)); assertThat(response, isA(RuntimeException.class)); assertThat(response.getMessage(), containsString("Could not allocate more timestamps")); }
@Test public void shouldRethrowMultipleRunningTimestampServiceErrorsAsServiceNotAvailableExceptions() { RuntimeException response = allocationFailures.responseTo(MULTIPLE_RUNNING_SERVICES_FAILURE); assertThat(response instanceof ServiceNotAvailableException, is(true)); assertThat(response.getCause(), is(MULTIPLE_RUNNING_SERVICES_FAILURE)); }
@Test public void shouldRethrowServiceNotAvailableExceptionsWithoutWrapping() { RuntimeException response = allocationFailures.responseTo(SERVICE_NOT_AVAILABLE_EXCEPTION); assertThat(response instanceof ServiceNotAvailableException, is(true)); assertThat(response, is(SERVICE_NOT_AVAILABLE_EXCEPTION)); }
@SuppressWarnings("Slf4jConstantLogMessage") @Test public void shouldLogTheFirstOfATypeOfExceptionToError() { ignoringExceptions(() -> allocationFailures.responseTo(FAILURE)); verify(log).error(anyString(), eq(FAILURE)); }
@SuppressWarnings("Slf4jConstantLogMessage") @Test public void shouldLogTheSecondOfATypeOfExceptionToInfo() { ignoringExceptions(() -> allocationFailures.responseTo(FAILURE)); ignoringExceptions(() -> allocationFailures.responseTo(FAILURE)); verify(log).info(anyString(), eq(FAILURE)); }
@SuppressWarnings("Slf4jConstantLogMessage") @Test public void shouldLog2DifferentExceptionsToError() { ignoringExceptions(() -> allocationFailures.responseTo(FAILURE)); ignoringExceptions(() -> allocationFailures.responseTo(MULTIPLE_RUNNING_SERVICES_FAILURE)); verify(log).error(anyString(), eq(FAILURE)); verify(log).error(anyString(), eq(MULTIPLE_RUNNING_SERVICES_FAILURE)); }