Search in sources :

Example 16 with StreamObserver

use of io.grpc.stub.StreamObserver in project beam by apache.

the class BeamFnControlClientTest method testDelegation.

@Test
public void testDelegation() throws Exception {
    AtomicBoolean clientClosedStream = new AtomicBoolean();
    BlockingQueue<BeamFnApi.InstructionResponse> values = new LinkedBlockingQueue<>();
    BlockingQueue<StreamObserver<BeamFnApi.InstructionRequest>> outboundServerObservers = new LinkedBlockingQueue<>();
    CallStreamObserver<BeamFnApi.InstructionResponse> inboundServerObserver = TestStreams.withOnNext(values::add).withOnCompleted(() -> clientClosedStream.set(true)).build();
    BeamFnApi.ApiServiceDescriptor apiServiceDescriptor = BeamFnApi.ApiServiceDescriptor.newBuilder().setUrl(this.getClass().getName() + "-" + UUID.randomUUID().toString()).build();
    Server server = InProcessServerBuilder.forName(apiServiceDescriptor.getUrl()).addService(new BeamFnControlGrpc.BeamFnControlImplBase() {

        @Override
        public StreamObserver<BeamFnApi.InstructionResponse> control(StreamObserver<BeamFnApi.InstructionRequest> outboundObserver) {
            Uninterruptibles.putUninterruptibly(outboundServerObservers, outboundObserver);
            return inboundServerObserver;
        }
    }).build();
    server.start();
    try {
        ManagedChannel channel = InProcessChannelBuilder.forName(apiServiceDescriptor.getUrl()).build();
        EnumMap<BeamFnApi.InstructionRequest.RequestCase, ThrowingFunction<BeamFnApi.InstructionRequest, BeamFnApi.InstructionResponse.Builder>> handlers = new EnumMap<>(BeamFnApi.InstructionRequest.RequestCase.class);
        handlers.put(BeamFnApi.InstructionRequest.RequestCase.PROCESS_BUNDLE, new ThrowingFunction<BeamFnApi.InstructionRequest, BeamFnApi.InstructionResponse.Builder>() {

            @Override
            public BeamFnApi.InstructionResponse.Builder apply(BeamFnApi.InstructionRequest value) throws Exception {
                return BeamFnApi.InstructionResponse.newBuilder().setProcessBundle(BeamFnApi.ProcessBundleResponse.getDefaultInstance());
            }
        });
        handlers.put(BeamFnApi.InstructionRequest.RequestCase.REGISTER, new ThrowingFunction<BeamFnApi.InstructionRequest, BeamFnApi.InstructionResponse.Builder>() {

            @Override
            public BeamFnApi.InstructionResponse.Builder apply(BeamFnApi.InstructionRequest value) throws Exception {
                throw FAILURE;
            }
        });
        BeamFnControlClient client = new BeamFnControlClient(apiServiceDescriptor, (BeamFnApi.ApiServiceDescriptor descriptor) -> channel, this::createStreamForTest, handlers);
        // Get the connected client and attempt to send and receive an instruction
        StreamObserver<BeamFnApi.InstructionRequest> outboundServerObserver = outboundServerObservers.take();
        ExecutorService executor = Executors.newCachedThreadPool();
        Future<Void> future = executor.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                client.processInstructionRequests(executor);
                return null;
            }
        });
        outboundServerObserver.onNext(SUCCESSFUL_REQUEST);
        assertEquals(SUCCESSFUL_RESPONSE, values.take());
        // Ensure that conversion of an unknown request type is properly converted to a
        // failure response.
        outboundServerObserver.onNext(UNKNOWN_HANDLER_REQUEST);
        assertEquals(UNKNOWN_HANDLER_RESPONSE, values.take());
        // Ensure that all exceptions are caught and translated to failures
        outboundServerObserver.onNext(FAILURE_REQUEST);
        assertEquals(FAILURE_RESPONSE, values.take());
        // Ensure that the server completing the stream translates to the completable future
        // being completed allowing for a successful shutdown of the client.
        outboundServerObserver.onCompleted();
        future.get();
    } finally {
        server.shutdownNow();
    }
}
Also used : Server(io.grpc.Server) InProcessServerBuilder(io.grpc.inprocess.InProcessServerBuilder) InProcessChannelBuilder(io.grpc.inprocess.InProcessChannelBuilder) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ManagedChannel(io.grpc.ManagedChannel) EnumMap(java.util.EnumMap) StreamObserver(io.grpc.stub.StreamObserver) CallStreamObserver(io.grpc.stub.CallStreamObserver) ThrowingFunction(org.apache.beam.fn.harness.fn.ThrowingFunction) BeamFnApi(org.apache.beam.fn.v1.BeamFnApi) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 17 with StreamObserver

