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();
}
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);
}
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);
});
}
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"));
}
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);
}
}
Aggregations