Search in sources :

Example 6 with FakeClock

use of io.grpc.internal.FakeClock in project grpc-java by grpc.

the class ClusterResolverLoadBalancerProviderTest method providesLoadBalancer.

@Test
public void providesLoadBalancer() {
    Helper helper = mock(Helper.class);
    SynchronizationContext syncContext = new SynchronizationContext(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            throw new AssertionError(e);
        }
    });
    FakeClock fakeClock = new FakeClock();
    NameResolverRegistry nsRegistry = new NameResolverRegistry();
    NameResolver.Args args = NameResolver.Args.newBuilder().setDefaultPort(8080).setProxyDetector(GrpcUtil.NOOP_PROXY_DETECTOR).setSynchronizationContext(syncContext).setServiceConfigParser(mock(ServiceConfigParser.class)).setChannelLogger(mock(ChannelLogger.class)).build();
    when(helper.getNameResolverRegistry()).thenReturn(nsRegistry);
    when(helper.getNameResolverArgs()).thenReturn(args);
    when(helper.getSynchronizationContext()).thenReturn(syncContext);
    when(helper.getScheduledExecutorService()).thenReturn(fakeClock.getScheduledExecutorService());
    when(helper.getAuthority()).thenReturn("api.google.com");
    LoadBalancerProvider provider = new ClusterResolverLoadBalancerProvider();
    LoadBalancer loadBalancer = provider.newLoadBalancer(helper);
    assertThat(loadBalancer).isInstanceOf(ClusterResolverLoadBalancer.class);
}
Also used : FakeClock(io.grpc.internal.FakeClock) LoadBalancer(io.grpc.LoadBalancer) NameResolverRegistry(io.grpc.NameResolverRegistry) Helper(io.grpc.LoadBalancer.Helper) SynchronizationContext(io.grpc.SynchronizationContext) LoadBalancerProvider(io.grpc.LoadBalancerProvider) ServiceConfigParser(io.grpc.NameResolver.ServiceConfigParser) NameResolver(io.grpc.NameResolver) Test(org.junit.Test)

Example 7 with FakeClock

use of io.grpc.internal.FakeClock in project grpc-java by grpc.

the class SdsProtocolNegotiatorsTest method clientSdsProtocolNegotiatorNewHandler_fireProtocolNegotiationEvent.

@Test
public void clientSdsProtocolNegotiatorNewHandler_fireProtocolNegotiationEvent() throws InterruptedException, TimeoutException, ExecutionException {
    FakeClock executor = new FakeClock();
    CommonCertProviderTestUtils.register(executor);
    Bootstrapper.BootstrapInfo bootstrapInfoForClient = CommonBootstrapperTestUtils.buildBootstrapInfo("google_cloud_private_spiffe-client", CLIENT_KEY_FILE, CLIENT_PEM_FILE, CA_PEM_FILE, null, null, null, null);
    UpstreamTlsContext upstreamTlsContext = CommonTlsContextTestsUtil.buildUpstreamTlsContext("google_cloud_private_spiffe-client", true);
    SslContextProviderSupplier sslContextProviderSupplier = new SslContextProviderSupplier(upstreamTlsContext, new TlsContextManagerImpl(bootstrapInfoForClient));
    SdsProtocolNegotiators.ClientSdsHandler clientSdsHandler = new SdsProtocolNegotiators.ClientSdsHandler(grpcHandler, sslContextProviderSupplier);
    pipeline.addLast(clientSdsHandler);
    channelHandlerCtx = pipeline.context(clientSdsHandler);
    // non-null since we just added it
    assertNotNull(channelHandlerCtx);
    // kick off protocol negotiation.
    pipeline.fireUserEventTriggered(InternalProtocolNegotiationEvent.getDefault());
    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);
        }
    });
    executor.runDueTasks();
    // need this for tasks to execute on eventLoop
    channel.runPendingTasks();
    Object fromFuture = future.get(5, TimeUnit.SECONDS);
    assertThat(fromFuture).isInstanceOf(SslContext.class);
    channel.runPendingTasks();
    channelHandlerCtx = pipeline.context(clientSdsHandler);
    assertThat(channelHandlerCtx).isNull();
    Object sslEvent = SslHandshakeCompletionEvent.SUCCESS;
    pipeline.fireUserEventTriggered(sslEvent);
    // need this for tasks to execute on eventLoop
    channel.runPendingTasks();
    assertTrue(channel.isOpen());
    CommonCertProviderTestUtils.register0();
}
Also used : ClientSdsHandler(io.grpc.xds.internal.sds.SdsProtocolNegotiators.ClientSdsHandler) FakeClock(io.grpc.internal.FakeClock) UpstreamTlsContext(io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext) Bootstrapper(io.grpc.xds.Bootstrapper) ClientSdsHandler(io.grpc.xds.internal.sds.SdsProtocolNegotiators.ClientSdsHandler) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.Test)

