use of com.hotels.styx.api.ByteStream in project styx by ExpediaGroup.
the class HttpResponseWriterTest method failsTheResultWhenResponseWriteFails.
@Test
public void failsTheResultWhenResponseWriteFails() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new CaptureChannelArgumentsHandler(channelArgs), new SimpleChannelInboundHandler<LiveHttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, LiveHttpResponse response) throws Exception {
HttpResponseWriter writer = new HttpResponseWriter(ctx);
CompletableFuture<Void> future = writer.write(response);
assertThat(future.isDone(), is(false));
writeError(channelArgs);
assertThat(future.isDone(), is(true));
future.get(200, MILLISECONDS);
}
});
assertThrows(ExecutionException.class, () -> ch.writeInbound(response(OK).body(new ByteStream(contentObservable)).build()));
}
use of com.hotels.styx.api.ByteStream in project styx by ExpediaGroup.
the class HttpResponseWriterTest method completesFutureOnlyAfterAllWritesAreSuccessfullyCompleted.
@Test
public void completesFutureOnlyAfterAllWritesAreSuccessfullyCompleted() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new CaptureChannelArgumentsHandler(channelArgs), new LoggingHandler(), new SimpleChannelInboundHandler<LiveHttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, LiveHttpResponse response) throws Exception {
HttpResponseWriter writer = new HttpResponseWriter(ctx);
CompletableFuture<Void> future = writer.write(response);
assertThat(future.isDone(), is(false));
contentObservable.onNext(new Buffer("aaa", UTF_8));
assertThat(future.isDone(), is(false));
contentObservable.onComplete();
assertThat(future.isDone(), is(false));
// For response headers
writeAck(channelArgs);
// For content chunk
writeAck(channelArgs);
// For EMPTY_LAST_CHUNK
writeAck(channelArgs);
assertThat(future.isDone(), is(true));
channelRead.set(true);
}
});
ch.writeInbound(response(OK).body(new ByteStream(contentObservable)).build());
assertThat(channelRead.get(), is(true));
}
use of com.hotels.styx.api.ByteStream in project styx by ExpediaGroup.
the class HttpResponseWriterTest method failsTheResultWhenContentWriteFails.
@Test
public void failsTheResultWhenContentWriteFails() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new CaptureChannelArgumentsHandler(channelArgs), new SimpleChannelInboundHandler<LiveHttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, LiveHttpResponse response) throws Exception {
HttpResponseWriter writer = new HttpResponseWriter(ctx);
CompletableFuture<Void> future = writer.write(response);
writeAck(channelArgs);
assertThat(future.isDone(), is(false));
contentObservable.onNext(new Buffer("aaa", UTF_8));
assertThat(future.isDone(), is(false));
contentObservable.onComplete();
assertThat(future.isDone(), is(false));
writeError(channelArgs);
assertThat(future.isDone(), is(true));
future.get(200, MILLISECONDS);
}
});
assertThrows(ExecutionException.class, () -> ch.writeInbound(response(OK).body(new ByteStream(contentObservable)).build()));
}
use of com.hotels.styx.api.ByteStream in project styx by ExpediaGroup.
the class HttpResponseWriterTest method logsSentAndAcknowledgedBytes.
@Test
public void logsSentAndAcknowledgedBytes() {
EmbeddedChannel ch = new EmbeddedChannel(new SimpleChannelInboundHandler<LiveHttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, LiveHttpResponse response) throws Exception {
HttpResponseWriter writer = new HttpResponseWriter(ctx);
CompletableFuture<Void> future = writer.write(response);
assertThat(future.isDone(), is(false));
contentObservable.onNext(new Buffer("aaa", UTF_8));
assertThat(future.isDone(), is(false));
contentObservable.onNext(new Buffer("bbbb", UTF_8));
assertThat(future.isDone(), is(false));
contentObservable.onError(new TransportLostException(new InetSocketAddress(getLoopbackAddress(), 5050), newOriginBuilder("localhost", 5050).build()));
assertThat(future.isDone(), is(true));
channelRead.set(true);
}
});
ch.writeInbound(response(OK).body(new ByteStream(contentObservable)).build());
assertThat(LOGGER.lastMessage(), is(loggingEvent(Level.WARN, "Content observable error. Written content bytes 7/7 \\(ackd/sent\\). Write events 3/3 \\(ackd/writes\\).*", TransportLostException.class, "Connection to origin lost. origin=\"generic-app:anonymous-origin:localhost:5050\", remoteAddress=\"localhost/127.0.0.1:5050.*")));
}
use of com.hotels.styx.api.ByteStream in project styx by ExpediaGroup.
the class HttpResponseWriterTest method unsubscribesFromContentWhenCancelled.
@Test
public void unsubscribesFromContentWhenCancelled() throws Exception {
CaptureHttpResponseWriteEventsHandler writeEventsCollector = new CaptureHttpResponseWriteEventsHandler();
AtomicBoolean unsubscribed = new AtomicBoolean(false);
EmbeddedChannel ch = new EmbeddedChannel(new CaptureChannelArgumentsHandler(channelArgs), writeEventsCollector, new SimpleChannelInboundHandler<LiveHttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, LiveHttpResponse response) throws Exception {
HttpResponseWriter writer = new HttpResponseWriter(ctx);
CompletableFuture<Void> future = writer.write(response);
writeAck(channelArgs);
assertThat(future.isDone(), is(false));
future.cancel(false);
assertThat(unsubscribed.get(), is(true));
assertThat(future.isDone(), is(true));
channelRead.set(true);
}
});
ch.writeInbound(response(OK).body(new ByteStream(contentObservable.doOnCancel(() -> unsubscribed.set(true)))).build());
assertThat(channelRead.get(), is(true));
}
Aggregations