Search in sources :

Example 1 with FlushSender

use of io.servicetalk.transport.netty.internal.FlushStrategy.FlushSender in project servicetalk by apple.

the class FlushStrategyOverrideTest method overrideFlush.

@Test
void overrideFlush() throws Throwable {
    NettyConnectionContext nctx = (NettyConnectionContext) conn.connectionContext();
    MockFlushStrategy clientStrategy = new MockFlushStrategy();
    Cancellable c = nctx.updateFlushStrategy((old, isOriginal) -> isOriginal ? clientStrategy : old);
    CountDownLatch reqWritten = new CountDownLatch(1);
    StreamingHttpRequest req = client.get("/flush").payloadBody(from(1, 2, 3).map(count -> ctx.bufferAllocator().fromAscii("" + count)).afterFinally(reqWritten::countDown));
    Future<? extends Collection<Object>> clientResp = conn.request(req).flatMapPublisher(StreamingHttpResponse::messageBody).toFuture();
    // Wait for request to be written.
    reqWritten.await();
    FlushSender clientFlush = clientStrategy.verifyApplied();
    clientStrategy.verifyWriteStarted();
    clientStrategy.verifyItemWritten(5);
    clientStrategy.verifyWriteTerminated();
    clientFlush.flush();
    MockFlushStrategy serverStrategy = service.getLastUsedStrategy();
    FlushSender serverFlush = serverStrategy.verifyApplied();
    serverStrategy.verifyWriteStarted();
    serverStrategy.verifyItemWritten(5);
    serverStrategy.verifyWriteTerminated();
    serverFlush.flush();
    Collection<Object> chunks = clientResp.get();
    assertThat("Unexpected items received.", chunks, hasSize(3));
    // revert to flush on each.
    c.cancel();
    // No more custom strategies.
    Collection<Object> secondReqChunks = conn.request(conn.get("")).flatMapPublisher(StreamingHttpResponse::messageBody).toFuture().get();
    clientStrategy.verifyNoMoreInteractions();
    service.getLastUsedStrategy();
    serverStrategy.verifyNoMoreInteractions();
    assertThat("Unexpected payload for regular flush.", secondReqChunks, empty());
}
Also used : NettyConnectionContext(io.servicetalk.transport.netty.internal.NettyConnectionContext) Cancellable(io.servicetalk.concurrent.Cancellable) FlushSender(io.servicetalk.transport.netty.internal.FlushStrategy.FlushSender) MockFlushStrategy(io.servicetalk.transport.netty.internal.MockFlushStrategy) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Cancellable (io.servicetalk.concurrent.Cancellable)1 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)1 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)1 FlushSender (io.servicetalk.transport.netty.internal.FlushStrategy.FlushSender)1 MockFlushStrategy (io.servicetalk.transport.netty.internal.MockFlushStrategy)1 NettyConnectionContext (io.servicetalk.transport.netty.internal.NettyConnectionContext)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.jupiter.api.Test)1