use of com.google.inject.Injector in project druid by druid-io.
the class CaffeineCacheProviderWithConfig method testDefaultFromProperties.
@Test
public void testDefaultFromProperties() {
final String keyPrefix = "cache.config.prefix";
final Properties properties = new Properties();
final Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.<Module>of(binder -> {
binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test");
binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
JsonConfigProvider.bind(binder, keyPrefix, CaffeineCacheConfig.class);
}));
final JsonConfigurator configurator = injector.getInstance(JsonConfigurator.class);
final JsonConfigProvider<CaffeineCacheConfig> caffeineCacheConfigJsonConfigProvider = JsonConfigProvider.of(keyPrefix, CaffeineCacheConfig.class);
caffeineCacheConfigJsonConfigProvider.inject(properties, configurator);
final CaffeineCacheConfig config = caffeineCacheConfigJsonConfigProvider.get().get();
Assert.assertEquals(-1, config.getExpireAfter());
Assert.assertEquals(-1, config.getSizeInBytes());
Assert.assertEquals(ForkJoinPool.commonPool(), config.createExecutor());
}
use of com.google.inject.Injector in project druid by druid-io.
the class CaffeineCacheProviderWithConfig method testFromProperties.
@Test
public void testFromProperties() {
final String keyPrefix = "cache.config.prefix";
final Properties properties = new Properties();
properties.put(keyPrefix + ".expireAfter", "10");
properties.put(keyPrefix + ".sizeInBytes", "100");
properties.put(keyPrefix + ".cacheExecutorFactory", "single_thread");
final Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.<Module>of(binder -> {
binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test");
binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
JsonConfigProvider.bind(binder, keyPrefix, CaffeineCacheConfig.class);
}));
final JsonConfigurator configurator = injector.getInstance(JsonConfigurator.class);
final JsonConfigProvider<CaffeineCacheConfig> caffeineCacheConfigJsonConfigProvider = JsonConfigProvider.of(keyPrefix, CaffeineCacheConfig.class);
caffeineCacheConfigJsonConfigProvider.inject(properties, configurator);
final CaffeineCacheConfig config = caffeineCacheConfigJsonConfigProvider.get().get();
Assert.assertEquals(10, config.getExpireAfter());
Assert.assertEquals(100, config.getSizeInBytes());
Assert.assertNotNull(config.createExecutor());
}
use of com.google.inject.Injector in project druid by druid-io.
the class NamespaceLookupExtractorFactoryTest method testSimpleIntrospectionHandler.
@Test
public void testSimpleIntrospectionHandler() throws Exception {
final Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(new Module() {
@Override
public void configure(Binder binder) {
JsonConfigProvider.bindInstance(binder, Key.get(DruidNode.class, Self.class), new DruidNode("test-inject", null, null));
}
}));
final ObjectMapper mapper = injector.getInstance(Key.get(ObjectMapper.class, Json.class));
mapper.registerSubtypes(NamespaceLookupExtractorFactory.class);
final String str = "{ \"type\": \"cachedNamespace\", \"extractionNamespace\": { \"type\": \"staticMap\", \"map\": {\"foo\":\"bar\"} }, \"firstCacheTimeout\":10000 }";
final LookupExtractorFactory lookupExtractorFactory = mapper.readValue(str, LookupExtractorFactory.class);
Assert.assertTrue(lookupExtractorFactory.start());
try {
final LookupIntrospectHandler handler = lookupExtractorFactory.getIntrospectHandler();
Assert.assertNotNull(handler);
final Class<? extends LookupIntrospectHandler> clazz = handler.getClass();
Assert.assertNotNull(clazz.getMethod("getVersion").invoke(handler));
Assert.assertEquals(ImmutableSet.of("foo"), ((Response) clazz.getMethod("getKeys").invoke(handler)).getEntity());
Assert.assertEquals(ImmutableSet.of("bar"), ((Response) clazz.getMethod("getValues").invoke(handler)).getEntity());
Assert.assertEquals(ImmutableMap.builder().put("foo", "bar").build(), ((Response) clazz.getMethod("getMap").invoke(handler)).getEntity());
} finally {
Assert.assertTrue(lookupExtractorFactory.close());
}
}
use of com.google.inject.Injector in project druid by druid-io.
the class OffHeapNamespaceExtractionCacheManagerTest method testInjection.
@Test
public void testInjection() {
final Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(new Module() {
@Override
public void configure(Binder binder) {
JsonConfigProvider.bindInstance(binder, Key.get(DruidNode.class, Self.class), new DruidNode("test-inject", null, null));
}
}));
final Properties properties = injector.getInstance(Properties.class);
properties.clear();
properties.put(NamespaceExtractionModule.TYPE_PREFIX, "offHeap");
final NamespaceExtractionCacheManager manager = injector.getInstance(NamespaceExtractionCacheManager.class);
Assert.assertEquals(OffHeapNamespaceExtractionCacheManager.class, manager.getClass());
}
use of com.google.inject.Injector in project druid by druid-io.
the class OnHeapNamespaceExtractionCacheManagerTest method testInjection.
@Test
public void testInjection() {
final Injector injector = Initialization.makeInjectorWithModules(GuiceInjectors.makeStartupInjector(), ImmutableList.of(new Module() {
@Override
public void configure(Binder binder) {
JsonConfigProvider.bindInstance(binder, Key.get(DruidNode.class, Self.class), new DruidNode("test-inject", null, null));
}
}));
final Properties properties = injector.getInstance(Properties.class);
properties.clear();
properties.put(NamespaceExtractionModule.TYPE_PREFIX, "onHeap");
final NamespaceExtractionCacheManager manager = injector.getInstance(NamespaceExtractionCacheManager.class);
Assert.assertEquals(OnHeapNamespaceExtractionCacheManager.class, manager.getClass());
}
Aggregations