Search in sources :

Example 11 with CallOptions

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());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) ClientInterceptor(io.grpc.ClientInterceptor) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) Test(org.junit.Test)

Example 12 with CallOptions

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);
}
Also used : Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Test(org.junit.Test)

Example 13 with CallOptions

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);
}
Also used : Context(io.grpc.Context) StatsContext(com.google.instrumentation.stats.StatsContext) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Test(org.junit.Test)

Example 14 with CallOptions

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();
}
Also used : Executor(java.util.concurrent.Executor) ArrayList(java.util.ArrayList) PickResult(io.grpc.LoadBalancer.PickResult) CallOptions(io.grpc.CallOptions)

Example 15 with CallOptions

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);
}
Also used : Context(io.grpc.Context) StatsContext(com.google.instrumentation.stats.StatsContext) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Test(org.junit.Test)

Aggregations

CallOptions (io.grpc.CallOptions)17 Test (org.junit.Test)12 Metadata (io.grpc.Metadata)10 MethodDescriptor (io.grpc.MethodDescriptor)7 Channel (io.grpc.Channel)4 ClientInterceptor (io.grpc.ClientInterceptor)4 Context (io.grpc.Context)4 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)3 ManagedChannel (io.grpc.ManagedChannel)3 StatsContext (com.google.instrumentation.stats.StatsContext)2 Subchannel (io.grpc.LoadBalancer.Subchannel)2 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)2 Executor (java.util.concurrent.Executor)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ByteString (com.google.protobuf.ByteString)1 PartialResultSet (com.google.spanner.v1.PartialResultSet)1 Attributes (io.grpc.Attributes)1 MetadataApplier (io.grpc.CallCredentials.MetadataApplier)1 ClientCall (io.grpc.ClientCall)1