use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class DefaultMultiAddressUrlHttpClientBuilderTest method buildWithProvidedServiceDiscoverer.
@Test
@SuppressWarnings("unchecked")
void buildWithProvidedServiceDiscoverer() throws Exception {
ServiceDiscoverer<HostAndPort, InetSocketAddress, ServiceDiscovererEvent<InetSocketAddress>> mockedServiceDiscoverer = mock(ServiceDiscoverer.class);
StreamingHttpRequester newRequester = HttpClients.forMultiAddressUrl(mockedServiceDiscoverer).ioExecutor(CTX.ioExecutor()).buildStreaming();
newRequester.closeAsync().toFuture().get();
verify(mockedServiceDiscoverer, never()).closeAsync();
}
use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class ClientEffectiveStrategyTest method clientStrategy.
@ParameterizedTest(name = "API={0} builder={1} filter={2} LB={3} CF={4}")
@MethodSource("casesSupplier")
void clientStrategy(ClientType clientType, @Nullable final HttpExecutionStrategy builderStrategy, @Nullable final HttpExecutionStrategy filterStrategy, @Nullable final HttpExecutionStrategy lbStrategy, @Nullable final HttpExecutionStrategy cfStrategy) throws Exception {
HttpExecutionStrategy effectiveStrategy = computeClientExecutionStrategy(builderStrategy, filterStrategy, lbStrategy, cfStrategy);
SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = HttpClients.forSingleAddress(serverHostAndPort(context));
if (builderStrategy != null) {
clientBuilder.executionStrategy(builderStrategy);
}
ClientInvokingThreadRecorder invokingThreadsRecorder = new ClientInvokingThreadRecorder(clientType, effectiveStrategy);
clientBuilder.appendClientFilter(invokingThreadsRecorder);
if (null != filterStrategy) {
clientBuilder.appendClientFilter(new StreamingHttpClientFilterFactory() {
@Override
public StreamingHttpClientFilter create(final FilterableStreamingHttpClient client) {
return new StreamingHttpClientFilter(client) {
};
}
@Override
public HttpExecutionStrategy requiredOffloads() {
return filterStrategy;
}
});
}
if (null != lbStrategy) {
HttpLoadBalancerFactory<InetSocketAddress> lfFactory = DefaultHttpLoadBalancerFactory.Builder.from(new LoadBalancerFactoryImpl() {
@Override
public ExecutionStrategy requiredOffloads() {
return lbStrategy;
}
}).build();
clientBuilder.loadBalancerFactory(lfFactory);
}
if (null != cfStrategy) {
clientBuilder.appendConnectionFilter(new StreamingHttpConnectionFilterFactory() {
@Override
public StreamingHttpConnectionFilter create(final FilterableStreamingHttpConnection connection) {
return new StreamingHttpConnectionFilter(connection) {
};
}
@Override
public HttpExecutionStrategy requiredOffloads() {
return cfStrategy;
}
});
}
// Exercise the client
try (StreamingHttpClient client = clientBuilder.buildStreaming()) {
String responseBody = getResponse(clientType, client);
assertThat(responseBody, is(GREETING));
invokingThreadsRecorder.verifyOffloads();
}
}
use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class AbstractTcpServerTest method getTcpClientConfig.
// Visible for overriding.
TcpClientConfig getTcpClientConfig() {
TcpClientConfig tcpClientConfig = new TcpClientConfig();
if (sslEnabled) {
HostAndPort serverHostAndPort = serverHostAndPort(serverContext);
tcpClientConfig.sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).peerHost(serverPemHostname()).peerPort(serverHostAndPort.port()).build());
}
tcpClientConfig.enableWireLogging("servicetalk-tests-wire-logger", TRACE, () -> true);
return tcpClientConfig;
}
use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class AbstractHttpServiceAsyncContextTest method newRequestsGetFreshContext.
final void newRequestsGetFreshContext(boolean useImmediate) throws Exception {
final ExecutorService executorService = Executors.newCachedThreadPool();
final int concurrency = 10;
final int numRequests = 10;
final String k1Value = "value";
// The service should get an empty AsyncContext regardless of what is done outside the service.
// There are utilities that may be accessed in a static context or before service initialization that
// shouldn't pollute the service's AsyncContext.
AsyncContext.put(K1, k1Value);
try (ServerContext ctx = serverWithEmptyAsyncContextService(HttpServers.forAddress(localAddress(0)), useImmediate)) {
AtomicReference<Throwable> causeRef = new AtomicReference<>();
CyclicBarrier barrier = new CyclicBarrier(concurrency);
CountDownLatch latch = new CountDownLatch(concurrency);
for (int i = 0; i < concurrency; ++i) {
final int finalI = i;
executorService.execute(() -> {
SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = forResolvedAddress(serverHostAndPort(ctx)).protocols(h1().maxPipelinedRequests(numRequests).build());
try (StreamingHttpClient client = (!useImmediate ? clientBuilder : clientBuilder.executionStrategy(offloadNone())).buildStreaming()) {
try (StreamingHttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get()) {
barrier.await();
for (int x = 0; x < numRequests; ++x) {
makeClientRequestWithId(connection, "thread=" + finalI + " request=" + x);
}
}
} catch (Throwable cause) {
causeRef.compareAndSet(null, cause);
} finally {
latch.countDown();
}
});
}
latch.await();
assertNull(causeRef.get());
assertEquals(k1Value, AsyncContext.get(K1));
} finally {
executorService.shutdown();
}
}
use of io.servicetalk.transport.api.HostAndPort in project servicetalk by apple.
the class AbstractNettyHttpServerTest method startServer.
private void startServer() throws Exception {
final InetSocketAddress bindAddress = localAddress(0);
service(new TestServiceStreaming(publisherSupplier));
// A small SNDBUF is needed to test that the server defers closing the connection until writes are complete.
// However, if it is too small, tests that expect certain chunks of data will see those chunks broken up
// differently.
final HttpServerBuilder serverBuilder = HttpServers.forAddress(bindAddress).executor(serverExecutor).socketOption(StandardSocketOptions.SO_SNDBUF, 100).protocols(protocol).transportObserver(serverTransportObserver).enableWireLogging("servicetalk-tests-wire-logger", TRACE, () -> true);
configureServerBuilder(serverBuilder);
if (sslEnabled) {
serverBuilder.sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).build());
}
if (nonOffloadingServiceFilterFactory != null) {
serverBuilder.appendNonOffloadingServiceFilter(nonOffloadingServiceFilterFactory);
}
if (serviceFilterFactory != null) {
serverBuilder.appendServiceFilter(serviceFilterFactory);
}
if (serverLifecycleObserver != NoopHttpLifecycleObserver.INSTANCE) {
serverBuilder.lifecycleObserver(serverLifecycleObserver);
}
serverContext = awaitIndefinitelyNonNull(listen(serverBuilder.ioExecutor(serverIoExecutor).appendConnectionAcceptorFilter(original -> new DelegatingConnectionAcceptor(connectionAcceptor))).beforeOnSuccess(ctx -> LOGGER.debug("Server started on {}.", ctx.listenAddress())).beforeOnError(throwable -> LOGGER.debug("Failed starting server on {}.", bindAddress)));
final SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = newClientBuilder();
if (sslEnabled) {
clientBuilder.sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).peerHost(serverPemHostname()).build());
}
if (connectionFactoryFilter != null) {
clientBuilder.appendConnectionFactoryFilter(connectionFactoryFilter);
}
if (connectionFilterFactory != null) {
clientBuilder.appendConnectionFilter(connectionFilterFactory);
}
if (clientTransportObserver != NoopTransportObserver.INSTANCE) {
clientBuilder.appendConnectionFactoryFilter(new TransportObserverConnectionFactoryFilter<>(clientTransportObserver));
}
if (clientLifecycleObserver != NoopHttpLifecycleObserver.INSTANCE) {
clientBuilder.appendClientFilter(new HttpLifecycleObserverRequesterFilter(clientLifecycleObserver));
}
if (clientFilterFactory != null) {
clientBuilder.appendClientFilter(clientFilterFactory);
}
httpClient = clientBuilder.ioExecutor(clientIoExecutor).executor(clientExecutor).executionStrategy(defaultStrategy()).protocols(protocol).enableWireLogging("servicetalk-tests-wire-logger", TRACE, Boolean.TRUE::booleanValue).buildStreaming();
httpConnection = httpClient.reserveConnection(httpClient.get("/")).toFuture().get();
}
Aggregations