Search in sources :

Example 6 with NoopClientCall

use of io.grpc.internal.NoopClientCall in project grpc-java by grpc.

the class ClientCallsTest method blockingResponseStreamFailed.

@Test
public void blockingResponseStreamFailed() throws Exception {
    final AtomicReference<ClientCall.Listener<String>> listener = new AtomicReference<>();
    NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {

        @Override
        public void start(io.grpc.ClientCall.Listener<String> responseListener, Metadata headers) {
            listener.set(responseListener);
        }
    };
    Integer req = 2;
    Iterator<String> iter = ClientCalls.blockingServerStreamingCall(call, req);
    Metadata trailers = new Metadata();
    listener.get().onClose(Status.INTERNAL, trailers);
    try {
        iter.next();
        fail("Should fail");
    } catch (Exception e) {
        Status status = Status.fromThrowable(e);
        assertEquals(Status.INTERNAL, status);
        Metadata metadata = Status.trailersFromThrowable(e);
        assertSame(trailers, metadata);
    }
}
Also used : Status(io.grpc.Status) SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) NoopClientCall(io.grpc.internal.NoopClientCall) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) CancellationException(java.util.concurrent.CancellationException) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 7 with NoopClientCall

use of io.grpc.internal.NoopClientCall in project grpc-java by grpc.

the class BinlogHelperTest method clientInterceptor_trailersOnlyResponseLogsPeerAddress.

@Test
public void clientInterceptor_trailersOnlyResponseLogsPeerAddress() throws Exception {
    final AtomicReference<ClientCall.Listener<byte[]>> interceptedListener = new AtomicReference<>();
    // capture these manually because ClientCall can not be mocked
    final AtomicReference<Metadata> actualClientInitial = new AtomicReference<>();
    final AtomicReference<Object> actualRequest = new AtomicReference<>();
    Channel channel = new Channel() {

        @Override
        public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
            return new NoopClientCall<RequestT, ResponseT>() {

                @Override
                @SuppressWarnings("unchecked")
                public void start(Listener<ResponseT> responseListener, Metadata headers) {
                    interceptedListener.set((Listener<byte[]>) responseListener);
                    actualClientInitial.set(headers);
                }

                @Override
                public void sendMessage(RequestT message) {
                    actualRequest.set(message);
                }

                @Override
                public Attributes getAttributes() {
                    return Attributes.newBuilder().set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, peer).build();
                }
            };
        }

        @Override
        public String authority() {
            return "the-authority";
        }
    };
    @SuppressWarnings("unchecked") ClientCall.Listener<byte[]> mockListener = mock(ClientCall.Listener.class);
    MethodDescriptor<byte[], byte[]> method = MethodDescriptor.<byte[], byte[]>newBuilder().setType(MethodType.UNKNOWN).setFullMethodName("service/method").setRequestMarshaller(BYTEARRAY_MARSHALLER).setResponseMarshaller(BYTEARRAY_MARSHALLER).build();
    ClientCall<byte[], byte[]> interceptedCall = new BinlogHelper(mockSinkWriter).getClientInterceptor(CALL_ID).interceptCall(method, CallOptions.DEFAULT.withDeadlineAfter(1, TimeUnit.SECONDS), channel);
    Metadata clientInitial = new Metadata();
    interceptedCall.start(mockListener, clientInitial);
    verify(mockSinkWriter).logClientHeader(/*seq=*/
    eq(1L), anyString(), anyString(), any(Duration.class), any(Metadata.class), eq(Logger.LOGGER_CLIENT), eq(CALL_ID), ArgumentMatchers.<SocketAddress>isNull());
    verifyNoMoreInteractions(mockSinkWriter);
    // trailer only response
    {
        Status status = Status.INTERNAL.withDescription("some description");
        Metadata trailers = new Metadata();
        interceptedListener.get().onClose(status, trailers);
        verify(mockSinkWriter).logTrailer(/*seq=*/
        eq(2L), same(status), same(trailers), eq(Logger.LOGGER_CLIENT), eq(CALL_ID), same(peer));
        verifyNoMoreInteractions(mockSinkWriter);
        verify(mockListener).onClose(same(status), same(trailers));
    }
}
Also used : Status(io.grpc.Status) Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(com.google.protobuf.Duration) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) NoopClientCall(io.grpc.internal.NoopClientCall) ClientCall(io.grpc.ClientCall) NoopClientCall(io.grpc.internal.NoopClientCall) Test(org.junit.Test)

