Java Code Examples for io.grpc.DecompressorRegistry#getDefaultInstance()

The following examples show how to use io.grpc.DecompressorRegistry#getDefaultInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServerCallImplTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
private void sendMessage_serverSendsOne_closeOnSecondCall(
    MethodDescriptor<Long, Long> method) {
  ServerCallImpl<Long, Long> serverCall = new ServerCallImpl<Long, Long>(
      stream,
      method,
      requestHeaders,
      context,
      DecompressorRegistry.getDefaultInstance(),
      CompressorRegistry.getDefaultInstance(),
      serverCallTracer);
  serverCall.sendHeaders(new Metadata());
  serverCall.sendMessage(1L);
  verify(stream, times(1)).writeMessage(any(InputStream.class));
  verify(stream, never()).close(any(Status.class), any(Metadata.class));

  // trying to send a second message causes gRPC to close the underlying stream
  serverCall.sendMessage(1L);
  verify(stream, times(1)).writeMessage(any(InputStream.class));
  ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
  verify(stream, times(1)).cancel(statusCaptor.capture());
  assertEquals(Status.Code.INTERNAL, statusCaptor.getValue().getCode());
  assertEquals(ServerCallImpl.TOO_MANY_RESPONSES, statusCaptor.getValue().getDescription());
}
 
Example 2
Source File: ServerCallImplTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
private void sendMessage_serverSendsOne_closeOnSecondCall_appRunToCompletion(
    MethodDescriptor<Long, Long> method) {
  ServerCallImpl<Long, Long> serverCall = new ServerCallImpl<Long, Long>(
      stream,
      method,
      requestHeaders,
      context,
      DecompressorRegistry.getDefaultInstance(),
      CompressorRegistry.getDefaultInstance(),
      serverCallTracer);
  serverCall.sendHeaders(new Metadata());
  serverCall.sendMessage(1L);
  serverCall.sendMessage(1L);
  verify(stream, times(1)).writeMessage(any(InputStream.class));
  verify(stream, times(1)).cancel(any(Status.class));

  // App runs to completion but everything is ignored
  serverCall.sendMessage(1L);
  serverCall.close(Status.OK, new Metadata());
  try {
    serverCall.close(Status.OK, new Metadata());
    fail("calling a second time should still cause an error");
  } catch (IllegalStateException expected) {
    // noop
  }
}
 
Example 3
Source File: ServerCallImplTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
private void serverSendsOne_okFailsOnMissingResponse(
    MethodDescriptor<Long, Long> method) {
  ServerCallImpl<Long, Long> serverCall = new ServerCallImpl<Long, Long>(
      stream,
      method,
      requestHeaders,
      context,
      DecompressorRegistry.getDefaultInstance(),
      CompressorRegistry.getDefaultInstance(),
      serverCallTracer);
  serverCall.close(Status.OK, new Metadata());
  ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
  verify(stream, times(1)).cancel(statusCaptor.capture());
  assertEquals(Status.Code.INTERNAL, statusCaptor.getValue().getCode());
  assertEquals(ServerCallImpl.MISSING_RESPONSE, statusCaptor.getValue().getDescription());
}
 
Example 4
Source File: GrpcCommonCodecAutoConfiguration.java    From grpc-spring-boot-starter with MIT License 6 votes vote down vote up
@ConditionalOnBean(GrpcCodecDiscoverer.class)
@ConditionalOnMissingBean
@Bean
public DecompressorRegistry defaultDecompressorRegistry(final GrpcCodecDiscoverer codecDiscoverer) {
    log.debug("Found GrpcCodecDiscoverer -> Creating custom DecompressorRegistry");
    DecompressorRegistry registry = DecompressorRegistry.getDefaultInstance();
    for (final GrpcCodecDefinition definition : codecDiscoverer.findGrpcCodecs()) {
        if (definition.getCodecType().isForDecompression()) {
            final Codec codec = definition.getCodec();
            final boolean isAdvertised = definition.isAdvertised();
            log.debug("Registering {} decompressor: '{}' ({})",
                    isAdvertised ? "advertised" : "", codec.getMessageEncoding(), codec.getClass().getName());
            registry = registry.with(codec, isAdvertised);
        }
    }
    return registry;
}
 
