Search in sources :

Example 41 with ClientStreamTracer

use of io.grpc.ClientStreamTracer 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 42 with ClientStreamTracer

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

the class DelayedClientTransportTest method reprocess_NoPendingStream.

@Test
public void reprocess_NoPendingStream() {
    SubchannelPicker picker = mock(SubchannelPicker.class);
    AbstractSubchannel subchannel = mock(AbstractSubchannel.class);
    when(subchannel.getInternalSubchannel()).thenReturn(mockInternalSubchannel);
    when(picker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withSubchannel(subchannel));
    when(mockRealTransport.newStream(any(MethodDescriptor.class), any(Metadata.class), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any())).thenReturn(mockRealStream);
    delayedTransport.reprocess(picker);
    verifyNoMoreInteractions(picker);
    verifyNoMoreInteractions(transportListener);
    // Though picker was not originally used, it will be saved and serve future streams.
    ClientStream stream = delayedTransport.newStream(method, headers, CallOptions.DEFAULT, tracers);
    verify(picker).pickSubchannel(new PickSubchannelArgsImpl(method, headers, CallOptions.DEFAULT));
    verify(mockInternalSubchannel).obtainActiveTransport();
    assertSame(mockRealStream, stream);
}
Also used : SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) ClientStreamTracer(io.grpc.ClientStreamTracer) Metadata(io.grpc.Metadata) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) CallOptions(io.grpc.CallOptions) MethodDescriptor(io.grpc.MethodDescriptor) Test(org.junit.Test)

Example 43 with ClientStreamTracer

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

the class AbstractTransportTest method doPingPong.

/**
 * Helper that simply does an RPC. It can be used similar to a sleep for negative testing: to give
 * time for actions _not_ to happen. Since it is based on doing an actual RPC with actual
 * callbacks, it generally provides plenty of time for Runnables to execute. But it is also faster
 * on faster machines and more reliable on slower machines.
 */
private void doPingPong(MockServerListener serverListener) throws Exception {
    ManagedClientTransport client = newClientTransport(server);
    ManagedClientTransport.Listener listener = mock(ManagedClientTransport.Listener.class);
    startTransport(client, listener);
    ClientStream clientStream = client.newStream(methodDescriptor, new Metadata(), callOptions, new ClientStreamTracer[] { new ClientStreamTracer() {
    } });
    ClientStreamListenerBase clientStreamListener = new ClientStreamListenerBase();
    clientStream.start(clientStreamListener);
    MockServerTransportListener serverTransportListener = serverListener.takeListenerOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    StreamCreation serverStreamCreation = serverTransportListener.takeStreamOrFail(TIMEOUT_MS, TimeUnit.MILLISECONDS);
    ServerStream serverStream = serverStreamCreation.stream;
    ServerStreamListenerBase serverStreamListener = serverStreamCreation.listener;
    serverStream.close(Status.OK, new Metadata());
    assertNotNull(clientStreamListener.status.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertNotNull(clientStreamListener.trailers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    assertNotNull(serverStreamListener.status.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));
    client.shutdown(Status.UNAVAILABLE);
}
Also used : TestClientStreamTracer(io.grpc.internal.testing.TestClientStreamTracer) ClientStreamTracer(io.grpc.ClientStreamTracer) Metadata(io.grpc.Metadata)

Example 44 with ClientStreamTracer

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

the class ClientCallImplTest method setUp.

@Before
public void setUp() {
    when(clientStreamProvider.newStream((MethodDescriptor<?, ?>) any(MethodDescriptor.class), any(CallOptions.class), any(Metadata.class), any(Context.class))).thenReturn(stream);
    when(streamTracerFactory.newClientStreamTracer(any(StreamInfo.class), any(Metadata.class))).thenReturn(new ClientStreamTracer() {
    });
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock in) {
            InsightBuilder insight = (InsightBuilder) in.getArguments()[0];
            insight.appendKeyValue("remote_addr", "127.0.0.1:443");
            return null;
        }
    }).when(stream).appendTimeoutInsight(any(InsightBuilder.class));
    baseCallOptions = CallOptions.DEFAULT.withStreamTracerFactory(streamTracerFactory);
}
Also used : Context(io.grpc.Context) ClientStreamTracer(io.grpc.ClientStreamTracer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Metadata(io.grpc.Metadata) StreamInfo(io.grpc.ClientStreamTracer.StreamInfo) CallOptions(io.grpc.CallOptions) Before(org.junit.Before)

Example 45 with ClientStreamTracer

use of io.grpc.ClientStreamTracer 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

ClientStreamTracer (io.grpc.ClientStreamTracer)57 Metadata (io.grpc.Metadata)54 Test (org.junit.Test)44 CallOptions (io.grpc.CallOptions)28 Subchannel (io.grpc.LoadBalancer.Subchannel)22 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)21 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)19 ForwardingSubchannel (io.grpc.util.ForwardingSubchannel)18 MethodDescriptor (io.grpc.MethodDescriptor)16 Status (io.grpc.Status)14 ChannelLogger (io.grpc.ChannelLogger)11 ClientTransportOptions (io.grpc.internal.ClientTransportFactory.ClientTransportOptions)10 ProxiedSocketAddress (io.grpc.ProxiedSocketAddress)9 StreamInfo (io.grpc.ClientStreamTracer.StreamInfo)8 CallAttemptsTracerFactory (io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)8 SocketAddress (java.net.SocketAddress)8 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)7 Helper (io.grpc.LoadBalancer.Helper)7 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)7 ManagedChannel (io.grpc.ManagedChannel)7