Search in sources :

Example 96 with StreamObserver

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

the class BeamFnDataGrpcMultiplexerTest method testInboundObserverBlocksTillConsumerConnects.

@Test
public void testInboundObserverBlocksTillConsumerConnects() throws Exception {
    Collection<BeamFnApi.Elements> outboundValues = new ArrayList<>();
    Collection<BeamFnApi.Elements.Data> inboundValues = new ArrayList<>();
    BeamFnDataGrpcMultiplexer multiplexer = new BeamFnDataGrpcMultiplexer(DESCRIPTOR, (StreamObserver<BeamFnApi.Elements> inboundObserver) -> TestStreams.withOnNext(outboundValues::add).build());
    ExecutorService executorService = Executors.newCachedThreadPool();
    executorService.submit(new Runnable() {

        @Override
        public void run() {
            // Purposefully sleep to simulate a delay in a consumer connecting.
            Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
            multiplexer.futureForKey(OUTPUT_LOCATION).complete(inboundValues::add);
        }
    });
    multiplexer.getInboundObserver().onNext(ELEMENTS);
    assertTrue(multiplexer.consumers.containsKey(OUTPUT_LOCATION));
    // Ensure that when we see a terminal Elements object, we remove the consumer
    multiplexer.getInboundObserver().onNext(TERMINAL_ELEMENTS);
    assertFalse(multiplexer.consumers.containsKey(OUTPUT_LOCATION));
    // Assert that normal and terminal Elements are passed to the consumer
    assertThat(inboundValues, contains(ELEMENTS.getData(0), TERMINAL_ELEMENTS.getData(0)));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) BeamFnApi(org.apache.beam.fn.v1.BeamFnApi) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 97 with StreamObserver

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

the class TestGrpcRootRangeService method testCreateNamespaceFailure.

@Test
public void testCreateNamespaceFailure() throws Exception {
    RangeStoreImpl rangeService = mock(RangeStoreImpl.class);
    GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService);
    CreateNamespaceResponse createResp = CreateNamespaceResponse.newBuilder().setCode(StatusCode.INTERNAL_SERVER_ERROR).build();
    CreateNamespaceRequest createReq = createCreateNamespaceRequest(nsName, namespaceConf);
    when(rangeService.createNamespace(createReq)).thenReturn(FutureUtils.exception(CAUSE));
    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());
    assertEquals(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 98 with StreamObserver

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

the class TestGrpcRootRangeService method testDeleteNamespaceFailure.

@Test
public void testDeleteNamespaceFailure() throws Exception {
    RangeStoreImpl rangeService = mock(RangeStoreImpl.class);
    GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService);
    DeleteNamespaceResponse deleteResp = DeleteNamespaceResponse.newBuilder().setCode(StatusCode.INTERNAL_SERVER_ERROR).build();
    DeleteNamespaceRequest deleteReq = createDeleteNamespaceRequest(nsName);
    when(rangeService.deleteNamespace(deleteReq)).thenReturn(FutureUtils.exception(CAUSE));
    AtomicReference<DeleteNamespaceResponse> resultHolder = new AtomicReference<>();
    AtomicReference<Throwable> exceptionHolder = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<DeleteNamespaceResponse> streamObserver = new StreamObserver<DeleteNamespaceResponse>() {

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

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

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

Example 99 with StreamObserver

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

the class TestGrpcRootRangeService method testGetStreamFailure.

@Test
public void testGetStreamFailure() throws Exception {
    RangeStoreImpl rangeService = mock(RangeStoreImpl.class);
    GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService);
    GetStreamResponse getResp = GetStreamResponse.newBuilder().setCode(StatusCode.INTERNAL_SERVER_ERROR).build();
    GetStreamRequest getReq = createGetStreamRequest(nsName, streamName);
    when(rangeService.getStream(getReq)).thenReturn(FutureUtils.exception(CAUSE));
    AtomicReference<GetStreamResponse> resultHolder = new AtomicReference<>();
    AtomicReference<Throwable> exceptionHolder = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<GetStreamResponse> streamObserver = new StreamObserver<GetStreamResponse>() {

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

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

        @Override
        public void onCompleted() {
            latch.countDown();
        }
    };
    grpcService.getStream(GetStreamRequest.newBuilder().setStreamName(StreamName.newBuilder().setColName(nsName).setStreamName(streamName)).build(), streamObserver);
    latch.await();
    assertNull(exceptionHolder.get());
    assertNotNull(resultHolder.get());
    assertEquals(getResp, resultHolder.get());
    verify(rangeService, times(1)).getStream(getReq);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AtomicReference(java.util.concurrent.atomic.AtomicReference) GetStreamResponse(org.apache.bookkeeper.stream.proto.storage.GetStreamResponse) GetStreamRequest(org.apache.bookkeeper.stream.proto.storage.GetStreamRequest) ProtoUtils.createGetStreamRequest(org.apache.bookkeeper.stream.protocol.util.ProtoUtils.createGetStreamRequest) CountDownLatch(java.util.concurrent.CountDownLatch) RangeStoreImpl(org.apache.bookkeeper.stream.storage.impl.RangeStoreImpl) Test(org.junit.Test)

Example 100 with StreamObserver

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

the class TestGrpcRootRangeService method testDeleteStreamSuccess.

@Test
public void testDeleteStreamSuccess() throws Exception {
    RangeStoreImpl rangeService = mock(RangeStoreImpl.class);
    GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService);
    DeleteStreamResponse deleteResp = DeleteStreamResponse.newBuilder().setCode(StatusCode.SUCCESS).build();
    DeleteStreamRequest deleteReq = createDeleteStreamRequest(nsName, streamName);
    when(rangeService.deleteStream(deleteReq)).thenReturn(CompletableFuture.completedFuture(deleteResp));
    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());
    assertTrue(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)

Aggregations

StreamObserver (io.grpc.stub.StreamObserver)133 Test (org.junit.Test)95 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