use of io.grpc.stub.StreamObserver in project beam by apache.

the class FnHarnessTest method testLaunchFnHarnessAndTeardownCleanly.

@Test
public void testLaunchFnHarnessAndTeardownCleanly() throws Exception {
    PipelineOptions options = PipelineOptionsFactory.create();
    List<BeamFnApi.LogEntry> logEntries = new ArrayList<>();
    List<BeamFnApi.InstructionResponse> instructionResponses = new ArrayList<>();
    BeamFnLoggingGrpc.BeamFnLoggingImplBase loggingService = new BeamFnLoggingGrpc.BeamFnLoggingImplBase() {

        @Override
        public StreamObserver<BeamFnApi.LogEntry.List> logging(StreamObserver<LogControl> responseObserver) {
            return TestStreams.withOnNext((BeamFnApi.LogEntry.List entries) -> logEntries.addAll(entries.getLogEntriesList())).withOnCompleted(() -> responseObserver.onCompleted()).build();
        }
    };
    BeamFnControlGrpc.BeamFnControlImplBase controlService = new BeamFnControlGrpc.BeamFnControlImplBase() {

        @Override
        public StreamObserver<InstructionResponse> control(StreamObserver<InstructionRequest> responseObserver) {
            CountDownLatch waitForResponses = new CountDownLatch(1);
            options.as(GcsOptions.class).getExecutorService().submit(new Runnable() {

                @Override
                public void run() {
                    responseObserver.onNext(INSTRUCTION_REQUEST);
                    Uninterruptibles.awaitUninterruptibly(waitForResponses);
                    responseObserver.onCompleted();
                }
            });
            return TestStreams.withOnNext(new Consumer<BeamFnApi.InstructionResponse>() {

                @Override
                public void accept(InstructionResponse t) {
                    instructionResponses.add(t);
                    waitForResponses.countDown();
                }
            }).withOnCompleted(waitForResponses::countDown).build();
        }
    };
    Server loggingServer = ServerBuilder.forPort(0).addService(loggingService).build();
    loggingServer.start();
    try {
        Server controlServer = ServerBuilder.forPort(0).addService(controlService).build();
        controlServer.start();
        try {
            BeamFnApi.ApiServiceDescriptor loggingDescriptor = BeamFnApi.ApiServiceDescriptor.newBuilder().setId("1L").setUrl("localhost:" + loggingServer.getPort()).build();
            BeamFnApi.ApiServiceDescriptor controlDescriptor = BeamFnApi.ApiServiceDescriptor.newBuilder().setId("2L").setUrl("localhost:" + controlServer.getPort()).build();
            FnHarness.main(options, loggingDescriptor, controlDescriptor);
            assertThat(instructionResponses, contains(INSTRUCTION_RESPONSE));
        } finally {
            controlServer.shutdownNow();
        }
    } finally {
        loggingServer.shutdownNow();
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) BeamFnLoggingGrpc(org.apache.beam.fn.v1.BeamFnLoggingGrpc) Server(io.grpc.Server) BeamFnApi(org.apache.beam.fn.v1.BeamFnApi) ArrayList(java.util.ArrayList) InstructionResponse(org.apache.beam.fn.v1.BeamFnApi.InstructionResponse) CountDownLatch(java.util.concurrent.CountDownLatch) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) ArrayList(java.util.ArrayList) List(java.util.List) BeamFnControlGrpc(org.apache.beam.fn.v1.BeamFnControlGrpc) Test(org.junit.Test)

Example 18 with StreamObserver

use of io.grpc.stub.StreamObserver in project grpc-java by grpc.

