io.grpc.MethodDescriptor Java Examples

The following examples show how to use io.grpc.MethodDescriptor. 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: DiscardClientInterceptorTest.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    this.descriptor = MethodDescriptor.<String, Integer>newBuilder()
            .setType(MethodDescriptor.MethodType.CLIENT_STREAMING)
            .setFullMethodName("a.service/method")
            .setRequestMarshaller(stringMarshaller)
            .setResponseMarshaller(intMarshaller)
            .build();
    this.callOptions = CallOptions.DEFAULT;
    this.clientCall = new ClientCallRecorder();
    when(channel.newCall(descriptor, callOptions)).thenReturn(clientCall);

    discardEventListener = spy(new LoggingDiscardEventListener<String>(DiscardClientInterceptorTest.class.getName(), 1));
    this.interceptor = new DiscardClientInterceptor(discardEventListener, 1);

    this.call = (DiscardClientInterceptor.DiscardClientCall<String, Integer>) interceptor.interceptCall(descriptor, callOptions, channel);
}
 
Example #2
Source File: ServiceConfigInterceptor.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@CheckForNull
private MethodInfo getMethodInfo(MethodDescriptor<?, ?> method) {
  ManagedChannelServiceConfig mcsc = managedChannelServiceConfig.get();
  if (mcsc == null) {
    return null;
  }
  MethodInfo info;
  info = mcsc.getServiceMethodMap().get(method.getFullMethodName());
  if (info == null) {
    String serviceName = method.getServiceName();
    info = mcsc.getServiceMap().get(serviceName);
  }
  if (info == null) {
    info = mcsc.getDefaultMethodConfig();
  }
  return info;
}
 
Example #3
Source File: AbstractGRPCClient.java    From client-java with Apache License 2.0 6 votes vote down vote up
<ReqT, RespT> StreamObserver<ReqT> callBidiStreamingWithRetry(
    BackOffer backOffer,
    MethodDescriptor<ReqT, RespT> method,
    StreamObserver<RespT> responseObserver,
    ErrorHandler<StreamObserver<ReqT>> handler) {
  logger.debug(String.format("Calling %s...", method.getFullMethodName()));

  RetryPolicy.Builder<StreamObserver<ReqT>> builder = new Builder<>(backOffer);
  StreamObserver<ReqT> observer =
      builder
          .create(handler)
          .callWithRetry(
              () -> {
                StubT stub = getAsyncStub();
                return asyncBidiStreamingCall(
                    stub.getChannel().newCall(method, stub.getCallOptions()), responseObserver);
              },
              method.getFullMethodName());
  logger.debug(String.format("leaving %s...", method.getFullMethodName()));
  return observer;
}
 
Example #4
Source File: AbstractGRPCClient.java    From tikv-client-lib-java with Apache License 2.0 6 votes vote down vote up
protected <ReqT, RespT> StreamingResponse callServerStreamingWithRetry(
    MethodDescriptor<ReqT, RespT> method,
    Supplier<ReqT> requestFactory,
    ErrorHandler<StreamingResponse> handler) {
  logger.debug(String.format("Calling %s...", method.getFullMethodName()));

  RetryPolicy.Builder<StreamingResponse> builder =
      new Builder<>(conf.getRetryTimes(), conf.getBackOffClass());
  StreamingResponse response =
      builder.create(handler)
          .callWithRetry(
              () -> {
                BlockingStubT stub = getBlockingStub();
                return new StreamingResponse(
                    blockingServerStreamingCall(
                        stub.getChannel(),
                        method,
                        stub.getCallOptions(),
                        requestFactory.get()
                    )
                );
              },
              method.getFullMethodName());
  logger.debug(String.format("leaving %s...", method.getFullMethodName()));
  return response;
}
 
