service_reflection_test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc.
  3. # http://code.google.com/p/protobuf/
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Tests for google.protobuf.internal.service_reflection."""
  17. __author__ = 'petar@google.com (Petar Petrov)'
  18. import unittest
  19. from google.protobuf import unittest_pb2
  20. from google.protobuf import service_reflection
  21. from google.protobuf import service
  22. class FooUnitTest(unittest.TestCase):
  23. def testService(self):
  24. class MockRpcChannel(service.RpcChannel):
  25. def CallMethod(self, method, controller, request, response, callback):
  26. self.method = method
  27. self.controller = controller
  28. self.request = request
  29. callback(response)
  30. class MockRpcController(service.RpcController):
  31. def SetFailed(self, msg):
  32. self.failure_message = msg
  33. self.callback_response = None
  34. class MyService(unittest_pb2.TestService):
  35. pass
  36. self.callback_response = None
  37. def MyCallback(response):
  38. self.callback_response = response
  39. rpc_controller = MockRpcController()
  40. channel = MockRpcChannel()
  41. srvc = MyService()
  42. srvc.Foo(rpc_controller, unittest_pb2.FooRequest(), MyCallback)
  43. self.assertEqual('Method Foo not implemented.',
  44. rpc_controller.failure_message)
  45. self.assertEqual(None, self.callback_response)
  46. rpc_controller.failure_message = None
  47. service_descriptor = unittest_pb2.TestService.DESCRIPTOR
  48. srvc.CallMethod(service_descriptor.methods[1], rpc_controller,
  49. unittest_pb2.BarRequest(), MyCallback)
  50. self.assertEqual('Method Bar not implemented.',
  51. rpc_controller.failure_message)
  52. self.assertEqual(None, self.callback_response)
  53. class MyServiceImpl(unittest_pb2.TestService):
  54. def Foo(self, rpc_controller, request, done):
  55. self.foo_called = True
  56. def Bar(self, rpc_controller, request, done):
  57. self.bar_called = True
  58. srvc = MyServiceImpl()
  59. rpc_controller.failure_message = None
  60. srvc.Foo(rpc_controller, unittest_pb2.FooRequest(), MyCallback)
  61. self.assertEqual(None, rpc_controller.failure_message)
  62. self.assertEqual(True, srvc.foo_called)
  63. rpc_controller.failure_message = None
  64. srvc.CallMethod(service_descriptor.methods[1], rpc_controller,
  65. unittest_pb2.BarRequest(), MyCallback)
  66. self.assertEqual(None, rpc_controller.failure_message)
  67. self.assertEqual(True, srvc.bar_called)
  68. def testServiceStub(self):
  69. class MockRpcChannel(service.RpcChannel):
  70. def CallMethod(self, method, controller, request,
  71. response_class, callback):
  72. self.method = method
  73. self.controller = controller
  74. self.request = request
  75. callback(response_class())
  76. self.callback_response = None
  77. def MyCallback(response):
  78. self.callback_response = response
  79. channel = MockRpcChannel()
  80. stub = unittest_pb2.TestService_Stub(channel)
  81. rpc_controller = 'controller'
  82. request = 'request'
  83. # Invoke method.
  84. stub.Foo(rpc_controller, request, MyCallback)
  85. self.assertTrue(isinstance(self.callback_response,
  86. unittest_pb2.FooResponse))
  87. self.assertEqual(request, channel.request)
  88. self.assertEqual(rpc_controller, channel.controller)
  89. self.assertEqual(stub.GetDescriptor().methods[0], channel.method)
  90. if __name__ == '__main__':
  91. unittest.main()