the class DetailErrorSample method asyncCall.

void asyncCall() {
    GreeterStub stub = GreeterGrpc.newStub(channel);
    HelloRequest request = HelloRequest.newBuilder().build();
    final CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<HelloReply> responseObserver = new StreamObserver<HelloReply>() {

        @Override
        public void onNext(HelloReply value) {
        // Won't be called.
        }

        @Override
        public void onError(Throwable t) {
            verifyErrorReply(t);
            latch.countDown();
        }

        @Override
        public void onCompleted() {
        // Won't be called, since the server in this example always fails.
        }
    };
    stub.sayHello(request, responseObserver);
    if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
        throw new RuntimeException("timeout!");
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) GreeterStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterStub) HelloRequest(io.grpc.examples.helloworld.HelloRequest) CountDownLatch(java.util.concurrent.CountDownLatch) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 19 with StreamObserver

use of io.grpc.stub.StreamObserver in project grpc-java by grpc.

the class HeaderServerInterceptorTest method setUp.

@Before
public void setUp() throws Exception {
    String uniqueServerName = "fake server for " + getClass();
    GreeterImplBase greeterImplBase = new GreeterImplBase() {

        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
            responseObserver.onNext(HelloReply.getDefaultInstance());
            responseObserver.onCompleted();
        }
    };
    fakeServer = InProcessServerBuilder.forName(uniqueServerName).addService(ServerInterceptors.intercept(greeterImplBase, new HeaderServerInterceptor())).directExecutor().build().start();
    inProcessChannel = InProcessChannelBuilder.forName(uniqueServerName).directExecutor().build();
}
Also used : GreeterImplBase(io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase) StreamObserver(io.grpc.stub.StreamObserver) HelloRequest(io.grpc.examples.helloworld.HelloRequest) Before(org.junit.Before)

Example 20 with StreamObserver

use of io.grpc.stub.StreamObserver in project grpc-java by grpc.

the class RouteGuideClientTest method getFeature.

/**
   * Example for testing blocking unary call.
   */
@Test
public void getFeature() {
    Point requestPoint = Point.newBuilder().setLatitude(-1).setLongitude(-1).build();
    Point responsePoint = Point.newBuilder().setLatitude(-123).setLongitude(-123).build();
    final AtomicReference<Point> pointDelivered = new AtomicReference<Point>();
    final Feature responseFeature = Feature.newBuilder().setName("dummyFeature").setLocation(responsePoint).build();
    // implement the fake service
    RouteGuideImplBase getFeatureImpl = new RouteGuideImplBase() {

        @Override
        public void getFeature(Point point, StreamObserver<Feature> responseObserver) {
            pointDelivered.set(point);
            responseObserver.onNext(responseFeature);
            responseObserver.onCompleted();
        }
    };
    serviceRegistry.addService(getFeatureImpl);
    client.getFeature(-1, -1);
    assertEquals(requestPoint, pointDelivered.get());
    verify(testHelper).onMessage(responseFeature);
    verify(testHelper, never()).onRpcError(any(Throwable.class));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AtomicReference(java.util.concurrent.atomic.AtomicReference) RouteGuideImplBase(io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Aggregations

StreamObserver (io.grpc.stub.StreamObserver)34 Test (org.junit.Test)24 CountDownLatch (java.util.concurrent.CountDownLatch)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 RouteGuideImplBase (io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideImplBase)10 StatusRuntimeException (io.grpc.StatusRuntimeException)9 ManagedChannel (io.grpc.ManagedChannel)8 Server (io.grpc.Server)8 ArrayList (java.util.ArrayList)8 BeamFnApi (org.apache.beam.fn.v1.BeamFnApi)7 CallStreamObserver (io.grpc.stub.CallStreamObserver)5 Status (io.grpc.Status)4 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ByteString (com.google.protobuf.ByteString)3 HelloRequest (io.grpc.examples.helloworld.HelloRequest)3 ServerCallStreamObserver (io.grpc.stub.ServerCallStreamObserver)3 SimpleRequest (io.grpc.testing.integration.Messages.SimpleRequest)3 StreamingInputCallRequest (io.grpc.testing.integration.Messages.StreamingInputCallRequest)3