use of io.servicetalk.grpc.api.GrpcStatusCode.CANCELLED in project servicetalk by apple.
the class ProtocolCompatibilityTest method serviceTalkServer.
private static TestServerContext serviceTalkServer(final ErrorMode errorMode, final boolean ssl, final GrpcExecutionStrategy strategy, @Nullable final String compression, @Nullable final Duration timeout, Queue<Throwable> reqStreamError) throws Exception {
final Compat.CompatService compatService = new Compat.CompatService() {
@Override
public Publisher<CompatResponse> bidirectionalStreamingCall(final GrpcServiceContext ctx, final Publisher<CompatRequest> pub) {
reqStreamError.add(SERVER_PROCESSED_TOKEN);
maybeThrowFromRpc(errorMode);
return pub.map(req -> response(req.getId())).beforeFinally(errorConsumer());
}
@Override
public Single<CompatResponse> clientStreamingCall(final GrpcServiceContext ctx, final Publisher<CompatRequest> pub) {
reqStreamError.add(SERVER_PROCESSED_TOKEN);
maybeThrowFromRpc(errorMode);
return pub.collect(() -> 0, (sum, req) -> sum + req.getId()).map(this::response).beforeFinally(errorConsumer());
}
@Override
public Single<CompatResponse> scalarCall(final GrpcServiceContext ctx, final CompatRequest req) {
maybeThrowFromRpc(errorMode);
return succeeded(response(req.getId()));
}
@Override
public Publisher<CompatResponse> serverStreamingCall(final GrpcServiceContext ctx, final CompatRequest req) {
maybeThrowFromRpc(errorMode);
return Publisher.fromIterable(() -> IntStream.range(0, req.getId()).iterator()).map(this::response);
}
private CompatResponse response(final int value) {
if (errorMode == ErrorMode.SIMPLE_IN_RESPONSE) {
throwGrpcStatusException();
} else if (errorMode == ErrorMode.STATUS_IN_RESPONSE) {
throwGrpcStatusExceptionWithStatus();
}
return computeResponse(value);
}
private TerminalSignalConsumer errorConsumer() {
return new TerminalSignalConsumer() {
@Override
public void onComplete() {
}
@Override
public void onError(final Throwable throwable) {
reqStreamError.add(throwable);
}
@Override
public void cancel() {
reqStreamError.add(new IOException("cancelled"));
}
};
}
};
final ServiceFactory serviceFactory = new ServiceFactory.Builder().bufferEncoders(serviceTalkCompressions(compression)).bufferDecoderGroup(serviceTalkDecompression(compression)).bidirectionalStreamingCall(strategy, compatService).clientStreamingCall(strategy, compatService).scalarCall(strategy, compatService).serverStreamingCall(strategy, compatService).build();
final ServerContext serverContext = serviceTalkServerBuilder(errorMode, ssl, timeout, b -> b.executionStrategy(strategy)).listenAndAwait(serviceFactory);
return TestServerContext.fromServiceTalkServerContext(serverContext);
}
Aggregations