Search in sources :

Example 1 with ConfigurationFactory

use of io.airlift.configuration.ConfigurationFactory in project airlift by airlift.

the class TestNodeModule method testDefaultConfig.

@Test
public void testDefaultConfig() throws UnknownHostException {
    long testStartTime = System.currentTimeMillis();
    ConfigurationFactory configFactory = new ConfigurationFactory(ImmutableMap.<String, String>of("node.environment", "environment"));
    Injector injector = Guice.createInjector(new NodeModule(), new ConfigurationModule(configFactory));
    NodeInfo nodeInfo = injector.getInstance(NodeInfo.class);
    assertNotNull(nodeInfo);
    assertEquals(nodeInfo.getEnvironment(), "environment");
    assertEquals(nodeInfo.getPool(), "general");
    assertNotNull(nodeInfo.getNodeId());
    assertNotNull(nodeInfo.getLocation());
    assertNull(nodeInfo.getBinarySpec());
    assertNull(nodeInfo.getConfigSpec());
    assertNotNull(nodeInfo.getInstanceId());
    assertNotEquals(nodeInfo.getNodeId(), nodeInfo.getInstanceId());
    assertNotNull(nodeInfo.getInternalAddress());
    assertFalse(InetAddress.getByName(nodeInfo.getInternalAddress()).isAnyLocalAddress());
    assertNotNull(nodeInfo.getBindIp());
    assertTrue(nodeInfo.getBindIp().isAnyLocalAddress());
    assertGreaterThanOrEqual(nodeInfo.getStartTime(), testStartTime);
    // make sure toString doesn't throw an exception
    assertNotNull(nodeInfo.toString());
}
Also used : Injector(com.google.inject.Injector) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) ConfigurationModule(io.airlift.configuration.ConfigurationModule) Test(org.testng.annotations.Test)

Example 2 with ConfigurationFactory

use of io.airlift.configuration.ConfigurationFactory in project airlift by airlift.

the class TestH2EmbeddedDataSourceModule method createInjector.

private static Injector createInjector(Map<String, String> properties, Module... modules) {
    ConfigurationFactory configurationFactory = new ConfigurationFactory(properties);
    // Required to bind a configuration module and an MBean server when binding an H2EmbeddedDataSourceModule
    List<Module> moduleList = ImmutableList.<Module>builder().add(modules).add(new ConfigurationModule(configurationFactory)).add(binder -> {
        binder.bind(MBeanServer.class).to(TestingMBeanServer.class).in(Scopes.SINGLETON);
        binder.bind(ObjectHolder.class);
    }).build();
    return Guice.createInjector(moduleList);
}
Also used : Module(com.google.inject.Module) Assertions.assertInstanceOf(io.airlift.testing.Assertions.assertInstanceOf) TestingMBeanServer(org.weakref.jmx.testing.TestingMBeanServer) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) AfterMethod(org.testng.annotations.AfterMethod) BindingAnnotation(com.google.inject.BindingAnnotation) Retention(java.lang.annotation.Retention) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) Assert.assertNotSame(org.testng.Assert.assertNotSame) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) MBeanServer(javax.management.MBeanServer) Map(java.util.Map) DataSource(javax.sql.DataSource) ConfigurationModule(io.airlift.configuration.ConfigurationModule) Assert.assertSame(org.testng.Assert.assertSame) BeforeMethod(org.testng.annotations.BeforeMethod) IOException(java.io.IOException) Scopes(com.google.inject.Scopes) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) File(java.io.File) Injector(com.google.inject.Injector) List(java.util.List) CreationException(com.google.inject.CreationException) Guice(com.google.inject.Guice) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) Module(com.google.inject.Module) ConfigurationModule(io.airlift.configuration.ConfigurationModule) ConfigurationModule(io.airlift.configuration.ConfigurationModule) TestingMBeanServer(org.weakref.jmx.testing.TestingMBeanServer) MBeanServer(javax.management.MBeanServer)

Example 3 with ConfigurationFactory

use of io.airlift.configuration.ConfigurationFactory in project airlift by airlift.

the class TestHttpServiceSelectorBinder method testHttpSelectorAnnotation.

