Search in sources :

Example 1 with NoopClientCall

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

the class ClientCallsTest method unaryFutureCallSuccess.

@Test
public void unaryFutureCallSuccess() throws Exception {
    final AtomicReference<ClientCall.Listener<String>> listener = new AtomicReference<>();
    final AtomicReference<Integer> message = new AtomicReference<>();
    final AtomicReference<Boolean> halfClosed = 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);
        }

        @Override
        public void sendMessage(Integer msg) {
            message.set(msg);
        }

        @Override
        public void halfClose() {
            halfClosed.set(true);
        }
    };
    Integer req = 2;
    ListenableFuture<String> future = ClientCalls.futureUnaryCall(call, req);
    assertEquals(req, message.get());
    assertTrue(halfClosed.get());
    listener.get().onMessage("bar");
    listener.get().onClose(Status.OK, new Metadata());
    assertEquals("bar", future.get());
}
Also used : SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) NoopClientCall(io.grpc.internal.NoopClientCall) Test(org.junit.Test)

Example 2 with NoopClientCall

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

the class ClientCallsTest method unaryBlockingCallSuccess.

@Test
public void unaryBlockingCallSuccess() throws Exception {
    Integer req = 2;
    final String resp = "bar";
    final Status status = Status.OK;
    final Metadata trailers = new Metadata();
    NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {

        @Override
        public void start(ClientCall.Listener<String> listener, Metadata headers) {
            listener.onMessage(resp);
            listener.onClose(status, trailers);
        }
    };
    String actualResponse = ClientCalls.blockingUnaryCall(call, req);
    assertEquals(resp, actualResponse);
}
Also used : Status(io.grpc.Status) NoopClientCall(io.grpc.internal.NoopClientCall) SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 3 with NoopClientCall

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

the class ClientCallsTest method blockingUnaryCall_HasBlockingStubType.

@Test
public void blockingUnaryCall_HasBlockingStubType() {
    NoopClientCall<Integer, Integer> call = new NoopClientCall<Integer, Integer>() {

        @Override
        public void start(io.grpc.ClientCall.Listener<Integer> listener, Metadata headers) {
            listener.onMessage(1);
            listener.onClose(Status.OK, new Metadata());
        }
    };
    when(mockChannel.newCall(ArgumentMatchers.<MethodDescriptor<Integer, Integer>>any(), any(CallOptions.class))).thenReturn(call);
    Integer unused = ClientCalls.blockingUnaryCall(mockChannel, UNARY_METHOD, CallOptions.DEFAULT, 1);
    verify(mockChannel).newCall(methodDescriptorCaptor.capture(), callOptionsCaptor.capture());
    CallOptions capturedCallOption = callOptionsCaptor.getValue();
    assertThat(capturedCallOption.getOption(ClientCalls.STUB_TYPE_OPTION)).isEquivalentAccordingToCompareTo(StubType.BLOCKING);
}
Also used : NoopClientCall(io.grpc.internal.NoopClientCall) SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Test(org.junit.Test)

Example 4 with NoopClientCall

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

the class ClientCallsTest method blockingServerStreamingCall_HasBlockingStubType.

@Test
public void blockingServerStreamingCall_HasBlockingStubType() {
    NoopClientCall<Integer, Integer> call = new NoopClientCall<Integer, Integer>() {

        @Override
        public void start(io.grpc.ClientCall.Listener<Integer> listener, Metadata headers) {
            listener.onMessage(1);
            listener.onClose(Status.OK, new Metadata());
        }
    };
    when(mockChannel.newCall(ArgumentMatchers.<MethodDescriptor<Integer, Integer>>any(), any(CallOptions.class))).thenReturn(call);
    Iterator<Integer> unused = ClientCalls.blockingServerStreamingCall(mockChannel, UNARY_METHOD, CallOptions.DEFAULT, 1);
    verify(mockChannel).newCall(methodDescriptorCaptor.capture(), callOptionsCaptor.capture());
    CallOptions capturedCallOption = callOptionsCaptor.getValue();
    assertThat(capturedCallOption.getOption(ClientCalls.STUB_TYPE_OPTION)).isEquivalentAccordingToCompareTo(StubType.BLOCKING);
}
Also used : NoopClientCall(io.grpc.internal.NoopClientCall) SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Test(org.junit.Test)

Example 5 with NoopClientCall

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

the class ClientCallsTest method unaryFutureCallCancelled.

@Test
public void unaryFutureCallCancelled() throws Exception {
    final AtomicReference<ClientCall.Listener<String>> listener = new AtomicReference<>();
    final AtomicReference<String> cancelMessage = new AtomicReference<>();
    final AtomicReference<Throwable> cancelCause = 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);
        }

        @Override
        public void cancel(String message, Throwable cause) {
            cancelMessage.set(message);
            cancelCause.set(cause);
        }
    };
    Integer req = 2;
    ListenableFuture<String> future = ClientCalls.futureUnaryCall(call, req);
    future.cancel(true);
    assertEquals("GrpcFuture was cancelled", cancelMessage.get());
    assertNull(cancelCause.get());
    listener.get().onMessage("bar");
    listener.get().onClose(Status.OK, new Metadata());
    try {
        future.get();
        fail("Should fail");
    } catch (CancellationException e) {
    // Exepcted
    }
}
Also used : SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) NoopClientCall(io.grpc.internal.NoopClientCall) CancellationException(java.util.concurrent.CancellationException) 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