Search in sources :

Example 1 with BufferEncoder

use of io.servicetalk.encoding.api.BufferEncoder 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);
    }
}
Also used : GrpcClientMetadata(io.servicetalk.grpc.api.GrpcClientMetadata) DefaultGrpcClientMetadata(io.servicetalk.grpc.api.DefaultGrpcClientMetadata) DefaultGrpcClientMetadata(io.servicetalk.grpc.api.DefaultGrpcClientMetadata) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) CompatResponse(io.servicetalk.grpc.netty.CompatProto.ResponseContainer.CompatResponse)

Example 2 with BufferEncoder

use of io.servicetalk.encoding.api.BufferEncoder 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);
    }
}
Also used : GrpcClientMetadata(io.servicetalk.grpc.api.GrpcClientMetadata) DefaultGrpcClientMetadata(io.servicetalk.grpc.api.DefaultGrpcClientMetadata) DefaultGrpcClientMetadata(io.servicetalk.grpc.api.DefaultGrpcClientMetadata) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) CompatResponse(io.servicetalk.grpc.netty.CompatProto.ResponseContainer.CompatResponse) ArrayList(java.util.ArrayList)

Example 3 with BufferEncoder

use of io.servicetalk.encoding.api.BufferEncoder 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);
    }
}
Also used : GrpcClientMetadata(io.servicetalk.grpc.api.GrpcClientMetadata) DefaultGrpcClientMetadata(io.servicetalk.grpc.api.DefaultGrpcClientMetadata) DefaultGrpcClientMetadata(io.servicetalk.grpc.api.DefaultGrpcClientMetadata) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) CompatResponse(io.servicetalk.grpc.netty.CompatProto.ResponseContainer.CompatResponse) ArrayList(java.util.ArrayList)

Example 4 with BufferEncoder

use of io.servicetalk.encoding.api.BufferEncoder in project servicetalk by apple.

the class ContentEncodingHttpServiceFilter method create.

@Override
public StreamingHttpServiceFilter create(final StreamingHttpService service) {
    return new StreamingHttpServiceFilter(service) {

        @Override
        public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
            return Single.defer(() -> {
                final StreamingHttpRequest requestDecompressed;
                Iterator<? extends CharSequence> contentEncodingItr = request.headers().valuesIterator(CONTENT_ENCODING);
                final boolean hasContentEncoding = contentEncodingItr.hasNext();
                if (hasContentEncoding) {
                    BufferDecoder decoder = matchAndRemoveEncoding(decompressors.decoders(), BufferDecoder::encodingName, contentEncodingItr, request.headers());
                    if (decoder == null) {
                        return succeeded(responseFactory.unsupportedMediaType()).shareContextOnSubscribe();
                    }
                    requestDecompressed = request.transformPayloadBody(pub -> decoder.streamingDecoder().deserialize(pub, ctx.executionContext().bufferAllocator()));
                } else {
                    requestDecompressed = request;
                }
                return super.handle(ctx, requestDecompressed, responseFactory).map(response -> {
                    final CharSequence reqAcceptEncoding;
                    if (isPassThrough(request.method(), response) || (reqAcceptEncoding = request.headers().get(ACCEPT_ENCODING)) == null) {
                        return response;
                    }
                    BufferEncoder encoder = negotiateAcceptedEncodingRaw(reqAcceptEncoding, compressors, BufferEncoder::encodingName);
                    if (encoder == null || identityEncoder().equals(encoder)) {
                        return response;
                    }
                    addContentEncoding(response.headers(), encoder.encodingName());
                    return response.transformPayloadBody(bufPub -> encoder.streamingEncoder().serialize(bufPub, ctx.executionContext().bufferAllocator()));
                }).shareContextOnSubscribe();
            });
        }
    };
}
Also used : EmptyBufferDecoderGroup(io.servicetalk.encoding.api.EmptyBufferDecoderGroup) Function(java.util.function.Function) ArrayList(java.util.ArrayList) CharSequences.regionMatches(io.servicetalk.buffer.api.CharSequences.regionMatches) HEAD(io.servicetalk.http.api.HttpRequestMethod.HEAD) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) INFORMATIONAL_1XX(io.servicetalk.http.api.HttpResponseStatus.StatusClass.INFORMATIONAL_1XX) Objects.requireNonNull(java.util.Objects.requireNonNull) BufferDecoderGroup(io.servicetalk.encoding.api.BufferDecoderGroup) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) Nullable(javax.annotation.Nullable) Iterator(java.util.Iterator) HeaderUtils.addContentEncoding(io.servicetalk.http.api.HeaderUtils.addContentEncoding) Single(io.servicetalk.concurrent.api.Single) NO_CONTENT(io.servicetalk.http.api.HttpResponseStatus.NO_CONTENT) CONNECT(io.servicetalk.http.api.HttpRequestMethod.CONNECT) HeaderUtils.negotiateAcceptedEncodingRaw(io.servicetalk.encoding.api.internal.HeaderUtils.negotiateAcceptedEncodingRaw) Identity.identityEncoder(io.servicetalk.encoding.api.Identity.identityEncoder) CharSequences(io.servicetalk.buffer.api.CharSequences) SUCCESSFUL_2XX(io.servicetalk.http.api.HttpResponseStatus.StatusClass.SUCCESSFUL_2XX) ACCEPT_ENCODING(io.servicetalk.http.api.HttpHeaderNames.ACCEPT_ENCODING) List(java.util.List) NOT_MODIFIED(io.servicetalk.http.api.HttpResponseStatus.NOT_MODIFIED) BufferDecoder(io.servicetalk.encoding.api.BufferDecoder) CONTENT_ENCODING(io.servicetalk.http.api.HttpHeaderNames.CONTENT_ENCODING) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) BufferDecoder(io.servicetalk.encoding.api.BufferDecoder)

