From 15a6cf2db2bfd33a5f6cda0f6bc4d6d3112f74fb Mon Sep 17 00:00:00 2001 From: Nikos Michalakis Date: Sun, 6 Nov 2016 19:07:46 -0800 Subject: [PATCH] Add integration test for python greeter server. --- examples/helloworld/python/BUILD | 19 ++++++++- .../helloworld/python/test_greeter_server.py | 39 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 examples/helloworld/python/test_greeter_server.py diff --git a/examples/helloworld/python/BUILD b/examples/helloworld/python/BUILD index 59cb93b9..c52d72d8 100644 --- a/examples/helloworld/python/BUILD +++ b/examples/helloworld/python/BUILD @@ -17,9 +17,24 @@ py_binary( py_binary( name = "greeter_server", + srcs = [":grpc_server"], +) + +py_test( + name = "test_greeter_server", + srcs = ["test_greeter_server.py"], + deps = [":grpc_server"] +) + +# This library is necessary for creating py_test cases o/w if we explicitly add +# "//examples/helloworld/proto:py" in the 'deps' list then bazel complains with: +# "'//examples/helloworld/proto:py' does not have mandatory provider 'py'". +# It's also useful for packaging all the server code in a lib to reuse for the :greeter_server. +py_library( + name = "grpc_server", srcs = [ "greeter_server.py", "//examples/proto:py", "//examples/helloworld/proto:py", - ], -) + ] +) \ No newline at end of file diff --git a/examples/helloworld/python/test_greeter_server.py b/examples/helloworld/python/test_greeter_server.py new file mode 100644 index 00000000..f9bc5f0d --- /dev/null +++ b/examples/helloworld/python/test_greeter_server.py @@ -0,0 +1,39 @@ +import unittest + +import grpc +import greeter_server + +from examples.helloworld.proto import helloworld_pb2 + + +def _get_random_port(): + import socket + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.bind(("", 0)) + s.listen(1) + port = s.getsockname()[1] + s.close() + return port + +TEST_PORT = _get_random_port() + + +class GreeterServerTest(unittest.TestCase): + _server = None + _client = None + + def setUp(self): + self._server = greeter_server._GreeterServer(greeter_server._GreeterService(), TEST_PORT) + self._server.start() + channel = grpc.insecure_channel('localhost:{port}'.format(port=TEST_PORT)) + self._client = helloworld_pb2.GreeterStub(channel) + + def tearDown(self): + self._server.stop() + + def test_sayhello(self): + hello_reply = self._client.SayHello(helloworld_pb2.HelloRequest(name='you')) + self.assertEqual(hello_reply.message, 'Hello you') + +if __name__ == '__main__': + unittest.main()