InMemoryTimeoutManager implements TimeoutManager, AutoCloseable { @Override public void addExpiredCallback(final TimeoutExpired callback) { checkNotNull(callback, "Expired callback not allowed to be null."); callbacks.add(callback); } InMemoryTimeoutManager();  InMemoryTimeoutManager(final int timerThreadPoolSize);  InMemoryTimeoutManager(final ScheduledExecutorService scheduledService, final Clock clock); @Override void addExpiredCallback(final TimeoutExpired callback); @Override TimeoutId requestTimeout(final ExecutionContext context, final String sagaId, final long delay, final TimeUnit timeUnit, @Nullable final String name,
                               @Nullable final Object data); @Override void cancelTimeouts(final String sagaId); @Override void cancelTimeout(final TimeoutId id); @Override void close();  }
@Test public void timeoutTriggered_callbackAdded_callbackTriggered() { int delayInSec = 5; Object expectedData = new Object(); Date expectedTimeoutIn = new Date(clock.now().getTime() + TimeUnit.SECONDS.toMillis(delayInSec)); TimeoutExpired expiredCallback = mock(TimeoutExpired.class); sut.addExpiredCallback(expiredCallback); Timeout expected = Timeout.create(UUIDTimeoutId.generateNewId(), "theSagaId", "theTimeoutName", expectedTimeoutIn, expectedData); requestAndTriggerTimeout(expected.getSagaId(), expected.getName(), delayInSec, TimeUnit.SECONDS, expectedData); ArgumentCaptor<Timeout> captor = ArgumentCaptor.forClass(Timeout.class); verify(expiredCallback).expired(captor.capture()); assertThat("Saga id does not match.", captor.getValue().getSagaId(), equalTo(expected.getSagaId())); assertThat("Timeout name does not match.", captor.getValue().getName(), equalTo(expected.getName())); assertThat("Expiration time stamp does not match.", captor.getValue().getExpiredAt(), equalTo(expected.getExpiredAt())); assertThat("Data object does not match.", captor.getValue().getData(), sameInstance(expectedData)); }
@Test public void timeoutTriggered_contextCallbackAdded_callbackTriggered() { int delayInSec = 5; Object expectedData = new Object(); Date expectedTimeoutIn = new Date(clock.now().getTime() + TimeUnit.SECONDS.toMillis(delayInSec)); TimeoutExpirationCallback expiredCallback = mock(TimeoutExpirationCallback.class); sut.addExpiredCallback(expiredCallback); Timeout expected = Timeout.create(UUIDTimeoutId.generateNewId(), "theSagaId", "theTimeoutName", expectedTimeoutIn, expectedData); requestAndTriggerTimeout(expected.getSagaId(), expected.getName(), delayInSec, TimeUnit.SECONDS, expectedData); verify(expiredCallback).expired(isA(Timeout.class), isA(TimeoutExpirationContext.class)); }
@Test public void timeoutTriggered_headerValue_headerValuePartOfCallback() { TimeoutExpirationCallback expiredCallback = mock(TimeoutExpirationCallback.class); sut.addExpiredCallback(expiredCallback); requestAndTriggerTimeout(); ArgumentCaptor<TimeoutExpirationContext> captor = ArgumentCaptor.forClass(TimeoutExpirationContext.class); verify(expiredCallback).expired(isA(Timeout.class), captor.capture()); Map<HeaderName<?>, Object> headers = captor.getValue().getOriginalHeaders(); assertThat("Expected header value in callback param.", headers.get(DEFAULT_HEADER), equalTo(DEFAULT_HEADER_VALUE)); }
@Test public void timeoutTriggered_multipleCallbacksAdded_allRegisteredCallbacksAreTriggered() { TimeoutExpired callback1 = mock(TimeoutExpired.class); TimeoutExpired callback2 = mock(TimeoutExpired.class); sut.addExpiredCallback(callback1); sut.addExpiredCallback(callback2); requestAndTriggerTimeout(); verify(callback1).expired(isA(Timeout.class)); verify(callback2).expired(isA(Timeout.class)); }