Example 8 with FakeClock

use of io.grpc.internal.FakeClock in project grpc-java by grpc.

the class SdsProtocolNegotiatorsTest method clientSdsHandler_addLast.

@Test
public void clientSdsHandler_addLast() throws InterruptedException, TimeoutException, ExecutionException {
    FakeClock executor = new FakeClock();
    CommonCertProviderTestUtils.register(executor);
    Bootstrapper.BootstrapInfo bootstrapInfoForClient = CommonBootstrapperTestUtils.buildBootstrapInfo("google_cloud_private_spiffe-client", CLIENT_KEY_FILE, CLIENT_PEM_FILE, CA_PEM_FILE, null, null, null, null);
    UpstreamTlsContext upstreamTlsContext = CommonTlsContextTestsUtil.buildUpstreamTlsContext("google_cloud_private_spiffe-client", true);
    SslContextProviderSupplier sslContextProviderSupplier = new SslContextProviderSupplier(upstreamTlsContext, new TlsContextManagerImpl(bootstrapInfoForClient));
    SdsProtocolNegotiators.ClientSdsHandler clientSdsHandler = new SdsProtocolNegotiators.ClientSdsHandler(grpcHandler, sslContextProviderSupplier);
    pipeline.addLast(clientSdsHandler);
    channelHandlerCtx = pipeline.context(clientSdsHandler);
    // clientSdsHandler ctx is non-null since we just added it
    assertNotNull(channelHandlerCtx);
    // kick off protocol negotiation.
    pipeline.fireUserEventTriggered(InternalProtocolNegotiationEvent.getDefault());
    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);
        }
    });
    assertThat(executor.runDueTasks()).isEqualTo(1);
    channel.runPendingTasks();
    Object fromFuture = future.get(2, TimeUnit.SECONDS);
    assertThat(fromFuture).isInstanceOf(SslContext.class);
    channel.runPendingTasks();
    channelHandlerCtx = pipeline.context(clientSdsHandler);
    assertThat(channelHandlerCtx).isNull();
    // pipeline should have SslHandler and ClientTlsHandler
    Iterator<Map.Entry<String, ChannelHandler>> iterator = pipeline.iterator();
    assertThat(iterator.next().getValue()).isInstanceOf(SslHandler.class);
    // ProtocolNegotiators.ClientTlsHandler.class not accessible, get canonical name
    assertThat(iterator.next().getValue().getClass().getCanonicalName()).contains("ProtocolNegotiators.ClientTlsHandler");
    CommonCertProviderTestUtils.register0();
}
Also used : ClientSdsHandler(io.grpc.xds.internal.sds.SdsProtocolNegotiators.ClientSdsHandler) FakeClock(io.grpc.internal.FakeClock) UpstreamTlsContext(io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext) Bootstrapper(io.grpc.xds.Bootstrapper) ClientSdsHandler(io.grpc.xds.internal.sds.SdsProtocolNegotiators.ClientSdsHandler) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.Test)

Example 9 with FakeClock

use of io.grpc.internal.FakeClock in project grpc-java by grpc.

the class InProcessChannelBuilderTest method scheduledExecutorService_custom.

@Test
public void scheduledExecutorService_custom() {
    InProcessChannelBuilder builder = InProcessChannelBuilder.forName("foo");
    ScheduledExecutorService scheduledExecutorService = new FakeClock().getScheduledExecutorService();
    InProcessChannelBuilder 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)

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