service_reflection_test.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #! /usr/bin/env 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. try:
  35. import unittest2 as unittest
  36. except ImportError:
  37. import unittest
  38. from google.protobuf import unittest_pb2
  39. from google.protobuf import service_reflection
  40. from google.protobuf import service
  41. class FooUnitTest(unittest.TestCase):
  42. def testService(self):
  43. class MockRpcChannel(service.RpcChannel):
  44. def CallMethod(self, method, controller, request, response, callback):
  45. self.method = method
  46. self.controller = controller
  47. self.request = request
  48. callback(response)
  49. class MockRpcController(service.RpcController):
  50. def SetFailed(self, msg):
  51. self.failure_message = msg
  52. self.callback_response = None
  53. class MyService(unittest_pb2.TestService):
  54. pass
  55. self.callback_response = None
  56. def MyCallback(response):
  57. self.callback_response = response
  58. rpc_controller = MockRpcController()
  59. channel = MockRpcChannel()
  60. srvc = MyService()
  61. srvc.Foo(rpc_controller, unittest_pb2.FooRequest(), MyCallback)
  62. self.assertEqual('Method Foo not implemented.',
  63. rpc_controller.failure_message)
  64. self.assertEqual(None, self.callback_response)
  65. rpc_controller.failure_message = None
  66. service_descriptor = unittest_pb2.TestService.GetDescriptor()
  67. srvc.CallMethod(service_descriptor.methods[1], rpc_controller,
  68. unittest_pb2.BarRequest(), MyCallback)
  69. self.assertEqual('Method Bar not implemented.',
  70. rpc_controller.failure_message)
  71. self.assertEqual(None, self.callback_response)
  72. class MyServiceImpl(unittest_pb2.TestService):
  73. def Foo(self, rpc_controller, request, done):
  74. self.foo_called = True
  75. def Bar(self, rpc_controller, request, done):
  76. self.bar_called = True
  77. srvc = MyServiceImpl()
  78. rpc_controller.failure_message = None
  79. srvc.Foo(rpc_controller, unittest_pb2.FooRequest(), MyCallback)
  80. self.assertEqual(None, rpc_controller.failure_message)
  81. self.assertEqual(True, srvc.foo_called)
  82. rpc_controller.failure_message = None
  83. srvc.CallMethod(service_descriptor.methods[1], rpc_controller,
  84. unittest_pb2.BarRequest(), MyCallback)
  85. self.assertEqual(None, rpc_controller.failure_message)
  86. self.assertEqual(True, srvc.bar_called)
  87. def testServiceStub(self):
  88. class MockRpcChannel(service.RpcChannel):
  89. def CallMethod(self, method, controller, request,
  90. response_class, callback):
  91. self.method = method
  92. self.controller = controller
  93. self.request = request
  94. callback(response_class())
  95. self.callback_response = None
  96. def MyCallback(response):
  97. self.callback_response = response
  98. channel = MockRpcChannel()
  99. stub = unittest_pb2.TestService_Stub(channel)
  100. rpc_controller = 'controller'
  101. request = 'request'
  102. # GetDescriptor now static, still works as instance method for compatibility
  103. self.assertEqual(unittest_pb2.TestService_Stub.GetDescriptor(),
  104. stub.GetDescriptor())
  105. # Invoke method.
  106. stub.Foo(rpc_controller, request, MyCallback)
  107. self.assertTrue(isinstance(self.callback_response,
  108. unittest_pb2.FooResponse))
  109. self.assertEqual(request, channel.request)
  110. self.assertEqual(rpc_controller, channel.controller)
  111. self.assertEqual(stub.GetDescriptor().methods[0], channel.method)
  112. if __name__ == '__main__':
  113. unittest.main()