@Override protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception { if (alreadyTriggeredException) { runnableWithTracingAndMdc( () -> logger.error( "IncompleteHttpCallTimeoutHandler triggered multiple times - this should not happen." ), ctx ).run(); return; } channelIdleTriggered(ctx, evt); alreadyTriggeredException = true; throw new IncompleteHttpCallTimeoutException(idleTimeoutMillis); }
@Test public void IncompleteHttpCallTimeoutHandler_throws_IncompleteHttpCallTimeoutException_and_sets_alreadyTriggeredException_to_true_when_channelIdle_is_called() throws Exception { // given long timeoutMillis = 4242; IncompleteHttpCallTimeoutHandler handlerSpy = spy(new IncompleteHttpCallTimeoutHandler(timeoutMillis)); assertThat(handlerSpy.alreadyTriggeredException).isFalse(); // when Throwable ex = catchThrowable(() -> handlerSpy.channelIdle(ctxMock, ALL_IDLE_STATE_EVENT)); // then assertThat(ex).isInstanceOf(IncompleteHttpCallTimeoutException.class); IncompleteHttpCallTimeoutException timeoutEx = (IncompleteHttpCallTimeoutException)ex; assertThat(timeoutEx.timeoutMillis).isEqualTo(timeoutMillis); assertThat(handlerSpy.alreadyTriggeredException).isTrue(); verify(handlerSpy).channelIdleTriggered(ctxMock, ALL_IDLE_STATE_EVENT); }
IncompleteHttpCallTimeoutHandler newHandler = new IncompleteHttpCallTimeoutHandler( incompleteHttpCallTimeoutMillis );
@Test public void IncompleteHttpCallTimeoutHandler_does_nothing_if_alreadyTriggeredException_is_true() { // given long timeoutMillis = 4242; IncompleteHttpCallTimeoutHandler handlerSpy = spy(new IncompleteHttpCallTimeoutHandler(timeoutMillis)); handlerSpy.alreadyTriggeredException = true; // when Throwable ex = catchThrowable(() -> handlerSpy.channelIdle(ctxMock, ALL_IDLE_STATE_EVENT)); // then assertThat(ex).isNull(); verify(handlerSpy, never()).channelIdleTriggered(any(ChannelHandlerContext.class), any(IdleStateEvent.class)); } }
IncompleteHttpCallTimeoutHandler newHandler = new IncompleteHttpCallTimeoutHandler( incompleteHttpCallTimeoutMillis );
@Override protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception { if (alreadyTriggeredException) { runnableWithTracingAndMdc( () -> logger.error( "IncompleteHttpCallTimeoutHandler triggered multiple times - this should not happen." ), ctx ).run(); return; } channelIdleTriggered(ctx, evt); alreadyTriggeredException = true; throw new IncompleteHttpCallTimeoutException(idleTimeoutMillis); }