Mercurial > hg
diff tests/test-wireproto-serverreactor.py @ 37055:61393f888dfe
wireproto: define and implement responses in framing protocol
Previously, we only had client-side frame types defined. This commit
defines and implements basic support for server-side frame types.
We introduce two frame types - one for representing the raw bytes
result of a command and another for representing error results.
The types are quite primitive and behavior will expand over time.
But you have to start somewhere.
Our server reactor gains methods to react to an intent to send a
response. Again, following the "sans I/O" pattern, the reactor
doesn't actually send the data. Instead, it gives the caller a
generator to frames that it can send out over the wire.
Differential Revision: https://phab.mercurial-scm.org/D2858
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 14 Mar 2018 13:57:52 -0700 |
parents | 8c3c47362934 |
children | 861e9d37e56e |
line wrap: on
line diff
--- a/tests/test-wireproto-serverreactor.py Wed Mar 14 13:32:31 2018 -0700 +++ b/tests/test-wireproto-serverreactor.py Wed Mar 14 13:57:52 2018 -0700 @@ -79,6 +79,10 @@ self.assertIsInstance(res[1], dict) self.assertEqual(res[0], expected) + def assertframesequal(self, frames, framestrings): + expected = [ffs(s) for s in framestrings] + self.assertEqual(list(frames), expected) + def test1framecommand(self): """Receiving a command in a single frame yields request to run it.""" reactor = makereactor() @@ -270,6 +274,42 @@ 'message': b'command data frame without flags', }) + def testsimpleresponse(self): + """Bytes response to command sends result frames.""" + reactor = makereactor() + list(sendcommandframes(reactor, b'mycommand', {})) + + result = reactor.onbytesresponseready(b'response') + self.assertaction(result, 'sendframes') + self.assertframesequal(result[1]['framegen'], [ + b'bytes-response eos response', + ]) + + def testmultiframeresponse(self): + """Bytes response spanning multiple frames is handled.""" + first = b'x' * framing.DEFAULT_MAX_FRAME_SIZE + second = b'y' * 100 + + reactor = makereactor() + list(sendcommandframes(reactor, b'mycommand', {})) + + result = reactor.onbytesresponseready(first + second) + self.assertaction(result, 'sendframes') + self.assertframesequal(result[1]['framegen'], [ + b'bytes-response continuation %s' % first, + b'bytes-response eos %s' % second, + ]) + + def testapplicationerror(self): + reactor = makereactor() + list(sendcommandframes(reactor, b'mycommand', {})) + + result = reactor.onapplicationerror(b'some message') + self.assertaction(result, 'sendframes') + self.assertframesequal(result[1]['framegen'], [ + b'error-response application some message', + ]) + if __name__ == '__main__': import silenttestrunner silenttestrunner.main(__name__)