Search in sources :

Example 6 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class AbstractHttpServiceAsyncContextTest method connectionAcceptorContextDoesNotLeak.

final void connectionAcceptorContextDoesNotLeak(boolean serverUseImmediate) throws Exception {
    try (ServerContext ctx = serverWithEmptyAsyncContextService(HttpServers.forAddress(localAddress(0)).appendConnectionAcceptorFilter(original -> new DelegatingConnectionAcceptor(context -> {
        AsyncContext.put(K1, "v1");
        return completed();
    })), serverUseImmediate);
        StreamingHttpClient client = HttpClients.forResolvedAddress(serverHostAndPort(ctx)).buildStreaming();
        StreamingHttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get()) {
        makeClientRequestWithId(connection, "1");
        makeClientRequestWithId(connection, "2");
    }
}
Also used : StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) StreamingHttpServiceFilterFactory(io.servicetalk.http.api.StreamingHttpServiceFilterFactory) CharSequences.newAsciiString(io.servicetalk.buffer.api.CharSequences.newAsciiString) Thread.currentThread(java.lang.Thread.currentThread) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Key.newKey(io.servicetalk.context.api.ContextMap.Key.newKey) StreamingHttpServiceFilter(io.servicetalk.http.api.StreamingHttpServiceFilter) HttpServiceContext(io.servicetalk.http.api.HttpServiceContext) AsyncContext(io.servicetalk.concurrent.api.AsyncContext) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) HttpExecutionStrategies.offloadNone(io.servicetalk.http.api.HttpExecutionStrategies.offloadNone) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) HttpServerBuilder(io.servicetalk.http.api.HttpServerBuilder) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) CyclicBarrier(java.util.concurrent.CyclicBarrier) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) Matchers.empty(org.hamcrest.Matchers.empty) ServerContext(io.servicetalk.transport.api.ServerContext) HttpProtocolConfigs.h1(io.servicetalk.http.netty.HttpProtocolConfigs.h1) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection) Single.defer(io.servicetalk.concurrent.api.Single.defer) PublisherSource(io.servicetalk.concurrent.PublisherSource) Single(io.servicetalk.concurrent.api.Single) OK(io.servicetalk.http.api.HttpResponseStatus.OK) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) HttpClients.forResolvedAddress(io.servicetalk.http.netty.HttpClients.forResolvedAddress) SingleAddressHttpClientBuilder(io.servicetalk.http.api.SingleAddressHttpClientBuilder) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) CountDownLatch(java.util.concurrent.CountDownLatch) ContextMap(io.servicetalk.context.api.ContextMap) DelegatingConnectionAcceptor(io.servicetalk.transport.api.DelegatingConnectionAcceptor) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) StreamingHttpResponseFactory(io.servicetalk.http.api.StreamingHttpResponseFactory) Queue(java.util.Queue) HostAndPort(io.servicetalk.transport.api.HostAndPort) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) ServerContext(io.servicetalk.transport.api.ServerContext) DelegatingConnectionAcceptor(io.servicetalk.transport.api.DelegatingConnectionAcceptor) StreamingHttpConnection(io.servicetalk.http.api.StreamingHttpConnection)

Example 7 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class DefaultHealthServiceTest method clearWatch.

@Test
void clearWatch() throws Exception {
    DefaultHealthService service = new DefaultHealthService();
    try (ServerContext serverCtx = GrpcServers.forAddress(localAddress(0)).listenAndAwait(service)) {
        try (Health.BlockingHealthClient client = GrpcClients.forResolvedAddress((InetSocketAddress) serverCtx.listenAddress()).buildBlocking(new Health.ClientFactory())) {
            assertThat(service.clearStatus(OVERALL_SERVICE_NAME), equalTo(true));
            BlockingIterator<HealthCheckResponse> itr = client.watch(newRequest(OVERALL_SERVICE_NAME)).iterator();
            assertThat(itr.next().getStatus(), equalTo(SERVICE_UNKNOWN));
            assertThat(service.setStatus(OVERALL_SERVICE_NAME, SERVING), equalTo(true));
            assertThat(itr.next().getStatus(), equalTo(SERVING));
        }
    }
}
Also used : ServerContext(io.servicetalk.transport.api.ServerContext) HealthCheckResponse(io.servicetalk.health.v1.HealthCheckResponse) Health(io.servicetalk.health.v1.Health) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test)

