use of io.grpc.internal.NoopClientCall in project grpc-java by grpc.
the class BinaryLogProviderTest method wrapChannel_handler.
@Test
public void wrapChannel_handler() throws Exception {
final List<byte[]> serializedReq = new ArrayList<>();
final AtomicReference<ClientCall.Listener<?>> listener = new AtomicReference<>();
Channel channel = new Channel() {
@Override
public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
return new NoopClientCall<RequestT, ResponseT>() {
@Override
public void start(ClientCall.Listener<ResponseT> responseListener, Metadata headers) {
listener.set(responseListener);
}
@Override
public void sendMessage(RequestT message) {
serializedReq.add((byte[]) message);
}
};
}
@Override
public String authority() {
throw new UnsupportedOperationException();
}
};
Channel wChannel = binlogProvider.wrapChannel(channel);
ClientCall<String, Integer> clientCall = wChannel.newCall(method, CallOptions.DEFAULT);
final List<Integer> observedResponse = new ArrayList<>();
clientCall.start(new NoopClientCall.NoopClientCallListener<Integer>() {
@Override
public void onMessage(Integer message) {
observedResponse.add(message);
}
}, new Metadata());
String expectedRequest = "hello world";
assertThat(binlogReq).isEmpty();
assertThat(serializedReq).isEmpty();
assertEquals(0, reqMarshaller.streamInvocations);
clientCall.sendMessage(expectedRequest);
// it is unacceptably expensive for the binlog to double parse every logged message
assertEquals(1, reqMarshaller.streamInvocations);
assertEquals(0, reqMarshaller.parseInvocations);
assertThat(binlogReq).hasSize(1);
assertThat(serializedReq).hasSize(1);
assertEquals(expectedRequest, StringMarshaller.INSTANCE.parse(new ByteArrayInputStream(binlogReq.get(0))));
assertEquals(expectedRequest, StringMarshaller.INSTANCE.parse(new ByteArrayInputStream(serializedReq.get(0))));
int expectedResponse = 12345;
assertThat(binlogResp).isEmpty();
assertThat(observedResponse).isEmpty();
assertEquals(0, respMarshaller.parseInvocations);
onClientMessageHelper(listener.get(), IntegerMarshaller.INSTANCE.stream(expectedResponse));
// it is unacceptably expensive for the binlog to double parse every logged message
assertEquals(1, respMarshaller.parseInvocations);
assertEquals(0, respMarshaller.streamInvocations);
assertThat(binlogResp).hasSize(1);
assertThat(observedResponse).hasSize(1);
assertEquals(expectedResponse, (int) IntegerMarshaller.INSTANCE.parse(new ByteArrayInputStream(binlogResp.get(0))));
assertEquals(expectedResponse, (int) observedResponse.get(0));
}
use of io.grpc.internal.NoopClientCall in project grpc-java by grpc.
the class BinaryLogProviderTest method wrapChannel_methodDescriptor.
@Test
public void wrapChannel_methodDescriptor() throws Exception {
final AtomicReference<MethodDescriptor<?, ?>> methodRef = new AtomicReference<>();
Channel channel = new Channel() {
@Override
public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(MethodDescriptor<RequestT, ResponseT> method, CallOptions callOptions) {
methodRef.set(method);
return new NoopClientCall<>();
}
@Override
public String authority() {
throw new UnsupportedOperationException();
}
};
Channel wChannel = binlogProvider.wrapChannel(channel);
ClientCall<String, Integer> unusedClientCall = wChannel.newCall(method, CallOptions.DEFAULT);
validateWrappedMethod(methodRef.get());
}
use of io.grpc.internal.NoopClientCall in project grpc-java by grpc.
the class ClientCallsTest method unaryFutureCallFailed.
@Test
public void unaryFutureCallFailed() throws Exception {
final AtomicReference<ClientCall.Listener<String>> listener = new AtomicReference<>();
NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {
@Override
public void start(io.grpc.ClientCall.Listener<String> responseListener, Metadata headers) {
listener.set(responseListener);
}
};
Integer req = 2;
ListenableFuture<String> future = ClientCalls.futureUnaryCall(call, req);
Metadata trailers = new Metadata();
listener.get().onClose(Status.INTERNAL, trailers);
try {
future.get();
fail("Should fail");
} catch (ExecutionException e) {
Status status = Status.fromThrowable(e);
assertEquals(Status.INTERNAL, status);
Metadata metadata = Status.trailersFromThrowable(e);
assertSame(trailers, metadata);
}
}
use of io.grpc.internal.NoopClientCall in project grpc-java by grpc.
the class ClientCallsTest method unaryBlockingCallFailed.
@Test
public void unaryBlockingCallFailed() throws Exception {
Integer req = 2;
final Status status = Status.INTERNAL.withDescription("Unique status");
final Metadata trailers = new Metadata();
NoopClientCall<Integer, String> call = new NoopClientCall<Integer, String>() {
@Override
public void start(io.grpc.ClientCall.Listener<String> listener, Metadata headers) {
listener.onClose(status, trailers);
}
};
try {
ClientCalls.blockingUnaryCall(call, req);
fail("Should fail");
} catch (StatusRuntimeException e) {
assertSame(status, e.getStatus());
assertSame(trailers, e.getTrailers());
}
}
Aggregations