use of io.servicetalk.grpc.api.GrpcClientMetadata in project servicetalk by apple.
the class ProtocolCompatibilityTest method grpcJavaClient.
// Wrap grpc client in our client interface to simplify test code
private static CompatClient grpcJavaClient(final SocketAddress address, @Nullable final String compression, final boolean ssl, @Nullable Duration timeout) throws Exception {
final NettyChannelBuilder builder = NettyChannelBuilder.forAddress(address);
if (ssl) {
final SslContext context = GrpcSslContexts.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
builder.sslContext(context);
} else {
builder.usePlaintext();
}
final ManagedChannel channel = builder.build();
// stub is immutable and each builder step returns a new instance.
CompatGrpc.CompatStub stub = CompatGrpc.newStub(channel);
if (compression != null) {
stub = stub.withCompression(compression);
}
if (null != timeout) {
stub = stub.withDeadlineAfter(timeout.toNanos(), NANOSECONDS);
}
final CompatGrpc.CompatStub finalStub = stub;
return new CompatClient() {
@Override
public GrpcExecutionContext executionContext() {
throw new UnsupportedOperationException();
}
@Override
public Publisher<CompatResponse> bidirectionalStreamingCall(final Publisher<CompatRequest> request) {
final PublisherSource.Processor<CompatResponse, CompatResponse> processor = newPublisherProcessor(3);
sendRequest(request, finalStub.bidirectionalStreamingCall(adaptResponse(processor)));
return fromSource(processor);
}
@Override
public Publisher<CompatResponse> bidirectionalStreamingCall(final GrpcClientMetadata metadata, final Publisher<CompatRequest> request) {
return bidirectionalStreamingCall(request);
}
@Deprecated
@Override
public Publisher<CompatResponse> bidirectionalStreamingCall(final BidirectionalStreamingCallMetadata metadata, final Publisher<CompatRequest> request) {
return bidirectionalStreamingCall(request);
}
@SuppressWarnings("unchecked")
@Override
public Single<CompatResponse> clientStreamingCall(final Publisher<CompatRequest> request) {
final Processor<CompatResponse, CompatResponse> processor = newSingleProcessor();
final StreamObserver<CompatRequest> requestObserver = finalStub.clientStreamingCall(adaptResponse(processor));
sendRequest(request, requestObserver);
return (Single<CompatResponse>) processor;
}
@Deprecated
@Override
public Single<CompatResponse> clientStreamingCall(final ClientStreamingCallMetadata metadata, final Publisher<CompatRequest> request) {
return clientStreamingCall(request);
}
@Override
public Single<CompatResponse> clientStreamingCall(final GrpcClientMetadata metadata, final Publisher<CompatRequest> request) {
return clientStreamingCall(request);
}
@SuppressWarnings("unchecked")
@Override
public Single<CompatResponse> scalarCall(final CompatRequest request) {
final Processor<CompatResponse, CompatResponse> processor = newSingleProcessor();
finalStub.scalarCall(request, adaptResponse(processor));
return (Single<CompatResponse>) processor;
}
@Deprecated
@Override
public Single<CompatResponse> scalarCall(final ScalarCallMetadata metadata, final CompatRequest request) {
return scalarCall(request);
}
@Override
public Single<CompatResponse> scalarCall(final GrpcClientMetadata metadata, final CompatRequest request) {
return scalarCall(request);
}
@Override
public Publisher<CompatResponse> serverStreamingCall(final CompatRequest request) {
final PublisherSource.Processor<CompatResponse, CompatResponse> processor = newPublisherProcessor(3);
finalStub.serverStreamingCall(request, adaptResponse(processor));
return fromSource(processor);
}
@Deprecated
@Override
public Publisher<CompatResponse> serverStreamingCall(final ServerStreamingCallMetadata metadata, final CompatRequest request) {
return serverStreamingCall(request);
}
@Override
public Publisher<CompatResponse> serverStreamingCall(final GrpcClientMetadata metadata, final CompatRequest request) {
return serverStreamingCall(request);
}
@Override
public void close() throws Exception {
channel.shutdown().awaitTermination(DEFAULT_TIMEOUT_SECONDS, SECONDS);
}
@Override
public Completable closeAsync() {
throw new UnsupportedOperationException();
}
@Override
public Completable onClose() {
throw new UnsupportedOperationException();
}
@Override
public BlockingCompatClient asBlockingClient() {
throw new UnsupportedOperationException();
}
private void sendRequest(final Publisher<CompatRequest> request, final StreamObserver<CompatRequest> requestObserver) {
request.whenOnComplete(requestObserver::onCompleted).whenOnError(requestObserver::onError).forEach(requestObserver::onNext);
}
private StreamObserver<CompatResponse> adaptResponse(final Processor<CompatResponse, CompatResponse> processor) {
return new StreamObserver<CompatResponse>() {
@Override
public void onNext(final CompatResponse value) {
processor.onSuccess(value);
}
@Override
public void onError(final Throwable t) {
processor.onError(t);
}
@Override
public void onCompleted() {
// ignored
}
};
}
private StreamObserver<CompatResponse> adaptResponse(final PublisherSource.Processor<CompatResponse, CompatResponse> processor) {
return new StreamObserver<CompatResponse>() {
@Override
public void onNext(final CompatResponse value) {
processor.onNext(value);
}
@Override
public void onError(final Throwable t) {
processor.onError(t);
}
@Override
public void onCompleted() {
processor.onComplete();
}
};
}
};
}
use of io.servicetalk.grpc.api.GrpcClientMetadata in project servicetalk by apple.
the class ProtocolCompatibilityTest method testGrpcErrorStreaming.
private static void testGrpcErrorStreaming(final CompatClient client, final TestServerContext server, final boolean withStatus, @Nullable final String compression, final GrpcStatusCode expectCode, @Nullable final String expectMessage) throws Exception {
try {
BufferEncoder encoder = serviceTalkCompression(compression);
GrpcClientMetadata metadata = encoder == null ? DefaultGrpcClientMetadata.INSTANCE : new DefaultGrpcClientMetadata(encoder);
final Publisher<CompatResponse> streamingResponse = client.bidirectionalStreamingCall(metadata, Publisher.from(CompatRequest.newBuilder().setId(3).build(), CompatRequest.newBuilder().setId(4).build(), CompatRequest.newBuilder().setId(5).build()));
validateGrpcErrorInResponse(streamingResponse.toFuture(), withStatus, expectCode, expectMessage);
} finally {
closeAll(client, server);
}
}
use of io.servicetalk.grpc.api.GrpcClientMetadata in project servicetalk by apple.
the class ProtocolCompatibilityTest method testBlockingRequestResponse.
private static void testBlockingRequestResponse(final BlockingCompatClient client, final TestServerContext server, final boolean streaming, @Nullable final String compression) throws Exception {
try {
final BufferEncoder compressor = serviceTalkCompression(compression);
final GrpcClientMetadata metadata = compressor == null ? DefaultGrpcClientMetadata.INSTANCE : new DefaultGrpcClientMetadata(compressor);
if (!streaming) {
final CompatResponse response1 = client.scalarCall(metadata, CompatRequest.newBuilder().setId(1).build());
assertEquals(1000001, response1.getSize());
} else {
// clientStreamingCall returns the "sum"
final CompatResponse response2 = client.clientStreamingCall(metadata, asList(CompatRequest.newBuilder().setId(1).build(), CompatRequest.newBuilder().setId(2).build(), CompatRequest.newBuilder().setId(3).build()));
assertEquals(1000006, response2.getSize());
// serverStreamingCall returns a stream from 0 to N-1
final BlockingIterable<CompatResponse> response3 = client.serverStreamingCall(metadata, CompatRequest.newBuilder().setId(3).build());
final List<CompatResponse> response3List = new ArrayList<>();
response3.forEach(response3List::add);
assertEquals(3, response3List.size());
assertEquals(1000000, response3List.get(0).getSize());
assertEquals(1000001, response3List.get(1).getSize());
assertEquals(1000002, response3List.get(2).getSize());
// bidirectionalStreamingCall basically echos also
final BlockingIterable<CompatResponse> response4 = client.bidirectionalStreamingCall(metadata, asList(CompatRequest.newBuilder().setId(3).build(), CompatRequest.newBuilder().setId(4).build(), CompatRequest.newBuilder().setId(5).build()));
final List<CompatResponse> response4List = new ArrayList<>();
response4.forEach(response4List::add);
assertEquals(3, response4List.size());
assertEquals(1000003, response4List.get(0).getSize());
assertEquals(1000004, response4List.get(1).getSize());
assertEquals(1000005, response4List.get(2).getSize());
}
} finally {
closeAll(client, server);
}
}
use of io.servicetalk.grpc.api.GrpcClientMetadata in project servicetalk by apple.
the class ProtocolCompatibilityTest method testRequestResponse.
private static void testRequestResponse(final CompatClient client, final TestServerContext server, final boolean streaming, @Nullable final String compression) throws Exception {
try {
final BufferEncoder compressor = serviceTalkCompression(compression);
final GrpcClientMetadata metadata = compressor == null ? DefaultGrpcClientMetadata.INSTANCE : new DefaultGrpcClientMetadata(compressor);
if (!streaming) {
// scalarCall basically echos
final Single<CompatResponse> response1 = client.scalarCall(metadata, CompatRequest.newBuilder().setId(1).build());
assertEquals(1000001, response1.toFuture().get().getSize());
} else {
// clientStreamingCall returns the "sum"
final Single<CompatResponse> response2 = client.clientStreamingCall(metadata, Publisher.from(CompatRequest.newBuilder().setId(1).build(), CompatRequest.newBuilder().setId(2).build(), CompatRequest.newBuilder().setId(3).build()));
CompatResponse r = response2.toFuture().get();
assertEquals(1000006, r.getSize());
// serverStreamingCall returns a stream from 0 to N-1
final Publisher<CompatResponse> response3 = client.serverStreamingCall(metadata, CompatRequest.newBuilder().setId(3).build());
final List<CompatResponse> response3List = new ArrayList<>(response3.toFuture().get());
assertEquals(3, response3List.size());
assertEquals(1000000, response3List.get(0).getSize());
assertEquals(1000001, response3List.get(1).getSize());
assertEquals(1000002, response3List.get(2).getSize());
// bidirectionalStreamingCall basically echos also
final Publisher<CompatResponse> response4 = client.bidirectionalStreamingCall(metadata, Publisher.from(CompatRequest.newBuilder().setId(3).build(), CompatRequest.newBuilder().setId(4).build(), CompatRequest.newBuilder().setId(5).build()));
final List<CompatResponse> response4List = new ArrayList<>(response4.toFuture().get());
assertEquals(3, response4List.size());
assertEquals(1000003, response4List.get(0).getSize());
assertEquals(1000004, response4List.get(1).getSize());
assertEquals(1000005, response4List.get(2).getSize());
}
} finally {
closeAll(client, server);
}
}
use of io.servicetalk.grpc.api.GrpcClientMetadata in project servicetalk by apple.
the class ProtocolCompatibilityTest method testGrpcErrorScalar.
private static void testGrpcErrorScalar(final CompatClient client, final TestServerContext server, final boolean withStatus, @Nullable final String compression, final GrpcStatusCode expectCode, @Nullable final String expectMessage) throws Exception {
try {
BufferEncoder encoder = serviceTalkCompression(compression);
GrpcClientMetadata metadata = encoder == null ? DefaultGrpcClientMetadata.INSTANCE : new DefaultGrpcClientMetadata(encoder);
final Single<CompatResponse> scalarResponse = client.scalarCall(metadata, CompatRequest.newBuilder().setId(1).build());
validateGrpcErrorInResponse(scalarResponse.toFuture(), withStatus, expectCode, expectMessage);
} finally {
closeAll(client, server);
}
}
Aggregations