Example 5
Source File: GrpcCommonCodecAutoConfiguration.java    From grpc-spring-boot-starter with MIT License 6 votes vote down vote up
@ConditionalOnBean(GrpcCodecDiscoverer.class)
@ConditionalOnMissingBean
@Bean
public DecompressorRegistry defaultDecompressorRegistry(final GrpcCodecDiscoverer codecDiscoverer) {
    log.debug("Found GrpcCodecDiscoverer -> Creating custom DecompressorRegistry");
    DecompressorRegistry registry = DecompressorRegistry.getDefaultInstance();
    for (final GrpcCodecDefinition definition : codecDiscoverer.findGrpcCodecs()) {
        if (definition.getCodecType().isForDecompression()) {
            final Codec codec = definition.getCodec();
            final boolean isAdvertised = definition.isAdvertised();
            log.debug("Registering {} decompressor: '{}' ({})",
                    isAdvertised ? "advertised" : "", codec.getMessageEncoding(), codec.getClass().getName());
            registry = registry.with(codec, isAdvertised);
        }
    }
    return registry;
}
 
Example 6
Source File: ArmeriaServerCallTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void messageRead_wrappedByteBuf() {
    tearDown();

    call = new ArmeriaServerCall<>(
            HttpHeaders.of(),
            TestServiceGrpc.getUnaryCallMethod(),
            CompressorRegistry.getDefaultInstance(),
            DecompressorRegistry.getDefaultInstance(),
            res,
            MAX_MESSAGE_BYTES,
            MAX_MESSAGE_BYTES,
            ctx,
            GrpcSerializationFormats.PROTO,
            new DefaultJsonMarshaller(MessageMarshaller.builder().build()),
            true,
            false,
            ResponseHeaders.builder(HttpStatus.OK)
                           .contentType(GrpcSerializationFormats.PROTO.mediaType())
                           .build());

    final ByteBuf buf = GrpcTestUtil.requestByteBuf();
    call.messageRead(new DeframedMessage(buf, 0));

    verify(buffersAttr).put(any(), same(buf));
}
 
Example 7
Source File: ServerCallImplTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private void sendMessage_serverSendsOne_closeOnSecondCall(
    MethodDescriptor<Long, Long> method) {
  ServerCallImpl<Long, Long> serverCall = new ServerCallImpl<>(
      stream,
      method,
      requestHeaders,
      context,
      DecompressorRegistry.getDefaultInstance(),
      CompressorRegistry.getDefaultInstance(),
      serverCallTracer,
      PerfMark.createTag());
  serverCall.sendHeaders(new Metadata());
  serverCall.sendMessage(1L);
  verify(stream, times(1)).writeMessage(any(InputStream.class));
  verify(stream, never()).close(any(Status.class), any(Metadata.class));

  // trying to send a second message causes gRPC to close the underlying stream
  serverCall.sendMessage(1L);
  verify(stream, times(1)).writeMessage(any(InputStream.class));
  ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
  verify(stream, times(1)).cancel(statusCaptor.capture());
  assertEquals(Status.Code.INTERNAL, statusCaptor.getValue().getCode());
  assertEquals(ServerCallImpl.TOO_MANY_RESPONSES, statusCaptor.getValue().getDescription());
}
 
Example 8
Source File: ServerCallImplTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private void serverSendsOne_okFailsOnMissingResponse(
    MethodDescriptor<Long, Long> method) {
  ServerCallImpl<Long, Long> serverCall = new ServerCallImpl<>(
      stream,
      method,
      requestHeaders,
      context,
      DecompressorRegistry.getDefaultInstance(),
      CompressorRegistry.getDefaultInstance(),
      serverCallTracer,
      PerfMark.createTag());
  serverCall.close(Status.OK, new Metadata());
  ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
  verify(stream, times(1)).cancel(statusCaptor.capture());
  assertEquals(Status.Code.INTERNAL, statusCaptor.getValue().getCode());
  assertEquals(ServerCallImpl.MISSING_RESPONSE, statusCaptor.getValue().getDescription());
}
 
