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);
}
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());
}
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]);
}
}
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));
}
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);
}
Aggregations