use of io.grpc.Channel in project grpc-java by grpc.
the class AbstractFutureStubTest method newStub_blockingStub_throwsException.
@Test
@SuppressWarnings("AssertionFailureIgnored")
public void newStub_blockingStub_throwsException() {
try {
NoopBlockingStub unused = NoopFutureStub.newStub(new StubFactory<NoopBlockingStub>() {
@Override
public NoopBlockingStub newStub(Channel channel, CallOptions callOptions) {
return new NoopBlockingStub(channel, callOptions);
}
}, channel, CallOptions.DEFAULT);
fail("should not reach here");
} catch (AssertionError e) {
assertThat(e).hasMessageThat().startsWith("Expected AbstractFutureStub");
}
}
use of io.grpc.Channel in project grpc-java by grpc.
the class AbstractFutureStubTest method newStub_asyncStub_throwsException.
@Test
@SuppressWarnings("AssertionFailureIgnored")
public void newStub_asyncStub_throwsException() {
try {
NoopAsyncStub unused = NoopFutureStub.newStub(new StubFactory<NoopAsyncStub>() {
@Override
public NoopAsyncStub newStub(Channel channel, CallOptions callOptions) {
return new NoopAsyncStub(channel, callOptions);
}
}, channel, CallOptions.DEFAULT);
fail("should not reach here");
} catch (AssertionError e) {
assertThat(e).hasMessageThat().startsWith("Expected AbstractFutureStub");
}
}
use of io.grpc.Channel in project grpc-java by grpc.
the class AltsProtocolNegotiatorTest method setup.
@Before
public void setup() throws Exception {
ChannelHandler uncaughtExceptionHandler = new ChannelDuplexHandler() {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
caughtException = cause;
super.exceptionCaught(ctx, cause);
ctx.close();
}
};
TsiHandshakerFactory handshakerFactory = new DelegatingTsiHandshakerFactory(FakeTsiHandshaker.clientHandshakerFactory()) {
@Override
public TsiHandshaker newHandshaker(String authority, ChannelLogger logger) {
return new DelegatingTsiHandshaker(super.newHandshaker(authority, logger)) {
@Override
public TsiPeer extractPeer() throws GeneralSecurityException {
return mockedTsiPeer;
}
@Override
public Object extractPeerObject() throws GeneralSecurityException {
return mockedAltsContext;
}
};
}
};
ManagedChannel fakeChannel = NettyChannelBuilder.forTarget("localhost:8080").build();
ObjectPool<Channel> fakeChannelPool = new FixedObjectPool<Channel>(fakeChannel);
LazyChannel lazyFakeChannel = new LazyChannel(fakeChannelPool);
ChannelHandler altsServerHandler = new ServerAltsProtocolNegotiator(handshakerFactory, lazyFakeChannel).newHandler(grpcHandler);
// On real server, WBAEH fires default ProtocolNegotiationEvent. KickNH provides this behavior.
ChannelHandler handler = new KickNegotiationHandler(altsServerHandler);
channel = new EmbeddedChannel(uncaughtExceptionHandler, handler);
}
use of io.grpc.Channel in project grpc-java by grpc.
the class AsyncClient method doBenchmark.
private List<Histogram> doBenchmark(SimpleRequest req, List<? extends Channel> channels, long endTime) throws Exception {
// Initiate the concurrent calls
List<Future<Histogram>> futures = new ArrayList<>(config.outstandingRpcsPerChannel);
for (int i = 0; i < config.channels; i++) {
for (int j = 0; j < config.outstandingRpcsPerChannel; j++) {
Channel channel = channels.get(i);
futures.add(doRpcs(channel, req, endTime));
}
}
// Wait for completion
List<Histogram> histograms = new ArrayList<>(futures.size());
for (Future<Histogram> future : futures) {
histograms.add(future.get());
}
return histograms;
}
use of io.grpc.Channel in project grpc-java by grpc.
the class CensusModulesTest method testClientInterceptors.
// Test that Census ClientInterceptors uses the TagContext and Span out of the current Context
// to create the ClientCallTracer, and that it intercepts ClientCall.Listener.onClose() to call
// ClientCallTracer.callEnded().
private void testClientInterceptors(boolean nonDefaultContext) {
grpcServerRule.getServiceRegistry().addService(ServerServiceDefinition.builder("package1.service2").addMethod(method, new ServerCallHandler<String, String>() {
@Override
public ServerCall.Listener<String> startCall(ServerCall<String, String> call, Metadata headers) {
call.sendHeaders(new Metadata());
call.sendMessage("Hello");
call.close(Status.PERMISSION_DENIED.withDescription("No you don't"), new Metadata());
return mockServerCallListener;
}
}).build());
final AtomicReference<CallOptions> capturedCallOptions = new AtomicReference<>();
ClientInterceptor callOptionsCaptureInterceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
capturedCallOptions.set(callOptions);
return next.newCall(method, callOptions);
}
};
Channel interceptedChannel = ClientInterceptors.intercept(grpcServerRule.getChannel(), callOptionsCaptureInterceptor, censusStats.getClientInterceptor(), censusTracing.getClientInterceptor());
ClientCall<String, String> call;
if (nonDefaultContext) {
Context ctx = io.opencensus.tags.unsafe.ContextUtils.withValue(Context.ROOT, tagger.emptyBuilder().putLocal(StatsTestUtils.EXTRA_TAG, TagValue.create("extra value")).build());
ctx = ContextUtils.withValue(ctx, fakeClientParentSpan);
Context origCtx = ctx.attach();
try {
call = interceptedChannel.newCall(method, CALL_OPTIONS);
} finally {
ctx.detach(origCtx);
}
} else {
assertEquals(io.opencensus.tags.unsafe.ContextUtils.getValue(Context.ROOT), io.opencensus.tags.unsafe.ContextUtils.getValue(Context.current()));
assertEquals(ContextUtils.getValue(Context.current()), BlankSpan.INSTANCE);
call = interceptedChannel.newCall(method, CALL_OPTIONS);
}
// The interceptor adds tracer factory to CallOptions
assertEquals("customvalue", capturedCallOptions.get().getOption(CUSTOM_OPTION));
assertEquals(2, capturedCallOptions.get().getStreamTracerFactories().size());
assertTrue(capturedCallOptions.get().getStreamTracerFactories().get(0) instanceof CallAttemptsTracerFactory);
assertTrue(capturedCallOptions.get().getStreamTracerFactories().get(1) instanceof CensusStatsModule.CallAttemptsTracerFactory);
// Make the call
Metadata headers = new Metadata();
call.start(mockClientCallListener, headers);
StatsTestUtils.MetricsRecord record = statsRecorder.pollRecord();
assertNotNull(record);
TagValue methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
if (nonDefaultContext) {
TagValue extraTag = record.tags.get(StatsTestUtils.EXTRA_TAG);
assertEquals("extra value", extraTag.asString());
assertEquals(2, record.tags.size());
} else {
assertNull(record.tags.get(StatsTestUtils.EXTRA_TAG));
assertEquals(1, record.tags.size());
}
if (nonDefaultContext) {
verify(tracer).spanBuilderWithExplicitParent(eq("Sent.package1.service2.method3"), same(fakeClientParentSpan));
verify(spyClientSpanBuilder).setRecordEvents(eq(true));
} else {
verify(tracer).spanBuilderWithExplicitParent(eq("Sent.package1.service2.method3"), ArgumentMatchers.<Span>isNotNull());
verify(spyClientSpanBuilder).setRecordEvents(eq(true));
}
verify(spyClientSpan, never()).end(any(EndSpanOptions.class));
// End the call
call.halfClose();
call.request(1);
verify(mockClientCallListener).onClose(statusCaptor.capture(), any(Metadata.class));
Status status = statusCaptor.getValue();
assertEquals(Status.Code.PERMISSION_DENIED, status.getCode());
assertEquals("No you don't", status.getDescription());
// The intercepting listener calls callEnded() on ClientCallTracer, which records to Census.
record = statsRecorder.pollRecord();
assertNotNull(record);
methodTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_METHOD);
assertEquals(method.getFullMethodName(), methodTag.asString());
TagValue statusTag = record.tags.get(RpcMeasureConstants.GRPC_CLIENT_STATUS);
assertEquals(Status.Code.PERMISSION_DENIED.toString(), statusTag.asString());
if (nonDefaultContext) {
TagValue extraTag = record.tags.get(StatsTestUtils.EXTRA_TAG);
assertEquals("extra value", extraTag.asString());
} else {
assertNull(record.tags.get(StatsTestUtils.EXTRA_TAG));
}
verify(spyClientSpan).end(EndSpanOptions.builder().setStatus(io.opencensus.trace.Status.PERMISSION_DENIED.withDescription("No you don't")).setSampleToLocalSpanStore(false).build());
verify(spyClientSpan, never()).end();
assertZeroRetryRecorded();
}
Aggregations