Search in sources :

Example 6 with ByteStream

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()));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ByteStream(com.hotels.styx.api.ByteStream) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 7 with ByteStream

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));
}
Also used : Buffer(com.hotels.styx.api.Buffer) LoggingHandler(io.netty.handler.logging.LoggingHandler) CompletableFuture(java.util.concurrent.CompletableFuture) ByteStream(com.hotels.styx.api.ByteStream) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 8 with ByteStream

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()));
}
Also used : Buffer(com.hotels.styx.api.Buffer) CompletableFuture(java.util.concurrent.CompletableFuture) ByteStream(com.hotels.styx.api.ByteStream) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 9 with ByteStream

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.*")));
}
Also used : Buffer(com.hotels.styx.api.Buffer) CompletableFuture(java.util.concurrent.CompletableFuture) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) InetSocketAddress(java.net.InetSocketAddress) ByteStream(com.hotels.styx.api.ByteStream) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 10 with ByteStream

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));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ByteStream(com.hotels.styx.api.ByteStream) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) TransportLostException(com.hotels.styx.api.exceptions.TransportLostException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

ByteStream (com.hotels.styx.api.ByteStream)12 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)11 Test (org.junit.jupiter.api.Test)10 TransportLostException (com.hotels.styx.api.exceptions.TransportLostException)9 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)9 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 ExecutionException (java.util.concurrent.ExecutionException)9 Buffer (com.hotels.styx.api.Buffer)8 LoggingHandler (io.netty.handler.logging.LoggingHandler)3 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)2 InetSocketAddress (java.net.InetSocketAddress)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Level (ch.qos.logback.classic.Level)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Buffers.toByteBuf (com.hotels.styx.api.Buffers.toByteBuf)1 Context (com.hotels.styx.api.HttpInterceptor.Context)1 HttpResponseStatus (com.hotels.styx.api.HttpResponseStatus)1 OK (com.hotels.styx.api.HttpResponseStatus.OK)1 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)1