Skip to content

Commit 70dd704

Browse files
authored
Merge pull request #35 from drtechniko/master
Add integration test for python greeter server.
2 parents 404977c + 15a6cf2 commit 70dd704

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

examples/helloworld/python/BUILD

+17-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,24 @@ py_binary(
1717

1818
py_binary(
1919
name = "greeter_server",
20+
srcs = [":grpc_server"],
21+
)
22+
23+
py_test(
24+
name = "test_greeter_server",
25+
srcs = ["test_greeter_server.py"],
26+
deps = [":grpc_server"]
27+
)
28+
29+
# This library is necessary for creating py_test cases o/w if we explicitly add
30+
# "//examples/helloworld/proto:py" in the 'deps' list then bazel complains with:
31+
# "'//examples/helloworld/proto:py' does not have mandatory provider 'py'".
32+
# It's also useful for packaging all the server code in a lib to reuse for the :greeter_server.
33+
py_library(
34+
name = "grpc_server",
2035
srcs = [
2136
"greeter_server.py",
2237
"//examples/proto:py",
2338
"//examples/helloworld/proto:py",
24-
],
25-
)
39+
]
40+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import unittest
2+
3+
import grpc
4+
import greeter_server
5+
6+
from examples.helloworld.proto import helloworld_pb2
7+
8+
9+
def _get_random_port():
10+
import socket
11+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
12+
s.bind(("", 0))
13+
s.listen(1)
14+
port = s.getsockname()[1]
15+
s.close()
16+
return port
17+
18+
TEST_PORT = _get_random_port()
19+
20+
21+
class GreeterServerTest(unittest.TestCase):
22+
_server = None
23+
_client = None
24+
25+
def setUp(self):
26+
self._server = greeter_server._GreeterServer(greeter_server._GreeterService(), TEST_PORT)
27+
self._server.start()
28+
channel = grpc.insecure_channel('localhost:{port}'.format(port=TEST_PORT))
29+
self._client = helloworld_pb2.GreeterStub(channel)
30+
31+
def tearDown(self):
32+
self._server.stop()
33+
34+
def test_sayhello(self):
35+
hello_reply = self._client.SayHello(helloworld_pb2.HelloRequest(name='you'))
36+
self.assertEqual(hello_reply.message, 'Hello you')
37+
38+
if __name__ == '__main__':
39+
unittest.main()

0 commit comments

Comments
 (0)