Search in sources :

Example 26 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class JankyServersTest method testHttpSilentServerWithRequestTimeout.

@Test
public void testHttpSilentServerWithRequestTimeout() throws Throwable {
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().withReadTimeout(new Duration(86400L * 365)).build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        final ListenableFuture<StatusResponseHolder> future = client.go(new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", silentServerSocket.getLocalPort()))), StatusResponseHandler.getInstance(), new Duration(100L));
        Throwable e = null;
        try {
            future.get();
        } catch (ExecutionException e1) {
            e = e1.getCause();
        }
        Assert.assertTrue("ReadTimeoutException thrown by 'get'", e instanceof ReadTimeoutException);
    } finally {
        lifecycle.stop();
    }
}
Also used : ReadTimeoutException(org.jboss.netty.handler.timeout.ReadTimeoutException) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) Duration(org.joda.time.Duration) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) Test(org.junit.Test)

Example 27 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class JankyServersTest method testHttpEchoServer.

@Test
public void testHttpEchoServer() throws Throwable {
    final Lifecycle lifecycle = new Lifecycle();
    try {
        final HttpClientConfig config = HttpClientConfig.builder().build();
        final HttpClient client = HttpClientInit.createClient(config, lifecycle);
        final ListenableFuture<StatusResponseHolder> response = client.go(new Request(HttpMethod.GET, new URL(StringUtils.format("http://localhost:%d/", echoServerSocket.getLocalPort()))), StatusResponseHandler.getInstance());
        expectedException.expect(ExecutionException.class);
        expectedException.expectMessage("java.lang.IllegalArgumentException: invalid version format: GET");
        response.get();
    } finally {
        lifecycle.stop();
    }
}
Also used : Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) URL(java.net.URL) Test(org.junit.Test)

Example 28 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class CustomEmitterFactoryTest method testCustomEmitter.

@Test
public void testCustomEmitter() {
    final Properties props = new Properties();
    props.put("org.apache.druid.java.util.emitter.stringProperty", "http://example.com/");
    props.put("org.apache.druid.java.util.emitter.intProperty", "1");
    props.put("org.apache.druid.java.util.emitter.type", "test");
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerSubtypes(TestEmitterConfig.class);
    final Lifecycle lifecycle = new Lifecycle();
    final Emitter emitter = Emitters.create(props, null, objectMapper, lifecycle);
    Assert.assertTrue("created emitter should be of class StubEmitter", emitter instanceof StubEmitter);
    StubEmitter stubEmitter = (StubEmitter) emitter;
    Assert.assertEquals("http://example.com/", stubEmitter.getStringProperty());
    Assert.assertEquals(1, stubEmitter.getIntProperty());
}
Also used : Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) Properties(java.util.Properties) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 29 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class RedisCacheProviderWithConfig method testBasicInjection.

@Test
public void testBasicInjection() throws Exception {
    String json = "{ \"host\": \"localhost\", \"port\": 6379, \"expiration\": 3600}";
    final RedisCacheConfig config = new ObjectMapper().readValue(json, RedisCacheConfig.class);
    Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(binder -> {
        binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/redis");
        binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
        binder.bindConstant().annotatedWith(Names.named("tlsServicePort")).to(-1);
        binder.bindConstant().annotatedWith(Names.named("host")).to("localhost");
        binder.bindConstant().annotatedWith(Names.named("port")).to(6379);
        binder.bind(RedisCacheConfig.class).toInstance(config);
        binder.bind(Cache.class).toProvider(RedisCacheProviderWithConfig.class).in(ManageLifecycle.class);
    }));
    Lifecycle lifecycle = injector.getInstance(Lifecycle.class);
    lifecycle.start();
    try {
        Cache cache = injector.getInstance(Cache.class);
        Assert.assertEquals(RedisStandaloneCache.class, cache.getClass());
    } finally {
        lifecycle.stop();
    }
}
Also used : Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) JsonConfigProvider(org.apache.druid.guice.JsonConfigProvider) Inject(com.google.inject.Inject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StringUtils(org.apache.druid.java.util.common.StringUtils) Test(org.junit.Test) Names(com.google.inject.name.Names) UUID(java.util.UUID) Ints(com.google.common.primitives.Ints) GuiceInjectors(org.apache.druid.guice.GuiceInjectors) Injector(com.google.inject.Injector) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Initialization(org.apache.druid.initialization.Initialization) Map(java.util.Map) MockJedis(com.fiftyonred.mock_jedis.MockJedis) ManageLifecycle(org.apache.druid.guice.ManageLifecycle) Assert(org.junit.Assert) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) MockJedisPool(com.fiftyonred.mock_jedis.MockJedisPool) Before(org.junit.Before) Injector(com.google.inject.Injector) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) ManageLifecycle(org.apache.druid.guice.ManageLifecycle) ManageLifecycle(org.apache.druid.guice.ManageLifecycle) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 30 with Lifecycle

use of org.apache.druid.java.util.common.lifecycle.Lifecycle in project druid by druid-io.

the class LifecycleModule method getLifecycle.

@Provides
@LazySingleton
public Lifecycle getLifecycle(final Injector injector) {
    final Key<Set<KeyHolder>> keyHolderKey = Key.get(new TypeLiteral<Set<KeyHolder>>() {
    }, Names.named("lifecycle"));
    final Set<KeyHolder> eagerClasses = injector.getInstance(keyHolderKey);
    Lifecycle lifecycle = new Lifecycle("module") {

        @Override
        public void start() throws Exception {
            for (KeyHolder<?> holder : eagerClasses) {
                // Pull the key so as to "eagerly" load up the class.
                injector.getInstance(holder.getKey());
            }
            super.start();
        }
    };
    initScope.setLifecycle(lifecycle);
    normalScope.setLifecycle(lifecycle);
    serverScope.setLifecycle(lifecycle);
    annoucementsScope.setLifecycle(lifecycle);
    return lifecycle;
}
Also used : Set(java.util.Set) Lifecycle(org.apache.druid.java.util.common.lifecycle.Lifecycle) Provides(com.google.inject.Provides)

Aggregations

Lifecycle (org.apache.druid.java.util.common.lifecycle.Lifecycle)46 Test (org.junit.Test)21 Injector (com.google.inject.Injector)12 URL (java.net.URL)12 ExecutionException (java.util.concurrent.ExecutionException)12 StatusResponseHolder (org.apache.druid.java.util.http.client.response.StatusResponseHolder)10 Before (org.junit.Before)10 ExecutorService (java.util.concurrent.ExecutorService)6 Binder (com.google.inject.Binder)5 Module (com.google.inject.Module)5 OutputStream (java.io.OutputStream)5 Properties (java.util.Properties)5 ManageLifecycle (org.apache.druid.guice.ManageLifecycle)5 NoopServiceEmitter (org.apache.druid.server.metrics.NoopServiceEmitter)5 ChannelException (org.jboss.netty.channel.ChannelException)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)4 IOException (java.io.IOException)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Provides (com.google.inject.Provides)3