Example 5 with BufferEncoder

use of io.servicetalk.encoding.api.BufferEncoder 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);
    }
}
Also used : GrpcClientMetadata(io.servicetalk.grpc.api.GrpcClientMetadata) DefaultGrpcClientMetadata(io.servicetalk.grpc.api.DefaultGrpcClientMetadata) DefaultGrpcClientMetadata(io.servicetalk.grpc.api.DefaultGrpcClientMetadata) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) CompatResponse(io.servicetalk.grpc.netty.CompatProto.ResponseContainer.CompatResponse)

Aggregations

BufferEncoder (io.servicetalk.encoding.api.BufferEncoder)7 DefaultGrpcClientMetadata (io.servicetalk.grpc.api.DefaultGrpcClientMetadata)4 GrpcClientMetadata (io.servicetalk.grpc.api.GrpcClientMetadata)4 CompatResponse (io.servicetalk.grpc.netty.CompatProto.ResponseContainer.CompatResponse)4 Single (io.servicetalk.concurrent.api.Single)3 Identity.identityEncoder (io.servicetalk.encoding.api.Identity.identityEncoder)3 ACCEPT_ENCODING (io.servicetalk.http.api.HttpHeaderNames.ACCEPT_ENCODING)3 CONTENT_ENCODING (io.servicetalk.http.api.HttpHeaderNames.CONTENT_ENCODING)3 ArrayList (java.util.ArrayList)3 BufferDecoder (io.servicetalk.encoding.api.BufferDecoder)2 BufferDecoderGroup (io.servicetalk.encoding.api.BufferDecoderGroup)2 HeaderUtils.addContentEncoding (io.servicetalk.http.api.HeaderUtils.addContentEncoding)2 Iterator (java.util.Iterator)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Nullable (javax.annotation.Nullable)2 CharSequences (io.servicetalk.buffer.api.CharSequences)1 CharSequences.contentEqualsIgnoreCase (io.servicetalk.buffer.api.CharSequences.contentEqualsIgnoreCase)1 CharSequences.regionMatches (io.servicetalk.buffer.api.CharSequences.regionMatches)1 Publisher (io.servicetalk.concurrent.api.Publisher)1 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)1