service_reflection_test.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #! /usr/bin/python
  2. #
  3. # Protocol Buffers - Google's data interchange format
  4. # Copyright 2008 Google Inc. All rights reserved.
  5. # https://developers.google.com/protocol-buffers/
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions are
  9. # met:
  10. #
  11. # * Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. # * Redistributions in binary form must reproduce the above
  14. # copyright notice, this list of conditions and the following disclaimer
  15. # in the documentation and/or other materials provided with the
  16. # distribution.
  17. # * Neither the name of Google Inc. nor the names of its
  18. # contributors may be used to endorse or promote products derived from
  19. # this software without specific prior written permission.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. """Tests for google.protobuf.internal.service_reflection."""
  33. __author__ = 'petar@google.com (Petar Petrov)'
  34. import unittest
  35. from google.protobuf import unittest_pb2
  36. from google.protobuf import service_reflection
  37. from google.protobuf import service
  38. class FooUnitTest(unittest.TestCase):
  39. def testService(self):
  40. class MockRpcChannel(service.RpcChannel):
  41. def CallMethod(self, method, controller, request, response, callback):
  42. self.method = method
  43. self.controller = controller
  44. self.request = request
  45. callback(response)
  46. class MockRpcController(service.RpcController):
  47. def SetFailed(self, msg):
  48. self.failure_message = msg
  49. self.callback_response = None
  50. class MyService(unittest_pb2.TestService):
  51. pass
  52. self.callback_response = None
  53. def MyCallback(response):
  54. self.callback_response = response
  55. rpc_controller = MockRpcController()
  56. channel = MockRpcChannel()
  57. srvc = MyService()
  58. srvc.Foo(rpc_controller, unittest_pb2.FooRequest(), MyCallback)
  59. self.assertEqual('Method Foo not implemented.',
  60. rpc_controller.failure_message)
  61. self.assertEqual(None, self.callback_response)
  62. rpc_controller.failure_message = None
  63. service_descriptor = unittest_pb2.TestService.GetDescriptor()
  64. srvc.CallMethod(service_descriptor.methods[1], rpc_controller,
  65. unittest_pb2.BarRequest(), MyCallback)
  66. self.assertEqual('Method Bar not implemented.',
  67. rpc_controller.failure_message)
  68. self.assertEqual(None, self.callback_response)
  69. class MyServiceImpl(unittest_pb2.TestService):
  70. def Foo(self, rpc_controller, request, done):
  71. self.foo_called = True
  72. def Bar(self, rpc_controller, request, done):
  73. self.bar_called = True
  74. srvc = MyServiceImpl()
  75. rpc_controller.failure_message = None
  76. srvc.Foo(rpc_controller, unittest_pb2.FooRequest(), MyCallback)
  77. self.assertEqual(None, rpc_controller.failure_message)
  78. self.assertEqual(True, srvc.foo_called)
  79. rpc_controller.failure_message = None
  80. srvc.CallMethod(service_descriptor.methods[1], rpc_controller,
  81. unittest_pb2.BarRequest(), MyCallback)
  82. self.assertEqual(None, rpc_controller.failure_message)
  83. self.assertEqual(True, srvc.bar_called)
  84. def testServiceStub(self):
  85. class MockRpcChannel(service.RpcChannel):
  86. def CallMethod(self, method, controller, request,
  87. response_class, callback):
  88. self.method = method
  89. self.controller = controller
  90. self.request = request
  91. callback(response_class())
  92. self.callback_response = None
  93. def MyCallback(response):
  94. self.callback_response = response
  95. channel = MockRpcChannel()
  96. stub = unittest_pb2.TestService_Stub(channel)
  97. rpc_controller = 'controller'
  98. request = 'request'
  99. # GetDescriptor now static, still works as instance method for compatibility
  100. self.assertEqual(unittest_pb2.TestService_Stub.GetDescriptor(),
  101. stub.GetDescriptor())
  102. # Invoke method.
  103. stub.Foo(rpc_controller, request, MyCallback)
  104. self.assertTrue(isinstance(self.callback_response,
  105. unittest_pb2.FooResponse))
  106. self.assertEqual(request, channel.request)
  107. self.assertEqual(rpc_controller, channel.controller)
  108. self.assertEqual(stub.GetDescriptor().methods[0], channel.method)
  109. if __name__ == '__main__':
  110. unittest.main()