Example 8 with NoopClientCall

use of io.grpc.internal.NoopClientCall in project grpc-java by grpc.

the class BinlogHelperTest method clientInterceptor.

@Test
public void clientInterceptor() throws Exception {
    final AtomicReference<ClientCall.Listener<byte[]>> interceptedListener = new AtomicReference<>();
    // capture these manually because ClientCall can not be mocked
    final AtomicReference<Metadata> actualClientInitial = new AtomicReference<>();
    final AtomicReference<Object> actualRequest = new AtomicReference<>();
    final SettableFuture<Void> halfCloseCalled = SettableFuture.create();
    final SettableFuture<Void> cancelCalled = SettableFuture.create();
    Channel channel = new Channel() {

        @Override
        public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
            return new NoopClientCall<RequestT, ResponseT>() {

                @Override
                @SuppressWarnings("unchecked")
                public void start(Listener<ResponseT> responseListener, Metadata headers) {
                    interceptedListener.set((Listener<byte[]>) responseListener);
                    actualClientInitial.set(headers);
                }

                @Override
                public void sendMessage(RequestT message) {
                    actualRequest.set(message);
                }

                @Override
                public void cancel(String message, Throwable cause) {
                    cancelCalled.set(null);
                }

                @Override
                public void halfClose() {
                    halfCloseCalled.set(null);
                }

                @Override
                public Attributes getAttributes() {
                    return Attributes.newBuilder().set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, peer).build();
                }
            };
        }

        @Override
        public String authority() {
            return "the-authority";
        }
    };
    @SuppressWarnings("unchecked") ClientCall.Listener<byte[]> mockListener = mock(ClientCall.Listener.class);
    MethodDescriptor<byte[], byte[]> method = MethodDescriptor.<byte[], byte[]>newBuilder().setType(MethodType.UNKNOWN).setFullMethodName("service/method").setRequestMarshaller(BYTEARRAY_MARSHALLER).setResponseMarshaller(BYTEARRAY_MARSHALLER).build();
    ClientCall<byte[], byte[]> interceptedCall = new BinlogHelper(mockSinkWriter).getClientInterceptor(CALL_ID).interceptCall(method, CallOptions.DEFAULT, channel);
    // send client header
    {
        Metadata clientInitial = new Metadata();
        interceptedCall.start(mockListener, clientInitial);
        verify(mockSinkWriter).logClientHeader(/*seq=*/
        eq(1L), eq("service/method"), eq("the-authority"), ArgumentMatchers.<Duration>isNull(), same(clientInitial), eq(Logger.LOGGER_CLIENT), eq(CALL_ID), ArgumentMatchers.<SocketAddress>isNull());
        verifyNoMoreInteractions(mockSinkWriter);
        assertSame(clientInitial, actualClientInitial.get());
    }
    // receive server header
    {
        Metadata serverInitial = new Metadata();
        interceptedListener.get().onHeaders(serverInitial);
        verify(mockSinkWriter).logServerHeader(/*seq=*/
        eq(2L), same(serverInitial), eq(Logger.LOGGER_CLIENT), eq(CALL_ID), same(peer));
        verifyNoMoreInteractions(mockSinkWriter);
        verify(mockListener).onHeaders(same(serverInitial));
    }
    // send client msg
    {
        byte[] request = "this is a request".getBytes(US_ASCII);
        interceptedCall.sendMessage(request);
        verify(mockSinkWriter).logRpcMessage(/*seq=*/
        eq(3L), eq(EventType.EVENT_TYPE_CLIENT_MESSAGE), same(BYTEARRAY_MARSHALLER), same(request), eq(Logger.LOGGER_CLIENT), eq(CALL_ID));
        verifyNoMoreInteractions(mockSinkWriter);
        assertSame(request, actualRequest.get());
    }
    // client half close
    {
        interceptedCall.halfClose();
        verify(mockSinkWriter).logHalfClose(/*seq=*/
        eq(4L), eq(Logger.LOGGER_CLIENT), eq(CALL_ID));
        halfCloseCalled.get(1, TimeUnit.SECONDS);
        verifyNoMoreInteractions(mockSinkWriter);
    }
    // receive server msg
    {
        byte[] response = "this is a response".getBytes(US_ASCII);
        interceptedListener.get().onMessage(response);
        verify(mockSinkWriter).logRpcMessage(/*seq=*/
        eq(5L), eq(EventType.EVENT_TYPE_SERVER_MESSAGE), same(BYTEARRAY_MARSHALLER), same(response), eq(Logger.LOGGER_CLIENT), eq(CALL_ID));
        verifyNoMoreInteractions(mockSinkWriter);
        verify(mockListener).onMessage(same(response));
    }
    // receive trailer
    {
        Status status = Status.INTERNAL.withDescription("some description");
        Metadata trailers = new Metadata();
        interceptedListener.get().onClose(status, trailers);
        verify(mockSinkWriter).logTrailer(/*seq=*/
        eq(6L), same(status), same(trailers), eq(Logger.LOGGER_CLIENT), eq(CALL_ID), ArgumentMatchers.<SocketAddress>isNull());
        verifyNoMoreInteractions(mockSinkWriter);
        verify(mockListener).onClose(same(status), same(trailers));
    }
    // cancel
    {
        interceptedCall.cancel(null, null);
        verify(mockSinkWriter).logCancel(/*seq=*/
        eq(7L), eq(Logger.LOGGER_CLIENT), eq(CALL_ID));
        cancelCalled.get(1, TimeUnit.SECONDS);
    }
}
Also used : Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) ByteString(com.google.protobuf.ByteString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NoopClientCall(io.grpc.internal.NoopClientCall) ClientCall(io.grpc.ClientCall) NoopClientCall(io.grpc.internal.NoopClientCall) SocketAddress(java.net.SocketAddress) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) InetSocketAddress(java.net.InetSocketAddress) Status(io.grpc.Status) Channel(io.grpc.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Duration(com.google.protobuf.Duration) MethodDescriptor(io.grpc.MethodDescriptor) Test(org.junit.Test)

Example 9 with NoopClientCall

use of io.grpc.internal.NoopClientCall in project grpc-java by grpc.

the class BinlogHelperTest method clientDeadlineLogged_deadlineSetViaCallOption.

@Test
public void clientDeadlineLogged_deadlineSetViaCallOption() {
    MethodDescriptor<byte[], byte[]> method = MethodDescriptor.<byte[], byte[]>newBuilder().setType(MethodType.UNKNOWN).setFullMethodName("service/method").setRequestMarshaller(BYTEARRAY_MARSHALLER).setResponseMarshaller(BYTEARRAY_MARSHALLER).build();
    @SuppressWarnings("unchecked") ClientCall.Listener<byte[]> mockListener = mock(ClientCall.Listener.class);
    ClientCall<byte[], byte[]> call = new BinlogHelper(mockSinkWriter).getClientInterceptor(CALL_ID).interceptCall(method, CallOptions.DEFAULT.withDeadlineAfter(1, TimeUnit.SECONDS), new Channel() {

        @Override
        public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
            return new NoopClientCall<>();
        }

        @Override
        public String authority() {
            return null;
        }
    });
    call.start(mockListener, new Metadata());
    ArgumentCaptor<Duration> callOptTimeoutCaptor = ArgumentCaptor.forClass(Duration.class);
    verify(mockSinkWriter).logClientHeader(anyLong(), AdditionalMatchers.or(ArgumentMatchers.<String>isNull(), anyString()), AdditionalMatchers.or(ArgumentMatchers.<String>isNull(), anyString()), callOptTimeoutCaptor.capture(), any(Metadata.class), any(GrpcLogEntry.Logger.class), anyLong(), AdditionalMatchers.or(ArgumentMatchers.<SocketAddress>isNull(), ArgumentMatchers.<SocketAddress>any()));
    Duration timeout = callOptTimeoutCaptor.getValue();
    assertThat(TimeUnit.SECONDS.toNanos(1) - Durations.toNanos(timeout)).isAtMost(TimeUnit.MILLISECONDS.toNanos(250));
}
Also used : Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) Duration(com.google.protobuf.Duration) CallOptions(io.grpc.CallOptions) ByteString(com.google.protobuf.ByteString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(io.grpc.binarylog.v1.GrpcLogEntry.Logger) ClientCall(io.grpc.ClientCall) NoopClientCall(io.grpc.internal.NoopClientCall) SocketAddress(java.net.SocketAddress) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 10 with NoopClientCall

use of io.grpc.internal.NoopClientCall in project grpc-java by grpc.

the class BinlogHelperTest method clientDeadlineLogged_deadlineSetViaContext.

@Test
public void clientDeadlineLogged_deadlineSetViaContext() throws Exception {
    // important: deadline is read from the ctx where call was created
    final SettableFuture<ClientCall<byte[], byte[]>> callFuture = SettableFuture.create();
    Context.current().withDeadline(Deadline.after(1, TimeUnit.SECONDS), Executors.newSingleThreadScheduledExecutor()).run(new Runnable() {

        @Override
        public void run() {
            MethodDescriptor<byte[], byte[]> method = MethodDescriptor.<byte[], byte[]>newBuilder().setType(MethodType.UNKNOWN).setFullMethodName("service/method").setRequestMarshaller(BYTEARRAY_MARSHALLER).setResponseMarshaller(BYTEARRAY_MARSHALLER).build();
            callFuture.set(new BinlogHelper(mockSinkWriter).getClientInterceptor(CALL_ID).interceptCall(method, CallOptions.DEFAULT.withDeadlineAfter(1, TimeUnit.SECONDS), new Channel() {

                @Override
                public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
                    return new NoopClientCall<>();
                }

                @Override
                public String authority() {
                    return null;
                }
            }));
        }
    });
    @SuppressWarnings("unchecked") ClientCall.Listener<byte[]> mockListener = mock(ClientCall.Listener.class);
    callFuture.get().start(mockListener, new Metadata());
    ArgumentCaptor<Duration> callOptTimeoutCaptor = ArgumentCaptor.forClass(Duration.class);
    verify(mockSinkWriter).logClientHeader(anyLong(), anyString(), ArgumentMatchers.<String>any(), callOptTimeoutCaptor.capture(), any(Metadata.class), any(GrpcLogEntry.Logger.class), anyLong(), AdditionalMatchers.or(ArgumentMatchers.<SocketAddress>isNull(), ArgumentMatchers.<SocketAddress>any()));
    Duration timeout = callOptTimeoutCaptor.getValue();
    assertThat(TimeUnit.SECONDS.toNanos(1) - Durations.toNanos(timeout)).isAtMost(TimeUnit.MILLISECONDS.toNanos(250));
}
Also used : Channel(io.grpc.Channel) Metadata(io.grpc.Metadata) Duration(com.google.protobuf.Duration) CallOptions(io.grpc.CallOptions) ByteString(com.google.protobuf.ByteString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(io.grpc.binarylog.v1.GrpcLogEntry.Logger) MethodDescriptor(io.grpc.MethodDescriptor) ClientCall(io.grpc.ClientCall) NoopClientCall(io.grpc.internal.NoopClientCall) SocketAddress(java.net.SocketAddress) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Aggregations

NoopClientCall (io.grpc.internal.NoopClientCall)14 Test (org.junit.Test)14 Metadata (io.grpc.Metadata)13 SimpleForwardingClientCallListener (io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener)9 CallOptions (io.grpc.CallOptions)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 Channel (io.grpc.Channel)6 Status (io.grpc.Status)6 MethodDescriptor (io.grpc.MethodDescriptor)5 Duration (com.google.protobuf.Duration)4 ClientCall (io.grpc.ClientCall)4 ByteString (com.google.protobuf.ByteString)3 DomainSocketAddress (io.netty.channel.unix.DomainSocketAddress)3 InetSocketAddress (java.net.InetSocketAddress)3 SocketAddress (java.net.SocketAddress)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 StatusRuntimeException (io.grpc.StatusRuntimeException)2 Logger (io.grpc.binarylog.v1.GrpcLogEntry.Logger)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2