Example 9
Source File: ServerCallImplTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  context = Context.ROOT.withCancellation();
  call = new ServerCallImpl<Long, Long>(stream, UNARY_METHOD, requestHeaders, context,
      DecompressorRegistry.getDefaultInstance(), CompressorRegistry.getDefaultInstance(),
      serverCallTracer);
}
 
Example 10
Source File: ServerCallImplTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
private void callTracer0(Status status) {
  CallTracer tracer = CallTracer.getDefaultFactory().create();
  ServerStats.Builder beforeBuilder = new ServerStats.Builder();
  tracer.updateBuilder(beforeBuilder);
  ServerStats before = beforeBuilder.build();
  assertEquals(0, before.callsStarted);
  assertEquals(0, before.lastCallStartedNanos);

  call = new ServerCallImpl<Long, Long>(stream, UNARY_METHOD, requestHeaders, context,
      DecompressorRegistry.getDefaultInstance(), CompressorRegistry.getDefaultInstance(),
      tracer);

  // required boilerplate
  call.sendHeaders(new Metadata());
  call.sendMessage(123L);
  // end: required boilerplate

  call.close(status, new Metadata());
  ServerStats.Builder afterBuilder = new ServerStats.Builder();
  tracer.updateBuilder(afterBuilder);
  ServerStats after = afterBuilder.build();
  assertEquals(1, after.callsStarted);
  if (status.isOk()) {
    assertEquals(1, after.callsSucceeded);
  } else {
    assertEquals(1, after.callsFailed);
  }
}
 
Example 11
Source File: ArmeriaServerCallTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    completionFuture = new CompletableFuture<>();
    when(res.whenComplete()).thenReturn(completionFuture);

    ctx = ServiceRequestContext.builder(HttpRequest.of(HttpMethod.POST, "/"))
                               .eventLoop(eventLoop.get())
                               .build();

    call = new ArmeriaServerCall<>(
            HttpHeaders.of(),
            TestServiceGrpc.getUnaryCallMethod(),
            CompressorRegistry.getDefaultInstance(),
            DecompressorRegistry.getDefaultInstance(),
            res,
            MAX_MESSAGE_BYTES,
            MAX_MESSAGE_BYTES,
            ctx,
            GrpcSerializationFormats.PROTO,
            new DefaultJsonMarshaller(MessageMarshaller.builder().build()),
            false,
            false,
            ResponseHeaders.builder(HttpStatus.OK)
                           .contentType(GrpcSerializationFormats.PROTO.mediaType())
                           .build());
    call.setListener(listener);
    call.messageReader().onSubscribe(subscription);

    ctx.setAttr(GrpcUnsafeBufferUtil.BUFFERS, buffersAttr);
}
 
Example 12
Source File: ServerCallImplTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  context = Context.ROOT.withCancellation();
  call = new ServerCallImpl<>(stream, UNARY_METHOD, requestHeaders, context,
      DecompressorRegistry.getDefaultInstance(), CompressorRegistry.getDefaultInstance(),
      serverCallTracer, PerfMark.createTag());
}
 
Example 13
Source File: ServerCallImplTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void callTracer0(Status status) {
  CallTracer tracer = CallTracer.getDefaultFactory().create();
  ServerStats.Builder beforeBuilder = new ServerStats.Builder();
  tracer.updateBuilder(beforeBuilder);
  ServerStats before = beforeBuilder.build();
  assertEquals(0, before.callsStarted);
  assertEquals(0, before.lastCallStartedNanos);

  call = new ServerCallImpl<>(stream, UNARY_METHOD, requestHeaders, context,
      DecompressorRegistry.getDefaultInstance(), CompressorRegistry.getDefaultInstance(),
      tracer, PerfMark.createTag());

  // required boilerplate
  call.sendHeaders(new Metadata());
  call.sendMessage(123L);
  // end: required boilerplate

  call.close(status, new Metadata());
  ServerStats.Builder afterBuilder = new ServerStats.Builder();
  tracer.updateBuilder(afterBuilder);
  ServerStats after = afterBuilder.build();
  assertEquals(1, after.callsStarted);
  if (status.isOk()) {
    assertEquals(1, after.callsSucceeded);
  } else {
    assertEquals(1, after.callsFailed);
  }
}
 
