Search in sources :

Example 1 with RpcConsumerConfig

use of com.baidu.hugegraph.rpc.RpcConsumerConfig 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 RpcConsumerConfig

use of com.baidu.hugegraph.rpc.RpcConsumerConfig 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 3 with RpcConsumerConfig

use of com.baidu.hugegraph.rpc.RpcConsumerConfig 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)

Example 4 with RpcConsumerConfig

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

the class ServerClientTest method testServiceProxy.

@Test
public void testServiceProxy() {
    // Init server
    RpcProviderConfig serverConfig = rpcServer.config();
    serverConfig.addService(HelloService.class, new HelloServiceImpl());
    serverConfig.addService("graph", HelloService.class, new GraphHelloServiceImpl("graph"));
    startServer(rpcServer);
    // Init client
    RpcConsumerConfig clientConfig = rpcClient.config();
    HelloService client = clientConfig.serviceProxy(HelloService.class);
    HelloService client2 = clientConfig.serviceProxy(HelloService.class);
    HelloService clientG = clientConfig.serviceProxy("graph", HelloService.class);
    // Test call
    Assert.assertNotEquals(client, client2);
    Assert.assertEquals("hello tom!", client.hello("tom"));
    Assert.assertEquals("hello tom!", client2.hello("tom"));
    Assert.assertEquals("graph: hello tom!", clientG.hello("tom"));
    // Test call after unrefer
    rpcClient.unreferAll();
    Assert.assertThrows(SofaRpcRuntimeException.class, () -> {
        client.hello("tom");
    });
    Assert.assertThrows(SofaRpcRuntimeException.class, () -> {
        client2.hello("tom");
    });
    Assert.assertThrows(SofaRpcRuntimeException.class, () -> {
        clientG.hello("tom");
    });
}
Also used : RpcProviderConfig(com.baidu.hugegraph.rpc.RpcProviderConfig) RpcConsumerConfig(com.baidu.hugegraph.rpc.RpcConsumerConfig) Test(org.junit.Test)

Example 5 with RpcConsumerConfig

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

the class ServerClientTest method testMultiService.

@Test
public void testMultiService() {
    // Init server
    GraphHelloServiceImpl g1 = new GraphHelloServiceImpl("g1");
    GraphHelloServiceImpl g2 = new GraphHelloServiceImpl("g2");
    GraphHelloServiceImpl g3 = new GraphHelloServiceImpl("g3");
    RpcProviderConfig serverConfig = rpcServer.config();
    serverConfig.addService(g1.graph(), HelloService.class, g1);
    serverConfig.addService(g2.graph(), HelloService.class, g2);
    serverConfig.addService(g3.graph(), HelloService.class, g3);
    startServer(rpcServer);
    // Init client
    RpcConsumerConfig clientConfig = rpcClient.config();
    HelloService c1 = clientConfig.serviceProxy("g1", HelloService.class);
    HelloService c2 = clientConfig.serviceProxy("g2", HelloService.class);
    HelloService c3 = clientConfig.serviceProxy("g3", HelloService.class);
    // Test call
    Assert.assertEquals("g1: hello tom!", c1.hello("tom"));
    Assert.assertEquals("g1: tom", c1.echo("tom"));
    Assert.assertEquals(5.14, c1.sum(2, 3.14), 0.00000001d);
    Assert.assertEquals("g2: hello tom!", c2.hello("tom"));
    Assert.assertEquals("g2: tom", c2.echo("tom"));
    Assert.assertEquals(6.14, c2.sum(3, 3.14), 0.00000001d);
    Assert.assertEquals("g3: hello tom!", c3.hello("tom"));
    Assert.assertEquals("g3: tom", c3.echo("tom"));
    Assert.assertEquals(103.14, c3.sum(100, 3.14), 0.00000001d);
    Assert.assertEquals(5.14, g1.result(), 0.00000001d);
    Assert.assertEquals(6.14, g2.result(), 0.00000001d);
    Assert.assertEquals(103.14, g3.result(), 0.00000001d);
}
Also used : RpcProviderConfig(com.baidu.hugegraph.rpc.RpcProviderConfig) RpcConsumerConfig(com.baidu.hugegraph.rpc.RpcConsumerConfig) Test(org.junit.Test)

Aggregations

RpcConsumerConfig (com.baidu.hugegraph.rpc.RpcConsumerConfig)8 RpcProviderConfig (com.baidu.hugegraph.rpc.RpcProviderConfig)8 Test (org.junit.Test)7 RpcServer (com.baidu.hugegraph.rpc.RpcServer)4 HugeConfig (com.baidu.hugegraph.config.HugeConfig)2 RpcClientProvider (com.baidu.hugegraph.rpc.RpcClientProvider)2 HugeGraph (com.baidu.hugegraph.HugeGraph)1 InetAddress (java.net.InetAddress)1 ServerSocket (java.net.ServerSocket)1 Graph (org.apache.tinkerpop.gremlin.structure.Graph)1