Search in sources :

Example 21 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class DelayedClientTransportTest method newStreamThenShutdownTransportThenCancelStream.

@Test
public void newStreamThenShutdownTransportThenCancelStream() {
    ClientStream stream = delayedTransport.newStream(method, new Metadata());
    delayedTransport.shutdown();
    verify(transportListener).transportShutdown(any(Status.class));
    verify(transportListener, times(0)).transportTerminated();
    assertEquals(1, delayedTransport.getPendingStreamsCount());
    stream.cancel(Status.CANCELLED);
    verify(transportListener).transportTerminated();
    assertEquals(0, delayedTransport.getPendingStreamsCount());
    verifyNoMoreInteractions(mockRealTransport);
    verifyNoMoreInteractions(mockRealStream);
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 22 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class DelayedClientTransportTest method startThenCancelStreamWithoutSetTransport.

@Test
public void startThenCancelStreamWithoutSetTransport() {
    ClientStream stream = delayedTransport.newStream(method, new Metadata());
    stream.start(streamListener);
    assertEquals(1, delayedTransport.getPendingStreamsCount());
    stream.cancel(Status.CANCELLED);
    assertEquals(0, delayedTransport.getPendingStreamsCount());
    verify(streamListener).closed(same(Status.CANCELLED), any(Metadata.class));
    verifyNoMoreInteractions(mockRealTransport);
    verifyNoMoreInteractions(mockRealStream);
}
Also used : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 23 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class DelayedStreamTest method listener_allQueued.

@Test
public void listener_allQueued() {
    final Metadata headers = new Metadata();
    final InputStream message1 = mock(InputStream.class);
    final InputStream message2 = mock(InputStream.class);
    final Metadata trailers = new Metadata();
    final Status status = Status.UNKNOWN.withDescription("unique status");
    final InOrder inOrder = inOrder(listener);
    stream.start(listener);
    stream.setStream(new NoopClientStream() {

        @Override
        public void start(ClientStreamListener passedListener) {
            passedListener.onReady();
            passedListener.headersRead(headers);
            passedListener.messageRead(message1);
            passedListener.onReady();
            passedListener.messageRead(message2);
            passedListener.closed(status, trailers);
            verifyNoMoreInteractions(listener);
        }
    });
    inOrder.verify(listener).onReady();
    inOrder.verify(listener).headersRead(headers);
    inOrder.verify(listener).messageRead(message1);
    inOrder.verify(listener).onReady();
    inOrder.verify(listener).messageRead(message2);
    inOrder.verify(listener).closed(status, trailers);
}
Also used : Status(io.grpc.Status) InOrder(org.mockito.InOrder) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 24 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class ClientCallImplTest method authorityNotPropagatedToStream.

@Test
public void authorityNotPropagatedToStream() {
    ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, MoreExecutors.directExecutor(), // Don't provide an authority
    CallOptions.DEFAULT, statsTraceCtx, provider, deadlineCancellationExecutor).setDecompressorRegistry(decompressorRegistry);
    call.start(callListener, new Metadata());
    verify(stream, never()).setAuthority(any(String.class));
}
Also used : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 25 with Metadata

use of io.grpc.Metadata in project grpc-java by grpc.

the class ClientCallImplTest method contextAlreadyCancelledNotifiesImmediately.

@Test
public void contextAlreadyCancelledNotifiesImmediately() throws Exception {
    // Attach the context which is recorded when the call is created
    Context.CancellableContext cancellableContext = Context.current().withCancellation();
    Throwable cause = new Throwable();
    cancellableContext.cancel(cause);
    Context previous = cancellableContext.attach();
    ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, new SerializingExecutor(Executors.newSingleThreadExecutor()), CallOptions.DEFAULT, statsTraceCtx, provider, deadlineCancellationExecutor).setDecompressorRegistry(decompressorRegistry);
    previous.attach();
    final SettableFuture<Status> statusFuture = SettableFuture.create();
    call.start(new ClientCall.Listener<Void>() {

        @Override
        public void onClose(Status status, Metadata trailers) {
            statusFuture.set(status);
        }
    }, new Metadata());
    // Caller should receive onClose callback.
    Status status = statusFuture.get(5, TimeUnit.SECONDS);
    assertEquals(Status.Code.CANCELLED, status.getCode());
    assertSame(cause, status.getCause());
    assertStatusInStats(Status.Code.CANCELLED);
    // Following operations should be no-op.
    call.request(1);
    call.sendMessage(null);
    call.halfClose();
    // Stream should never be created.
    verifyZeroInteractions(transport);
    try {
        call.sendMessage(null);
        fail("Call has been cancelled");
    } catch (IllegalStateException ise) {
    // expected
    }
}
Also used : Context(io.grpc.Context) StatsContext(com.google.instrumentation.stats.StatsContext) Status(io.grpc.Status) Metadata(io.grpc.Metadata) ClientCall(io.grpc.ClientCall) Test(org.junit.Test)

Aggregations

Metadata (io.grpc.Metadata)283 Test (org.junit.Test)229 Status (io.grpc.Status)78 ByteArrayInputStream (java.io.ByteArrayInputStream)25 ClientStream (io.grpc.internal.ClientStream)20 InputStream (java.io.InputStream)19 Buffer (okio.Buffer)16 ClientCall (io.grpc.ClientCall)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 ServerStreamListener (io.grpc.internal.ServerStreamListener)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 ServerCall (io.grpc.ServerCall)12 StatsContext (com.google.instrumentation.stats.StatsContext)11 CallOptions (io.grpc.CallOptions)11 IOException (java.io.IOException)11 Context (io.grpc.Context)10 StatusRuntimeException (io.grpc.StatusRuntimeException)10 Matchers.anyString (org.mockito.Matchers.anyString)10 ServerStream (io.grpc.internal.ServerStream)9 ServiceDescriptor (io.grpc.ServiceDescriptor)8