Search in sources :

Example 11 with SimpleForwardingClientCall

use of io.grpc.ForwardingClientCall.SimpleForwardingClientCall in project beam by apache.

the class GrpcContextHeaderAccessorProviderTest method testWorkerIdOnConnect.

@SuppressWarnings("unchecked")
@Test
public void testWorkerIdOnConnect() throws Exception {
    final String worker1 = "worker1";
    CompletableFuture<String> workerId = new CompletableFuture<>();
    Consumer<StreamObserver<Elements>> consumer = elementsStreamObserver -> {
        workerId.complete(GrpcContextHeaderAccessorProvider.getHeaderAccessor().getSdkWorkerId());
        elementsStreamObserver.onCompleted();
    };
    TestDataService testService = new TestDataService(Mockito.mock(StreamObserver.class), consumer);
    ApiServiceDescriptor serviceDescriptor = ApiServiceDescriptor.newBuilder().setUrl("testServer").build();
    cleanupRule.register(InProcessServerFactory.create().create(ImmutableList.of(testService), serviceDescriptor));
    final Metadata.Key<String> workerIdKey = Metadata.Key.of("worker_id", Metadata.ASCII_STRING_MARSHALLER);
    Channel channel = cleanupRule.register(InProcessChannelBuilder.forName(serviceDescriptor.getUrl()).intercept(new ClientInterceptor() {

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
            return new SimpleForwardingClientCall<ReqT, RespT>(call) {

                @Override
                public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) {
                    headers.put(workerIdKey, worker1);
                    super.start(responseListener, headers);
                }
            };
        }
    }).build());
    BeamFnDataGrpc.BeamFnDataStub stub = BeamFnDataGrpc.newStub(channel);
    stub.data(Mockito.mock(StreamObserver.class)).onCompleted();
    Assert.assertEquals(worker1, workerId.get());
}
Also used : StreamObserver(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver) ClientInterceptor(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor) Elements(org.apache.beam.model.fnexecution.v1.BeamFnApi.Elements) RunWith(org.junit.runner.RunWith) Channel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Channel) CompletableFuture(java.util.concurrent.CompletableFuture) BeamFnDataGrpc(org.apache.beam.model.fnexecution.v1.BeamFnDataGrpc) GrpcContextHeaderAccessorProvider(org.apache.beam.sdk.fn.server.GrpcContextHeaderAccessorProvider) InProcessChannelBuilder(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.inprocess.InProcessChannelBuilder) MethodDescriptor(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.MethodDescriptor) ClientCall(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientCall) ApiServiceDescriptor(org.apache.beam.model.pipeline.v1.Endpoints.ApiServiceDescriptor) InProcessServerFactory(org.apache.beam.sdk.fn.server.InProcessServerFactory) Metadata(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) BeamFnApi(org.apache.beam.model.fnexecution.v1.BeamFnApi) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CallOptions(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.CallOptions) GrpcCleanupRule(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.testing.GrpcCleanupRule) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) StreamObserver(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver) SimpleForwardingClientCall(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ForwardingClientCall.SimpleForwardingClientCall) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Assert(org.junit.Assert) ApiServiceDescriptor(org.apache.beam.model.pipeline.v1.Endpoints.ApiServiceDescriptor) Channel(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Channel) Metadata(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Metadata) CallOptions(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.CallOptions) CompletableFuture(java.util.concurrent.CompletableFuture) ClientCall(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientCall) SimpleForwardingClientCall(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ForwardingClientCall.SimpleForwardingClientCall) ClientInterceptor(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ClientInterceptor) BeamFnDataGrpc(org.apache.beam.model.fnexecution.v1.BeamFnDataGrpc) Test(org.junit.Test)

Aggregations

SimpleForwardingClientCall (io.grpc.ForwardingClientCall.SimpleForwardingClientCall)10 Metadata (io.grpc.Metadata)9 CallOptions (io.grpc.CallOptions)8 Channel (io.grpc.Channel)8 ClientInterceptor (io.grpc.ClientInterceptor)8 ClientCall (io.grpc.ClientCall)6 ManagedChannel (io.grpc.ManagedChannel)6 MethodDescriptor (io.grpc.MethodDescriptor)5 SimpleForwardingClientCallListener (io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener)4 Test (org.junit.Test)4 Status (io.grpc.Status)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Span (zipkin2.Span)2 CurrentSpanCustomizer (brave.CurrentSpanCustomizer)1 Span (brave.Span)1 SpanCustomizer (brave.SpanCustomizer)1 Tracer (brave.Tracer)1 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)1 ByteString (com.google.protobuf.ByteString)1 Duration (com.google.protobuf.Duration)1