@Test public void testConnectionPing() throws Exception { Configuration conf = new Configuration(); int pingInterval = 50; conf.setBoolean(CommonConfigurationKeys.IPC_CLIENT_PING_KEY, true); conf.setInt(CommonConfigurationKeys.IPC_PING_INTERVAL_KEY, pingInterval); final Server server = new RPC.Builder(conf) .setProtocol(TestProtocol.class) .setInstance(new TestImpl()) .setBindAddress(ADDRESS) .setPort(0) .setNumHandlers(5) .setVerbose(true) .build(); server.start(); final TestProtocol proxy = RPC.getProxy(TestProtocol.class, TestProtocol.versionID, server.getListenerAddress(), conf); try { // this call will throw exception if server couldn't decode the ping proxy.sleep(pingInterval * 4); } finally { if (proxy != null) RPC.stopProxy(proxy); } }
@Test public void testRpcMetrics() throws Exception { Configuration configuration = new Configuration(); final int interval = 1; configuration.setBoolean(CommonConfigurationKeys.RPC_METRICS_QUANTILE_ENABLE, true); configuration.set(CommonConfigurationKeys.RPC_METRICS_PERCENTILES_INTERVALS_KEY, "" + interval); final Server server = new RPC.Builder(configuration) .setProtocol(TestProtocol.class) .setInstance(new TestImpl()) .setBindAddress(ADDRESS) .setPort(0) .setNumHandlers(5) .setVerbose(true) .build(); server.start(); final TestProtocol proxy = RPC.getProxy( TestProtocol.class, TestProtocol.versionID, server.getListenerAddress(), configuration); try { for (int i = 0; i < 1000; i++) { proxy.ping(); proxy.echo("" + i); } MetricsRecordBuilder rpcMetrics = getMetrics(server.getRpcMetrics().name()); assertTrue( "Expected non-zero rpc queue time", getLongCounter("RpcQueueTimeNumOps", rpcMetrics) > 0); assertTrue( "Expected non-zero rpc processing time", getLongCounter("RpcProcessingTimeNumOps", rpcMetrics) > 0); MetricsAsserts.assertQuantileGauges("RpcQueueTime" + interval + "s", rpcMetrics); MetricsAsserts.assertQuantileGauges("RpcProcessingTime" + interval + "s", rpcMetrics); } finally { if (proxy != null) { RPC.stopProxy(proxy); } server.stop(); } }