Example #5
Source File: ClientCallsTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void blockingUnaryCall_HasBlockingStubType() {
  NoopClientCall<Integer, Integer> call = new NoopClientCall<Integer, Integer>() {
    @Override
    public void start(io.grpc.ClientCall.Listener<Integer> listener, Metadata headers) {
      listener.onMessage(1);
      listener.onClose(Status.OK, new Metadata());
    }
  };
  when(mockChannel.newCall(
      ArgumentMatchers.<MethodDescriptor<Integer, Integer>>any(), any(CallOptions.class)))
      .thenReturn(call);

  Integer unused =
      ClientCalls.blockingUnaryCall(mockChannel, UNARY_METHOD, CallOptions.DEFAULT, 1);

  verify(mockChannel).newCall(methodDescriptorCaptor.capture(), callOptionsCaptor.capture());
  CallOptions capturedCallOption = callOptionsCaptor.getValue();
  assertThat(capturedCallOption.getOption(ClientCalls.STUB_TYPE_OPTION))
      .isEquivalentAccordingToCompareTo(StubType.BLOCKING);
}
 
Example #6
Source File: AbstractStream.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
AbstractStream(
    FirestoreChannel channel,
    MethodDescriptor<ReqT, RespT> methodDescriptor,
    AsyncQueue workerQueue,
    TimerId connectionTimerId,
    TimerId idleTimerId,
    CallbackT listener) {
  this.firestoreChannel = channel;
  this.methodDescriptor = methodDescriptor;
  this.workerQueue = workerQueue;
  this.idleTimerId = idleTimerId;
  this.listener = listener;
  this.idleTimeoutRunnable = new IdleTimeoutRunnable();

  backoff =
      new ExponentialBackoff(
          workerQueue,
          connectionTimerId,
          BACKOFF_INITIAL_DELAY_MS,
          BACKOFF_FACTOR,
          BACKOFF_MAX_DELAY_MS);
}
 
Example #7
Source File: BinaryLogProviderTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void wrapChannel_methodDescriptor() throws Exception {
  final AtomicReference<MethodDescriptor<?, ?>> methodRef =
      new AtomicReference<>();
  Channel channel = new Channel() {
    @Override
    public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(
        MethodDescriptor<RequestT, ResponseT> method, CallOptions callOptions) {
      methodRef.set(method);
      return new NoopClientCall<>();
    }

    @Override
    public String authority() {
      throw new UnsupportedOperationException();
    }
  };
  Channel wChannel = binlogProvider.wrapChannel(channel);
  ClientCall<String, Integer> unusedClientCall = wChannel.newCall(method, CallOptions.DEFAULT);
  validateWrappedMethod(methodRef.get());
}
 
Example #8
Source File: BaseITTracingClientInterceptor.java    From brave with Apache License 2.0 5 votes vote down vote up
/**
 * NOTE: for this to work, the tracing interceptor must be last (so that it executes first)
 *
 * <p>Also notice that we are only making the current context available in the request side.
 */
@Test public void currentSpanVisibleToUserInterceptors() {
  closeClient(client);

  client = newClient(
      new ClientInterceptor() {
        @Override public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
            MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
          return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
            @Override
            public void start(Listener<RespT> responseListener, Metadata headers) {
              tracing.tracer().currentSpanCustomizer().annotate("start");
              super.start(responseListener, headers);
            }

            @Override public void sendMessage(ReqT message) {
              tracing.tracer().currentSpanCustomizer().annotate("sendMessage");
              super.sendMessage(message);
            }
          };
        }
      },
      grpcTracing.newClientInterceptor()
  );

  GreeterGrpc.newBlockingStub(client).sayHello(HELLO_REQUEST);

  assertThat(testSpanHandler.takeRemoteSpan(CLIENT).annotations())
      .extracting(Entry::getValue)
      .containsOnly("start", "sendMessage");
}
 
Example #9
Source File: ClientConnectionManager.java    From jetcd with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
    CallOptions callOptions, Channel next) {
    return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
        @Override
        public void start(Listener<RespT> responseListener, Metadata headers) {
            String token = getToken(next);
            if (token != null) {
                headers.put(TOKEN, token);
            }
            super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
                @Override
                public void onClose(Status status, Metadata trailers) {
                    if (isInvalidTokenError(status)) {
                        try {
                            refreshToken(next);
                        } catch (Exception e) {
                            // don't throw any error here.
                            // rpc will retry on expired auth token.
                        }
                    }
                    super.onClose(status, trailers);
                }
            }, headers);
        }
    };
}
 
Example #10
Source File: LogGrpcInterceptor.java    From spring-boot-grpc-example with MIT License 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
        CallOptions callOptions,
        Channel next) {
    log.info(method.getFullMethodName());
    return next.newCall(method, callOptions);
}
 
