blob: 013b006ed11f617b8e433e71d9769ca7c70ce3fa [file] [log] [blame]
Jan Tattermusch7897ae92017-06-07 22:57:36 +02001# Copyright 2016 gRPC authors.
Alexander Polcyn50fdc8a2017-02-09 21:06:08 -08002#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003# 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
Alexander Polcyn50fdc8a2017-02-09 21:06:08 -08006#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02007# http://www.apache.org/licenses/LICENSE-2.0
Alexander Polcyn50fdc8a2017-02-09 21:06:08 -08008#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009# 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.
Alexander Polcyn50fdc8a2017-02-09 21:06:08 -080014
15import http2_base_server
16import logging
17import messages_pb2
18
19# Set the number of padding bytes per data frame to be very large
20# relative to the number of data bytes for each data frame sent.
21_LARGE_PADDING_LENGTH = 255
22_SMALL_READ_CHUNK_SIZE = 5
23
24class TestDataFramePadding(object):
25 """
26 In response to an incoming request, this test sends headers, followed by
27 data, followed by a reset stream frame. Client asserts that the RPC failed.
28 Client needs to deliver the complete message to the application layer.
29 """
30 def __init__(self, use_padding=True):
31 self._base_server = http2_base_server.H2ProtocolBaseServer()
32 self._base_server._handlers['DataReceived'] = self.on_data_received
33 self._base_server._handlers['WindowUpdated'] = self.on_window_update
34 self._base_server._handlers['RequestReceived'] = self.on_request_received
35
36 # _total_updates maps stream ids to total flow control updates received
37 self._total_updates = {}
38 # zero window updates so far for connection window (stream id '0')
39 self._total_updates[0] = 0
40 self._read_chunk_size = _SMALL_READ_CHUNK_SIZE
41
42 if use_padding:
43 self._pad_length = _LARGE_PADDING_LENGTH
44 else:
45 self._pad_length = None
46
47 def get_base_server(self):
48 return self._base_server
49
50 def on_data_received(self, event):
51 logging.info('on data received. Stream id: %d. Data length: %d' % (event.stream_id, len(event.data)))
52 self._base_server.on_data_received_default(event)
53 if len(event.data) == 0:
54 return
55 sr = self._base_server.parse_received_data(event.stream_id)
56 stream_bytes = ''
57 # Check if full grpc msg has been read into the recv buffer yet
58 if sr:
59 response_data = self._base_server.default_response_data(sr.response_size)
60 logging.info('Stream id: %d. total resp size: %d' % (event.stream_id, len(response_data)))
61 # Begin sending the response. Add ``self._pad_length`` padding to each
62 # data frame and split the whole message into data frames each carrying
63 # only self._read_chunk_size of data.
64 # The purpose is to have the majority of the data frame response bytes
65 # be padding bytes, since ``self._pad_length`` >> ``self._read_chunk_size``.
66 self._base_server.setup_send(response_data , event.stream_id, pad_length=self._pad_length, read_chunk_size=self._read_chunk_size)
67
68 def on_request_received(self, event):
69 self._base_server.on_request_received_default(event)
70 logging.info('on request received. Stream id: %s.' % event.stream_id)
71 self._total_updates[event.stream_id] = 0
72
73 # Log debug info and try to resume sending on all currently active streams.
74 def on_window_update(self, event):
75 logging.info('on window update. Stream id: %s. Delta: %s' % (event.stream_id, event.delta))
76 self._total_updates[event.stream_id] += event.delta
77 total = self._total_updates[event.stream_id]
78 logging.info('... - total updates for stream %d : %d' % (event.stream_id, total))
79 self._base_server.on_window_update_default(event, pad_length=self._pad_length, read_chunk_size=self._read_chunk_size)