@Test
public void testHttpSelectorAnnotation() {
    Injector injector = Guice.createInjector(new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<String, String>of())), new TestingNodeModule(), new TestingDiscoveryModule(), binder -> discoveryBinder(binder).bindHttpSelector(serviceType("apple")));
    InMemoryDiscoveryClient discoveryClient = injector.getInstance(InMemoryDiscoveryClient.class);
    discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("http", "fake://server-http").build()));
    HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
    assertEquals(selector.selectHttpService().stream().collect(onlyElement()), URI.create("fake://server-http"));
    ServiceSelectorManager manager = injector.getInstance(ServiceSelectorManager.class);
    assertEquals(manager.getServiceSelectors().size(), 1);
    manager.attemptRefresh();
    manager.forceRefresh();
}
Also used : TestingDiscoveryModule(io.airlift.discovery.client.testing.TestingDiscoveryModule) Injector(com.google.inject.Injector) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) InMemoryDiscoveryClient(io.airlift.discovery.client.testing.InMemoryDiscoveryClient) ConfigurationModule(io.airlift.configuration.ConfigurationModule) Test(org.testng.annotations.Test)

Example 4 with ConfigurationFactory

use of io.airlift.configuration.ConfigurationFactory in project airlift by airlift.

the class TestHttpServiceSelectorBinder method testInvalidUris.

@Test
public void testInvalidUris() {
    Injector injector = Guice.createInjector(new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<String, String>of())), new TestingNodeModule(), new TestingDiscoveryModule(), binder -> discoveryBinder(binder).bindHttpSelector("apple"));
    InMemoryDiscoveryClient discoveryClient = injector.getInstance(InMemoryDiscoveryClient.class);
    discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("http", ":::INVALID:::").build()));
    discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("https", ":::INVALID:::").build()));
    HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
    assertEquals(selector.selectHttpService(), ImmutableList.of());
}
Also used : TestingDiscoveryModule(io.airlift.discovery.client.testing.TestingDiscoveryModule) Injector(com.google.inject.Injector) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) InMemoryDiscoveryClient(io.airlift.discovery.client.testing.InMemoryDiscoveryClient) ConfigurationModule(io.airlift.configuration.ConfigurationModule) Test(org.testng.annotations.Test)

Example 5 with ConfigurationFactory

use of io.airlift.configuration.ConfigurationFactory in project airlift by airlift.

the class TestHttpServiceSelectorBinder method testFavorHttpsOverHttpSelector.

@Test
public void testFavorHttpsOverHttpSelector() {
    Injector injector = Guice.createInjector(new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<String, String>of())), new TestingNodeModule(), new TestingDiscoveryModule(), binder -> discoveryBinder(binder).bindHttpSelector("apple"));
    InMemoryDiscoveryClient discoveryClient = injector.getInstance(InMemoryDiscoveryClient.class);
    discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("http", "fake://server-http").build(), serviceAnnouncement("apple").addProperty("https", "fake://server-https").build()));
    HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
    assertEquals(selector.selectHttpService(), ImmutableList.of(URI.create("fake://server-https"), URI.create("fake://server-http")));
}
Also used : TestingDiscoveryModule(io.airlift.discovery.client.testing.TestingDiscoveryModule) Injector(com.google.inject.Injector) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) ConfigurationFactory(io.airlift.configuration.ConfigurationFactory) InMemoryDiscoveryClient(io.airlift.discovery.client.testing.InMemoryDiscoveryClient) ConfigurationModule(io.airlift.configuration.ConfigurationModule) Test(org.testng.annotations.Test)

Aggregations

Injector (com.google.inject.Injector)12 ConfigurationFactory (io.airlift.configuration.ConfigurationFactory)12 ConfigurationModule (io.airlift.configuration.ConfigurationModule)12 Test (org.testng.annotations.Test)11 TestingNodeModule (io.airlift.node.testing.TestingNodeModule)8 InMemoryDiscoveryClient (io.airlift.discovery.client.testing.InMemoryDiscoveryClient)6 TestingDiscoveryModule (io.airlift.discovery.client.testing.TestingDiscoveryModule)6 ImmutableList (com.google.common.collect.ImmutableList)2 Guice (com.google.inject.Guice)2 Module (com.google.inject.Module)2 JsonModule (io.airlift.json.JsonModule)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Builder (com.google.common.collect.ImmutableList.Builder)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 Binder (com.google.inject.Binder)1 BindingAnnotation (com.google.inject.BindingAnnotation)1