Search in sources :

Example 61 with CallOptions

use of io.grpc.CallOptions in project grpc-java by grpc.

the class CallCredentialsApplyingTest method fail_delayed.

@Test
public void fail_delayed() {
    when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);
    // Will call applyRequestMetadata(), which is no-op.
    DelayedStream stream = (DelayedStream) transport.newStream(method, origHeaders, callOptions, tracers);
    ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
    verify(mockCreds).applyRequestMetadata(any(RequestInfo.class), same(mockExecutor), applierCaptor.capture());
    Status error = Status.FAILED_PRECONDITION.withDescription("channel not secure for creds");
    applierCaptor.getValue().fail(error);
    verify(mockTransport, never()).newStream(any(MethodDescriptor.class), any(Metadata.class), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any());
    FailingClientStream failingStream = (FailingClientStream) stream.getRealStream();
    assertSame(error, failingStream.getError());
    transport.shutdown(Status.UNAVAILABLE);
    assertTrue(transport.newStream(method, origHeaders, callOptions, tracers) instanceof FailingClientStream);
    verify(mockTransport).shutdown(Status.UNAVAILABLE);
}
Also used : Status(io.grpc.Status) ClientStreamTracer(io.grpc.ClientStreamTracer) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) RequestInfo(io.grpc.CallCredentials.RequestInfo) MethodDescriptor(io.grpc.MethodDescriptor) Test(org.junit.Test)

Example 62 with CallOptions

use of io.grpc.CallOptions in project grpc-java by grpc.

the class ClientCallImplTest method contextDeadlineShouldOverrideLargerCallOptionsDeadline.

@Test
public void contextDeadlineShouldOverrideLargerCallOptionsDeadline() {
    Context context = Context.current().withDeadlineAfter(1000, TimeUnit.MILLISECONDS, deadlineCancellationExecutor);
    Context origContext = context.attach();
    CallOptions callOpts = baseCallOptions.withDeadlineAfter(2000, TimeUnit.MILLISECONDS);
    ClientCallImpl<Void, Void> call = new ClientCallImpl<>(method, MoreExecutors.directExecutor(), callOpts, clientStreamProvider, deadlineCancellationExecutor, channelCallTracer, configSelector);
    call.start(callListener, new Metadata());
    context.detach(origContext);
    ArgumentCaptor<Deadline> deadlineCaptor = ArgumentCaptor.forClass(Deadline.class);
    verify(stream).setDeadline(deadlineCaptor.capture());
    assertTimeoutBetween(deadlineCaptor.getValue().timeRemaining(TimeUnit.MILLISECONDS), 600, 1000);
}
Also used : Context(io.grpc.Context) Deadline(io.grpc.Deadline) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Test(org.junit.Test)

Example 63 with CallOptions

use of io.grpc.CallOptions in project grpc-java by grpc.

the class ClientCallImplTest method callOptionsDeadlineShouldBePropagatedToStream.

@Test
public void callOptionsDeadlineShouldBePropagatedToStream() {
    CallOptions callOpts = baseCallOptions.withDeadlineAfter(1000, TimeUnit.MILLISECONDS);
    ClientCallImpl<Void, Void> call = new ClientCallImpl<>(method, MoreExecutors.directExecutor(), callOpts, clientStreamProvider, deadlineCancellationExecutor, channelCallTracer, configSelector);
    call.start(callListener, new Metadata());
    ArgumentCaptor<Deadline> deadlineCaptor = ArgumentCaptor.forClass(Deadline.class);
    verify(stream).setDeadline(deadlineCaptor.capture());
    assertTimeoutBetween(deadlineCaptor.getValue().timeRemaining(TimeUnit.MILLISECONDS), 600, 1000);
}
Also used : Deadline(io.grpc.Deadline) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Test(org.junit.Test)

Example 64 with CallOptions

use of io.grpc.CallOptions in project grpc-java by grpc.

the class ClientCallImplTest method startAddsMaxSize.