Example #11
Source File: TracingServerInterceptorTest.java    From java-grpc with Apache License 2.0 5 votes vote down vote up
@Test
public void testTracedServerWithCustomOperationName() {
  TracingServerInterceptor tracingInterceptor =
      TracingServerInterceptor.newBuilder()
          .withTracer(serverTracer)
          .withOperationName(
              new OperationNameConstructor() {
                @Override
                public <ReqT, RespT> String constructOperationName(
                    MethodDescriptor<ReqT, RespT> method) {
                  return PREFIX + method.getFullMethodName();
                }
              })
          .build();
  TracedService.addGeeterService(grpcServer.getServiceRegistry(), tracingInterceptor);

  assertEquals("call should complete successfully", "Hello world", client.greet().getMessage());
  await().atMost(5, TimeUnit.SECONDS).until(reportedSpansSize(serverTracer), equalTo(1));
  assertEquals(
      "one span should have been created and finished for one client request",
      serverTracer.finishedSpans().size(),
      1);

  MockSpan span = serverTracer.finishedSpans().get(0);
  assertEquals(
      "span should have prefix", span.operationName(), PREFIX + "helloworld.Greeter/SayHello");
  assertEquals("span should have no parents", span.parentId(), 0);
  assertEquals("span should have no logs", span.logEntries().size(), 0);
  Assertions.assertThat(span.tags())
      .as("span should have base server tags")
      .isEqualTo(BASE_SERVER_TAGS);
  assertFalse("span should have no baggage", span.context().baggageItems().iterator().hasNext());
}
 
Example #12
Source File: AbstractInteropTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void maxInboundSize_tooBig() {
  StreamingOutputCallRequest request = StreamingOutputCallRequest.newBuilder()
      .addResponseParameters(ResponseParameters.newBuilder().setSize(1))
      .build();

  MethodDescriptor<StreamingOutputCallRequest, StreamingOutputCallResponse> md =
      TestServiceGrpc.getStreamingOutputCallMethod();
  ByteSizeMarshaller<StreamingOutputCallRequest> mar =
      new ByteSizeMarshaller<>(md.getRequestMarshaller());
  blockingServerStreamingCall(
      blockingStub.getChannel(),
      md.toBuilder(mar, md.getResponseMarshaller()).build(),
      blockingStub.getCallOptions(),
      request)
      .next();

  int size = mar.lastOutSize;

  TestServiceGrpc.TestServiceBlockingStub stub =
      blockingStub.withMaxInboundMessageSize(size - 1);

  try {
    stub.streamingOutputCall(request).next();
    fail();
  } catch (StatusRuntimeException ex) {
    Status s = ex.getStatus();
    assertWithMessage(s.toString()).that(s.getCode()).isEqualTo(Status.Code.RESOURCE_EXHAUSTED);
    assertThat(Throwables.getStackTraceAsString(ex)).contains("exceeds maximum");
  }
}
 
Example #13
Source File: TracingServerCallListener.java    From skywalking with Apache License 2.0 5 votes vote down vote up
protected TracingServerCallListener(ServerCall.Listener<REQUEST> delegate, MethodDescriptor<REQUEST, ?> descriptor,
    ContextSnapshot contextSnapshot) {
    super(delegate);
    this.contextSnapshot = contextSnapshot;
    this.methodType = descriptor.getType();
    this.operationPrefix = OperationNameFormatUtil.formatOperationName(descriptor) + SERVER;
}
 
Example #14
Source File: CensusTracingModule.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
  // New RPCs on client-side inherit the tracing context from the current Context.
  // Safe usage of the unsafe trace API because CONTEXT_SPAN_KEY.get() returns the same value
  // as Tracer.getCurrentSpan() except when no value available when the return value is null
  // for the direct access and BlankSpan when Tracer API is used.
  final ClientCallTracer tracerFactory = newClientCallTracer(CONTEXT_SPAN_KEY.get(), method);
  ClientCall<ReqT, RespT> call =
      next.newCall(
          method,
          callOptions.withStreamTracerFactory(tracerFactory));
  return new SimpleForwardingClientCall<ReqT, RespT>(call) {
    @Override
    public void start(Listener<RespT> responseListener, Metadata headers) {
      delegate().start(
          new SimpleForwardingClientCallListener<RespT>(responseListener) {
            @Override
            public void onClose(io.grpc.Status status, Metadata trailers) {
              tracerFactory.callEnded(status);
              super.onClose(status, trailers);
            }
          },
          headers);
    }
  };
}
 
