Search in sources :

Example 6 with SingleMessageProducer

use of io.grpc.internal.testing.SingleMessageProducer in project grpc-java by grpc.

the class DelayedStreamTest method listener_noQueued.

@Test
public void listener_noQueued() {
    final Metadata headers = new Metadata();
    final InputStream message = mock(InputStream.class);
    final SingleMessageProducer producer = new SingleMessageProducer(message);
    final Metadata trailers = new Metadata();
    final Status status = Status.UNKNOWN.withDescription("unique status");
    stream.start(listener);
    callMeMaybe(stream.setStream(realStream));
    verify(realStream).start(listenerCaptor.capture());
    ClientStreamListener delayedListener = listenerCaptor.getValue();
    delayedListener.onReady();
    verify(listener).onReady();
    delayedListener.headersRead(headers);
    verify(listener).headersRead(headers);
    delayedListener.messagesAvailable(producer);
    verify(listener).messagesAvailable(producer);
    delayedListener.closed(status, RpcProgress.PROCESSED, trailers);
    verify(listener).closed(status, RpcProgress.PROCESSED, trailers);
}
Also used : Status(io.grpc.Status) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Metadata(io.grpc.Metadata) SingleMessageProducer(io.grpc.internal.testing.SingleMessageProducer) Test(org.junit.Test)

Example 7 with SingleMessageProducer

use of io.grpc.internal.testing.SingleMessageProducer in project grpc-java by grpc.

the class ClientCallImplTest method cancelInOnMessageShouldInvokeStreamCancel.

@Test
public void cancelInOnMessageShouldInvokeStreamCancel() throws Exception {
    final ClientCallImpl<Void, Void> call = new ClientCallImpl<>(method, MoreExecutors.directExecutor(), baseCallOptions, clientStreamProvider, deadlineCancellationExecutor, channelCallTracer, configSelector);
    final Exception cause = new Exception();
    ClientCall.Listener<Void> callListener = new ClientCall.Listener<Void>() {

        @Override
        public void onMessage(Void message) {
            call.cancel("foo", cause);
        }
    };
    call.start(callListener, new Metadata());
    call.halfClose();
    call.request(1);
    verify(stream).start(listenerArgumentCaptor.capture());
    ClientStreamListener streamListener = listenerArgumentCaptor.getValue();
    streamListener.onReady();
    streamListener.headersRead(new Metadata());
    streamListener.messagesAvailable(new SingleMessageProducer(new ByteArrayInputStream(new byte[0])));
    verify(stream).cancel(statusCaptor.capture());
    Status status = statusCaptor.getValue();
    assertEquals(Status.CANCELLED.getCode(), status.getCode());
    assertEquals("foo", status.getDescription());
    assertSame(cause, status.getCause());
}
Also used : Status(io.grpc.Status) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientCall(io.grpc.ClientCall) Metadata(io.grpc.Metadata) SingleMessageProducer(io.grpc.internal.testing.SingleMessageProducer) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with SingleMessageProducer

use of io.grpc.internal.testing.SingleMessageProducer in project grpc-java by grpc.

the class ServerCallImplTest method streamListener_unexpectedRuntimeException.