Example 14
Source File: ServerCallImplTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private void sendMessage_serverSendsOne_closeOnSecondCall_appRunToCompletion(
    MethodDescriptor<Long, Long> method) {
  ServerCallImpl<Long, Long> serverCall = new ServerCallImpl<>(
      stream,
      method,
      requestHeaders,
      context,
      DecompressorRegistry.getDefaultInstance(),
      CompressorRegistry.getDefaultInstance(),
      serverCallTracer,
      PerfMark.createTag());
  serverCall.sendHeaders(new Metadata());
  serverCall.sendMessage(1L);
  serverCall.sendMessage(1L);
  verify(stream, times(1)).writeMessage(any(InputStream.class));
  verify(stream, times(1)).cancel(any(Status.class));

  // App runs to completion but everything is ignored
  serverCall.sendMessage(1L);
  serverCall.close(Status.OK, new Metadata());
  try {
    serverCall.close(Status.OK, new Metadata());
    fail("calling a second time should still cause an error");
  } catch (IllegalStateException expected) {
    // noop
  }
}
 
Example 15
Source File: ArmeriaChannel.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public <I, O> ClientCall<I, O> newCall(
        MethodDescriptor<I, O> method, CallOptions callOptions) {
    final HttpRequestWriter req = HttpRequest.streaming(
            RequestHeaders.of(HttpMethod.POST, uri().getPath() + method.getFullMethodName(),
                              HttpHeaderNames.CONTENT_TYPE, serializationFormat.mediaType(),
                              HttpHeaderNames.TE, HttpHeaderValues.TRAILERS));
    final DefaultClientRequestContext ctx = newContext(HttpMethod.POST, req);

    final String fullMethodName = method.getFullMethodName();
    final int methodIndex = fullMethodName.lastIndexOf('/') + 1;
    ctx.logBuilder().name(method.getServiceName(), fullMethodName.substring(methodIndex));
    ctx.logBuilder().serializationFormat(serializationFormat);
    ctx.logBuilder().defer(RequestLogProperty.REQUEST_CONTENT,
                           RequestLogProperty.RESPONSE_CONTENT);

    final ClientOptions options = options();
    final int maxOutboundMessageSizeBytes = options.get(GrpcClientOptions.MAX_OUTBOUND_MESSAGE_SIZE_BYTES);
    final int maxInboundMessageSizeBytes = options.get(GrpcClientOptions.MAX_INBOUND_MESSAGE_SIZE_BYTES);
    final boolean unsafeWrapResponseBuffers = options.get(GrpcClientOptions.UNSAFE_WRAP_RESPONSE_BUFFERS);

    final PooledHttpClient client;

    final CallCredentials credentials = callOptions.getCredentials();
    if (credentials != null) {
        client = new CallCredentialsDecoratingClient(httpClient, credentials, method, authority());
    } else {
        client = httpClient;
    }

    return new ArmeriaClientCall<>(
            ctx,
            params.endpointGroup(),
            client,
            req,
            method,
            maxOutboundMessageSizeBytes,
            maxInboundMessageSizeBytes > 0 ? maxInboundMessageSizeBytes
                                           : Ints.saturatedCast(options.maxResponseLength()),
            callOptions,
            CompressorRegistry.getDefaultInstance(),
            DecompressorRegistry.getDefaultInstance(),
            serializationFormat,
            jsonMarshaller,
            unsafeWrapResponseBuffers,
            advertisedEncodingsHeader);
}
 
Example 16
Source File: HttpStreamReaderTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    reader = new HttpStreamReader(DecompressorRegistry.getDefaultInstance(), deframer,
                                  transportStatusListener);
}