use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class ErrorHandlingTest method setUp.
private void setUp(TestMode testMode, GrpcExecutionStrategy serverStrategy, GrpcExecutionStrategy clientStrategy) throws Exception {
this.testMode = testMode;
cannedResponse = TestResponse.newBuilder().setMessage("foo").build();
ServiceFactory serviceFactory;
StreamingHttpServiceFilterFactory serviceFilterFactory = IDENTITY_FILTER;
StreamingHttpClientFilterFactory clientFilterFactory = IDENTITY_CLIENT_FILTER;
Publisher<TestRequest> requestPublisher = from(TestRequest.newBuilder().build());
switch(testMode) {
case HttpClientFilterThrows:
clientFilterFactory = new ErrorProducingClientFilter(true, DELIBERATE_EXCEPTION);
serviceFactory = setupForSuccess();
break;
case HttpClientFilterThrowsGrpcException:
clientFilterFactory = new ErrorProducingClientFilter(true, cannedException);
serviceFactory = setupForSuccess();
break;
case HttpClientFilterEmitsError:
clientFilterFactory = new ErrorProducingClientFilter(false, DELIBERATE_EXCEPTION);
serviceFactory = setupForSuccess();
break;
case HttpClientFilterEmitsGrpcException:
clientFilterFactory = new ErrorProducingClientFilter(false, cannedException);
serviceFactory = setupForSuccess();
break;
case HttpFilterThrows:
serviceFilterFactory = new ErrorProducingSvcFilter(true, DELIBERATE_EXCEPTION);
serviceFactory = setupForSuccess();
break;
case HttpFilterThrowsGrpcException:
serviceFilterFactory = new ErrorProducingSvcFilter(true, cannedException);
serviceFactory = setupForSuccess();
break;
case HttpFilterEmitsError:
serviceFilterFactory = new ErrorProducingSvcFilter(false, DELIBERATE_EXCEPTION);
serviceFactory = setupForSuccess();
break;
case HttpFilterEmitsGrpcException:
serviceFilterFactory = new ErrorProducingSvcFilter(false, cannedException);
serviceFactory = setupForSuccess();
break;
case ServiceThrows:
serviceFactory = setupForServiceThrows(DELIBERATE_EXCEPTION);
break;
case ServiceThrowsGrpcException:
serviceFactory = setupForServiceThrows(cannedException);
break;
case ServiceOperatorThrows:
serviceFactory = setupForServiceOperatorThrows(DELIBERATE_EXCEPTION);
break;
case ServiceOperatorThrowsGrpcException:
serviceFactory = setupForServiceOperatorThrows(cannedException);
break;
case ServiceSecondOperatorThrowsGrpcException:
serviceFactory = setupForServiceSecondOperatorThrows(cannedException);
requestPublisher = from(TestRequest.newBuilder().build(), TestRequest.newBuilder().setName(REQ_THROW_NAME).build());
break;
case ServiceEmitsError:
serviceFactory = setupForServiceEmitsError(DELIBERATE_EXCEPTION);
break;
case ServiceEmitsGrpcException:
serviceFactory = setupForServiceEmitsError(cannedException);
break;
case ServiceEmitsDataThenError:
serviceFactory = setupForServiceEmitsDataThenError(DELIBERATE_EXCEPTION);
break;
case ServiceEmitsDataThenGrpcException:
serviceFactory = setupForServiceEmitsDataThenError(cannedException);
break;
case BlockingServiceThrows:
serviceFactory = setupForBlockingServiceThrows(DELIBERATE_EXCEPTION);
break;
case BlockingServiceThrowsGrpcException:
serviceFactory = setupForBlockingServiceThrows(cannedException);
break;
case BlockingServiceWritesThenThrows:
serviceFactory = setupForBlockingServiceWritesThenThrows(DELIBERATE_EXCEPTION);
break;
case BlockingServiceWritesThenThrowsGrpcException:
serviceFactory = setupForBlockingServiceWritesThenThrows(cannedException);
break;
default:
throw new IllegalArgumentException("Unknown mode: " + testMode);
}
this.requestPublisher = requestPublisher;
final StreamingHttpServiceFilterFactory filterFactory = serviceFilterFactory;
serverContext = GrpcServers.forAddress(localAddress(0)).initializeHttp(builder -> builder.appendServiceFilter(filterFactory).executionStrategy(serverStrategy)).listenAndAwait(serviceFactory);
final StreamingHttpClientFilterFactory pickedClientFilterFactory = clientFilterFactory;
GrpcClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = GrpcClients.forAddress(serverHostAndPort(serverContext)).initializeHttp(builder -> builder.appendClientFilter(pickedClientFilterFactory).executionStrategy(clientStrategy));
client = clientBuilder.build(new ClientFactory());
blockingClient = clientBuilder.buildBlocking(new ClientFactory());
}
use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class GracefulConnectionClosureHandlingTest method setUp.
void setUp(HttpProtocol protocol, boolean initiateClosureFromClient, boolean useUds, boolean viaProxy) throws Exception {
this.protocol = protocol;
this.initiateClosureFromClient = initiateClosureFromClient;
if (useUds) {
Assumptions.assumeTrue(SERVER_CTX.ioExecutor().isUnixDomainSocketSupported(), "Server's IoExecutor does not support UnixDomainSocket");
Assumptions.assumeTrue(CLIENT_CTX.ioExecutor().isUnixDomainSocketSupported(), "Client's IoExecutor does not support UnixDomainSocket");
assumeFalse(viaProxy, "UDS cannot be used via proxy");
}
assumeFalse(protocol == HTTP_2 && viaProxy, "Proxy is not supported with HTTP/2");
HttpServerBuilder serverBuilder = (useUds ? forAddress(newSocketAddress()) : forAddress(localAddress(0))).protocols(protocol.config).ioExecutor(SERVER_CTX.ioExecutor()).executor(SERVER_CTX.executor()).executionStrategy(defaultStrategy()).enableWireLogging("servicetalk-tests-wire-logger", TRACE, () -> true).appendConnectionAcceptorFilter(original -> new DelegatingConnectionAcceptor(original) {
@Override
public Completable accept(final ConnectionContext context) {
if (!initiateClosureFromClient) {
((NettyConnectionContext) context).onClosing().whenFinally(onClosing::countDown).subscribe();
}
context.onClose().whenFinally(serverConnectionClosed::countDown).subscribe();
connectionAccepted.countDown();
return completed();
}
});
HostAndPort proxyAddress = null;
if (viaProxy) {
// Dummy proxy helps to emulate old intermediate systems that do not support half-closed TCP connections
proxyTunnel = new ProxyTunnel();
proxyAddress = proxyTunnel.startProxy();
serverBuilder.sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).build());
} else {
proxyTunnel = null;
}
serverContext = serverBuilder.listenBlockingStreamingAndAwait((ctx, request, response) -> {
serverReceivedRequest.countDown();
response.addHeader(CONTENT_LENGTH, valueOf(RESPONSE_CONTENT.length()));
serverSendResponse.await();
try (HttpPayloadWriter<String> writer = response.sendMetaData(RAW_STRING_SERIALIZER)) {
// Subscribe to the request payload body before response writer closes
BlockingIterator<Buffer> iterator = request.payloadBody().iterator();
// Consume request payload body asynchronously:
ctx.executionContext().executor().submit(() -> {
int receivedSize = 0;
while (iterator.hasNext()) {
Buffer chunk = iterator.next();
assert chunk != null;
receivedSize += chunk.readableBytes();
}
serverReceivedRequestPayload.add(receivedSize);
}).beforeOnError(cause -> {
LOGGER.error("failure while reading request", cause);
serverReceivedRequestPayload.add(-1);
}).toFuture();
serverSendResponsePayload.await();
writer.write(RESPONSE_CONTENT);
}
});
serverContext.onClose().whenFinally(serverContextClosed::countDown).subscribe();
client = (viaProxy ? forSingleAddress(serverHostAndPort(serverContext)).proxyAddress(proxyAddress).sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).peerHost(serverPemHostname()).build()) : forResolvedAddress(serverContext.listenAddress())).protocols(protocol.config).executor(CLIENT_CTX.executor()).ioExecutor(CLIENT_CTX.ioExecutor()).executionStrategy(defaultStrategy()).enableWireLogging("servicetalk-tests-wire-logger", TRACE, Boolean.TRUE::booleanValue).appendConnectionFactoryFilter(ConnectionFactoryFilter.withStrategy(cf -> initiateClosureFromClient ? new OnClosingConnectionFactoryFilter<>(cf, onClosing) : cf, ExecutionStrategy.offloadNone())).buildStreaming();
connection = client.reserveConnection(client.get("/")).toFuture().get();
connection.onClose().whenFinally(clientConnectionClosed::countDown).subscribe();
// wait until server accepts connection
connectionAccepted.await();
toClose = initiateClosureFromClient ? connection : serverContext;
}
use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class ExecutionStrategyInContextTest method initClientAndServer.
private SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> initClientAndServer(Function<HttpServerBuilder, Single<HttpServerContext>> serverStarter, boolean customStrategy) throws Exception {
HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0));
if (customStrategy) {
expectedServerStrategy = HttpExecutionStrategies.offloadAll();
serverBuilder.executionStrategy(expectedServerStrategy);
}
context = serverStarter.apply(serverBuilder).toFuture().get();
SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = forSingleAddress(serverHostAndPort(context));
if (customStrategy) {
expectedClientStrategy = HttpExecutionStrategies.offloadAll();
clientBuilder.executionStrategy(expectedClientStrategy);
}
return clientBuilder;
}
use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class AbstractHttpRequestMetaDataTest method assertEffectiveHostAndPort.
private void assertEffectiveHostAndPort(String hostName, int port) {
HostAndPort effectiveHostAndPort = fixture.effectiveHostAndPort();
assertNotNull(effectiveHostAndPort);
assertEquals(hostName, effectiveHostAndPort.hostName());
assertEquals(port, effectiveHostAndPort.port());
}
use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class InvokingThreadsRecorder method init.
void init(BiFunction<IoExecutor, HttpServerBuilder, Single<HttpServerContext>> serverStarter, BiConsumer<IoExecutor, SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress>> clientUpdater) {
try {
HttpServerBuilder serverBuilder = HttpServers.forAddress(localAddress(0));
context = serverStarter.apply(ioExecutor, serverBuilder).toFuture().get();
} catch (Exception e) {
fail("Exception in initialization", e);
}
SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = HttpClients.forSingleAddress(serverHostAndPort(context));
clientUpdater.accept(ioExecutor, clientBuilder);
client = clientBuilder.buildStreaming();
}
Aggregations