comparison tests/test-wireproto-serverreactor.py @ 37056:861e9d37e56e

wireproto: buffer output frames when in half duplex mode Previously, when told that a response was ready, the server reactor would instruct the caller to send frames immediately. This was OK as an initial implementation. But it would not work for half-duplex connections where the sender can't receive until all data has been transmitted - such as httplib based clients. In this commit, we teach the reactor that output frames should be buffered until end of input is seen. This required a new event to inform the reactor of end of input. The result from that event will instruct the consumer to send all buffered frames. The HTTP server is buffered by default. This change effectively hides the complexity of buffering within the reactor so that transports need not be concerned about it. This helps keep the transports "dumb" and will make implementing multiple requests-responses per atomic exchange (like an HTTP request) much simpler. Differential Revision: https://phab.mercurial-scm.org/D2860
author Gregory Szorc <gregory.szorc@gmail.com>
date Wed, 14 Mar 2018 14:01:16 -0700
parents 61393f888dfe
children 2ec1fb9de638
comparison
equal deleted inserted replaced
37055:61393f888dfe 37056:861e9d37e56e
7 wireprotoframing as framing, 7 wireprotoframing as framing,
8 ) 8 )
9 9
10 ffs = framing.makeframefromhumanstring 10 ffs = framing.makeframefromhumanstring
11 11
12 def makereactor(): 12 def makereactor(deferoutput=False):
13 return framing.serverreactor() 13 return framing.serverreactor(deferoutput=deferoutput)
14 14
15 def sendframes(reactor, gen): 15 def sendframes(reactor, gen):
16 """Send a generator of frame bytearray to a reactor. 16 """Send a generator of frame bytearray to a reactor.
17 17
18 Emits a generator of results from ``onframerecv()`` calls. 18 Emits a generator of results from ``onframerecv()`` calls.
93 'command': b'mycommand', 93 'command': b'mycommand',
94 'args': {}, 94 'args': {},
95 'data': None, 95 'data': None,
96 }) 96 })
97 97
98 result = reactor.oninputeof()
99 self.assertaction(result, 'noop')
100
98 def test1argument(self): 101 def test1argument(self):
99 reactor = makereactor() 102 reactor = makereactor()
100 results = list(sendcommandframes(reactor, b'mycommand', 103 results = list(sendcommandframes(reactor, b'mycommand',
101 {b'foo': b'bar'})) 104 {b'foo': b'bar'}))
102 self.assertEqual(len(results), 2) 105 self.assertEqual(len(results), 2)
308 self.assertaction(result, 'sendframes') 311 self.assertaction(result, 'sendframes')
309 self.assertframesequal(result[1]['framegen'], [ 312 self.assertframesequal(result[1]['framegen'], [
310 b'error-response application some message', 313 b'error-response application some message',
311 ]) 314 ])
312 315
316 def test1commanddeferresponse(self):
317 """Responses when in deferred output mode are delayed until EOF."""
318 reactor = makereactor(deferoutput=True)
319 results = list(sendcommandframes(reactor, b'mycommand', {}))
320 self.assertEqual(len(results), 1)
321 self.assertaction(results[0], 'runcommand')
322
323 result = reactor.onbytesresponseready(b'response')
324 self.assertaction(result, 'noop')
325 result = reactor.oninputeof()
326 self.assertaction(result, 'sendframes')
327 self.assertframesequal(result[1]['framegen'], [
328 b'bytes-response eos response',
329 ])
330
331 def testmultiplecommanddeferresponse(self):
332 reactor = makereactor(deferoutput=True)
333 list(sendcommandframes(reactor, b'command1', {}))
334 list(sendcommandframes(reactor, b'command2', {}))
335
336 result = reactor.onbytesresponseready(b'response1')
337 self.assertaction(result, 'noop')
338 result = reactor.onbytesresponseready(b'response2')
339 self.assertaction(result, 'noop')
340 result = reactor.oninputeof()
341 self.assertaction(result, 'sendframes')
342 self.assertframesequal(result[1]['framegen'], [
343 b'bytes-response eos response1',
344 b'bytes-response eos response2'
345 ])
346
313 if __name__ == '__main__': 347 if __name__ == '__main__':
314 import silenttestrunner 348 import silenttestrunner
315 silenttestrunner.main(__name__) 349 silenttestrunner.main(__name__)