/java/src/test/java/com/googlecode/protobuf/socketrpc/IntegrationTest.java

http://protobuf-socket-rpc.googlecode.com/ · Java · 160 lines · 116 code · 22 blank · 22 comment · 1 complexity · 49cf9285afe26d28ec36833c6a6b8144 MD5 · raw file

  1. // Copyright (c) 2010 Shardul Deo
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package com.googlecode.protobuf.socketrpc;
  21. import java.io.Closeable;
  22. import java.io.IOException;
  23. import java.util.concurrent.ExecutorService;
  24. import java.util.concurrent.Executors;
  25. import com.google.protobuf.BlockingRpcChannel;
  26. import com.google.protobuf.RpcCallback;
  27. import com.google.protobuf.RpcChannel;
  28. import com.google.protobuf.ServiceException;
  29. import com.googlecode.protobuf.socketrpc.TestProtos.Request;
  30. import com.googlecode.protobuf.socketrpc.TestProtos.Response;
  31. import com.googlecode.protobuf.socketrpc.TestProtos.TestService;
  32. import com.googlecode.protobuf.socketrpc.TestProtos.TestService.BlockingInterface;
  33. import junit.framework.TestCase;
  34. /**
  35. * @author Shardul Deo
  36. */
  37. public class IntegrationTest extends TestCase {
  38. private static final Request REQUEST = Request.newBuilder().setStrData(
  39. "Request").build();
  40. private static final Response RESPONSE = Response.newBuilder().setStrData(
  41. "Response").build();
  42. private ExecutorService threadPool;
  43. private RpcConnectionFactory clientConnectionFactory;
  44. private ServerRpcConnectionFactory serverConnectionFactory;
  45. private FakeServiceImpl service;
  46. @Override
  47. protected void setUp() throws Exception {
  48. super.setUp();
  49. threadPool = Executors.newFixedThreadPool(10);
  50. clientConnectionFactory = SocketRpcConnectionFactories
  51. .createRpcConnectionFactory("localhost", 8080);
  52. serverConnectionFactory = SocketRpcConnectionFactories
  53. .createServerRpcConnectionFactory(8080, -1, null);
  54. service = new FakeServiceImpl(REQUEST).withResponse(RESPONSE);
  55. }
  56. public void testBlockingService() throws InterruptedException,
  57. ServiceException, IOException {
  58. RpcServer rpcServer = new RpcServer(serverConnectionFactory, threadPool,
  59. true);
  60. rpcServer.registerBlockingService(TestService
  61. .newReflectiveBlockingService(service));
  62. doTest(rpcServer);
  63. }
  64. public void testNonBlockingService() throws InterruptedException,
  65. ServiceException, IOException {
  66. RpcServer rpcServer = new RpcServer(serverConnectionFactory, threadPool,
  67. true);
  68. rpcServer.registerService(service);
  69. doTest(rpcServer);
  70. }
  71. public void testBlockingService_persistent() throws InterruptedException,
  72. ServiceException, IOException {
  73. serverConnectionFactory = PersistentRpcConnectionFactory
  74. .createServerInstance(serverConnectionFactory);
  75. RpcServer rpcServer = new RpcServer(serverConnectionFactory, threadPool,
  76. true);
  77. rpcServer.registerBlockingService(TestService
  78. .newReflectiveBlockingService(service));
  79. clientConnectionFactory = PersistentRpcConnectionFactory
  80. .createInstance(clientConnectionFactory);
  81. doTest(rpcServer);
  82. }
  83. public void testNonBlockingService_persistent() throws InterruptedException,
  84. ServiceException, IOException {
  85. serverConnectionFactory = PersistentRpcConnectionFactory
  86. .createServerInstance(serverConnectionFactory);
  87. RpcServer rpcServer = new RpcServer(serverConnectionFactory, threadPool,
  88. true);
  89. rpcServer.registerService(service);
  90. clientConnectionFactory = PersistentRpcConnectionFactory
  91. .createInstance(clientConnectionFactory);
  92. doTest(rpcServer);
  93. }
  94. private void doTest(RpcServer rpcServer) throws InterruptedException,
  95. ServiceException, IOException {
  96. BlockingRpcChannel blockingChannel = RpcChannels
  97. .newBlockingRpcChannel(clientConnectionFactory);
  98. RpcChannel channel = RpcChannels.newRpcChannel(clientConnectionFactory,
  99. threadPool);
  100. BlockingInterface blockingStub = TestService
  101. .newBlockingStub(blockingChannel);
  102. TestService stub = TestService.newStub(channel);
  103. try {
  104. rpcServer.startServer();
  105. Thread.sleep(500);
  106. doRpc(stub);
  107. doBlockingRpc(blockingStub);
  108. doBlockingRpc(blockingStub);
  109. doRpc(stub);
  110. } finally {
  111. Thread.sleep(500);
  112. System.out.println("Closing Client");
  113. if (clientConnectionFactory instanceof Closeable) {
  114. ((PersistentRpcConnectionFactory) clientConnectionFactory).close();
  115. }
  116. Thread.sleep(100);
  117. System.out.println("Closing Server");
  118. rpcServer.shutDown();
  119. }
  120. }
  121. private static void doRpc(TestService stub) {
  122. final SocketRpcController controller = new SocketRpcController();
  123. stub.testMethod(controller, REQUEST, new RpcCallback<Response>() {
  124. @Override
  125. public void run(Response response) {
  126. assertFalse(controller.failed());
  127. assertEquals(RESPONSE, response);
  128. }
  129. });
  130. }
  131. private static void doBlockingRpc(BlockingInterface blockingStub)
  132. throws ServiceException {
  133. SocketRpcController controller = new SocketRpcController();
  134. Response response = blockingStub.testMethod(controller, REQUEST);
  135. assertFalse(controller.failed());
  136. assertEquals(RESPONSE, response);
  137. }
  138. }