@Test public void StreamingChannel_doStreamChunk_works_as_expected_when_crazy_exception_is_thrown() { // given Throwable crazyEx = new RuntimeException("kaboom"); doThrow(crazyEx).when(channelMock).writeAndFlush(any(Object.class)); // when ChannelFuture result = streamingChannelSpy.doStreamChunk(contentChunkMock); // then verify(channelMock).writeAndFlush(any(Object.class)); verify(contentChunkMock, never()).release(); verifyFailedChannelFuture( result, "StreamingChannel.doStreamChunk() threw an exception", crazyEx ); }
@DataProvider(value = { "true", "false" }) @Test public void StreamingChannel_doCloseChannelDueToUnrecoverableError_works_as_expected(boolean callActive) { // given streamingChannelSpy.callActiveHolder.heldObject = callActive; Throwable unrecoverableError = new RuntimeException("kaboom"); // when streamingChannelSpy.doCloseChannelDueToUnrecoverableError(unrecoverableError); // then if (callActive) { verify(channelIsBrokenAttrMock).set(true); verifyChannelReleasedBackToPool(streamingChannelSpy.callActiveHolder, channelPoolMock, channelMock); verify(channelMock).close(); } else { verify(channelIsBrokenAttrMock, never()).set(anyBoolean()); verify(channelMock, never()).close(); } }
listener.operationComplete(getFutureForCase(true, false, null)); listener.operationComplete(getFutureForCase(false, true, null)); listener.operationComplete(getFutureForCase(false, false, normalFutureFailure)); listener.operationComplete(getFutureForCase(false, false, null));
@DataProvider(value = { "80 | false | localhost | localhost", "80 | true | localhost | localhost:80", "8080 | false | localhost | localhost:8080", "443 | true | localhost | localhost", "443 | false | localhost | localhost:443", "8080 | true | localhost | localhost:8080", }, splitBy = "\\|") @Test public void streamDownstreamCall_setsHostHeaderCorrectly(int downstreamPort, boolean isSecure, String downstreamHost, String expectedHostHeader) { // given DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, ""); ChannelHandlerContext ctx = mockChannelHandlerContext(); StreamingCallback streamingCallback = mock(StreamingCallback.class); // when new StreamingAsyncHttpClient( 200, 200, true, mock(DistributedTracingConfig.class) ).streamDownstreamCall( downstreamHost, downstreamPort, request, isSecure, false, streamingCallback, 200, true, true, ctx ); // then assertThat(request.headers().get(HOST)).isEqualTo(expectedHostHeader); }
@Test public void StreamingChannel_doStreamChunk_works_as_expected_when_downstream_call_is_not_active() { // given streamingChannelSpy.callActiveHolder.heldObject = false; // when ChannelFuture result = streamingChannelSpy.doStreamChunk(contentChunkMock); // then verify(channelMock, never()).writeAndFlush(any(Object.class)); verify(contentChunkMock).release(); verifyFailedChannelFuture( result, "Unable to stream chunk - downstream call is no longer active.", null ); }
@Test public void StreamingChannel_streamChunk_fails_promise_with_unexpected_exception() { // given Throwable crazyEx = new RuntimeException("kaboom"); doThrow(crazyEx).when(eventLoopMock).execute(any(Runnable.class)); // when ChannelFuture result = streamingChannelSpy.streamChunk(contentChunkMock); // then verifyFailedChannelFuture(result, "StreamingChannel.streamChunk() threw an exception", crazyEx); }
@Test public void StreamingChannel_doStreamChunk_works_as_expected_when_closeChannelDueToUnrecoverableError_was_called_previously() { // given streamingChannelSpy.channelClosedDueToUnrecoverableError = true; // when ChannelFuture result = streamingChannelSpy.doStreamChunk(contentChunkMock); // then verify(channelMock, never()).writeAndFlush(any(Object.class)); verify(contentChunkMock).release(); verifyFailedChannelFuture( result, "Unable to stream chunks downstream - the channel was closed previously due to an unrecoverable error", null ); }