Search in sources :

Example 36 with HugeConfig

use of com.baidu.hugegraph.config.HugeConfig in project hugegraph-common by hugegraph.

the class ServerClientTest method testStartServerWithAdaptivePort.

@Test
public void testStartServerWithAdaptivePort() throws IOException {
    // Init server
    RpcServer rpcServerAdaptive = new RpcServer(config("server-adaptive"));
    RpcProviderConfig serverConfig = rpcServerAdaptive.config();
    serverConfig.addService(HelloService.class, new HelloServiceImpl());
    // Start other server bound the port
    int usedPort = rpcServerAdaptive.port();
    InetAddress ip = InetAddress.getByName(rpcServerAdaptive.host());
    ServerSocket inUse = new ServerSocket(usedPort, 50, ip);
    // Start server after the port in use
    startServer(rpcServerAdaptive);
    Assert.assertNotEquals(0, rpcServerAdaptive.port());
    Assert.assertNotEquals(usedPort, rpcServerAdaptive.port());
    // Init client
    HugeConfig config = config(false);
    String url = rpcServerAdaptive.host() + ":" + rpcServerAdaptive.port();
    String remoteUrlKey = com.baidu.hugegraph.config.RpcOptions.RPC_REMOTE_URL.name();
    config.setProperty(remoteUrlKey, url);
    RpcClientProvider rpcClientAdaptive = new RpcClientProvider(config);
    RpcConsumerConfig clientConfig = rpcClientAdaptive.config();
    HelloService client = clientConfig.serviceProxy(HelloService.class);
    // Test call
    Assert.assertEquals("hello tom!", client.hello("tom"));
    Assert.assertEquals("tom", client.echo("tom"));
    Assert.assertEquals(5.14, client.sum(2, 3.14), 0.00000001d);
    // Destroy all
    rpcClientAdaptive.destroy();
    stopServer(rpcServerAdaptive);
    inUse.close();
}
Also used : RpcProviderConfig(com.baidu.hugegraph.rpc.RpcProviderConfig) RpcServer(com.baidu.hugegraph.rpc.RpcServer) ServerSocket(java.net.ServerSocket) RpcConsumerConfig(com.baidu.hugegraph.rpc.RpcConsumerConfig) HugeConfig(com.baidu.hugegraph.config.HugeConfig) RpcClientProvider(com.baidu.hugegraph.rpc.RpcClientProvider) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 37 with HugeConfig

use of com.baidu.hugegraph.config.HugeConfig in project hugegraph-common by hugegraph.

the class ServerClientTest method testStartBothServerAndClientThroughSameConfig.

@Test
public void testStartBothServerAndClientThroughSameConfig() {
    // Init server1
    HugeConfig server1 = config("server1-client");
    RpcServer rpcServer1 = new RpcServer(server1);
    RpcClientProvider rpcClient1 = new RpcClientProvider(server1);
    GraphHelloServiceImpl s1g1 = new GraphHelloServiceImpl("g1");
    GraphHelloServiceImpl s1g2 = new GraphHelloServiceImpl("g2");
    rpcServer1.config().addService(s1g1.graph(), HelloService.class, s1g1);
    rpcServer1.config().addService(s1g2.graph(), HelloService.class, s1g2);
    startServer(rpcServer1);
    // Init server2
    HugeConfig server2 = config("server2-client");
    RpcServer rpcServer2 = new RpcServer(server2);
    RpcClientProvider rpcClient2 = new RpcClientProvider(server2);
    GraphHelloServiceImpl s2g1 = new GraphHelloServiceImpl("g1");
    GraphHelloServiceImpl s2g2 = new GraphHelloServiceImpl("g2");
    rpcServer2.config().addService(s2g1.graph(), HelloService.class, s2g1);
    rpcServer2.config().addService(s2g2.graph(), HelloService.class, s2g2);
    startServer(rpcServer2);
    // Init client1
    HelloService s2g1Client = rpcClient1.config().serviceProxy("g1", HelloService.class);
    HelloService s2g2Client = rpcClient1.config().serviceProxy("g2", HelloService.class);
    // Init client2
    HelloService s1g1Client = rpcClient2.config().serviceProxy("g1", HelloService.class);
    HelloService s1g2Client = rpcClient2.config().serviceProxy("g2", HelloService.class);
    // Test call
    Assert.assertEquals(2.1, s2g1Client.sum(1, 1.1), 0.00000001d);
    Assert.assertEquals(2.2, s2g2Client.sum(1, 1.2), 0.00000001d);
    Assert.assertEquals(1.1, s1g1Client.sum(1, 0.1), 0.00000001d);
    Assert.assertEquals(1.2, s1g2Client.sum(0, 1.2), 0.00000001d);
    Assert.assertEquals(1.1, s1g1.result(), 0.00000001d);
    Assert.assertEquals(1.2, s1g2.result(), 0.00000001d);
    Assert.assertEquals(2.1, s2g1.result(), 0.00000001d);
    Assert.assertEquals(2.2, s2g2.result(), 0.00000001d);
    // Destroy all
    rpcClient1.destroy();
    rpcClient2.destroy();
    stopServer(rpcServer1);
    stopServer(rpcServer2);
}
Also used : RpcServer(com.baidu.hugegraph.rpc.RpcServer) HugeConfig(com.baidu.hugegraph.config.HugeConfig) RpcClientProvider(com.baidu.hugegraph.rpc.RpcClientProvider) Test(org.junit.Test)

Example 38 with HugeConfig

