def testrequestidtracking(self): reactor = makereactor(deferoutput=True) instream = framing.stream(1) list(sendcommandframes(reactor, instream, 1, b'command1', {})) list(sendcommandframes(reactor, instream, 3, b'command2', {})) list(sendcommandframes(reactor, instream, 5, b'command3', {})) # Register results for commands out of order. outstream = reactor.makeoutputstream() reactor.oncommandresponsereadyobjects(outstream, 3, [b'response3']) reactor.oncommandresponsereadyobjects(outstream, 1, [b'response1']) reactor.oncommandresponsereadyobjects(outstream, 5, [b'response5']) result = reactor.oninputeof() self.assertaction(result, b'sendframes') self.assertframesequal(result[1][b'framegen'], [ b'3 2 stream-begin stream-settings eos cbor:b"identity"', b'3 2 encoded command-response continuation %s' % OK, b'3 2 encoded command-response continuation cbor:b"response3"', b'3 2 0 command-response eos ', b'1 2 encoded command-response continuation %s' % OK, b'1 2 encoded command-response continuation cbor:b"response1"', b'1 2 0 command-response eos ', b'5 2 encoded command-response continuation %s' % OK, b'5 2 encoded command-response continuation cbor:b"response5"', b'5 2 0 command-response eos ', ])
def testtextoutput1simpleatom(self): stream = framing.stream(1) val = list(framing.createtextoutputframe(stream, 1, [(b'foo', [], [])])) self.assertEqual(val, [ ffs(b'1 1 stream-begin text-output 0 ' b"cbor:[{b'msg': b'foo'}]"), ])
def testtextoutput1label(self): stream = framing.stream(1) val = list(framing.createtextoutputframe(stream, 1, [ (b'foo', [], [b'label']), ])) self.assertEqual(val, [ ffs(b'1 1 stream-begin text-output 0 ' b"cbor:[{b'msg': b'foo', b'labels': [b'label']}]") ])
def testtextoutput2arg(self): stream = framing.stream(1) val = list(framing.createtextoutputframe(stream, 1, [ (b'foo %s %s', [b'val', b'value'], []), ])) self.assertEqual(val, [ ffs(b'1 1 stream-begin text-output 0 ' b"cbor:[{b'msg': b'foo %s %s', b'args': [b'val', b'value']}]") ])
def testduplicaterequestonactivecommand(self): """Receiving a request ID that matches a request that isn't finished.""" reactor = makereactor() stream = framing.stream(1) list(sendcommandframes(reactor, stream, 1, b'command1', {})) results = list(sendcommandframes(reactor, stream, 1, b'command1', {})) self.assertaction(results[0], b'error') self.assertEqual(results[0][1], { b'message': b'request with ID 1 is already active', })
def testsimpleresponse(self): """Bytes response to command sends result frames.""" reactor = makereactor() instream = framing.stream(1) list(sendcommandframes(reactor, instream, 1, b'mycommand', {})) outstream = reactor.makeoutputstream() result = reactor.oncommandresponseready(outstream, 1, b'response') self.assertaction(result, b'sendframes') self.assertframesequal(result[1][b'framegen'], [ b'1 2 stream-begin command-response eos %sresponse' % OK, ])
def testdataexactframesize(self): data = util.bytesio(b'x' * framing.DEFAULT_MAX_FRAME_SIZE) stream = framing.stream(1) frames = list( framing.createcommandframes(stream, 1, b'command', {}, data)) self.assertEqual(frames, [ ffs(b'1 1 stream-begin command-request new|have-data ' b"cbor:{b'name': b'command'}"), ffs(b'1 1 0 command-data continuation %s' % data.getvalue()), ffs(b'1 1 0 command-data eos ') ])
def testduplicaterequestaftersend(self): """We can use a duplicate request ID after we've sent the response.""" reactor = makereactor() instream = framing.stream(1) list(sendcommandframes(reactor, instream, 1, b'command1', {})) outstream = reactor.makeoutputstream() res = reactor.oncommandresponseready(outstream, 1, b'response') list(res[1][b'framegen']) results = list(sendcommandframes(reactor, instream, 1, b'command1', {})) self.assertaction(results[0], b'runcommand')
def testservererror(self): reactor = makereactor() instream = framing.stream(1) list(sendcommandframes(reactor, instream, 1, b'mycommand', {})) outstream = reactor.makeoutputstream() result = reactor.onservererror(outstream, 1, b'some message') self.assertaction(result, b'sendframes') self.assertframesequal(result[1][b'framegen'], [ b"1 2 stream-begin error-response 0 " b"cbor:{b'type': b'server', " b"b'message': [{b'msg': b'some message'}]}", ])
def testprotocolsettingsnoninitial(self): # Cannot have protocol settings frames as non-initial frames. reactor = makereactor() stream = framing.stream(1) results = list(sendcommandframes(reactor, stream, 1, b'mycommand', {})) self.assertEqual(len(results), 1) self.assertaction(results[0], b'runcommand') result = self._sendsingleframe( reactor, ffs(b'0 1 0 sender-protocol-settings eos ')) self.assertaction(result, b'error') self.assertEqual(result[1], { b'message': b'expected command request frame; got 8', })
def testargsanddata(self): data = util.bytesio(b'x' * 100) stream = framing.stream(1) frames = list(framing.createcommandframes(stream, 1, b'command', { b'key1': b'key1value', b'key2': b'key2value', b'key3': b'key3value', }, data)) self.assertEqual(frames, [ ffs(b'1 1 stream-begin command-request new|have-data ' b"cbor:{b'name': b'command', b'args': {b'key1': b'key1value', " b"b'key2': b'key2value', b'key3': b'key3value'}}"), ffs(b'1 1 0 command-data eos %s' % data.getvalue()), ])
def testsimplecommanddata(self): reactor = makereactor() stream = framing.stream(1) results = list( sendcommandframes(reactor, stream, 1, b'mycommand', {}, util.bytesio(b'data!'))) self.assertEqual(len(results), 2) self.assertaction(results[0], b'wantframe') self.assertaction(results[1], b'runcommand') self.assertEqual( results[1][1], { b'requestid': 1, b'command': b'mycommand', b'args': {}, b'data': b'data!', })
def testsimpleresponse(self): """Bytes response to command sends result frames.""" reactor = makereactor() instream = framing.stream(1) list(sendcommandframes(reactor, instream, 1, b'mycommand', {})) outstream = reactor.makeoutputstream() result = reactor.oncommandresponsereadyobjects(outstream, 1, [b'response']) self.assertaction(result, b'sendframes') self.assertframesequal(result[1][b'framegen'], [ b'1 2 stream-begin stream-settings eos cbor:b"identity"', b'1 2 encoded command-response continuation %s' % OK, b'1 2 encoded command-response continuation cbor:b"response"', b'1 2 0 command-response eos ', ])
def test1argument(self): reactor = makereactor() stream = framing.stream(1) results = list( sendcommandframes(reactor, stream, 41, b'mycommand', {b'foo': b'bar'})) self.assertEqual(len(results), 1) self.assertaction(results[0], b'runcommand') self.assertEqual( results[0][1], { b'requestid': 41, b'command': b'mycommand', b'args': { b'foo': b'bar' }, b'data': None, })
def testduplicaterequestonactivecommandnosend(self): """Same as above but we've registered a response but haven't sent it.""" reactor = makereactor() instream = framing.stream(1) list(sendcommandframes(reactor, instream, 1, b'command1', {})) outstream = reactor.makeoutputstream() reactor.oncommandresponsereadyobjects(outstream, 1, [b'response']) # We've registered the response but haven't sent it. From the # perspective of the reactor, the command is still active. results = list(sendcommandframes(reactor, instream, 1, b'command1', {})) self.assertaction(results[0], b'error') self.assertEqual(results[0][1], { b'message': b'request with ID 1 is already active', })
def testmultiframeresponse(self): """Bytes response spanning multiple frames is handled.""" first = b'x' * framing.DEFAULT_MAX_FRAME_SIZE second = b'y' * 100 reactor = makereactor() instream = framing.stream(1) list(sendcommandframes(reactor, instream, 1, b'mycommand', {})) outstream = reactor.makeoutputstream() result = reactor.oncommandresponseready(outstream, 1, first + second) self.assertaction(result, b'sendframes') self.assertframesequal(result[1][b'framegen'], [ b'1 2 stream-begin command-response continuation %s' % OK, b'1 2 0 command-response continuation %s' % first, b'1 2 0 command-response eos %s' % second, ])
def test1commanddeferresponse(self): """Responses when in deferred output mode are delayed until EOF.""" reactor = makereactor(deferoutput=True) instream = framing.stream(1) results = list( sendcommandframes(reactor, instream, 1, b'mycommand', {})) self.assertEqual(len(results), 1) self.assertaction(results[0], b'runcommand') outstream = reactor.makeoutputstream() result = reactor.oncommandresponseready(outstream, 1, b'response') self.assertaction(result, b'noop') result = reactor.oninputeof() self.assertaction(result, b'sendframes') self.assertframesequal(result[1][b'framegen'], [ b'1 2 stream-begin command-response eos %sresponse' % OK, ])
def test1framecommand(self): """Receiving a command in a single frame yields request to run it.""" reactor = makereactor() stream = framing.stream(1) results = list(sendcommandframes(reactor, stream, 1, b'mycommand', {})) self.assertEqual(len(results), 1) self.assertaction(results[0], b'runcommand') self.assertEqual( results[0][1], { b'requestid': 1, b'command': b'mycommand', b'args': {}, b'data': None, }) result = reactor.oninputeof() self.assertaction(result, b'noop')
def testmultiplecommanddeferresponse(self): reactor = makereactor(deferoutput=True) instream = framing.stream(1) list(sendcommandframes(reactor, instream, 1, b'command1', {})) list(sendcommandframes(reactor, instream, 3, b'command2', {})) outstream = reactor.makeoutputstream() result = reactor.oncommandresponseready(outstream, 1, b'response1') self.assertaction(result, b'noop') result = reactor.oncommandresponseready(outstream, 3, b'response2') self.assertaction(result, b'noop') result = reactor.oninputeof() self.assertaction(result, b'sendframes') self.assertframesequal(result[1][b'framegen'], [ b'1 2 stream-begin command-response eos %sresponse1' % OK, b'3 2 0 command-response eos %sresponse2' % OK, ])
def testrequestidtracking(self): reactor = makereactor(deferoutput=True) instream = framing.stream(1) list(sendcommandframes(reactor, instream, 1, b'command1', {})) list(sendcommandframes(reactor, instream, 3, b'command2', {})) list(sendcommandframes(reactor, instream, 5, b'command3', {})) # Register results for commands out of order. outstream = reactor.makeoutputstream() reactor.oncommandresponseready(outstream, 3, b'response3') reactor.oncommandresponseready(outstream, 1, b'response1') reactor.oncommandresponseready(outstream, 5, b'response5') result = reactor.oninputeof() self.assertaction(result, b'sendframes') self.assertframesequal(result[1][b'framegen'], [ b'3 2 stream-begin command-response eos %sresponse3' % OK, b'1 2 0 command-response eos %sresponse1' % OK, b'5 2 0 command-response eos %sresponse5' % OK, ])
def test1commanddeferresponse(self): """Responses when in deferred output mode are delayed until EOF.""" reactor = makereactor(deferoutput=True) instream = framing.stream(1) results = list( sendcommandframes(reactor, instream, 1, b'mycommand', {})) self.assertEqual(len(results), 1) self.assertaction(results[0], b'runcommand') outstream = reactor.makeoutputstream() result = reactor.oncommandresponsereadyobjects(outstream, 1, [b'response']) self.assertaction(result, b'noop') result = reactor.oninputeof() self.assertaction(result, b'sendframes') self.assertframesequal(result[1][b'framegen'], [ b'1 2 stream-begin stream-settings eos cbor:b"identity"', b'1 2 encoded command-response continuation %s' % OK, b'1 2 encoded command-response continuation cbor:b"response"', b'1 2 0 command-response eos ', ])
def testmultiplecommanddeferresponse(self): reactor = makereactor(deferoutput=True) instream = framing.stream(1) list(sendcommandframes(reactor, instream, 1, b'command1', {})) list(sendcommandframes(reactor, instream, 3, b'command2', {})) outstream = reactor.makeoutputstream() result = reactor.oncommandresponsereadyobjects(outstream, 1, [b'response1']) self.assertaction(result, b'noop') result = reactor.oncommandresponsereadyobjects(outstream, 3, [b'response2']) self.assertaction(result, b'noop') result = reactor.oninputeof() self.assertaction(result, b'sendframes') self.assertframesequal(result[1][b'framegen'], [ b'1 2 stream-begin stream-settings eos cbor:b"identity"', b'1 2 encoded command-response continuation %s' % OK, b'1 2 encoded command-response continuation cbor:b"response1"', b'1 2 0 command-response eos ', b'3 2 encoded command-response continuation %s' % OK, b'3 2 encoded command-response continuation cbor:b"response2"', b'3 2 0 command-response eos ', ])