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