Search in sources :

Example 31 with Metadata

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

the class ClientCallImplTest method exceptionInOnMessageTakesPrecedenceOverServer.

@Test
public void exceptionInOnMessageTakesPrecedenceOverServer() {
    DelayedExecutor executor = new DelayedExecutor();
    ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, executor, CallOptions.DEFAULT, statsTraceCtx, provider, deadlineCancellationExecutor);
    call.start(callListener, new Metadata());
    verify(stream).start(listenerArgumentCaptor.capture());
    final ClientStreamListener streamListener = listenerArgumentCaptor.getValue();
    streamListener.headersRead(new Metadata());
    RuntimeException failure = new RuntimeException("bad");
    doThrow(failure).when(callListener).onMessage(any(Void.class));
    /*
     * In unary calls, the server closes the call right after responding, so the onClose call is
     * queued to run.  When messageRead is called, an exception will occur and attempt to cancel the
     * stream.  However, since the server closed it "first" the second exception is lost leading to
     * the call being counted as successful.
     */
    streamListener.messageRead(new ByteArrayInputStream(new byte[] {}));
    streamListener.closed(Status.OK, new Metadata());
    executor.release();
    verify(callListener).onClose(statusArgumentCaptor.capture(), Matchers.isA(Metadata.class));
    assertThat(statusArgumentCaptor.getValue().getCode()).isEqualTo(Status.Code.CANCELLED);
    assertThat(statusArgumentCaptor.getValue().getCause()).isSameAs(failure);
    verify(stream).cancel(statusArgumentCaptor.getValue());
    assertStatusInStats(Status.Code.CANCELLED);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 32 with Metadata

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

the class DelayedClientTransportTest method startStreamThenShutdownNow.

@Test
public void startStreamThenShutdownNow() {
    ClientStream stream = delayedTransport.newStream(method, new Metadata());
    stream.start(streamListener);
    delayedTransport.shutdownNow(Status.UNAVAILABLE);
    verify(transportListener).transportShutdown(any(Status.class));
    verify(transportListener).transportTerminated();
    verify(streamListener).closed(statusCaptor.capture(), any(Metadata.class));
    assertEquals(Status.Code.UNAVAILABLE, statusCaptor.getValue().getCode());
}
Also used : Status(io.grpc.Status) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 33 with Metadata

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

the class AbstractClientStream2Test method inboundHeadersReceived_acceptsGzipEncoding.

@Test
public void inboundHeadersReceived_acceptsGzipEncoding() {
    AbstractClientStream2 stream = new BaseAbstractClientStream(allocator, statsTraceCtx);
    stream.start(mockListener);
    Metadata headers = new Metadata();
    headers.put(GrpcUtil.MESSAGE_ENCODING_KEY, new Codec.Gzip().getMessageEncoding());
    stream.transportState().inboundHeadersReceived(headers);
    verify(mockListener).headersRead(headers);
}
Also used : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 34 with Metadata

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

the class AbstractClientStream2Test method inboundHeadersReceived_acceptsIdentityEncoding.

@Test
public void inboundHeadersReceived_acceptsIdentityEncoding() {
    AbstractClientStream2 stream = new BaseAbstractClientStream(allocator, statsTraceCtx);
    stream.start(mockListener);
    Metadata headers = new Metadata();
    headers.put(GrpcUtil.MESSAGE_ENCODING_KEY, Codec.Identity.NONE.getMessageEncoding());
    stream.transportState().inboundHeadersReceived(headers);
    verify(mockListener).headersRead(headers);
}
Also used : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 35 with Metadata

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

the class AbstractClientStream2Test method inboundHeadersReceived_failsIfStatusReported.

@Test
public void inboundHeadersReceived_failsIfStatusReported() {
    AbstractClientStream2 stream = new BaseAbstractClientStream(allocator, statsTraceCtx);
    stream.start(mockListener);
    stream.transportState().transportReportStatus(Status.CANCELLED, false, new Metadata());
    TransportState state = stream.transportState();
    thrown.expect(IllegalStateException.class);
    state.inboundHeadersReceived(new Metadata());
}
Also used : Metadata(io.grpc.Metadata) TransportState(io.grpc.internal.AbstractClientStream2.TransportState) 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