Search in sources :

Example 1 with RpcServer

use of com.baidu.hugegraph.rpc.RpcServer in project hugegraph-common by hugegraph.

the class ServerClientTest method testStartServerWithRandomPort.

@Test
public void testStartServerWithRandomPort() {
    // Init server
    RpcServer rpcServerRandom = new RpcServer(config("server-random"));
    RpcProviderConfig serverConfig = rpcServerRandom.config();
    serverConfig.addService(HelloService.class, new HelloServiceImpl());
    startServer(rpcServerRandom);
    Assert.assertNotEquals(0, rpcServerRandom.port());
    Assert.assertNotEquals(8090, rpcServerRandom.port());
    // Init client
    HugeConfig config = config(false);
    String url = rpcServerRandom.host() + ":" + rpcServerRandom.port();
    String remoteUrlKey = com.baidu.hugegraph.config.RpcOptions.RPC_REMOTE_URL.name();
    config.setProperty(remoteUrlKey, url);
    RpcClientProvider rpcClientRandom = new RpcClientProvider(config);
    RpcConsumerConfig clientConfig = rpcClientRandom.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
    rpcClientRandom.destroy();
    stopServer(rpcServerRandom);
}
Also used : RpcProviderConfig(com.baidu.hugegraph.rpc.RpcProviderConfig) RpcServer(com.baidu.hugegraph.rpc.RpcServer) RpcConsumerConfig(com.baidu.hugegraph.rpc.RpcConsumerConfig) HugeConfig(com.baidu.hugegraph.config.HugeConfig) RpcClientProvider(com.baidu.hugegraph.rpc.RpcClientProvider) Test(org.junit.Test)

Example 2 with RpcServer

use of com.baidu.hugegraph.rpc.RpcServer in project hugegraph-common by hugegraph.

the class ServerClientTest method testExportMultiTimesOfSameService.

@Test
public void testExportMultiTimesOfSameService() {
    RpcServer rpcServerExport = new RpcServer(config(true));
    rpcServerExport.config().addService(HelloService.class, new HelloServiceImpl());
    rpcServerExport.exportAll();
    rpcServerExport.config().addService("graph", HelloService.class, new HelloServiceImpl());
    rpcServerExport.exportAll();
    stopServer(rpcServerExport);
}
Also used : RpcServer(com.baidu.hugegraph.rpc.RpcServer) Test(org.junit.Test)

Example 3 with RpcServer

use of com.baidu.hugegraph.rpc.RpcServer in project hugegraph-common by hugegraph.

the class ServerClientTest method testUnexportService.

@Test
public void testUnexportService() {
    RpcServer rpcServerUnexport = new RpcServer(config(true));
    RpcProviderConfig serverConfig = rpcServerUnexport.config();
    String service = serverConfig.addService(HelloService.class, new HelloServiceImpl());
    rpcServerUnexport.exportAll();
    RpcConsumerConfig clientConfig = rpcClient.config();
    HelloService client = clientConfig.serviceProxy(HelloService.class);
    Assert.assertEquals("hello tom!", client.hello("tom"));
    rpcServerUnexport.unexport(service);
    Assert.assertThrows(SofaRpcException.class, () -> {
        client.hello("tom");
    });
    stopServer(rpcServerUnexport);
}
Also used : RpcProviderConfig(com.baidu.hugegraph.rpc.RpcProviderConfig) RpcServer(com.baidu.hugegraph.rpc.RpcServer) RpcConsumerConfig(com.baidu.hugegraph.rpc.RpcConsumerConfig) Test(org.junit.Test)

Example 4 with RpcServer

use of com.baidu.hugegraph.rpc.RpcServer in project hugegraph-common by hugegraph.

the class ServerClientTest method testFanoutCallService.

