package org.craft.atom.rpc;

import org.craft.atom.rpc.api.RpcClient;
import org.craft.atom.rpc.api.RpcContext;
import org.craft.atom.rpc.api.RpcFactory;
import org.craft.atom.rpc.api.RpcParameter;
import org.craft.atom.rpc.api.RpcServer;
import org.craft.atom.test.AvailablePortFinder;
import org.craft.atom.test.CaseCounter;
import org.junit.Assert;
import org.junit.Test;


/**
 * Test for RPC
 * 
 * @author mindwind
 * @version 1.0, Sep 5, 2014
 */
public class TestRpc {
	
	
	@Test
	public void testBasic() {
		int port = AvailablePortFinder.getNextAvailable();
		RpcServer server = RpcFactory.newRpcServer(port);
		server.expose(DemoService.class, new DefaultDemoService(), new RpcParameter());
		server.serve();
		RpcClient client = RpcFactory.newRpcClient("localhost", port);
		client.open();
		
		DemoService ds = client.refer(DemoService.class);
		String hi = ds.echo("hi");
		Assert.assertEquals("hi", hi);
		System.out.println(String.format("[CRAFT-ATOM-NIO] (^_^)  <%s>  Case -> test basic. ", CaseCounter.incr(1)));
	}
	
	@Test
	public void testRt() {
		int port = AvailablePortFinder.getNextAvailable();
		RpcServer server = RpcFactory.newRpcServer(port);
		server.expose(DemoService.class, new DefaultDemoService(), new RpcParameter());
		server.serve();
		RpcClient client = RpcFactory.newRpcClient("localhost", port);
		client.open();
		
		DemoService ds = client.refer(DemoService.class);
		RpcContext.getContext().setRpcTimeoutInMillis(5000);
		ds.echo("hi");
		long s = System.currentTimeMillis();
		for (int i = 0; i < 1000; i++) {
			ds.echo("hi");
		}
		long e = System.currentTimeMillis();
		long rt = e - s;
		Assert.assertTrue(rt < 2000);
		System.out.println(String.format("[CRAFT-ATOM-NIO] (^_^)  <%s>  Case -> test rt <%s> ms. ", CaseCounter.incr(1), rt));
	}
	
}

@@ -345,16 +345,41 @@
 Assert;%0A
+import org.junit.Before;%0A
 import o
@@ -496,29 +496,63 @@
 tRpc %7B%0A%09%0A%09%0A%09
-@Test
+private RpcClient client; %0A%09%0A%09%0A%09@Before
 %0A%09public voi
@@ -557,17 +557,14 @@
 oid 
-testBasic
+before
 () %7B
@@ -767,34 +767,24 @@
 .serve();%0A%09%09
-RpcClient 
 client = Rpc
@@ -834,33 +834,69 @@
 client.open();%0A%09
-%09
+%7D%0A%09%0A%09@Test%0A%09public void testBasic() %7B
 %0A%09%09DemoService d
@@ -1139,17 +1139,22 @@
 test
-R
+Timeou
 t() %7B%0A%09%09
 int 
@@ -1153,295 +1153,44 @@
 %7B%0A%09%09
-int port = AvailablePortFinder.getNextAvailable();%0A%09%09RpcServer server = RpcFactory.newRpcServer(port);%0A%09%09server.expose(DemoService.class, new DefaultDemoService(), new RpcParameter());%0A%09%09server.serve();%0A%09%09RpcClient client = RpcFactory.newRpcClient(%22localhost%22, port);%0A%09%09client.open();%0A%09%09
+%0A%09%7D%0A%09%0A%09@Test%0A%09public void testRt() %7B
 %0A%09%09D

improve - extract before() method for unit test