Search in sources :

Example 66 with Metadata

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

the class ServerImplTest method handlerRegistryPriorities.

@Test
public void handlerRegistryPriorities() throws Exception {
    fallbackRegistry = mock(HandlerRegistry.class);
    MethodDescriptor<String, Integer> method1 = MethodDescriptor.<String, Integer>newBuilder().setType(MethodDescriptor.MethodType.UNKNOWN).setFullMethodName("Service1/Method1").setRequestMarshaller(STRING_MARSHALLER).setResponseMarshaller(INTEGER_MARSHALLER).build();
    registry = new InternalHandlerRegistry.Builder().addService(ServerServiceDefinition.builder(new ServiceDescriptor("Service1", method1)).addMethod(method1, callHandler).build()).build();
    transportServer = new SimpleServer();
    createAndStartServer(NO_FILTERS);
    ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport());
    Metadata requestHeaders = new Metadata();
    StatsTraceContext statsTraceCtx = transportListener.methodDetermined("Waiter/serve", requestHeaders);
    assertNotNull(statsTraceCtx);
    when(stream.statsTraceContext()).thenReturn(statsTraceCtx);
    // This call will be handled by callHandler from the internal registry
    transportListener.streamCreated(stream, "Service1/Method1", requestHeaders);
    assertEquals(1, executor.runDueTasks());
    verify(callHandler).startCall(Matchers.<ServerCall<String, Integer>>anyObject(), Matchers.<Metadata>anyObject());
    // This call will be handled by the fallbackRegistry because it's not registred in the internal
    // registry.
    transportListener.streamCreated(stream, "Service1/Method2", requestHeaders);
    assertEquals(1, executor.runDueTasks());
    verify(fallbackRegistry).lookupMethod("Service1/Method2", null);
    verifyNoMoreInteractions(callHandler);
    verifyNoMoreInteractions(fallbackRegistry);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MutableHandlerRegistry(io.grpc.util.MutableHandlerRegistry) HandlerRegistry(io.grpc.HandlerRegistry) ServiceDescriptor(io.grpc.ServiceDescriptor) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 67 with Metadata

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

the class StatsTraceContextTest method tagPropagation.

/**
   * Tags that are propagated by the {@link StatsContextFactory} are properly propagated via
   * the headers.
   */
@Test
public void tagPropagation() {
    String methodName = MethodDescriptor.generateFullMethodName("Service1", "method3");
    // EXTRA_TAG is propagated by the FakeStatsContextFactory. Note that not all tags are
    // propagated.  The StatsContextFactory decides which tags are to propagated.  gRPC facilitates
    // the propagation by putting them in the headers.
    StatsContext parentCtx = statsCtxFactory.getDefault().with(StatsTestUtils.EXTRA_TAG, TagValue.create("extra-tag-value-897"));
    StatsTraceContext clientCtx = StatsTraceContext.newClientContextForTesting(methodName, statsCtxFactory, parentCtx, fakeClock.getStopwatchSupplier());
    Metadata headers = new Metadata();
    clientCtx.propagateToHeaders(headers);
    // The server gets the propagated tag from the headers, and puts it on the server-side
    // StatsContext.
    StatsTraceContext serverCtx = StatsTraceContext.newServerContext(methodName, statsCtxFactory, headers, fakeClock.getStopwatchSupplier());
    serverCtx.callEnded(Status.OK);
    clientCtx.callEnded(Status.OK);
    StatsTestUtils.MetricsRecord serverRecord = statsCtxFactory.pollRecord();
    assertNotNull(serverRecord);
    assertNoClientContent(serverRecord);
    TagValue serverMethodTag = serverRecord.tags.get(RpcConstants.RPC_SERVER_METHOD);
    assertEquals(methodName, serverMethodTag.toString());
    TagValue serverStatusTag = serverRecord.tags.get(RpcConstants.RPC_STATUS);
    assertEquals(Status.Code.OK.toString(), serverStatusTag.toString());
    assertNull(serverRecord.getMetric(RpcConstants.RPC_SERVER_ERROR_COUNT));
    TagValue serverPropagatedTag = serverRecord.tags.get(StatsTestUtils.EXTRA_TAG);
    assertEquals("extra-tag-value-897", serverPropagatedTag.toString());
    StatsTestUtils.MetricsRecord clientRecord = statsCtxFactory.pollRecord();
    assertNotNull(clientRecord);
    assertNoServerContent(clientRecord);
    TagValue clientMethodTag = clientRecord.tags.get(RpcConstants.RPC_CLIENT_METHOD);
    assertEquals(methodName, clientMethodTag.toString());
    TagValue clientStatusTag = clientRecord.tags.get(RpcConstants.RPC_STATUS);
    assertEquals(Status.Code.OK.toString(), clientStatusTag.toString());
    assertNull(clientRecord.getMetric(RpcConstants.RPC_CLIENT_ERROR_COUNT));
    TagValue clientPropagatedTag = clientRecord.tags.get(StatsTestUtils.EXTRA_TAG);
    assertEquals("extra-tag-value-897", clientPropagatedTag.toString());
}
Also used : StatsTestUtils(io.grpc.internal.testing.StatsTestUtils) StatsContext(com.google.instrumentation.stats.StatsContext) Metadata(io.grpc.Metadata) TagValue(com.google.instrumentation.stats.TagValue) Test(org.junit.Test)