@Test
public void streamListener_unexpectedRuntimeException() {
    ServerStreamListenerImpl<Long> streamListener = new ServerCallImpl.ServerStreamListenerImpl<>(call, callListener, context);
    doThrow(new RuntimeException("unexpected exception")).when(callListener).onMessage(any(Long.class));
    InputStream inputStream = UNARY_METHOD.streamRequest(1234L);
    thrown.expect(RuntimeException.class);
    thrown.expectMessage("unexpected exception");
    streamListener.messagesAvailable(new SingleMessageProducer(inputStream));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SingleMessageProducer(io.grpc.internal.testing.SingleMessageProducer) ServerStreamListenerImpl(io.grpc.internal.ServerCallImpl.ServerStreamListenerImpl) Test(org.junit.Test)

Example 9 with SingleMessageProducer

use of io.grpc.internal.testing.SingleMessageProducer in project grpc-java by grpc.

the class ServerCallImplTest method streamListener_messageRead.

@Test
public void streamListener_messageRead() {
    ServerStreamListenerImpl<Long> streamListener = new ServerCallImpl.ServerStreamListenerImpl<>(call, callListener, context);
    streamListener.messagesAvailable(new SingleMessageProducer(UNARY_METHOD.streamRequest(1234L)));
    verify(callListener).onMessage(1234L);
}
Also used : SingleMessageProducer(io.grpc.internal.testing.SingleMessageProducer) ServerStreamListenerImpl(io.grpc.internal.ServerCallImpl.ServerStreamListenerImpl) Test(org.junit.Test)

Example 10 with SingleMessageProducer

use of io.grpc.internal.testing.SingleMessageProducer in project grpc-java by grpc.

the class ServerImplTest method testCallContextIsBoundInListenerCallbacks.

@Test
public void testCallContextIsBoundInListenerCallbacks() throws Exception {
    createAndStartServer();
    final AtomicBoolean onReadyCalled = new AtomicBoolean(false);
    final AtomicBoolean onMessageCalled = new AtomicBoolean(false);
    final AtomicBoolean onHalfCloseCalled = new AtomicBoolean(false);
    final AtomicBoolean onCancelCalled = new AtomicBoolean(false);
    mutableFallbackRegistry.addService(ServerServiceDefinition.builder(new ServiceDescriptor("Waiter", METHOD)).addMethod(METHOD, new ServerCallHandler<String, Integer>() {

        @Override
        public ServerCall.Listener<String> startCall(ServerCall<String, Integer> call, Metadata headers) {
            // Check that the current context is a descendant of SERVER_CONTEXT
            final Context initial = Context.current();
            assertEquals("yes", SERVER_ONLY.get(initial));
            assertNotSame(SERVER_CONTEXT, initial);
            assertFalse(initial.isCancelled());
            return new ServerCall.Listener<String>() {

                @Override
                public void onReady() {
                    checkContext();
                    onReadyCalled.set(true);
                }

                @Override
                public void onMessage(String message) {
                    checkContext();
                    onMessageCalled.set(true);
                }

                @Override
                public void onHalfClose() {
                    checkContext();
                    onHalfCloseCalled.set(true);
                }

                @Override
                public void onCancel() {
                    checkContext();
                    onCancelCalled.set(true);
                }

                @Override
                public void onComplete() {
                    checkContext();
                }

                private void checkContext() {
                    // Check that the bound context is the same as the initial one.
                    assertSame(initial, Context.current());
                }
            };
        }
    }).build());
    ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport());
    transportListener.transportReady(Attributes.EMPTY);
    Metadata requestHeaders = new Metadata();
    StatsTraceContext statsTraceCtx = StatsTraceContext.newServerContext(streamTracerFactories, "Waitier/serve", requestHeaders);
    when(stream.statsTraceContext()).thenReturn(statsTraceCtx);
    transportListener.streamCreated(stream, "Waiter/serve", requestHeaders);
    verify(stream).setListener(streamListenerCaptor.capture());
    ServerStreamListener streamListener = streamListenerCaptor.getValue();
    assertNotNull(streamListener);
    streamListener.onReady();
    assertEquals(1, executor.runDueTasks());
    assertTrue(onReadyCalled.get());
    streamListener.messagesAvailable(new SingleMessageProducer(new ByteArrayInputStream(new byte[0])));
    assertEquals(1, executor.runDueTasks());
    assertTrue(onMessageCalled.get());
    streamListener.halfClosed();
    assertEquals(1, executor.runDueTasks());
    assertTrue(onHalfCloseCalled.get());
    streamListener.closed(Status.CANCELLED);
    assertEquals(1, executor.numPendingTasks(CONTEXT_CLOSER_TASK_FITLER));
    assertEquals(2, executor.runDueTasks());
    assertTrue(onCancelCalled.get());
    // Close should never be called if asserts in listener pass.
    verify(stream, times(0)).close(isA(Status.class), ArgumentMatchers.<Metadata>isNotNull());
}
Also used : Context(io.grpc.Context) Status(io.grpc.Status) JumpToApplicationThreadServerStreamListener(io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener) Listener(io.grpc.ServerCall.Listener) Metadata(io.grpc.Metadata) SingleMessageProducer(io.grpc.internal.testing.SingleMessageProducer) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServiceDescriptor(io.grpc.ServiceDescriptor) ServerCall(io.grpc.ServerCall) ByteArrayInputStream(java.io.ByteArrayInputStream) JumpToApplicationThreadServerStreamListener(io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener) Test(org.junit.Test)

Aggregations

SingleMessageProducer (io.grpc.internal.testing.SingleMessageProducer)10 Test (org.junit.Test)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Metadata (io.grpc.Metadata)7 Status (io.grpc.Status)7 InputStream (java.io.InputStream)4 Context (io.grpc.Context)3 ServerStreamListenerImpl (io.grpc.internal.ServerCallImpl.ServerStreamListenerImpl)3 ClientCall (io.grpc.ClientCall)2 ServerCall (io.grpc.ServerCall)2 Listener (io.grpc.ServerCall.Listener)2 ServiceDescriptor (io.grpc.ServiceDescriptor)2 JumpToApplicationThreadServerStreamListener (io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Compressor (io.grpc.Compressor)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1