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();
}
}
Aggregations