Search in sources :

Example 11 with StreamObserver

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project bookkeeper by apache.

the class TestGrpcRootRangeService method testDeleteStreamFailure.

@Test
public void testDeleteStreamFailure() throws Exception {
    RangeStoreImpl rangeService = mock(RangeStoreImpl.class);
    GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService);
    DeleteStreamResponse deleteResp = DeleteStreamResponse.newBuilder().setCode(StatusCode.INTERNAL_SERVER_ERROR).build();
    DeleteStreamRequest deleteReq = createDeleteStreamRequest(nsName, streamName);
    when(rangeService.deleteStream(deleteReq)).thenReturn(FutureUtils.exception(CAUSE));
    AtomicReference<DeleteStreamResponse> resultHolder = new AtomicReference<>();
    AtomicReference<Throwable> exceptionHolder = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<DeleteStreamResponse> streamObserver = new StreamObserver<DeleteStreamResponse>() {

        @Override
        public void onNext(DeleteStreamResponse resp) {
            resultHolder.set(resp);
        }

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

        @Override
        public void onCompleted() {
            latch.countDown();
        }
    };
    grpcService.deleteStream(DeleteStreamRequest.newBuilder().setColName(nsName).setName(streamName).build(), streamObserver);
    latch.await();
    assertNull(exceptionHolder.get());
    assertNotNull(resultHolder.get());
    assertEquals(deleteResp, resultHolder.get());
    verify(rangeService, times(1)).deleteStream(deleteReq);
}
Also used : DeleteStreamRequest(org.apache.bookkeeper.stream.proto.storage.DeleteStreamRequest) ProtoUtils.createDeleteStreamRequest(org.apache.bookkeeper.stream.protocol.util.ProtoUtils.createDeleteStreamRequest) StreamObserver(io.grpc.stub.StreamObserver) DeleteStreamResponse(org.apache.bookkeeper.stream.proto.storage.DeleteStreamResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) RangeStoreImpl(org.apache.bookkeeper.stream.storage.impl.RangeStoreImpl) Test(org.junit.Test)

Example 12 with StreamObserver

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project bookkeeper by apache.

the class TestGrpcRootRangeService method testCreateNamespaceSuccess.

// 
// Test Namespace API
// 
@Test
public void testCreateNamespaceSuccess() throws Exception {
    RangeStoreImpl rangeService = mock(RangeStoreImpl.class);
    GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService);
    CreateNamespaceResponse createResp = CreateNamespaceResponse.newBuilder().setCode(StatusCode.SUCCESS).setColProps(namespaceProps).build();
    CreateNamespaceRequest createReq = createCreateNamespaceRequest(nsName, namespaceConf);
    when(rangeService.createNamespace(createReq)).thenReturn(CompletableFuture.completedFuture(createResp));
    AtomicReference<CreateNamespaceResponse> resultHolder = new AtomicReference<>();
    AtomicReference<Throwable> exceptionHolder = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<CreateNamespaceResponse> streamObserver = new StreamObserver<CreateNamespaceResponse>() {

        @Override
        public void onNext(CreateNamespaceResponse resp) {
            resultHolder.set(resp);
        }

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

        @Override
        public void onCompleted() {
            latch.countDown();
        }
    };
    grpcService.createNamespace(CreateNamespaceRequest.newBuilder().setName(nsName).setColConf(namespaceConf).build(), streamObserver);
    latch.await();
    assertNull(exceptionHolder.get());
    assertNotNull(resultHolder.get());
    assertTrue(createResp == resultHolder.get());
    verify(rangeService, times(1)).createNamespace(createReq);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) RangeStoreImpl(org.apache.bookkeeper.stream.storage.impl.RangeStoreImpl) CreateNamespaceResponse(org.apache.bookkeeper.stream.proto.storage.CreateNamespaceResponse) ProtoUtils.createCreateNamespaceRequest(org.apache.bookkeeper.stream.protocol.util.ProtoUtils.createCreateNamespaceRequest) CreateNamespaceRequest(org.apache.bookkeeper.stream.proto.storage.CreateNamespaceRequest) Test(org.junit.Test)

