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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations