PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/common/rpc/StreamRpcChannelTest.cpp

https://code.google.com/
C++ | 203 lines | 139 code | 33 blank | 31 comment | 0 complexity | 66e53816591f273f57019a8bd4eb2af9 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Library General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. * StreamRpcChannelTest.cpp
  17. * Test fixture for the StreamRpcChannel class
  18. * Copyright (C) 2005-2008 Simon Newton
  19. */
  20. #include <cppunit/extensions/HelperMacros.h>
  21. #include <google/protobuf/stubs/common.h>
  22. #include <string>
  23. #include "ola/io/SelectServer.h"
  24. #include "ola/network/Socket.h"
  25. #include "common/rpc/StreamRpcChannel.h"
  26. #include "common/rpc/SimpleRpcController.h"
  27. #include "common/rpc/TestService.pb.h"
  28. using google::protobuf::NewCallback;
  29. using ola::io::LoopbackDescriptor;
  30. using ola::io::SelectServer;
  31. using ola::rpc::EchoReply;
  32. using ola::rpc::EchoRequest;
  33. using ola::rpc::STREAMING_NO_RESPONSE;
  34. using ola::rpc::SimpleRpcController;
  35. using ola::rpc::StreamRpcChannel;
  36. using ola::rpc::TestService;
  37. using ola::rpc::TestService_Stub;
  38. using std::string;
  39. /*
  40. * Our test implementation
  41. */
  42. class TestServiceImpl: public TestService {
  43. public:
  44. explicit TestServiceImpl(SelectServer *ss): m_ss(ss) {}
  45. ~TestServiceImpl() {}
  46. void Echo(::google::protobuf::RpcController* controller,
  47. const EchoRequest* request,
  48. EchoReply* response,
  49. ::google::protobuf::Closure* done);
  50. void FailedEcho(::google::protobuf::RpcController* controller,
  51. const EchoRequest* request,
  52. EchoReply* response,
  53. ::google::protobuf::Closure* done);
  54. void Stream(::google::protobuf::RpcController* controller,
  55. const ::ola::rpc::EchoRequest* request,
  56. STREAMING_NO_RESPONSE* response,
  57. ::google::protobuf::Closure* done);
  58. private:
  59. SelectServer *m_ss;
  60. };
  61. class StreamRpcChannelTest: public CppUnit::TestFixture {
  62. CPPUNIT_TEST_SUITE(StreamRpcChannelTest);
  63. CPPUNIT_TEST(testEcho);
  64. CPPUNIT_TEST(testFailedEcho);
  65. CPPUNIT_TEST(testStreamRequest);
  66. CPPUNIT_TEST_SUITE_END();
  67. public:
  68. void setUp();
  69. void tearDown();
  70. void testEcho();
  71. void testFailedEcho();
  72. void testStreamRequest();
  73. void EchoComplete();
  74. void FailedEchoComplete();
  75. private:
  76. int m_fd_pair[2];
  77. SimpleRpcController m_controller;
  78. EchoRequest m_request;
  79. EchoReply m_reply;
  80. TestService_Stub *m_stub;
  81. SelectServer m_ss;
  82. TestServiceImpl *m_service;
  83. StreamRpcChannel *m_channel;
  84. LoopbackDescriptor *m_socket;
  85. };
  86. CPPUNIT_TEST_SUITE_REGISTRATION(StreamRpcChannelTest);
  87. void TestServiceImpl::Echo(::google::protobuf::RpcController* controller,
  88. const EchoRequest* request,
  89. EchoReply* response,
  90. ::google::protobuf::Closure* done) {
  91. response->set_data(request->data());
  92. done->Run();
  93. (void) controller;
  94. (void) request;
  95. }
  96. void TestServiceImpl::FailedEcho(::google::protobuf::RpcController* controller,
  97. const EchoRequest* request,
  98. EchoReply* response,
  99. ::google::protobuf::Closure* done) {
  100. controller->SetFailed("Error");
  101. done->Run();
  102. (void) request;
  103. (void) response;
  104. }
  105. void TestServiceImpl::Stream(::google::protobuf::RpcController* controller,
  106. const ::ola::rpc::EchoRequest* request,
  107. STREAMING_NO_RESPONSE* response,
  108. ::google::protobuf::Closure* done) {
  109. CPPUNIT_ASSERT(!controller);
  110. CPPUNIT_ASSERT(!response);
  111. CPPUNIT_ASSERT(!done);
  112. CPPUNIT_ASSERT(request);
  113. CPPUNIT_ASSERT_EQUAL(string("foo"), request->data());
  114. m_ss->Terminate();
  115. }
  116. void StreamRpcChannelTest::setUp() {
  117. m_socket = new LoopbackDescriptor();
  118. m_socket->Init();
  119. m_service = new TestServiceImpl(&m_ss);
  120. m_channel = new StreamRpcChannel(m_service, m_socket);
  121. m_ss.AddReadDescriptor(m_socket);
  122. m_stub = new TestService_Stub(m_channel);
  123. }
  124. void StreamRpcChannelTest::tearDown() {
  125. m_ss.RemoveReadDescriptor(m_socket);
  126. delete m_socket;
  127. delete m_stub;
  128. delete m_channel;
  129. delete m_service;
  130. }
  131. void StreamRpcChannelTest::EchoComplete() {
  132. m_ss.Terminate();
  133. CPPUNIT_ASSERT(!m_controller.Failed());
  134. CPPUNIT_ASSERT_EQUAL(m_reply.data(), m_request.data());
  135. }
  136. void StreamRpcChannelTest::FailedEchoComplete() {
  137. m_ss.Terminate();
  138. CPPUNIT_ASSERT(m_controller.Failed());
  139. }
  140. /*
  141. * Check that we can call the echo method in the TestServiceImpl.
  142. */
  143. void StreamRpcChannelTest::testEcho() {
  144. m_request.set_data("foo");
  145. m_stub->Echo(&m_controller,
  146. &m_request,
  147. &m_reply,
  148. NewCallback(this, &StreamRpcChannelTest::EchoComplete));
  149. m_ss.Run();
  150. }
  151. /*
  152. * Check that method that fail return correctly
  153. */
  154. void StreamRpcChannelTest::testFailedEcho() {
  155. m_request.set_data("foo");
  156. m_stub->FailedEcho(
  157. &m_controller,
  158. &m_request,
  159. &m_reply,
  160. NewCallback(this, &StreamRpcChannelTest::FailedEchoComplete));
  161. m_ss.Run();
  162. }
  163. /*
  164. * Check stream requests work
  165. */
  166. void StreamRpcChannelTest::testStreamRequest() {
  167. m_request.set_data("foo");
  168. m_stub->Stream(NULL, &m_request, NULL, NULL);
  169. m_ss.Run();
  170. }