Example 13 with StreamObserver

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project bookkeeper by apache.

the class TestStorageContainerResponseHandler method testInternalError.

@SuppressWarnings("unchecked")
@Test
public void testInternalError() throws Exception {
    StreamObserver<StorageContainerResponse> observer = mock(StreamObserver.class);
    AtomicReference<StorageContainerResponse> responseHolder = new AtomicReference<>(null);
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer((Answer<Void>) invocation -> {
        StorageContainerResponse resp = invocation.getArgument(0);
        responseHolder.set(resp);
        latch.countDown();
        return null;
    }).when(observer).onNext(any(StorageContainerResponse.class));
    StorageContainerResponseHandler handler = StorageContainerResponseHandler.of(observer);
    StorageException exception = new StorageException("test-exception");
    handler.accept(null, exception);
    verify(observer, times(1)).onNext(any());
    verify(observer, times(1)).onCompleted();
    verify(observer, times(0)).onError(any());
    latch.await();
    assertNotNull(responseHolder.get());
    assertEquals(StatusCode.INTERNAL_SERVER_ERROR, responseHolder.get().getCode());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assert.assertNotNull(org.junit.Assert.assertNotNull) StatusException(io.grpc.StatusException) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Mockito.verify(org.mockito.Mockito.verify) StatusRuntimeException(io.grpc.StatusRuntimeException) CountDownLatch(java.util.concurrent.CountDownLatch) StorageContainerResponse(org.apache.bookkeeper.stream.proto.storage.StorageContainerResponse) Answer(org.mockito.stubbing.Answer) StreamObserver(io.grpc.stub.StreamObserver) StorageException(org.apache.bookkeeper.stream.storage.exceptions.StorageException) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Status(io.grpc.Status) Assert.assertEquals(org.junit.Assert.assertEquals) StatusCode(org.apache.bookkeeper.stream.proto.storage.StatusCode) Mockito.mock(org.mockito.Mockito.mock) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StorageException(org.apache.bookkeeper.stream.storage.exceptions.StorageException) StorageContainerResponse(org.apache.bookkeeper.stream.proto.storage.StorageContainerResponse) Test(org.junit.Test)

Example 14 with StreamObserver

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project spring_boot by hryou0922.

the class RouteGuideClientTest method recordRoute.

/**
 * Example for testing async client-streaming.
 */
@Test
public void recordRoute() throws Exception {
    client.setRandom(noRandomness);
    Point point1 = Point.newBuilder().setLatitude(1).setLongitude(1).build();
    Point point2 = Point.newBuilder().setLatitude(2).setLongitude(2).build();
    Point point3 = Point.newBuilder().setLatitude(3).setLongitude(3).build();
    Feature requestFeature1 = Feature.newBuilder().setLocation(point1).build();
    Feature requestFeature2 = Feature.newBuilder().setLocation(point2).build();
    Feature requestFeature3 = Feature.newBuilder().setLocation(point3).build();
    final List<Feature> features = Arrays.asList(requestFeature1, requestFeature2, requestFeature3);
    final List<Point> pointsDelivered = new ArrayList<Point>();
    final RouteSummary fakeResponse = RouteSummary.newBuilder().setPointCount(7).setFeatureCount(8).setDistance(9).setElapsedTime(10).build();
    // implement the fake service
    RouteGuideImplBase recordRouteImpl = new RouteGuideImplBase() {

        @Override
        public StreamObserver<Point> recordRoute(final StreamObserver<RouteSummary> responseObserver) {
            StreamObserver<Point> requestObserver = new StreamObserver<Point>() {

                @Override
                public void onNext(Point value) {
                    pointsDelivered.add(value);
                }

                @Override
                public void onError(Throwable t) {
                }

                @Override
                public void onCompleted() {
                    responseObserver.onNext(fakeResponse);
                    responseObserver.onCompleted();
                }
            };
            return requestObserver;
        }
    };
    serviceRegistry.addService(recordRouteImpl);
    // send requestFeature1, requestFeature2, requestFeature3, and then requestFeature1 again
    client.recordRoute(features, 4);
    assertEquals(Arrays.asList(requestFeature1.getLocation(), requestFeature2.getLocation(), requestFeature3.getLocation(), requestFeature1.getLocation()), pointsDelivered);
    verify(testHelper).onMessage(fakeResponse);
    verify(testHelper, never()).onRpcError(any(Throwable.class));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) RouteSummary(com.hry.spring.grpc.stream.RouteSummary) ArrayList(java.util.ArrayList) Point(com.hry.spring.grpc.stream.Point) Feature(com.hry.spring.grpc.stream.Feature) RouteGuideImplBase(com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Example 15 with StreamObserver

use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project spring_boot by hryou0922.

the class RouteGuideClientTest method listFeatures.

/**
 * Example for testing blocking server-streaming.
 */
@Test
public void listFeatures() {
    final Feature responseFeature1 = Feature.newBuilder().setName("feature 1").build();
    final Feature responseFeature2 = Feature.newBuilder().setName("feature 2").build();
    final AtomicReference<Rectangle> rectangleDelivered = new AtomicReference<Rectangle>();
    // implement the fake service
    RouteGuideImplBase listFeaturesImpl = new RouteGuideImplBase() {

        @Override
        public void listFeatures(Rectangle rectangle, StreamObserver<Feature> responseObserver) {
            rectangleDelivered.set(rectangle);
            // send two response messages
            responseObserver.onNext(responseFeature1);
            responseObserver.onNext(responseFeature2);
            // complete the response
            responseObserver.onCompleted();
        }
    };
    serviceRegistry.addService(listFeaturesImpl);
    client.listFeatures(1, 2, 3, 4);
    assertEquals(Rectangle.newBuilder().setLo(Point.newBuilder().setLatitude(1).setLongitude(2).build()).setHi(Point.newBuilder().setLatitude(3).setLongitude(4).build()).build(), rectangleDelivered.get());
    verify(testHelper).onMessage(responseFeature1);
    verify(testHelper).onMessage(responseFeature2);
    verify(testHelper, never()).onRpcError(any(Throwable.class));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Rectangle(com.hry.spring.grpc.stream.Rectangle) AtomicReference(java.util.concurrent.atomic.AtomicReference) Feature(com.hry.spring.grpc.stream.Feature) RouteGuideImplBase(com.hry.spring.grpc.stream.RouteGuideGrpc.RouteGuideImplBase) Test(org.junit.Test)

Aggregations

StreamObserver (io.grpc.stub.StreamObserver)130 Test (org.junit.Test)93 CountDownLatch (java.util.concurrent.CountDownLatch)50 ArrayList (java.util.ArrayList)47 AtomicReference (java.util.concurrent.atomic.AtomicReference)38 StreamObserver (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver)27 StatusRuntimeException (io.grpc.StatusRuntimeException)26 Status (io.grpc.Status)20 List (java.util.List)18 BeamFnApi (org.apache.beam.model.fnexecution.v1.BeamFnApi)18 ManagedChannel (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel)18 CompletableFuture (java.util.concurrent.CompletableFuture)17 ExecutorService (java.util.concurrent.ExecutorService)16 SegmentId (io.pravega.controller.stream.api.grpc.v1.Controller.SegmentId)14 ServerRequest (io.pravega.controller.stream.api.grpc.v1.Controller.ServerRequest)14 VisibleForTesting (com.google.common.annotations.VisibleForTesting)12 Strings (com.google.common.base.Strings)12 Throwables (com.google.common.base.Throwables)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 AuthHandler (io.pravega.auth.AuthHandler)12