Example #15
Source File: ServerCallInfoImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
ServerCallInfoImpl(
    MethodDescriptor<ReqT, RespT> methodDescriptor,
    Attributes attributes,
    @Nullable String authority) {
  this.methodDescriptor = methodDescriptor;
  this.attributes = attributes;
  this.authority = authority;
}
 
Example #16
Source File: GrpcClient.java    From etcd-java with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param method
 * @param respStream
 * @param responseExecutor
 */
public ResilientBiDiStream(MethodDescriptor<ReqT,RespT> method,
        ResilientResponseObserver<ReqT,RespT> respStream,
        Executor responseExecutor) {
    this.method = method;
    this.respStream = respStream;
    this.responseExecutor = serialized(responseExecutor != null
            ? responseExecutor : userExecutor);
    this.requestExecutor = sendViaEventLoop ? serialized(ses) : null;
}
 
Example #17
Source File: StubConfigTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up mocks.
 */
@Before public void setUp() {
  MockitoAnnotations.initMocks(this);
  ClientCall<SimpleRequest, SimpleResponse> call =
      new NoopClientCall<>();
  when(channel.newCall(
          ArgumentMatchers.<MethodDescriptor<SimpleRequest, SimpleResponse>>any(),
          any(CallOptions.class)))
      .thenReturn(call);
}
 
Example #18
Source File: CallCredentialsApplyingTransportFactory.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public ClientStream newStream(
    MethodDescriptor<?, ?> method, Metadata headers, CallOptions callOptions) {
  CallCredentials creds = callOptions.getCredentials();
  if (creds != null) {
    MetadataApplierImpl applier = new MetadataApplierImpl(
        delegate, method, headers, callOptions);
    Attributes.Builder effectiveAttrsBuilder = Attributes.newBuilder()
        .set(CallCredentials.ATTR_AUTHORITY, authority)
        .set(CallCredentials.ATTR_SECURITY_LEVEL, SecurityLevel.NONE)
        .setAll(delegate.getAttributes());
    if (callOptions.getAuthority() != null) {
      effectiveAttrsBuilder.set(CallCredentials.ATTR_AUTHORITY, callOptions.getAuthority());
    }
    try {
      creds.applyRequestMetadata(method, effectiveAttrsBuilder.build(),
          firstNonNull(callOptions.getExecutor(), appExecutor), applier);
    } catch (Throwable t) {
      applier.fail(Status.UNAUTHENTICATED
          .withDescription("Credentials should use fail() instead of throwing exceptions")
          .withCause(t));
    }
    return applier.returnStream();
  } else {
    return delegate.newStream(method, headers, callOptions);
  }
}
 
Example #19
Source File: LoggingInterceptorTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private LoggingInterceptor getInterceptorWithAlwaysThisHandler(
    LoggingHandler handler, AsynchronousFileOutputStream outputFile) {
  return new LoggingInterceptor(outputFile, clock) {
    @Override
    public <ReqT, RespT> LoggingHandler<ReqT, RespT> selectHandler(
        MethodDescriptor<ReqT, RespT> method) {
      return handler;
    }
  };
}
 
Example #20
Source File: NettyClientTransport.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public ClientStream newStream(
    MethodDescriptor<?, ?> method, Metadata headers, CallOptions callOptions) {
  Preconditions.checkNotNull(method, "method");
  Preconditions.checkNotNull(headers, "headers");
  if (channel == null) {
    return new FailingClientStream(statusExplainingWhyTheChannelIsNull);
  }
  StatsTraceContext statsTraceCtx =
      StatsTraceContext.newClientContext(callOptions, getAttributes(), headers);
  return new NettyClientStream(
      new NettyClientStream.TransportState(
          handler,
          channel.eventLoop(),
          maxMessageSize,
          statsTraceCtx,
          transportTracer,
          method.getFullMethodName()) {
        @Override
        protected Status statusFromFailedFuture(ChannelFuture f) {
          return NettyClientTransport.this.statusFromFailedFuture(f);
        }
      },
      method,
      headers,
      channel,
      authority,
      negotiationScheme,
      userAgent,
      statsTraceCtx,
      transportTracer,
      callOptions,
      useGetForSafeMethods);
}
 
