use of io.grpc.internal.FakeClock in project grpc-java by grpc.
the class InProcessServerTest method serverHoldsRefToScheduler.
@Test
public void serverHoldsRefToScheduler() throws Exception {
final ScheduledExecutorService ses = new FakeClock().getScheduledExecutorService();
class RefCountingObjectPool implements ObjectPool<ScheduledExecutorService> {
private int count;
@Override
public ScheduledExecutorService getObject() {
count++;
return ses;
}
@Override
public ScheduledExecutorService returnObject(Object returned) {
count--;
return null;
}
}
RefCountingObjectPool pool = new RefCountingObjectPool();
builder.schedulerPool = pool;
InProcessServer s = new InProcessServer(builder, Collections.<ServerStreamTracer.Factory>emptyList());
Truth.assertThat(pool.count).isEqualTo(0);
s.start(new ServerListener() {
@Override
public ServerTransportListener transportCreated(ServerTransport transport) {
throw new UnsupportedOperationException();
}
@Override
public void serverShutdown() {
}
});
Truth.assertThat(pool.count).isEqualTo(1);
s.shutdown();
Truth.assertThat(pool.count).isEqualTo(0);
}
use of io.grpc.internal.FakeClock in project grpc-java by grpc.
the class InProcessServerBuilderTest method scheduledExecutorService_custom.
@Test
public void scheduledExecutorService_custom() {
InProcessServerBuilder builder = InProcessServerBuilder.forName("foo");
ScheduledExecutorService scheduledExecutorService = new FakeClock().getScheduledExecutorService();
InProcessServerBuilder builder1 = builder.scheduledExecutorService(scheduledExecutorService);
assertSame(builder, builder1);
InProcessServer server = builder1.buildTransportServers(new ArrayList<ServerStreamTracer.Factory>());
ObjectPool<ScheduledExecutorService> scheduledExecutorServicePool = server.getScheduledExecutorServicePool();
assertSame(scheduledExecutorService, scheduledExecutorServicePool.getObject());
scheduledExecutorServicePool.returnObject(scheduledExecutorService);
}
use of io.grpc.internal.FakeClock in project grpc-java by grpc.
the class OkHttpChannelBuilderTest method scheduledExecutorService_custom.
@Test
public void scheduledExecutorService_custom() {
OkHttpChannelBuilder builder = OkHttpChannelBuilder.forTarget("foo");
ScheduledExecutorService scheduledExecutorService = new FakeClock().getScheduledExecutorService();
OkHttpChannelBuilder builder1 = builder.scheduledExecutorService(scheduledExecutorService);
assertSame(builder, builder1);
ClientTransportFactory clientTransportFactory = builder1.buildTransportFactory();
assertSame(scheduledExecutorService, clientTransportFactory.getScheduledExecutorService());
clientTransportFactory.close();
}
use of io.grpc.internal.FakeClock in project grpc-java by grpc.
the class NettyClientTransportTest method failingToConstructChannelShouldFailGracefully.
@Test
public void failingToConstructChannelShouldFailGracefully() throws Exception {
address = TestUtils.testServerAddress(new InetSocketAddress(12345));
authority = GrpcUtil.authorityFromHostAndPort(address.getHostString(), address.getPort());
NettyClientTransport transport = new NettyClientTransport(address, new ReflectiveChannelFactory<>(CantConstructChannel.class), new HashMap<ChannelOption<?>, Object>(), group, newNegotiator(), false, DEFAULT_WINDOW_SIZE, DEFAULT_MAX_MESSAGE_SIZE, GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE, KEEPALIVE_TIME_NANOS_DISABLED, 1, false, authority, null, tooManyPingsRunnable, new TransportTracer(), Attributes.EMPTY, new SocketPicker(), new FakeChannelLogger(), false);
transports.add(transport);
// Should not throw
callMeMaybe(transport.start(clientTransportListener));
// And RPCs and PINGs should fail cleanly, reporting the failure
Rpc rpc = new Rpc(transport);
try {
rpc.waitForResponse();
fail("Expected exception");
} catch (Exception ex) {
if (!(getRootCause(ex) instanceof CantConstructChannelError)) {
throw new AssertionError("Could not find expected error", ex);
}
}
final SettableFuture<Object> pingResult = SettableFuture.create();
FakeClock clock = new FakeClock();
ClientTransport.PingCallback pingCallback = new ClientTransport.PingCallback() {
@Override
public void onSuccess(long roundTripTimeNanos) {
pingResult.set(roundTripTimeNanos);
}
@Override
public void onFailure(Throwable cause) {
pingResult.setException(cause);
}
};
transport.ping(pingCallback, clock.getScheduledExecutorService());
assertFalse(pingResult.isDone());
clock.runDueTasks();
assertTrue(pingResult.isDone());
try {
pingResult.get();
fail("Expected exception");
} catch (Exception ex) {
if (!(getRootCause(ex) instanceof CantConstructChannelError)) {
throw new AssertionError("Could not find expected error", ex);
}
}
}
use of io.grpc.internal.FakeClock in project grpc-java by grpc.
the class SdsProtocolNegotiatorsTest method serverSdsHandler_addLast.
@Test
public void serverSdsHandler_addLast() throws InterruptedException, TimeoutException, ExecutionException {
FakeClock executor = new FakeClock();
CommonCertProviderTestUtils.register(executor);
// we need InetSocketAddress instead of EmbeddedSocketAddress as localAddress for this test
channel = new EmbeddedChannel() {
@Override
public SocketAddress localAddress() {
return new InetSocketAddress("172.168.1.1", 80);
}
@Override
public SocketAddress remoteAddress() {
return new InetSocketAddress("172.168.2.2", 90);
}
};
pipeline = channel.pipeline();
Bootstrapper.BootstrapInfo bootstrapInfoForServer = CommonBootstrapperTestUtils.buildBootstrapInfo("google_cloud_private_spiffe-server", SERVER_1_KEY_FILE, SERVER_1_PEM_FILE, CA_PEM_FILE, null, null, null, null);
DownstreamTlsContext downstreamTlsContext = CommonTlsContextTestsUtil.buildDownstreamTlsContext("google_cloud_private_spiffe-server", true, true);
TlsContextManagerImpl tlsContextManager = new TlsContextManagerImpl(bootstrapInfoForServer);
SdsProtocolNegotiators.HandlerPickerHandler handlerPickerHandler = new SdsProtocolNegotiators.HandlerPickerHandler(grpcHandler, InternalProtocolNegotiators.serverPlaintext());
pipeline.addLast(handlerPickerHandler);
channelHandlerCtx = pipeline.context(handlerPickerHandler);
// should find HandlerPickerHandler
assertThat(channelHandlerCtx).isNotNull();
// kick off protocol negotiation: should replace HandlerPickerHandler with ServerSdsHandler
ProtocolNegotiationEvent event = InternalProtocolNegotiationEvent.getDefault();
Attributes attr = InternalProtocolNegotiationEvent.getAttributes(event).toBuilder().set(ATTR_SERVER_SSL_CONTEXT_PROVIDER_SUPPLIER, new SslContextProviderSupplier(downstreamTlsContext, tlsContextManager)).build();
pipeline.fireUserEventTriggered(InternalProtocolNegotiationEvent.withAttributes(event, attr));
channelHandlerCtx = pipeline.context(handlerPickerHandler);
assertThat(channelHandlerCtx).isNull();
channelHandlerCtx = pipeline.context(SdsProtocolNegotiators.ServerSdsHandler.class);
assertThat(channelHandlerCtx).isNotNull();
SslContextProviderSupplier sslContextProviderSupplier = new SslContextProviderSupplier(downstreamTlsContext, tlsContextManager);
final SettableFuture<Object> future = SettableFuture.create();
sslContextProviderSupplier.updateSslContext(new SslContextProvider.Callback(MoreExecutors.directExecutor()) {
@Override
public void updateSecret(SslContext sslContext) {
future.set(sslContext);
}
@Override
protected void onException(Throwable throwable) {
future.set(throwable);
}
});
// need this for tasks to execute on eventLoop
channel.runPendingTasks();
assertThat(executor.runDueTasks()).isEqualTo(1);
Object fromFuture = future.get(2, TimeUnit.SECONDS);
assertThat(fromFuture).isInstanceOf(SslContext.class);
channel.runPendingTasks();
channelHandlerCtx = pipeline.context(SdsProtocolNegotiators.ServerSdsHandler.class);
assertThat(channelHandlerCtx).isNull();
// pipeline should only have SslHandler and ServerTlsHandler
Iterator<Map.Entry<String, ChannelHandler>> iterator = pipeline.iterator();
assertThat(iterator.next().getValue()).isInstanceOf(SslHandler.class);
// ProtocolNegotiators.ServerTlsHandler.class is not accessible, get canonical name
assertThat(iterator.next().getValue().getClass().getCanonicalName()).contains("ProtocolNegotiators.ServerTlsHandler");
CommonCertProviderTestUtils.register0();
}
Aggregations