Search in sources :

Example 11 with NoopClientCall

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

the class BinaryLogProviderTest method wrapChannel_handler.

@Test
public void wrapChannel_handler() throws Exception {
    final List<byte[]> serializedReq = new ArrayList<>();
    final AtomicReference<ClientCall.Listener<?>> listener = 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
                public void start(ClientCall.Listener<ResponseT> responseListener, Metadata headers) {
                    listener.set(responseListener);
                }

                @Override
                public void sendMessage(RequestT message) {
                    serializedReq.add((byte[]) message);
                }
            };
        }

        @Override
        public String authority() {
            throw new UnsupportedOperationException();
        }
    };
    Channel wChannel = binlogProvider.wrapChannel(channel);
    ClientCall<String, Integer> clientCall = wChannel.newCall(method, CallOptions.DEFAULT);
    final List<Integer> observedResponse = new ArrayList<>();
    clientCall.start(new NoopClientCall.NoopClientCallListener<Integer>() {

        @Override
        public void onMessage(Integer message) {
            observedResponse.add(message);
        }
    }, new Metadata());
    String expectedRequest = "hello world";
    assertThat(binlogReq).isEmpty();
    assertThat(serializedReq).isEmpty();
    assertEquals(0, reqMarshaller.streamInvocations);
    clientCall.sendMessage(expectedRequest);
    // it is unacceptably expensive for the binlog to double parse every logged message
    assertEquals(1, reqMarshaller.streamInvocations);
    assertEquals(0, reqMarshaller.parseInvocations);
    assertThat(binlogReq).hasSize(1);
    assertThat(serializedReq).hasSize(1);
    assertEquals(expectedRequest, StringMarshaller.INSTANCE.parse(new ByteArrayInputStream(binlogReq.get(0))));
    assertEquals(expectedRequest, StringMarshaller.INSTANCE.parse(new ByteArrayInputStream(serializedReq.get(0))));
    int expectedResponse = 12345;
    assertThat(binlogResp).isEmpty();
    assertThat(observedResponse).isEmpty();
    assertEquals(0, respMarshaller.parseInvocations);
    onClientMessageHelper(listener.get(), IntegerMarshaller.INSTANCE.stream(expectedResponse));
    // it is unacceptably expensive for the binlog to double parse every logged message
    assertEquals(1, respMarshaller.parseInvocations);
    assertEquals(0, respMarshaller.streamInvocations);
    assertThat(binlogResp).hasSize(1);
    assertThat(observedResponse).hasSize(1);
    assertEquals(expectedResponse, (int) IntegerMarshaller.INSTANCE.parse(new ByteArrayInputStream(binlogResp.get(0))));
    assertEquals(expectedResponse, (int) observedResponse.get(0));
}
Also used : SimpleForwardingServerCallListener(io.grpc.ForwardingServerCallListener.SimpleForwardingServerCallListener) SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) Channel(io.grpc.Channel) ArrayList(java.util.ArrayList) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) NoopClientCall(io.grpc.internal.NoopClientCall) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 12 with NoopClientCall

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

the class BinaryLogProviderTest method wrapChannel_methodDescriptor.

@Test
public void wrapChannel_methodDescriptor() throws Exception {
    final AtomicReference<MethodDescriptor<?, ?>> methodRef = new AtomicReference<>();
    Channel channel = new Channel() {

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

        @Override
        public String authority() {
            throw new UnsupportedOperationException();
        }
    };
    Channel wChannel = binlogProvider.wrapChannel(channel);
    ClientCall<String, Integer> unusedClientCall = wChannel.newCall(method, CallOptions.DEFAULT);
    validateWrappedMethod(methodRef.get());
}
Also used : NoopClientCall(io.grpc.internal.NoopClientCall) Channel(io.grpc.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) Test(org.junit.Test)

Example 13 with NoopClientCall

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

the class ClientCallsTest method unaryFutureCallFailed.

@Test
public void unaryFutureCallFailed() 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;
    ListenableFuture<String> future = ClientCalls.futureUnaryCall(call, req);
    Metadata trailers = new Metadata();
    listener.get().onClose(Status.INTERNAL, trailers);
    try {
        future.get();
        fail("Should fail");
    } catch (ExecutionException 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) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 14 with NoopClientCall

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

the class ClientCallsTest method unaryBlockingCallFailed.

@Test
public void unaryBlockingCallFailed() throws Exception {
    Integer req = 2;
    final Status status = Status.INTERNAL.withDescription("Unique status");
    final Metadata trailers = new Metadata();
    NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {

        @Override
        public void start(io.grpc.ClientCall.Listener<String> listener, Metadata headers) {
            listener.onClose(status, trailers);
        }
    };
    try {
        ClientCalls.blockingUnaryCall(call, req);
        fail("Should fail");
    } catch (StatusRuntimeException e) {
        assertSame(status, e.getStatus());
        assertSame(trailers, e.getTrailers());
    }
}
Also used : Status(io.grpc.Status) NoopClientCall(io.grpc.internal.NoopClientCall) SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) Metadata(io.grpc.Metadata) StatusRuntimeException(io.grpc.StatusRuntimeException) 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