use of io.grpc.internal.NoopServerCall in project grpc-java by grpc.
the class ServerInterceptorsTest method typedMarshalledMessages.
@Test
@SuppressWarnings("unchecked")
public void typedMarshalledMessages() {
final List<String> order = new ArrayList<>();
Marshaller<Holder> marshaller = new Marshaller<Holder>() {
@Override
public InputStream stream(Holder value) {
return value.get();
}
@Override
public Holder parse(InputStream stream) {
return new Holder(stream);
}
};
ServerCallHandler<Holder, Holder> handler2 = new ServerCallHandler<Holder, Holder>() {
@Override
public Listener<Holder> startCall(final ServerCall<Holder, Holder> call, final Metadata headers) {
return new Listener<Holder>() {
@Override
public void onMessage(Holder message) {
order.add("handler");
call.sendMessage(message);
}
};
}
};
MethodDescriptor<Holder, Holder> wrappedMethod = MethodDescriptor.<Holder, Holder>newBuilder().setType(MethodType.UNKNOWN).setFullMethodName("basic/wrapped").setRequestMarshaller(marshaller).setResponseMarshaller(marshaller).build();
ServerServiceDefinition serviceDef = ServerServiceDefinition.builder(new ServiceDescriptor("basic", wrappedMethod)).addMethod(wrappedMethod, handler2).build();
ServerInterceptor interceptor1 = new ServerInterceptor() {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
ServerCall<ReqT, RespT> interceptedCall = new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendMessage(RespT message) {
order.add("i1sendMessage");
assertTrue(message instanceof Holder);
super.sendMessage(message);
}
};
ServerCall.Listener<ReqT> originalListener = next.startCall(interceptedCall, headers);
return new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(originalListener) {
@Override
public void onMessage(ReqT message) {
order.add("i1onMessage");
assertTrue(message instanceof Holder);
super.onMessage(message);
}
};
}
};
ServerInterceptor interceptor2 = new ServerInterceptor() {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
ServerCall<ReqT, RespT> interceptedCall = new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendMessage(RespT message) {
order.add("i2sendMessage");
assertTrue(message instanceof InputStream);
super.sendMessage(message);
}
};
ServerCall.Listener<ReqT> originalListener = next.startCall(interceptedCall, headers);
return new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(originalListener) {
@Override
public void onMessage(ReqT message) {
order.add("i2onMessage");
assertTrue(message instanceof InputStream);
super.onMessage(message);
}
};
}
};
ServerServiceDefinition intercepted = ServerInterceptors.intercept(serviceDef, interceptor1);
ServerServiceDefinition inputStreamMessageService = ServerInterceptors.useInputStreamMessages(intercepted);
ServerServiceDefinition intercepted2 = ServerInterceptors.intercept(inputStreamMessageService, interceptor2);
ServerMethodDefinition<InputStream, InputStream> serverMethod = (ServerMethodDefinition<InputStream, InputStream>) intercepted2.getMethod("basic/wrapped");
ServerCall<InputStream, InputStream> call2 = new NoopServerCall<>();
byte[] bytes = {};
serverMethod.getServerCallHandler().startCall(call2, headers).onMessage(new ByteArrayInputStream(bytes));
assertEquals(Arrays.asList("i2onMessage", "i1onMessage", "handler", "i1sendMessage", "i2sendMessage"), order);
}
use of io.grpc.internal.NoopServerCall in project grpc-java by grpc.
the class BinlogHelperTest method serverInterceptor.
@Test
public void serverInterceptor() throws Exception {
final AtomicReference<ServerCall<byte[], byte[]>> interceptedCall = new AtomicReference<>();
ServerCall.Listener<byte[]> capturedListener;
@SuppressWarnings("unchecked") final ServerCall.Listener<byte[]> mockListener = mock(ServerCall.Listener.class);
// capture these manually because ServerCall can not be mocked
final AtomicReference<Metadata> actualServerInitial = new AtomicReference<>();
final AtomicReference<byte[]> actualResponse = new AtomicReference<>();
final AtomicReference<Status> actualStatus = new AtomicReference<>();
final AtomicReference<Metadata> actualTrailers = new AtomicReference<>();
// begin call and receive client header
{
Metadata clientInitial = new Metadata();
final MethodDescriptor<byte[], byte[]> method = MethodDescriptor.<byte[], byte[]>newBuilder().setType(MethodType.UNKNOWN).setFullMethodName("service/method").setRequestMarshaller(BYTEARRAY_MARSHALLER).setResponseMarshaller(BYTEARRAY_MARSHALLER).build();
capturedListener = new BinlogHelper(mockSinkWriter).getServerInterceptor(CALL_ID).interceptCall(new NoopServerCall<byte[], byte[]>() {
@Override
public void sendHeaders(Metadata headers) {
actualServerInitial.set(headers);
}
@Override
public void sendMessage(byte[] message) {
actualResponse.set(message);
}
@Override
public void close(Status status, Metadata trailers) {
actualStatus.set(status);
actualTrailers.set(trailers);
}
@Override
public MethodDescriptor<byte[], byte[]> getMethodDescriptor() {
return method;
}
@Override
public Attributes getAttributes() {
return Attributes.newBuilder().set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, peer).build();
}
@Override
public String getAuthority() {
return "the-authority";
}
}, clientInitial, new ServerCallHandler<byte[], byte[]>() {
@Override
public ServerCall.Listener<byte[]> startCall(ServerCall<byte[], byte[]> call, Metadata headers) {
interceptedCall.set(call);
return mockListener;
}
});
verify(mockSinkWriter).logClientHeader(/*seq=*/
eq(1L), eq("service/method"), eq("the-authority"), ArgumentMatchers.<Duration>isNull(), same(clientInitial), eq(Logger.LOGGER_SERVER), eq(CALL_ID), same(peer));
verifyNoMoreInteractions(mockSinkWriter);
}
// send server header
{
Metadata serverInital = new Metadata();
interceptedCall.get().sendHeaders(serverInital);
verify(mockSinkWriter).logServerHeader(/*seq=*/
eq(2L), same(serverInital), eq(Logger.LOGGER_SERVER), eq(CALL_ID), ArgumentMatchers.<SocketAddress>isNull());
verifyNoMoreInteractions(mockSinkWriter);
assertSame(serverInital, actualServerInitial.get());
}
// receive client msg
{
byte[] request = "this is a request".getBytes(US_ASCII);
capturedListener.onMessage(request);
verify(mockSinkWriter).logRpcMessage(/*seq=*/
eq(3L), eq(EventType.EVENT_TYPE_CLIENT_MESSAGE), same(BYTEARRAY_MARSHALLER), same(request), eq(Logger.LOGGER_SERVER), eq(CALL_ID));
verifyNoMoreInteractions(mockSinkWriter);
verify(mockListener).onMessage(same(request));
}
// client half close
{
capturedListener.onHalfClose();
verify(mockSinkWriter).logHalfClose(eq(4L), eq(Logger.LOGGER_SERVER), eq(CALL_ID));
verifyNoMoreInteractions(mockSinkWriter);
verify(mockListener).onHalfClose();
}
// send server msg
{
byte[] response = "this is a response".getBytes(US_ASCII);
interceptedCall.get().sendMessage(response);
verify(mockSinkWriter).logRpcMessage(/*seq=*/
eq(5L), eq(EventType.EVENT_TYPE_SERVER_MESSAGE), same(BYTEARRAY_MARSHALLER), same(response), eq(Logger.LOGGER_SERVER), eq(CALL_ID));
verifyNoMoreInteractions(mockSinkWriter);
assertSame(response, actualResponse.get());
}
// send trailer
{
Status status = Status.INTERNAL.withDescription("some description");
Metadata trailers = new Metadata();
interceptedCall.get().close(status, trailers);
verify(mockSinkWriter).logTrailer(/*seq=*/
eq(6L), same(status), same(trailers), eq(Logger.LOGGER_SERVER), eq(CALL_ID), ArgumentMatchers.<SocketAddress>isNull());
verifyNoMoreInteractions(mockSinkWriter);
assertSame(status, actualStatus.get());
assertSame(trailers, actualTrailers.get());
}
// cancel
{
capturedListener.onCancel();
verify(mockSinkWriter).logCancel(/*seq=*/
eq(7L), eq(Logger.LOGGER_SERVER), eq(CALL_ID));
verify(mockListener).onCancel();
}
}
use of io.grpc.internal.NoopServerCall in project grpc-java by grpc.
the class BinlogHelperTest method serverDeadlineLogged.
@Test
public void serverDeadlineLogged() {
final MethodDescriptor<byte[], byte[]> method = MethodDescriptor.<byte[], byte[]>newBuilder().setType(MethodType.UNKNOWN).setFullMethodName("service/method").setRequestMarshaller(BYTEARRAY_MARSHALLER).setResponseMarshaller(BYTEARRAY_MARSHALLER).build();
final ServerCall<byte[], byte[]> noopServerCall = new NoopServerCall<byte[], byte[]>() {
@Override
public MethodDescriptor<byte[], byte[]> getMethodDescriptor() {
return method;
}
};
final ServerCallHandler<byte[], byte[]> noopHandler = new ServerCallHandler<byte[], byte[]>() {
@Override
public ServerCall.Listener<byte[]> startCall(ServerCall<byte[], byte[]> call, Metadata headers) {
return new ServerCall.Listener<byte[]>() {
};
}
};
// Warm-up JVM
new BinlogHelper(mock(SinkWriter.class)).getServerInterceptor(1234).interceptCall(noopServerCall, new Metadata(), noopHandler);
// We expect the contents of the "grpc-timeout" header to be installed the context
Context.current().withDeadlineAfter(1, TimeUnit.SECONDS, Executors.newSingleThreadScheduledExecutor()).run(new Runnable() {
@Override
public void run() {
ServerCall.Listener<byte[]> unused = new BinlogHelper(mockSinkWriter).getServerInterceptor(CALL_ID).interceptCall(noopServerCall, new Metadata(), noopHandler);
}
});
ArgumentCaptor<Duration> timeoutCaptor = ArgumentCaptor.forClass(Duration.class);
verify(mockSinkWriter).logClientHeader(/*seq=*/
eq(1L), eq("service/method"), ArgumentMatchers.<String>isNull(), timeoutCaptor.capture(), any(Metadata.class), eq(Logger.LOGGER_SERVER), eq(CALL_ID), ArgumentMatchers.<SocketAddress>isNull());
verifyNoMoreInteractions(mockSinkWriter);
Duration timeout = timeoutCaptor.getValue();
assertThat(TimeUnit.SECONDS.toNanos(1) - Durations.toNanos(timeout)).isAtMost(TimeUnit.MILLISECONDS.toNanos(250));
}
Aggregations