Example 8 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class DefaultHealthServiceTest method defaultCheck.

@Test
void defaultCheck() throws Exception {
    DefaultHealthService service = new DefaultHealthService();
    try (ServerContext serverCtx = GrpcServers.forAddress(localAddress(0)).listenAndAwait(service)) {
        try (Health.BlockingHealthClient client = GrpcClients.forResolvedAddress((InetSocketAddress) serverCtx.listenAddress()).buildBlocking(new Health.ClientFactory())) {
            assertThat(client.check(newRequest(OVERALL_SERVICE_NAME)).getStatus(), equalTo(SERVING));
            assertThat(service.setStatus(OVERALL_SERVICE_NAME, NOT_SERVING), equalTo(true));
            assertThat(client.check(newRequest(OVERALL_SERVICE_NAME)).getStatus(), equalTo(NOT_SERVING));
        }
    }
}
Also used : ServerContext(io.servicetalk.transport.api.ServerContext) Health(io.servicetalk.health.v1.Health) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test)

Example 9 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class BackendStarter method start.

ServerContext start(int listenPort, String name, StreamingHttpService service) throws Exception {
    // Starting the server will start listening for incoming client requests.
    final ServerContext ctx = HttpServers.forPort(listenPort).ioExecutor(ioExecutor).appendServiceFilter(new ErrorResponseGeneratingServiceFilter(name)).listenStreamingAndAwait(service);
    LOGGER.info("Started {} listening on {}.", name, ctx.listenAddress());
    return ctx;
}
Also used : ServerContext(io.servicetalk.transport.api.ServerContext)

Example 10 with ServerContext

use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.

the class BackendsStarter method main.

public static void main(String[] args) throws Exception {
    // Create an AutoCloseable representing all resources used in this example.
    try (CompositeCloseable resources = newCompositeCloseable()) {
        // Shared IoExecutor for the application.
        IoExecutor ioExecutor = resources.prepend(createIoExecutor());
        // This is a single Completable used to await closing of all backends started by this class. It is used to
        // provide a way to not let main() exit.
        Completable allServicesOnClose = completed();
        BackendStarter starter = new BackendStarter(ioExecutor, resources);
        final ServerContext recommendationService = starter.start(RECOMMENDATIONS_BACKEND_ADDRESS.port(), RECOMMENDATION_SERVICE_NAME, newRecommendationsService());
        allServicesOnClose = allServicesOnClose.merge(recommendationService.onClose());
        final ServerContext metadataService = starter.start(METADATA_BACKEND_ADDRESS.port(), METADATA_SERVICE_NAME, newMetadataService());
        allServicesOnClose = allServicesOnClose.merge(metadataService.onClose());
        final ServerContext userService = starter.start(USER_BACKEND_ADDRESS.port(), USER_SERVICE_NAME, newUserService());
        allServicesOnClose = allServicesOnClose.merge(userService.onClose());
        final ServerContext ratingService = starter.start(RATINGS_BACKEND_ADDRESS.port(), RATING_SERVICE_NAME, newRatingService());
        allServicesOnClose = allServicesOnClose.merge(ratingService.onClose());
        // Await termination of all backends started by this class.
        allServicesOnClose.toFuture().get();
    }
}
Also used : Completable(io.servicetalk.concurrent.api.Completable) IoExecutor(io.servicetalk.transport.api.IoExecutor) NettyIoExecutors.createIoExecutor(io.servicetalk.transport.netty.NettyIoExecutors.createIoExecutor) ServerContext(io.servicetalk.transport.api.ServerContext) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)

Aggregations

ServerContext (io.servicetalk.transport.api.ServerContext)106 Test (org.junit.jupiter.api.Test)57 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)42 HttpResponse (io.servicetalk.http.api.HttpResponse)39 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)34 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)34 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)33 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)28 InetSocketAddress (java.net.InetSocketAddress)27 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)26 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)24 MethodSource (org.junit.jupiter.params.provider.MethodSource)22 HostAndPort (io.servicetalk.transport.api.HostAndPort)20 Single (io.servicetalk.concurrent.api.Single)19 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)19 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)16 ClientSslConfigBuilder (io.servicetalk.transport.api.ClientSslConfigBuilder)15 OK (io.servicetalk.http.api.HttpResponseStatus.OK)14 DefaultTestCerts (io.servicetalk.test.resources.DefaultTestCerts)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14