Search in sources :

Example 1 with FakeClock

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);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ServerTransportListener(io.grpc.internal.ServerTransportListener) ServerStreamTracer(io.grpc.ServerStreamTracer) ServerTransport(io.grpc.internal.ServerTransport) FakeClock(io.grpc.internal.FakeClock) ObjectPool(io.grpc.internal.ObjectPool) ServerListener(io.grpc.internal.ServerListener) Test(org.junit.Test)

Example 2 with FakeClock

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);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) FakeClock(io.grpc.internal.FakeClock) Test(org.junit.Test)

Example 3 with FakeClock

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();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) FakeClock(io.grpc.internal.FakeClock) ClientTransportFactory(io.grpc.internal.ClientTransportFactory) Test(org.junit.Test)

Example 4 with FakeClock

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);
        }
    }
}
Also used : ChannelOption(io.netty.channel.ChannelOption) InetSocketAddress(java.net.InetSocketAddress) FakeClock(io.grpc.internal.FakeClock) LocalSocketPicker(io.grpc.netty.NettyChannelBuilder.LocalSocketPicker) ClientTransport(io.grpc.internal.ClientTransport) ManagedClientTransport(io.grpc.internal.ManagedClientTransport) TimeoutException(java.util.concurrent.TimeoutException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLException(javax.net.ssl.SSLException) StatusException(io.grpc.StatusException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TransportTracer(io.grpc.internal.TransportTracer) TrackingObjectPoolForTest(io.grpc.netty.NettyTestUtil.TrackingObjectPoolForTest) Test(org.junit.Test)

Example 5 with FakeClock

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();
}
Also used : ProtocolNegotiationEvent(io.grpc.netty.ProtocolNegotiationEvent) InternalProtocolNegotiationEvent(io.grpc.netty.InternalProtocolNegotiationEvent) FakeClock(io.grpc.internal.FakeClock) InetSocketAddress(java.net.InetSocketAddress) DownstreamTlsContext(io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext) Attributes(io.grpc.Attributes) InternalXdsAttributes(io.grpc.xds.InternalXdsAttributes) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Bootstrapper(io.grpc.xds.Bootstrapper) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.Test)

Aggregations

FakeClock (io.grpc.internal.FakeClock)9 Test (org.junit.Test)9 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 Bootstrapper (io.grpc.xds.Bootstrapper)3 SslContext (io.netty.handler.ssl.SslContext)3 ClientTransportFactory (io.grpc.internal.ClientTransportFactory)2 UpstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext)2 ClientSdsHandler (io.grpc.xds.internal.sds.SdsProtocolNegotiators.ClientSdsHandler)2 InetSocketAddress (java.net.InetSocketAddress)2 Attributes (io.grpc.Attributes)1 LoadBalancer (io.grpc.LoadBalancer)1 Helper (io.grpc.LoadBalancer.Helper)1 LoadBalancerProvider (io.grpc.LoadBalancerProvider)1 NameResolver (io.grpc.NameResolver)1 ServiceConfigParser (io.grpc.NameResolver.ServiceConfigParser)1 NameResolverRegistry (io.grpc.NameResolverRegistry)1 ServerStreamTracer (io.grpc.ServerStreamTracer)1 StatusException (io.grpc.StatusException)1 SynchronizationContext (io.grpc.SynchronizationContext)1 ClientTransport (io.grpc.internal.ClientTransport)1