use of io.grpc.CallOptions in project grpc-java by grpc.
the class ManagedChannelImplTest method interceptor.
@Test
public void interceptor() throws Exception {
final AtomicLong atomic = new AtomicLong();
ClientInterceptor interceptor = new ClientInterceptor() {
@Override
public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> interceptCall(MethodDescriptor<RequestT, ResponseT> method, CallOptions callOptions, Channel next) {
atomic.set(1);
return next.newCall(method, callOptions);
}
};
createChannel(new FakeNameResolverFactory(true), Arrays.asList(interceptor));
assertNotNull(channel.newCall(method, CallOptions.DEFAULT));
assertEquals(1, atomic.get());
}
use of io.grpc.CallOptions in project grpc-java by grpc.
the class ClientCallImplTest method startAddsMaxSize.
@Test
public void startAddsMaxSize() {
CallOptions callOptions = CallOptions.DEFAULT.withMaxInboundMessageSize(1).withMaxOutboundMessageSize(2);
ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, new SerializingExecutor(Executors.newSingleThreadExecutor()), callOptions, statsTraceCtx, provider, deadlineCancellationExecutor).setDecompressorRegistry(decompressorRegistry);
call.start(callListener, new Metadata());
verify(stream).setMaxInboundMessageSize(1);
verify(stream).setMaxOutboundMessageSize(2);
}
use of io.grpc.CallOptions in project grpc-java by grpc.
the class ClientCallImplTest method contextDeadlineShouldNotOverrideSmallerMetadataTimeout.
@Test
public void contextDeadlineShouldNotOverrideSmallerMetadataTimeout() {
long deadlineNanos = TimeUnit.SECONDS.toNanos(2);
Context context = Context.current().withDeadlineAfter(deadlineNanos, TimeUnit.NANOSECONDS, deadlineCancellationExecutor);
context.attach();
CallOptions callOpts = CallOptions.DEFAULT.withDeadlineAfter(1, TimeUnit.SECONDS);
ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, MoreExecutors.directExecutor(), callOpts, statsTraceCtx, provider, deadlineCancellationExecutor);
Metadata headers = new Metadata();
call.start(callListener, headers);
assertTrue(headers.containsKey(GrpcUtil.TIMEOUT_KEY));
Long timeout = headers.get(GrpcUtil.TIMEOUT_KEY);
assertNotNull(timeout);
long callOptsNanos = TimeUnit.SECONDS.toNanos(1);
long deltaNanos = TimeUnit.MILLISECONDS.toNanos(400);
assertTimeoutBetween(timeout, callOptsNanos - deltaNanos, callOptsNanos);
}
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(SubchannelPicker picker) {
ArrayList<PendingStream> toProcess;
ArrayList<PendingStream> toRemove = new ArrayList<PendingStream>();
synchronized (lock) {
lastPicker = picker;
lastPickerVersion++;
if (pendingStreams == null || pendingStreams.isEmpty()) {
return;
}
toProcess = new ArrayList<PendingStream>(pendingStreams);
}
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();
}
executor.execute(new Runnable() {
@Override
public void run() {
stream.createRealStream(transport);
}
});
toRemove.add(stream);
}
// else: stay pending
}
synchronized (lock) {
// - shutdown() may be called, which may turn pendingStreams into null.
if (pendingStreams == null || pendingStreams.isEmpty()) {
return;
}
pendingStreams.removeAll(toRemove);
if (pendingStreams.isEmpty()) {
// 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).
channelExecutor.executeLater(reportTransportNotInUse);
if (shutdown) {
pendingStreams = null;
channelExecutor.executeLater(reportTransportTerminated);
} else {
// Because delayed transport is long-lived, we take this opportunity to down-size the
// hashmap.
pendingStreams = new LinkedHashSet<PendingStream>();
}
}
}
channelExecutor.drain();
}
use of io.grpc.CallOptions in project grpc-java by grpc.
the class ClientCallImplTest method contextDeadlineShouldOverrideLargerMetadataTimeout.
@Test
public void contextDeadlineShouldOverrideLargerMetadataTimeout() {
long deadlineNanos = TimeUnit.SECONDS.toNanos(1);
Context context = Context.current().withDeadlineAfter(deadlineNanos, TimeUnit.NANOSECONDS, deadlineCancellationExecutor);
context.attach();
CallOptions callOpts = CallOptions.DEFAULT.withDeadlineAfter(2, TimeUnit.SECONDS);
ClientCallImpl<Void, Void> call = new ClientCallImpl<Void, Void>(method, MoreExecutors.directExecutor(), callOpts, statsTraceCtx, provider, deadlineCancellationExecutor);
Metadata headers = new Metadata();
call.start(callListener, headers);
assertTrue(headers.containsKey(GrpcUtil.TIMEOUT_KEY));
Long timeout = headers.get(GrpcUtil.TIMEOUT_KEY);
assertNotNull(timeout);
long deltaNanos = TimeUnit.MILLISECONDS.toNanos(400);
assertTimeoutBetween(timeout, deadlineNanos - deltaNanos, deadlineNanos);
}
Aggregations