Search in sources :

Example 46 with Metadata

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

the class ClientCallImplTest method prepareHeaders_ignoreIdentityEncoding.

@Test
public void prepareHeaders_ignoreIdentityEncoding() {
    Metadata m = new Metadata();
    ClientCallImpl.prepareHeaders(m, decompressorRegistry, Codec.Identity.NONE, statsTraceCtx);
    assertNull(m.get(GrpcUtil.MESSAGE_ENCODING_KEY));
}
Also used : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 47 with Metadata

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

the class ClientCallImplTest method exceptionInOnReadyTakesPrecedenceOverServer.

@Test
public void exceptionInOnReadyTakesPrecedenceOverServer() {
    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();
    RuntimeException failure = new RuntimeException("bad");
    doThrow(failure).when(callListener).onReady();
    /*
     * In unary calls, the server closes the call right after responding, so the onClose call is
     * queued to run.  When onReady 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.onReady();
    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 : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 48 with Metadata

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

the class ClientCallImplTest method prepareHeaders_statsCtxAdded.

@Test
public void prepareHeaders_statsCtxAdded() {
    Metadata m = new Metadata();
    ClientCallImpl.prepareHeaders(m, decompressorRegistry, Codec.Identity.NONE, statsTraceCtx);
    assertEquals(parentStatsContext, m.get(statsTraceCtx.getStatsHeader()));
}
Also used : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 49 with Metadata

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

the class ClientCallImplTest method deadlineExceededBeforeCallStarted.

@Test
public void deadlineExceededBeforeCallStarted() {
    CallOptions callOptions = CallOptions.DEFAULT.withDeadlineAfter(0, TimeUnit.SECONDS);
    fakeClock.forwardTime(System.nanoTime(), TimeUnit.NANOSECONDS);
    ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, new SerializingExecutor(Executors.newSingleThreadExecutor()), callOptions, statsTraceCtx, provider, deadlineCancellationExecutor).setDecompressorRegistry(decompressorRegistry);
    call.start(callListener, new Metadata());
    verify(transport, times(0)).newStream(any(MethodDescriptor.class), any(Metadata.class));
    verify(callListener, timeout(1000)).onClose(statusCaptor.capture(), any(Metadata.class));
    assertEquals(Status.Code.DEADLINE_EXCEEDED, statusCaptor.getValue().getCode());
    assertStatusInStats(Status.Code.DEADLINE_EXCEEDED);
    verifyZeroInteractions(provider);
}
Also used : Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) Test(org.junit.Test)

Example 50 with Metadata

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

the class ClientCallImplTest method contextCancellationCancelsStream.

@Test
public void contextCancellationCancelsStream() throws Exception {
    // Attach the context which is recorded when the call is created
    Context.CancellableContext cancellableContext = Context.current().withCancellation();
    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();
    call.start(callListener, new Metadata());
    Throwable t = new Throwable();
    cancellableContext.cancel(t);
    verify(stream, times(1)).cancel(statusArgumentCaptor.capture());
    verify(stream, times(1)).cancel(statusCaptor.capture());
    assertEquals(Status.Code.CANCELLED, statusCaptor.getValue().getCode());
}
Also used : Context(io.grpc.Context) StatsContext(com.google.instrumentation.stats.StatsContext) Metadata(io.grpc.Metadata) 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