http2_test_server.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright 2016 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. """HTTP2 Test Server"""
  15. import argparse
  16. import logging
  17. import sys
  18. import http2_base_server
  19. import test_data_frame_padding
  20. import test_goaway
  21. import test_max_streams
  22. import test_ping
  23. import test_rst_after_data
  24. import test_rst_after_header
  25. import test_rst_during_data
  26. import twisted
  27. import twisted.internet
  28. import twisted.internet.endpoints
  29. import twisted.internet.reactor
  30. _TEST_CASE_MAPPING = {
  31. 'rst_after_header': test_rst_after_header.TestcaseRstStreamAfterHeader,
  32. 'rst_after_data': test_rst_after_data.TestcaseRstStreamAfterData,
  33. 'rst_during_data': test_rst_during_data.TestcaseRstStreamDuringData,
  34. 'goaway': test_goaway.TestcaseGoaway,
  35. 'ping': test_ping.TestcasePing,
  36. 'max_streams': test_max_streams.TestcaseSettingsMaxStreams,
  37. # Positive tests below:
  38. 'data_frame_padding': test_data_frame_padding.TestDataFramePadding,
  39. 'no_df_padding_sanity_test': test_data_frame_padding.TestDataFramePadding,
  40. }
  41. _exit_code = 0
  42. class H2Factory(twisted.internet.protocol.Factory):
  43. def __init__(self, testcase):
  44. logging.info('Creating H2Factory for new connection (%s)', testcase)
  45. self._num_streams = 0
  46. self._testcase = testcase
  47. def buildProtocol(self, addr):
  48. self._num_streams += 1
  49. logging.info('New Connection: %d' % self._num_streams)
  50. if not _TEST_CASE_MAPPING.has_key(self._testcase):
  51. logging.error('Unknown test case: %s' % self._testcase)
  52. assert (0)
  53. else:
  54. t = _TEST_CASE_MAPPING[self._testcase]
  55. if self._testcase == 'goaway':
  56. return t(self._num_streams).get_base_server()
  57. elif self._testcase == 'no_df_padding_sanity_test':
  58. return t(use_padding=False).get_base_server()
  59. else:
  60. return t().get_base_server()
  61. def parse_arguments():
  62. parser = argparse.ArgumentParser()
  63. parser.add_argument(
  64. '--base_port',
  65. type=int,
  66. default=8080,
  67. help='base port to run the servers (default: 8080). One test server is '
  68. 'started on each incrementing port, beginning with base_port, in the '
  69. 'following order: data_frame_padding,goaway,max_streams,'
  70. 'no_df_padding_sanity_test,ping,rst_after_data,rst_after_header,'
  71. 'rst_during_data')
  72. return parser.parse_args()
  73. def listen(endpoint, test_case):
  74. deferred = endpoint.listen(H2Factory(test_case))
  75. def listen_error(reason):
  76. # If listening fails, we stop the reactor and exit the program
  77. # with exit code 1.
  78. global _exit_code
  79. _exit_code = 1
  80. logging.error('Listening failed: %s' % reason.value)
  81. twisted.internet.reactor.stop()
  82. deferred.addErrback(listen_error)
  83. def start_test_servers(base_port):
  84. """ Start one server per test case on incrementing port numbers
  85. beginning with base_port """
  86. index = 0
  87. for test_case in sorted(_TEST_CASE_MAPPING.keys()):
  88. portnum = base_port + index
  89. logging.warning('serving on port %d : %s' % (portnum, test_case))
  90. endpoint = twisted.internet.endpoints.TCP4ServerEndpoint(
  91. twisted.internet.reactor, portnum, backlog=128)
  92. # Wait until the reactor is running before calling endpoint.listen().
  93. twisted.internet.reactor.callWhenRunning(listen, endpoint, test_case)
  94. index += 1
  95. if __name__ == '__main__':
  96. logging.basicConfig(
  97. format=
  98. '%(levelname) -10s %(asctime)s %(module)s:%(lineno)s | %(message)s',
  99. level=logging.INFO)
  100. args = parse_arguments()
  101. start_test_servers(args.base_port)
  102. twisted.internet.reactor.run()
  103. sys.exit(_exit_code)