Search in sources :

Example 26 with Metadata

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

the class ClientCallImplTest method authorityPropagatedToStream.

@Test
public void authorityPropagatedToStream() {
    ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, MoreExecutors.directExecutor(), CallOptions.DEFAULT.withAuthority("overridden-authority"), statsTraceCtx, provider, deadlineCancellationExecutor).setDecompressorRegistry(decompressorRegistry);
    call.start(callListener, new Metadata());
    verify(stream).setAuthority("overridden-authority");
}
Also used : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 27 with Metadata

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

the class ClientCallImplTest method callOptionsPropagatedToTransport.

@Test
public void callOptionsPropagatedToTransport() {
    final CallOptions callOptions = CallOptions.DEFAULT.withAuthority("dummy_value");
    final ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, MoreExecutors.directExecutor(), callOptions, statsTraceCtx, provider, deadlineCancellationExecutor).setDecompressorRegistry(decompressorRegistry);
    final Metadata metadata = new Metadata();
    call.start(callListener, metadata);
    verify(transport).newStream(same(method), same(metadata), same(callOptions), same(statsTraceCtx));
}
Also used : Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Test(org.junit.Test)

Example 28 with Metadata

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

the class ClientCallImplTest method callerContextPropagatedToListener.

@Test
public void callerContextPropagatedToListener() throws Exception {
    // Attach the context which is recorded when the call is created
    final Context.Key<String> testKey = Context.key("testing");
    Context.current().withValue(testKey, "testValue").attach();
    ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, new SerializingExecutor(Executors.newSingleThreadExecutor()), CallOptions.DEFAULT, statsTraceCtx, provider, deadlineCancellationExecutor).setDecompressorRegistry(decompressorRegistry);
    Context.ROOT.attach();
    // Override the value after creating the call, this should not be seen by callbacks
    Context.current().withValue(testKey, "badValue").attach();
    final AtomicBoolean onHeadersCalled = new AtomicBoolean();
    final AtomicBoolean onMessageCalled = new AtomicBoolean();
    final AtomicBoolean onReadyCalled = new AtomicBoolean();
    final AtomicBoolean observedIncorrectContext = new AtomicBoolean();
    final CountDownLatch latch = new CountDownLatch(1);
    call.start(new ClientCall.Listener<Void>() {

        @Override
        public void onHeaders(Metadata headers) {
            onHeadersCalled.set(true);
            checkContext();
        }

        @Override
        public void onMessage(Void message) {
            onMessageCalled.set(true);
            checkContext();
        }

        @Override
        public void onClose(Status status, Metadata trailers) {
            checkContext();
            latch.countDown();
        }

        @Override
        public void onReady() {
            onReadyCalled.set(true);
            checkContext();
        }

        private void checkContext() {
            if (!"testValue".equals(testKey.get())) {
                observedIncorrectContext.set(true);
            }
        }
    }, new Metadata());
    verify(stream).start(listenerArgumentCaptor.capture());
    ClientStreamListener listener = listenerArgumentCaptor.getValue();
    listener.onReady();
    listener.headersRead(new Metadata());
    listener.messageRead(new ByteArrayInputStream(new byte[0]));
    listener.messageRead(new ByteArrayInputStream(new byte[0]));
    listener.closed(Status.OK, new Metadata());
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertTrue(onHeadersCalled.get());
    assertTrue(onMessageCalled.get());
    assertTrue(onReadyCalled.get());
    assertFalse(observedIncorrectContext.get());
}
Also used : Context(io.grpc.Context) StatsContext(com.google.instrumentation.stats.StatsContext) Status(io.grpc.Status) Metadata(io.grpc.Metadata) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientCall(io.grpc.ClientCall) Test(org.junit.Test)

Example 29 with Metadata

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

the class ClientCallImplTest method timeoutShouldNotBeSet.

/**
   * Without a context or call options deadline,
   * a timeout should not be set in metadata.
   */
@Test
public void timeoutShouldNotBeSet() {
    ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, MoreExecutors.directExecutor(), CallOptions.DEFAULT, statsTraceCtx, provider, deadlineCancellationExecutor);
    Metadata headers = new Metadata();
    call.start(callListener, headers);
    assertFalse(headers.containsKey(GrpcUtil.TIMEOUT_KEY));
}
Also used : Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 30 with Metadata

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

the class ClientCallImplTest method contextDeadlineShouldBePropagatedInMetadata.

@Test
public void contextDeadlineShouldBePropagatedInMetadata() {
    long deadlineNanos = TimeUnit.SECONDS.toNanos(1);
    Context context = Context.current().withDeadlineAfter(deadlineNanos, TimeUnit.NANOSECONDS, deadlineCancellationExecutor);
    context.attach();
    ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, MoreExecutors.directExecutor(), CallOptions.DEFAULT, statsTraceCtx, provider, deadlineCancellationExecutor);
    Metadata headers = new Metadata();
    call.start(callListener, headers);
    assertTrue(headers.containsKey(GrpcUtil.TIMEOUT_KEY));
    Long timeout = headers.get(GrpcUtil.TIMEOUT_KEY);
    assertNotNull(timeout);
    long deltaNanos = TimeUnit.MILLISECONDS.toNanos(400);
    assertTimeoutBetween(timeout, deadlineNanos - deltaNanos, deadlineNanos);
}
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