Search in sources :

Example 1 with GrpcClientCallFactory

use of io.servicetalk.grpc.api.GrpcClientCallFactory in project servicetalk by apple.

the class CustomTransportTest method testCustomTransport.

@ParameterizedTest
@EnumSource(ServiceType.class)
void testCustomTransport(final ServiceType serviceType) throws Exception {
    // You can re-use the EventLoopGroup used by your Netty application, we create one to demonstrate its use.
    EventLoopAwareNettyIoExecutor ioExecutor = createIoExecutor("netty-el");
    // This is the Netty channel which is reading the request. See getServiceContext(Channel), depending
    // upon what control you want to give users knowing this may not be necessary.
    Channel c = new EmbeddedChannel();
    try {
        ServerTransport serverTransport = new InMemoryServerTransport(DEFAULT_ALLOCATOR, serviceType.grpcService);
        // Build the client with the custom transport and bridge to server's transport.
        Tester.TesterClient client = new Tester.ClientFactory() {

            @Override
            public TesterClient newClient(final GrpcClientCallFactory factory) {
                return super.newClient(factory);
            }
        }.newClient(new ClientTransportGrpcCallFactory(// Build the client transport, which just calls the server transport directly.
        (method, requestMessages) -> serverTransport.handle(c, "clientId", method, requestMessages), ioExecutor.eventLoopGroup()));
        // Test using the client.
        assertThat(client.test(newReq("scalar")).toFuture().get(), is(newResp("hello scalar")));
        assertThat(client.testRequestStream(newReqStream("req")).toFuture().get(), is(newResp("hello reqstream1, reqstream2, ")));
        assertThat(client.testResponseStream(newReq("respStream")).toFuture().get(), contains(newResp("hello respStream1"), newResp("hello respStream2")));
        assertThat(client.testBiDiStream(newReqStream("duplex")).toFuture().get(), contains(newResp("hello duplexstream1"), newResp("hello duplexstream2")));
    } finally {
        c.close();
        ioExecutor.closeAsync().toFuture().get();
    }
}
Also used : EventLoopAwareNettyIoExecutor(io.servicetalk.transport.netty.internal.EventLoopAwareNettyIoExecutor) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) Publisher(io.servicetalk.concurrent.api.Publisher) NettyIoExecutors.createIoExecutor(io.servicetalk.transport.netty.internal.NettyIoExecutors.createIoExecutor) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) EnumSource(org.junit.jupiter.params.provider.EnumSource) TestRequest(io.servicetalk.grpc.netty.TesterProto.TestRequest) Utils.newResp(io.servicetalk.grpc.customtransport.Utils.newResp) Channel(io.netty.channel.Channel) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) GrpcBindableService(io.servicetalk.grpc.api.GrpcBindableService) Matchers.contains(org.hamcrest.Matchers.contains) GrpcClientCallFactory(io.servicetalk.grpc.api.GrpcClientCallFactory) TesterClient(io.servicetalk.grpc.netty.TesterProto.Tester.TesterClient) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) Tester(io.servicetalk.grpc.netty.TesterProto.Tester) Tester(io.servicetalk.grpc.netty.TesterProto.Tester) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) EventLoopAwareNettyIoExecutor(io.servicetalk.transport.netty.internal.EventLoopAwareNettyIoExecutor) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) TesterClient(io.servicetalk.grpc.netty.TesterProto.Tester.TesterClient) GrpcClientCallFactory(io.servicetalk.grpc.api.GrpcClientCallFactory) TesterClient(io.servicetalk.grpc.netty.TesterProto.Tester.TesterClient) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Channel (io.netty.channel.Channel)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 DEFAULT_ALLOCATOR (io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR)1 Publisher (io.servicetalk.concurrent.api.Publisher)1 Publisher.from (io.servicetalk.concurrent.api.Publisher.from)1 GrpcBindableService (io.servicetalk.grpc.api.GrpcBindableService)1 GrpcClientCallFactory (io.servicetalk.grpc.api.GrpcClientCallFactory)1 Utils.newResp (io.servicetalk.grpc.customtransport.Utils.newResp)1 TestRequest (io.servicetalk.grpc.netty.TesterProto.TestRequest)1 Tester (io.servicetalk.grpc.netty.TesterProto.Tester)1 TesterClient (io.servicetalk.grpc.netty.TesterProto.Tester.TesterClient)1 EventLoopAwareNettyIoExecutor (io.servicetalk.transport.netty.internal.EventLoopAwareNettyIoExecutor)1 NettyIoExecutors.createIoExecutor (io.servicetalk.transport.netty.internal.NettyIoExecutors.createIoExecutor)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.contains (org.hamcrest.Matchers.contains)1 Matchers.is (org.hamcrest.Matchers.is)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 EnumSource (org.junit.jupiter.params.provider.EnumSource)1