/JGameSocketServer/src/test/java/com/glu/CallTest.java

http://flex-mmorpg-server.googlecode.com/ · Java · 121 lines · 86 code · 11 blank · 24 comment · 0 complexity · bc3f250d7ded40d2372cd96f94038b6c MD5 · raw file

  1. /**
  2. * Copyright 2009/9/2 com.glu Group.
  3. */
  4. package com.glu;
  5. import java.io.IOException;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import org.apache.mina.core.service.IoHandler;
  9. import org.easymock.EasyMock;
  10. import org.testng.Assert;
  11. import org.testng.annotations.AfterClass;
  12. import org.testng.annotations.BeforeClass;
  13. import org.testng.annotations.Test;
  14. import com.glu.rpc.proto.RpcChannelHandler;
  15. import com.glu.rpc.server.RpcServerHandler;
  16. import com.glu.rpc.server.io.RpcIoHandler;
  17. import com.glu.rpc.test.TestServiceHandler;
  18. import com.glu.rpc.test.TestProto.Result;
  19. import com.glu.rpc.test.TestProto.TestService;
  20. import com.glu.rpc.test.TestProto.User;
  21. import com.glu.rpc.test.TestProto.TestService.Stub;
  22. import com.google.protobuf.RpcCallback;
  23. import com.google.protobuf.RpcController;
  24. import com.google.protobuf.Service;
  25. /**
  26. * @author yubingxing
  27. *
  28. */
  29. public class CallTest {
  30. private RpcServerHandler server;
  31. private String host;
  32. private int port;
  33. @BeforeClass
  34. void setUpServer() throws IOException {
  35. host = "localhost";
  36. port = 12345;
  37. Map<String, Service> services = new HashMap<String, Service>();
  38. services.put(TestService.getDescriptor().getFullName(), new TestServiceHandler());
  39. IoHandler handler = new RpcIoHandler(services);
  40. // start rpc server
  41. server = new RpcServerHandler(host, port, handler);
  42. server.start();
  43. }
  44. @AfterClass
  45. void stopServer() {
  46. server.stop();
  47. }
  48. /**
  49. *
  50. * @throws IOException
  51. */
  52. @Test
  53. public void testRpcServer() throws IOException {
  54. final RpcController mock = EasyMock.createMock(RpcController.class);
  55. mock.reset();
  56. EasyMock.replay(mock);
  57. try {
  58. // create client to call rpc
  59. RpcChannelHandler channel = new RpcChannelHandler(host, port);
  60. RpcController controller = channel.newRpcController();
  61. Stub service = TestService.newStub(channel);
  62. // request data
  63. String reqdata = "yubingxing";
  64. User request = User.newBuilder().setUserName(reqdata).build();
  65. // response callBack
  66. RpcCallback<Result> done = new RpcCallback<Result>() {
  67. @Override
  68. public void run(Result result) {
  69. Assert.assertEquals(result.getResult(), "get user : yubingxing");
  70. Assert.assertTrue(result.getSuccess());
  71. mock.reset();
  72. }
  73. };
  74. // excute remote method
  75. service.testMethod(controller, request, done);
  76. Assert.assertFalse(controller.failed());
  77. Assert.assertEquals(controller.errorText(), null);
  78. EasyMock.verify(mock);
  79. } finally {
  80. }
  81. }
  82. /**
  83. *
  84. * @throws IOException
  85. */
  86. @Test
  87. public void testFailRpcServer() throws IOException {
  88. try {
  89. // create client to call rpc
  90. RpcChannelHandler channel = new RpcChannelHandler(host, port);
  91. RpcController controller = channel.newRpcController();
  92. Stub service = TestService.newStub(channel);
  93. // request data
  94. String reqdata = "fail";
  95. User request = User.newBuilder().setUserName(reqdata).build();
  96. // response callback
  97. RpcCallback<Result> done = new RpcCallback<Result>() {
  98. @Override
  99. public void run(Result result) {
  100. Assert.fail("should not be here!");
  101. }
  102. };
  103. // execute remote method
  104. service.testMethod(controller, request, done);
  105. Assert.assertTrue(controller.failed());
  106. Assert.assertEquals(controller.errorText(), "RPC_ERROR : java.lang.RuntimeException: try to throw exception!");
  107. }finally {
  108. }
  109. }
  110. }