use of io.servicetalk.transport.netty.internal.MockFlushStrategy in project servicetalk by apple.
the class NettyHttpServerConnectionTest method updateFlushStrategy.
@ParameterizedTest(name = "server={0} client={1}")
@MethodSource("executionStrategies")
void updateFlushStrategy(HttpExecutionStrategy serverExecutionStrategy, HttpExecutionStrategy clientExecutionStrategy) throws Exception {
customStrategy = new MockFlushStrategy();
AtomicReference<Cancellable> customCancellableRef = new AtomicReference<>();
AtomicBoolean handledFirstRequest = new AtomicBoolean();
serverContext = HttpServers.forAddress(localAddress(0)).ioExecutor(contextRule.ioExecutor()).appendConnectionAcceptorFilter(original -> original.append(ctx -> {
customCancellableRef.set(((NettyConnectionContext) ctx).updateFlushStrategy((__, ___) -> customStrategy));
return completed();
})).executionStrategy(serverExecutionStrategy).listenStreaming((ctx, request, responseFactory) -> {
if (handledFirstRequest.compareAndSet(false, true)) {
customStrategy.afterFirstWrite(FlushStrategy.FlushSender::flush);
return succeeded(responseFactory.ok().payloadBody(responsePublisher));
}
return succeeded(responseFactory.ok().payloadBody(responsePublisher2));
}).toFuture().get();
client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).executionStrategy(clientExecutionStrategy).buildStreaming();
StreamingHttpResponse response = client.request(client.newRequest(GET, "/1")).toFuture().get();
FlushStrategy.FlushSender customFlushSender = customStrategy.verifyApplied();
Cancellable customCancellable = customCancellableRef.get();
assertNotNull(customCancellable);
// Verify that the custom strategy is applied and used for flushing.
customStrategy.verifyWriteStarted();
customStrategy.verifyItemWritten(1);
customStrategy.verifyNoMoreInteractions();
String payloadBodyString = "foo";
TestSubscription testSubscription1 = new TestSubscription();
responsePublisher.onSubscribe(testSubscription1);
testSubscription1.awaitRequestN(1);
responsePublisher.onNext(DEFAULT_ALLOCATOR.fromAscii(payloadBodyString));
responsePublisher.onComplete();
customFlushSender.flush();
Buffer responsePayload = response.payloadBody().collect(DEFAULT_ALLOCATOR::newBuffer, (results, current) -> {
results.writeBytes(current);
return results;
}).toFuture().get();
assertEquals(payloadBodyString, responsePayload.toString(US_ASCII));
customStrategy.verifyItemWritten(2);
customStrategy.verifyWriteTerminated();
// Restore the default flush strategy, which should flush on each
customCancellable.cancel();
StreamingHttpResponse response2 = client.request(client.newRequest(GET, "/2")).toFuture().get();
TestSubscription testSubscription2 = new TestSubscription();
responsePublisher2.onSubscribe(testSubscription2);
responsePublisher2.onNext(DEFAULT_ALLOCATOR.fromAscii(payloadBodyString));
responsePublisher2.onComplete();
responsePayload = response2.payloadBody().collect(DEFAULT_ALLOCATOR::newBuffer, (results, current) -> {
results.writeBytes(current);
return results;
}).toFuture().get();
assertEquals(payloadBodyString, responsePayload.toString(US_ASCII));
}
use of io.servicetalk.transport.netty.internal.MockFlushStrategy 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());
}
Aggregations