use of com.baidu.hugegraph.config.HugeConfig in project hugegraph-common by hugegraph.

the class HugeConfigTest method testHugeConfig.

@Test
public void testHugeConfig() throws Exception {
    Configuration conf = new PropertiesConfiguration();
    Assert.assertEquals(DisabledListDelimiterHandler.INSTANCE, ((AbstractConfiguration) conf).getListDelimiterHandler());
    HugeConfig config = new HugeConfig(conf);
    Assert.assertEquals("text1-value", config.get(TestOptions.text1));
    Assert.assertEquals("text2-value", config.get(TestOptions.text2));
    Assert.assertEquals("CHOICE-1", config.get(TestOptions.text3));
    Assert.assertEquals(1, (int) config.get(TestOptions.int1));
    Assert.assertEquals(10, (int) config.get(TestOptions.int2));
    Assert.assertEquals(10, (int) config.get(TestOptions.int3));
    Assert.assertEquals(100L, (long) config.get(TestOptions.long1));
    Assert.assertEquals(100.0f, config.get(TestOptions.float1), 0f);
    Assert.assertEquals(100.0f, config.get(TestOptions.double1), 0d);
    Assert.assertEquals(true, config.get(TestOptions.bool));
    Assert.assertEquals(Object.class, config.get(TestOptions.clazz));
    Assert.assertThrows(ConfigException.class, () -> {
        config.setProperty(TestOptions.clazz.name(), "com.baidu.hugegraph.HugeGraph");
    }, e -> {
        Assert.assertTrue(e.getCause() instanceof ClassNotFoundException);
    });
    Assert.assertEquals(Arrays.asList("list-value1", "list-value2"), config.get(TestOptions.list));
    Assert.assertEquals(ImmutableMap.of("key1", "value1", "key2", "value2"), config.getMap(TestOptions.map));
    Assert.assertEquals(WeekDay.WEDNESDAY, config.get(TestOptions.weekday));
    Assert.assertEquals(Arrays.asList(WeekDay.SATURDAY, WeekDay.SUNDAY), config.get(TestOptions.weekdays));
    Assert.assertThrows(ConfigException.class, () -> {
        new HugeConfig((Configuration) null);
    });
}
Also used : MapConfiguration(org.apache.commons.configuration2.MapConfiguration) AbstractConfiguration(org.apache.commons.configuration2.AbstractConfiguration) Configuration(org.apache.commons.configuration2.Configuration) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) HugeConfig(com.baidu.hugegraph.config.HugeConfig) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 39 with HugeConfig

use of com.baidu.hugegraph.config.HugeConfig in project hugegraph-common by hugegraph.

the class HugeConfigTest method testFromMapConfigurationWithList.

@Test
public void testFromMapConfigurationWithList() {
    Map<String, String> options = new HashMap<>();
    options.put(TestOptions.list.name(), "[a, b]");
    MapConfiguration mapConfiguration = new MapConfiguration(options);
    HugeConfig hugeConfig = new HugeConfig(mapConfiguration);
    List<String> values = hugeConfig.get(TestOptions.list);
    Assert.assertEquals(2, values.size());
    Assert.assertTrue(values.contains("a"));
    Assert.assertTrue(values.contains("b"));
}
Also used : HashMap(java.util.HashMap) MapConfiguration(org.apache.commons.configuration2.MapConfiguration) HugeConfig(com.baidu.hugegraph.config.HugeConfig) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 40 with HugeConfig

use of com.baidu.hugegraph.config.HugeConfig in project hugegraph-common by hugegraph.

the class HugeConfigTest method testSaveHugeConfig.

@Test
public void testSaveHugeConfig() throws ConfigurationException, IOException {
    HugeConfig config = new HugeConfig(CONF);
    Assert.assertEquals("file-text1-value", config.get(TestOptions.text1));
    File copiedFile = new File("copied.conf");
    config.save(copiedFile);
    Assert.assertTrue(copiedFile.exists());
    Assert.assertTrue(copiedFile.length() > 0);
    try {
        HugeConfig copiedConfig = new HugeConfig(copiedFile.getPath());
        Assert.assertEquals(IteratorUtils.toList(config.getKeys()), IteratorUtils.toList(copiedConfig.getKeys()));
        Assert.assertEquals(config.get(TestOptions.text1), copiedConfig.get(TestOptions.text1));
    } finally {
        FileUtils.forceDelete(copiedFile);
    }
}
Also used : HugeConfig(com.baidu.hugegraph.config.HugeConfig) File(java.io.File) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Aggregations

HugeConfig (com.baidu.hugegraph.config.HugeConfig)63 Test (org.junit.Test)33 PropertiesConfiguration (org.apache.commons.configuration2.PropertiesConfiguration)15 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)12 Configuration (org.apache.commons.configuration2.Configuration)11 RpcClientProvider (com.baidu.hugegraph.rpc.RpcClientProvider)7 RpcServer (com.baidu.hugegraph.rpc.RpcServer)7 HugeGraph (com.baidu.hugegraph.HugeGraph)5 File (java.io.File)5 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)4 HashMap (java.util.HashMap)4 RocksDBSessions (com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions)3 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)3 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)3 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)3 MapConfiguration (org.apache.commons.configuration.MapConfiguration)3 HugeException (com.baidu.hugegraph.HugeException)2 BackendException (com.baidu.hugegraph.backend.BackendException)2 BinaryBackendEntry (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry)2 BinaryScatterSerializer (com.baidu.hugegraph.backend.serializer.BinaryScatterSerializer)2