Example #21
Source File: MetricCollectingServerInterceptor.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
@Override
protected Counter newRequestCounterFor(final MethodDescriptor<?, ?> method) {
    return this.counterCustomizer.apply(
            prepareCounterFor(method,
                    METRIC_NAME_SERVER_REQUESTS_RECEIVED,
                    "The total number of requests received"))
            .register(this.registry);
}
 
Example #22
Source File: ServerImpl.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * 将服务注册与注销相关的参数封装成一个对象
 *
 * @author sxp
 * @since V1.0 2017/3/21
 */
private List<Map<String,Object>> createServerParams() {
  int port = transportServer.getPort();

  List<ServerServiceDefinition> serviceDfs = getServices();

  List<Map<String,Object>> params = new ArrayList<>(serviceDfs.size());
  StringBuilder sb = new StringBuilder();
  Map<String, Object> oneService;
  Collection<MethodDescriptor<?, ?>> methodDesps;
  String methodName;

  for (ServerServiceDefinition item : serviceDfs) {
    sb.setLength(0);

    methodDesps = item.getServiceDescriptor().getMethods();
    for (MethodDescriptor<?, ?> md : methodDesps) {
      methodName = GrpcUtils.getSimpleMethodName(md.getFullMethodName());
      sb.append(methodName);
      sb.append(",");// 多个方法之间用英文逗号分隔
    }
    sb.deleteCharAt(sb.lastIndexOf(","));

    oneService = new HashMap<>();
    oneService.put(GlobalConstants.Provider.Key.INTERFACE, item.getServiceDescriptor().getName());
    oneService.put(GlobalConstants.CommonKey.METHODS, sb.toString());
    oneService.put(GlobalConstants.PROVIDER_SERVICE_PORT, port);

    params.add(oneService);
  }

  return params;
}
 
Example #23
Source File: TripleServer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private ServiceDescriptor getServiceDescriptor(ServerServiceDefinition template, ProviderConfig providerConfig,
                                               List<MethodDescriptor<Request, Response>> methodDescriptors) {
    String serviceName = providerConfig.getInterfaceId();
    ServiceDescriptor.Builder builder = ServiceDescriptor.newBuilder(serviceName)
        .setSchemaDescriptor(template.getServiceDescriptor().getSchemaDescriptor());
    for (MethodDescriptor<Request, Response> methodDescriptor : methodDescriptors) {
        builder.addMethod(methodDescriptor);
    }
    return builder.build();

}
 
Example #24
Source File: AbstractInteropTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void maxOutboundSize_tooBig() {
  // set at least one field to ensure the size is non-zero.
  StreamingOutputCallRequest request = StreamingOutputCallRequest.newBuilder()
      .addResponseParameters(ResponseParameters.newBuilder().setSize(1))
      .build();


  MethodDescriptor<StreamingOutputCallRequest, StreamingOutputCallResponse> md =
      TestServiceGrpc.getStreamingOutputCallMethod();
  ByteSizeMarshaller<StreamingOutputCallRequest> mar =
      new ByteSizeMarshaller<>(md.getRequestMarshaller());
  blockingServerStreamingCall(
      blockingStub.getChannel(),
      md.toBuilder(mar, md.getResponseMarshaller()).build(),
      blockingStub.getCallOptions(),
      request)
      .next();

  TestServiceGrpc.TestServiceBlockingStub stub =
      blockingStub.withMaxOutboundMessageSize(mar.lastOutSize - 1);
  try {
    stub.streamingOutputCall(request).next();
    fail();
  } catch (StatusRuntimeException ex) {
    Status s = ex.getStatus();
    assertWithMessage(s.toString()).that(s.getCode()).isEqualTo(Status.Code.CANCELLED);
    assertThat(Throwables.getStackTraceAsString(ex)).contains("message too large");
  }
}
 
