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