Search in sources :

Example 1 with NoopClientCall

use of io.grpc.testing.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<ClientCall.Listener<String>>();
    final AtomicReference<Integer> message = new AtomicReference<Integer>();
    final AtomicReference<Boolean> halfClosed = new AtomicReference<Boolean>();
    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 : Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) NoopClientCall(io.grpc.testing.NoopClientCall) NoopClientCall(io.grpc.testing.NoopClientCall) ClientCall(io.grpc.ClientCall) Test(org.junit.Test)

Example 2 with NoopClientCall

use of io.grpc.testing.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.testing.NoopClientCall) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 3 with NoopClientCall

use of io.grpc.testing.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<ClientCall.Listener<String>>();
    final AtomicReference<String> cancelMessage = new AtomicReference<String>();
    final AtomicReference<Throwable> cancelCause = new AtomicReference<Throwable>();
    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 : Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) NoopClientCall(io.grpc.testing.NoopClientCall) CancellationException(java.util.concurrent.CancellationException) NoopClientCall(io.grpc.testing.NoopClientCall) ClientCall(io.grpc.ClientCall) Test(org.junit.Test)

Example 4 with NoopClientCall

use of io.grpc.testing.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<ClientCall.Listener<String>>();
    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 (Throwable e) {
        Status status = Status.fromThrowable(e);
        assertEquals(Status.INTERNAL, status);
        Metadata metadata = Status.trailersFromThrowable(e);
        assertSame(trailers, metadata);
    }
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) NoopClientCall(io.grpc.testing.NoopClientCall) NoopClientCall(io.grpc.testing.NoopClientCall) ClientCall(io.grpc.ClientCall) Test(org.junit.Test)

Example 5 with NoopClientCall

use of io.grpc.testing.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<ClientCall.Listener<String>>();
    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) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) NoopClientCall(io.grpc.testing.NoopClientCall) NoopClientCall(io.grpc.testing.NoopClientCall) ClientCall(io.grpc.ClientCall) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

Metadata (io.grpc.Metadata)6 NoopClientCall (io.grpc.testing.NoopClientCall)6 Test (org.junit.Test)6 ClientCall (io.grpc.ClientCall)4 Status (io.grpc.Status)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 StatusRuntimeException (io.grpc.StatusRuntimeException)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1