Imported Upstream version 1.27.0
[platform/upstream/grpc.git] / test / http2_test / test_goaway.py
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
15 import logging
16 import time
17
18 import http2_base_server
19
20
21 class 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'] = self.on_request_received
33         self._base_server._handlers['DataReceived'] = self.on_data_received
34         self._base_server._handlers['SendDone'] = self.on_send_done
35         self._base_server._handlers['ConnectionLost'] = self.on_connection_lost
36         self._ready_to_send = False
37         self._iteration = iteration
38
39     def get_base_server(self):
40         return self._base_server
41
42     def on_connection_lost(self, reason):
43         logging.info('Disconnect received. Count %d' % self._iteration)
44         # _iteration == 2 => Two different connections have been used.
45         if self._iteration == 2:
46             self._base_server.on_connection_lost(reason)
47
48     def on_send_done(self, stream_id):
49         self._base_server.on_send_done_default(stream_id)
50         logging.info('Sending GOAWAY for stream %d:' % stream_id)
51         self._base_server._conn.close_connection(error_code=0,
52                                                  additional_data=None,
53                                                  last_stream_id=stream_id)
54         self._base_server._stream_status[stream_id] = False
55
56     def on_request_received(self, event):
57         self._ready_to_send = False
58         self._base_server.on_request_received_default(event)
59
60     def on_data_received(self, event):
61         self._base_server.on_data_received_default(event)
62         sr = self._base_server.parse_received_data(event.stream_id)
63         if sr:
64             logging.info('Creating response size = %s' % sr.response_size)
65             response_data = self._base_server.default_response_data(
66                 sr.response_size)
67             self._ready_to_send = True
68             self._base_server.setup_send(response_data, event.stream_id)