use of io.grpc.CallOptions 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.CallOptions 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.CallOptions in project grpc-java by grpc.
the class DelayedClientTransport method reprocess.
/**
* Use the picker to try picking a transport for every pending stream, proceed the stream if the
* pick is successful, otherwise keep it pending.
*
* <p>This method may be called concurrently with {@code newStream()}, and it's safe. All pending
* streams will be served by the latest picker (if a same picker is given more than once, they are
* considered different pickers) as soon as possible.
*
* <p>This method <strong>must not</strong> be called concurrently with itself.
*/
final void reprocess(@Nullable SubchannelPicker picker) {
ArrayList<PendingStream> toProcess;
synchronized (lock) {
lastPicker = picker;
lastPickerVersion++;
if (picker == null || !hasPendingStreams()) {
return;
}
toProcess = new ArrayList<>(pendingStreams);
}
ArrayList<PendingStream> toRemove = new ArrayList<>();
for (final PendingStream stream : toProcess) {
PickResult pickResult = picker.pickSubchannel(stream.args);
CallOptions callOptions = stream.args.getCallOptions();
final ClientTransport transport = GrpcUtil.getTransportFromPickResult(pickResult, callOptions.isWaitForReady());
if (transport != null) {
Executor executor = defaultAppExecutor;
// we are now on transport thread, we need to offload the work to an executor.
if (callOptions.getExecutor() != null) {
executor = callOptions.getExecutor();
}
Runnable runnable = stream.createRealStream(transport);
if (runnable != null) {
executor.execute(runnable);
}
toRemove.add(stream);
}
// else: stay pending
}
synchronized (lock) {
// - shutdown() may be called, which may turn pendingStreams into null.
if (!hasPendingStreams()) {
return;
}
pendingStreams.removeAll(toRemove);
// hashmap.
if (pendingStreams.isEmpty()) {
pendingStreams = new LinkedHashSet<>();
}
if (!hasPendingStreams()) {
// There may be a brief gap between delayed transport clearing in-use state, and first real
// transport starting streams and setting in-use state. During the gap the whole channel's
// in-use state may be false. However, it shouldn't cause spurious switching to idleness
// (which would shutdown the transports and LoadBalancer) because the gap should be shorter
// than IDLE_MODE_DEFAULT_TIMEOUT_MILLIS (1 second).
syncContext.executeLater(reportTransportNotInUse);
if (shutdownStatus != null && reportTransportTerminated != null) {
syncContext.executeLater(reportTransportTerminated);
reportTransportTerminated = null;
}
}
}
syncContext.drain();
}
use of io.grpc.CallOptions in project grpc-java by grpc.
the class AbstractFutureStubTest method defaultCallOptions.
@Test
public void defaultCallOptions() {
NoopFutureStub stub = NoopFutureStub.newStub(new StubFactory<NoopFutureStub>() {
@Override
public NoopFutureStub newStub(Channel channel, CallOptions callOptions) {
return create(channel, callOptions);
}
}, channel, CallOptions.DEFAULT);
assertThat(stub.getCallOptions().getOption(ClientCalls.STUB_TYPE_OPTION)).isEqualTo(StubType.FUTURE);
}
use of io.grpc.CallOptions in project grpc-java by grpc.
the class BaseAbstractStubTest method withExecutor.
@Test
public void withExecutor() {
T stub = create(channel);
CallOptions callOptions = stub.getCallOptions();
assertNull(callOptions.getExecutor());
Executor executor = mock(Executor.class);
stub = stub.withExecutor(executor);
callOptions = stub.getCallOptions();
assertEquals(callOptions.getExecutor(), executor);
}
Aggregations