@Test
public void testFanoutCallService() {
    // Init 3 servers
    HugeConfig server3 = config("server3");
    RpcServer rpcServer3 = new RpcServer(server3);
    HugeConfig server4 = config("server4");
    RpcServer rpcServer4 = new RpcServer(server4);
    HugeConfig server5 = config("server5");
    RpcServer rpcServer5 = new RpcServer(server5);
    GraphHelloServiceImpl s3g1 = new GraphHelloServiceImpl("g1");
    GraphHelloServiceImpl s4g1 = new GraphHelloServiceImpl("g1");
    GraphHelloServiceImpl s5g1 = new GraphHelloServiceImpl("g1");
    rpcServer3.config().addService(s3g1.graph(), HelloService.class, s3g1);
    rpcServer4.config().addService(s4g1.graph(), HelloService.class, s4g1);
    rpcServer5.config().addService(s5g1.graph(), HelloService.class, s5g1);
    startServer(rpcServer3);
    startServer(rpcServer4);
    startServer(rpcServer5);
    // Init client
    HugeConfig client345 = config("client345");
    RpcClientProvider rpcClient345 = new RpcClientProvider(client345);
    HelloService g1 = rpcClient345.config().serviceProxy("g1", HelloService.class);
    // Test fanout
    Assert.assertEquals("g1: fanout", g1.echo("fanout"));
    Assert.assertEquals(16.8, g1.sum(10, 6.8), 0.00000001d);
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        g1.hello("");
    }, e -> {
        Assert.assertContains("empty hello parameter", e.getMessage());
    });
    Assert.assertEquals(16.8, s3g1.result(), 0.00000001d);
    Assert.assertEquals(16.8, s4g1.result(), 0.00000001d);
    Assert.assertEquals(16.8, s5g1.result(), 0.00000001d);
    // Destroy all
    rpcClient345.destroy();
    stopServer(rpcServer3);
    stopServer(rpcServer4);
    stopServer(rpcServer5);
}
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 5 with RpcServer

use of com.baidu.hugegraph.rpc.RpcServer in project hugegraph-common by hugegraph.

the class ServerClientTest method testUnexportAllService.

@Test
public void testUnexportAllService() {
    RpcServer rpcServerUnexport = new RpcServer(config(true));
    RpcProviderConfig serverConfig = rpcServerUnexport.config();
    serverConfig.addService(HelloService.class, new HelloServiceImpl());
    serverConfig.addService("graph", HelloService.class, new GraphHelloServiceImpl("graph"));
    rpcServerUnexport.exportAll();
    RpcConsumerConfig clientConfig = rpcClient.config();
    HelloService client = clientConfig.serviceProxy(HelloService.class);
    HelloService clientG = clientConfig.serviceProxy("graph", HelloService.class);
    Assert.assertEquals("hello tom!", client.hello("tom"));
    Assert.assertEquals("graph: hello tom!", clientG.hello("tom"));
    rpcServerUnexport.unexportAll();
    Assert.assertThrows(SofaRpcException.class, () -> {
        client.hello("tom");
    });
    Assert.assertThrows(SofaRpcException.class, () -> {
        clientG.hello("tom");
    });
    stopServer(rpcServerUnexport);
}
Also used : RpcProviderConfig(com.baidu.hugegraph.rpc.RpcProviderConfig) RpcServer(com.baidu.hugegraph.rpc.RpcServer) RpcConsumerConfig(com.baidu.hugegraph.rpc.RpcConsumerConfig) Test(org.junit.Test)

Aggregations

RpcServer (com.baidu.hugegraph.rpc.RpcServer)14 Test (org.junit.Test)13 HugeConfig (com.baidu.hugegraph.config.HugeConfig)7 RpcClientProvider (com.baidu.hugegraph.rpc.RpcClientProvider)7 RpcConsumerConfig (com.baidu.hugegraph.rpc.RpcConsumerConfig)4 RpcProviderConfig (com.baidu.hugegraph.rpc.RpcProviderConfig)4 InetAddress (java.net.InetAddress)1 ServerSocket (java.net.ServerSocket)1 BeforeClass (org.junit.BeforeClass)1