compression_example_test.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright 2019 the gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Test for compression example."""
  15. import contextlib
  16. import os
  17. import socket
  18. import subprocess
  19. import unittest
  20. _BINARY_DIR = os.path.realpath(
  21. os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
  22. _SERVER_PATH = os.path.join(_BINARY_DIR, 'server')
  23. _CLIENT_PATH = os.path.join(_BINARY_DIR, 'client')
  24. @contextlib.contextmanager
  25. def _get_port():
  26. sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
  27. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
  28. if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 0:
  29. raise RuntimeError("Failed to set SO_REUSEPORT.")
  30. sock.bind(('', 0))
  31. try:
  32. yield sock.getsockname()[1]
  33. finally:
  34. sock.close()
  35. class CompressionExampleTest(unittest.TestCase):
  36. def test_compression_example(self):
  37. with _get_port() as test_port:
  38. server_process = subprocess.Popen(
  39. (_SERVER_PATH, '--port', str(test_port), '--server_compression',
  40. 'gzip'))
  41. try:
  42. server_target = 'localhost:{}'.format(test_port)
  43. client_process = subprocess.Popen(
  44. (_CLIENT_PATH, '--server', server_target,
  45. '--channel_compression', 'gzip'))
  46. client_return_code = client_process.wait()
  47. self.assertEqual(0, client_return_code)
  48. self.assertIsNone(server_process.poll())
  49. finally:
  50. server_process.kill()
  51. server_process.wait()
  52. if __name__ == '__main__':
  53. unittest.main(verbosity=2)