test_goaway.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. import logging
  15. import time
  16. import http2_base_server
  17. class TestcaseGoaway(object):
  18. """
  19. This test does the following:
  20. Process incoming request normally, i.e. send headers, data and trailers.
  21. Then send a GOAWAY frame with the stream id of the processed request.
  22. It checks that the next request is made on a different TCP connection.
  23. """
  24. def __init__(self, iteration):
  25. self._base_server = http2_base_server.H2ProtocolBaseServer()
  26. self._base_server._handlers[
  27. 'RequestReceived'] = self.on_request_received
  28. self._base_server._handlers['DataReceived'] = self.on_data_received
  29. self._base_server._handlers['SendDone'] = self.on_send_done
  30. self._base_server._handlers['ConnectionLost'] = self.on_connection_lost
  31. self._ready_to_send = False
  32. self._iteration = iteration
  33. def get_base_server(self):
  34. return self._base_server
  35. def on_connection_lost(self, reason):
  36. logging.info('Disconnect received. Count %d' % self._iteration)
  37. # _iteration == 2 => Two different connections have been used.
  38. if self._iteration == 2:
  39. self._base_server.on_connection_lost(reason)
  40. def on_send_done(self, stream_id):
  41. self._base_server.on_send_done_default(stream_id)
  42. logging.info('Sending GOAWAY for stream %d:' % stream_id)
  43. self._base_server._conn.close_connection(error_code=0,
  44. additional_data=None,
  45. last_stream_id=stream_id)
  46. self._base_server._stream_status[stream_id] = False
  47. def on_request_received(self, event):
  48. self._ready_to_send = False
  49. self._base_server.on_request_received_default(event)
  50. def on_data_received(self, event):
  51. self._base_server.on_data_received_default(event)
  52. sr = self._base_server.parse_received_data(event.stream_id)
  53. if sr:
  54. logging.info('Creating response size = %s' % sr.response_size)
  55. response_data = self._base_server.default_response_data(
  56. sr.response_size)
  57. self._ready_to_send = True
  58. self._base_server.setup_send(response_data, event.stream_id)