Example #25
Source File: ServerCallImpl.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
ServerCallImpl(ServerStream stream, MethodDescriptor<ReqT, RespT> method,
    Metadata inboundHeaders, Context.CancellableContext context,
    DecompressorRegistry decompressorRegistry, CompressorRegistry compressorRegistry,
    CallTracer serverCallTracer, Tag tag) {
  this.stream = stream;
  this.method = method;
  this.context = context;
  this.messageAcceptEncoding = inboundHeaders.get(MESSAGE_ACCEPT_ENCODING_KEY);
  this.decompressorRegistry = decompressorRegistry;
  this.compressorRegistry = compressorRegistry;
  this.serverCallTracer = serverCallTracer;
  this.serverCallTracer.reportCallStarted();
  this.tag = tag;
}
 
Example #26
Source File: GrpcHelperImpl.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public Span startSpan(@Nullable AbstractSpan<?> parent,
                      @Nullable MethodDescriptor<?, ?> method,
                      @Nullable String authority) {

    if (null == parent) {
        return null;
    }

    // we only support unary method calls and ignore others for now
    if (method != null && method.getType() != MethodDescriptor.MethodType.UNARY) {
        return null;
    }

    Span span = parent.createExitSpan();
    if (span == null) {
        // as it's an external call, we only need a single span for nested calls
        return null;
    }

    span.withName(method == null ? null : method.getFullMethodName())
        .withType("external")
        .withSubtype(GRPC);

    if (authority != null) {
        Destination destination = span.getContext().getDestination()
            .withAddressPort(authority);

        destination.getService()
            .withName(GRPC)
            .withResource(authority)
            .withType(GRPC);
    }
    return span.activate();
}
 
Example #27
Source File: CronetClientStreamTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void idempotentMethod_usesHttpPut() {
  SetStreamFactoryRunnable callback = new SetStreamFactoryRunnable(factory);
  MethodDescriptor<?, ?> idempotentMethod = method.toBuilder().setIdempotent(true).build();
  CronetClientStream stream =
      new CronetClientStream(
          "https://www.google.com:443",
          "cronet",
          executor,
          metadata,
          transport,
          callback,
          lock,
          100,
          false /* alwaysUsePut */,
          idempotentMethod,
          StatsTraceContext.NOOP,
          CallOptions.DEFAULT,
          transportTracer);
  callback.setStream(stream);
  ExperimentalBidirectionalStream.Builder builder =
      mock(ExperimentalBidirectionalStream.Builder.class);
  when(factory.newBidirectionalStreamBuilder(
          any(String.class), any(BidirectionalStream.Callback.class), any(Executor.class)))
      .thenReturn(builder);
  when(builder.build()).thenReturn(cronetStream);
  stream.start(clientListener);

  verify(builder).setHttpMethod("PUT");
}
 
Example #28
Source File: ChannelLoggingInterceptor.java    From genie with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    final MethodDescriptor<ReqT, RespT> method,
    final CallOptions callOptions,
    final Channel next
) {
    final String methodType = method.getType().toString();
    final String methodName = method.getFullMethodName();
    final int channelId = next.hashCode();
    log.info("gRPC {} call: {} (channel: {})", methodType, methodName, channelId);
    return next.newCall(method, callOptions);
}
 
Example #29
Source File: GrpcServerMetricAutoConfiguration.java    From grpc-spring-boot-starter with MIT License 5 votes vote down vote up
/**
 * Gets all method names from the given service descriptor.
 *
 * @param serviceDescriptor The service descriptor to get the names from.
 * @return The newly created and sorted list of the method names.
 */
protected List<String> collectMethodNamesForService(final ServiceDescriptor serviceDescriptor) {
    final List<String> methods = new ArrayList<>();
    for (final MethodDescriptor<?, ?> grpcMethod : serviceDescriptor.getMethods()) {
        methods.add(extractMethodName(grpcMethod));
    }
    methods.sort(String.CASE_INSENSITIVE_ORDER);
    return methods;
}
 
Example #30
Source File: TesterActivity.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
    MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
  return new CheckedForwardingClientCall<ReqT, RespT>(
      next.newCall(method.toBuilder().setSafe(true).build(), callOptions)) {
    @Override
    public void checkedStart(Listener<RespT> responseListener, Metadata headers) {
      delegate().start(responseListener, headers);
    }
  };
}