Example 68 with Metadata

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

the class TransportFrameUtilTest method testToHttp2Headers.

@Test
public void testToHttp2Headers() {
    Metadata headers = new Metadata();
    headers.put(PLAIN_STRING, COMPLIANT_ASCII_STRING);
    headers.put(BINARY_STRING, NONCOMPLIANT_ASCII_STRING);
    headers.put(BINARY_STRING_WITHOUT_SUFFIX, NONCOMPLIANT_ASCII_STRING);
    byte[][] http2Headers = TransportFrameUtil.toHttp2Headers(headers);
    // BINARY_STRING_WITHOUT_SUFFIX should not get in because it contains non-compliant ASCII
    // characters but doesn't have "-bin" in the name.
    byte[][] answer = new byte[][] { "plainstring".getBytes(US_ASCII), COMPLIANT_ASCII_STRING.getBytes(US_ASCII), "string-bin".getBytes(US_ASCII), base64Encode(NONCOMPLIANT_ASCII_STRING.getBytes(US_ASCII)) };
    assertEquals(answer.length, http2Headers.length);
    // http2Headers may re-sort the keys, so we cannot compare it with the answer side-by-side.
    for (int i = 0; i < answer.length; i += 2) {
        assertContains(http2Headers, answer[i], answer[i + 1]);
    }
}
Also used : Metadata(io.grpc.Metadata) InternalMetadata(io.grpc.InternalMetadata) Test(org.junit.Test)

Example 69 with Metadata

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

the class TransportFrameUtilTest method testToAndFromHttp2Headers.

@Test
public void testToAndFromHttp2Headers() {
    Metadata headers = new Metadata();
    headers.put(PLAIN_STRING, COMPLIANT_ASCII_STRING);
    headers.put(BINARY_STRING, NONCOMPLIANT_ASCII_STRING);
    headers.put(BINARY_STRING_WITHOUT_SUFFIX, NONCOMPLIANT_ASCII_STRING);
    byte[][] http2Headers = TransportFrameUtil.toHttp2Headers(headers);
    byte[][] rawSerialized = TransportFrameUtil.toRawSerializedHeaders(http2Headers);
    Metadata recoveredHeaders = InternalMetadata.newMetadata(rawSerialized);
    assertEquals(COMPLIANT_ASCII_STRING, recoveredHeaders.get(PLAIN_STRING));
    assertEquals(NONCOMPLIANT_ASCII_STRING, recoveredHeaders.get(BINARY_STRING));
    assertNull(recoveredHeaders.get(BINARY_STRING_WITHOUT_SUFFIX));
}
Also used : Metadata(io.grpc.Metadata) InternalMetadata(io.grpc.InternalMetadata) Test(org.junit.Test)

Example 70 with Metadata

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

the class MessageFramerTest method setUp.

/** Set up for test. */
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    statsCtxFactory = new FakeStatsContextFactory();
    // MessageDeframerTest tests with a client-side StatsTraceContext, so here we test with a
    // server-side StatsTraceContext.
    statsTraceCtx = StatsTraceContext.newServerContext("service/method", statsCtxFactory, new Metadata(), GrpcUtil.STOPWATCH_SUPPLIER);
    framer = new MessageFramer(sink, allocator, statsTraceCtx);
}
Also used : FakeStatsContextFactory(io.grpc.internal.testing.StatsTestUtils.FakeStatsContextFactory) Metadata(io.grpc.Metadata) Before(org.junit.Before)

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