@Test
public void startAddsMaxSize() {
    CallOptions callOptions = baseCallOptions.withMaxInboundMessageSize(1).withMaxOutboundMessageSize(2);
    ClientCallImpl<Void, Void> call = new ClientCallImpl<>(method, new SerializingExecutor(Executors.newSingleThreadExecutor()), callOptions, clientStreamProvider, deadlineCancellationExecutor, channelCallTracer, configSelector).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 65 with CallOptions

use of io.grpc.CallOptions in project grpc-java by grpc.

the class RetryTest method transparentRetryStatsRecorded.

@Test
public void transparentRetryStatsRecorded() throws Exception {
    startNewServer();
    createNewChannel();
    final AtomicBoolean originalAttemptFailed = new AtomicBoolean();
    class TransparentRetryTriggeringTracer extends ClientStreamTracer {

        @Override
        public void streamCreated(Attributes transportAttrs, Metadata metadata) {
            if (originalAttemptFailed.get()) {
                return;
            }
            // Send GOAWAY from server. The client may either receive GOAWAY or create the underlying
            // netty stream and write headers first, even we await server termination as below.
            // In the latter case, we rerun the test. We can also call localServer.shutdown() to trigger
            // GOAWAY, but it takes a lot longer time to gracefully shut down.
            localServer.shutdownNow();
            try {
                localServer.awaitTermination();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new AssertionError(e);
            }
        }

        @Override
        public void streamClosed(Status status) {
            if (originalAttemptFailed.get()) {
                return;
            }
            originalAttemptFailed.set(true);
            try {
                startNewServer();
                channel.resetConnectBackoff();
            } catch (Exception e) {
                throw new AssertionError("local server can not be restarted", e);
            }
        }
    }
    class TransparentRetryTracerFactory extends ClientStreamTracer.Factory {

        @Override
        public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata headers) {
            return new TransparentRetryTriggeringTracer();
        }
    }
    CallOptions callOptions = CallOptions.DEFAULT.withWaitForReady().withStreamTracerFactory(new TransparentRetryTracerFactory());
    while (true) {
        ClientCall<String, Integer> call = channel.newCall(clientStreamingMethod, callOptions);
        call.start(mockCallListener, new Metadata());
        // original attempt
        assertRpcStartedRecorded();
        MetricsRecord record = clientStatsRecorder.pollRecord(5, SECONDS);
        assertThat(record.getMetricAsLongOrFail(DeprecatedCensusConstants.RPC_CLIENT_FINISHED_COUNT)).isEqualTo(1);
        TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
        if (statusTag.asString().equals(Code.UNAVAILABLE.toString())) {
            break;
        } else {
            // Due to race condition, GOAWAY is not received/processed before the stream is closed due
            // to connection error. Rerun the test.
            assertThat(statusTag.asString()).isEqualTo(Code.UNKNOWN.toString());
            assertRetryStatsRecorded(0, 0, 0);
            originalAttemptFailed.set(false);
        }
    }
    // retry attempt
    assertRpcStartedRecorded();
    ServerCall<String, Integer> serverCall = serverCalls.poll(5, SECONDS);
    serverCall.close(Status.INVALID_ARGUMENT, new Metadata());
    assertRpcStatusRecorded(Code.INVALID_ARGUMENT, 0, 0);
    assertRetryStatsRecorded(0, 1, 0);
}
Also used : Status(io.grpc.Status) ClientStreamTracer(io.grpc.ClientStreamTracer) MetricsRecord(io.grpc.internal.testing.StatsTestUtils.MetricsRecord) Attributes(io.grpc.Attributes) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StreamInfo(io.grpc.ClientStreamTracer.StreamInfo) TagValue(io.opencensus.tags.TagValue) Test(org.junit.Test)

Aggregations

CallOptions (io.grpc.CallOptions)81 Test (org.junit.Test)61 Metadata (io.grpc.Metadata)50 Channel (io.grpc.Channel)36 MethodDescriptor (io.grpc.MethodDescriptor)28 ClientInterceptor (io.grpc.ClientInterceptor)23 ManagedChannel (io.grpc.ManagedChannel)18 ClientStreamTracer (io.grpc.ClientStreamTracer)17 ClientCall (io.grpc.ClientCall)15 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)11 Status (io.grpc.Status)11 Context (io.grpc.Context)10 Subchannel (io.grpc.LoadBalancer.Subchannel)10 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)10 ForwardingSubchannel (io.grpc.util.ForwardingSubchannel)9 SimpleForwardingClientCall (io.grpc.ForwardingClientCall.SimpleForwardingClientCall)8 NoopClientCall (io.grpc.internal.NoopClientCall)8 SocketAddress (java.net.SocketAddress)8 SimpleForwardingClientCallListener (io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener)7 ByteString (com.google.protobuf.ByteString)6