| 1 |
import unittest |
| 2 |
|
| 3 |
from mercurial import ( |
| 4 |
ui as uimod, |
| 5 |
util, |
| 6 |
wireprotoframing as framing, |
| 7 |
) |
| 8 |
from mercurial.utils import cborutil |
| 9 |
|
| 10 |
ffs = framing.makeframefromhumanstring |
| 11 |
|
| 12 |
OK = b''.join(cborutil.streamencode({b'status': b'ok'})) |
| 13 |
|
| 14 |
|
| 15 |
def makereactor(deferoutput=False): |
| 16 |
ui = uimod.ui() |
| 17 |
return framing.serverreactor(ui, deferoutput=deferoutput) |
| 18 |
|
| 19 |
|
| 20 |
def sendframes(reactor, gen): |
| 21 |
"""Send a generator of frame bytearray to a reactor. |
| 22 |
|
| 23 |
Emits a generator of results from ``onframerecv()`` calls. |
| 24 |
""" |
| 25 |
for frame in gen: |
| 26 |
header = framing.parseheader(frame) |
| 27 |
payload = frame[framing.FRAME_HEADER_SIZE :] |
| 28 |
assert len(payload) == header.length |
| 29 |
|
| 30 |
yield reactor.onframerecv( |
| 31 |
framing.frame( |
| 32 |
header.requestid, |
| 33 |
header.streamid, |
| 34 |
header.streamflags, |
| 35 |
header.typeid, |
| 36 |
header.flags, |
| 37 |
payload, |
| 38 |
) |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
def sendcommandframes(reactor, stream, rid, cmd, args, datafh=None): |
| 43 |
"""Generate frames to run a command and send them to a reactor.""" |
| 44 |
return sendframes( |
| 45 |
reactor, framing.createcommandframes(stream, rid, cmd, args, datafh) |
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
class ServerReactorTests(unittest.TestCase): |
| 50 |
def _sendsingleframe(self, reactor, f): |
| 51 |
results = list(sendframes(reactor, [f])) |
| 52 |
self.assertEqual(len(results), 1) |
| 53 |
|
| 54 |
return results[0] |
| 55 |
|
| 56 |
def assertaction(self, res, expected): |
| 57 |
self.assertIsInstance(res, tuple) |
| 58 |
self.assertEqual(len(res), 2) |
| 59 |
self.assertIsInstance(res[1], dict) |
| 60 |
self.assertEqual(res[0], expected) |
| 61 |
|
| 62 |
def assertframesequal(self, frames, framestrings): |
| 63 |
expected = [ffs(s) for s in framestrings] |
| 64 |
self.assertEqual(list(frames), expected) |
| 65 |
|
| 66 |
def test1framecommand(self): |
| 67 |
"""Receiving a command in a single frame yields request to run it.""" |
| 68 |
reactor = makereactor() |
| 69 |
stream = framing.stream(1) |
| 70 |
results = list(sendcommandframes(reactor, stream, 1, b'mycommand', {})) |
| 71 |
self.assertEqual(len(results), 1) |
| 72 |
self.assertaction(results[0], b'runcommand') |
| 73 |
self.assertEqual( |
| 74 |
results[0][1], |
| 75 |
{ |
| 76 |
b'requestid': 1, |
| 77 |
b'command': b'mycommand', |
| 78 |
b'args': {}, |
| 79 |
b'redirect': None, |
| 80 |
b'data': None, |
| 81 |
}, |
| 82 |
) |
| 83 |
|
| 84 |
result = reactor.oninputeof() |
| 85 |
self.assertaction(result, b'noop') |
| 86 |
|
| 87 |
def test1argument(self): |
| 88 |
reactor = makereactor() |
| 89 |
stream = framing.stream(1) |
| 90 |
results = list( |
| 91 |
sendcommandframes( |
| 92 |
reactor, stream, 41, b'mycommand', {b'foo': b'bar'} |
| 93 |
) |
| 94 |
) |
| 95 |
self.assertEqual(len(results), 1) |
| 96 |
self.assertaction(results[0], b'runcommand') |
| 97 |
self.assertEqual( |
| 98 |
results[0][1], |
| 99 |
{ |
| 100 |
b'requestid': 41, |
| 101 |
b'command': b'mycommand', |
| 102 |
b'args': {b'foo': b'bar'}, |
| 103 |
b'redirect': None, |
| 104 |
b'data': None, |
| 105 |
}, |
| 106 |
) |
| 107 |
|
| 108 |
def testmultiarguments(self): |
| 109 |
reactor = makereactor() |
| 110 |
stream = framing.stream(1) |
| 111 |
results = list( |
| 112 |
sendcommandframes( |
| 113 |
reactor, |
| 114 |
stream, |
| 115 |
1, |
| 116 |
b'mycommand', |
| 117 |
{b'foo': b'bar', b'biz': b'baz'}, |
| 118 |
) |
| 119 |
) |
| 120 |
self.assertEqual(len(results), 1) |
| 121 |
self.assertaction(results[0], b'runcommand') |
| 122 |
self.assertEqual( |
| 123 |
results[0][1], |
| 124 |
{ |
| 125 |
b'requestid': 1, |
| 126 |
b'command': b'mycommand', |
| 127 |
b'args': {b'foo': b'bar', b'biz': b'baz'}, |
| 128 |
b'redirect': None, |
| 129 |
b'data': None, |
| 130 |
}, |
| 131 |
) |
| 132 |
|
| 133 |
def testsimplecommanddata(self): |
| 134 |
reactor = makereactor() |
| 135 |
stream = framing.stream(1) |
| 136 |
results = list( |
| 137 |
sendcommandframes( |
| 138 |
reactor, stream, 1, b'mycommand', {}, util.bytesio(b'data!') |
| 139 |
) |
| 140 |
) |
| 141 |
self.assertEqual(len(results), 2) |
| 142 |
self.assertaction(results[0], b'wantframe') |
| 143 |
self.assertaction(results[1], b'runcommand') |
| 144 |
self.assertEqual( |
| 145 |
results[1][1], |
| 146 |
{ |
| 147 |
b'requestid': 1, |
| 148 |
b'command': b'mycommand', |
| 149 |
b'args': {}, |
| 150 |
b'redirect': None, |
| 151 |
b'data': b'data!', |
| 152 |
}, |
| 153 |
) |
| 154 |
|
| 155 |
def testmultipledataframes(self): |
| 156 |
frames = [ |
| 157 |
ffs( |
| 158 |
b'1 1 stream-begin command-request new|have-data ' |
| 159 |
b"cbor:{b'name': b'mycommand'}" |
| 160 |
), |
| 161 |
ffs(b'1 1 0 command-data continuation data1'), |
| 162 |
ffs(b'1 1 0 command-data continuation data2'), |
| 163 |
ffs(b'1 1 0 command-data eos data3'), |
| 164 |
] |
| 165 |
|
| 166 |
reactor = makereactor() |
| 167 |
results = list(sendframes(reactor, frames)) |
| 168 |
self.assertEqual(len(results), 4) |
| 169 |
for i in range(3): |
| 170 |
self.assertaction(results[i], b'wantframe') |
| 171 |
self.assertaction(results[3], b'runcommand') |
| 172 |
self.assertEqual( |
| 173 |
results[3][1], |
| 174 |
{ |
| 175 |
b'requestid': 1, |
| 176 |
b'command': b'mycommand', |
| 177 |
b'args': {}, |
| 178 |
b'redirect': None, |
| 179 |
b'data': b'data1data2data3', |
| 180 |
}, |
| 181 |
) |
| 182 |
|
| 183 |
def testargumentanddata(self): |
| 184 |
frames = [ |
| 185 |
ffs( |
| 186 |
b'1 1 stream-begin command-request new|have-data ' |
| 187 |
b"cbor:{b'name': b'command', b'args': {b'key': b'val'," |
| 188 |
b"b'foo': b'bar'}}" |
| 189 |
), |
| 190 |
ffs(b'1 1 0 command-data continuation value1'), |
| 191 |
ffs(b'1 1 0 command-data eos value2'), |
| 192 |
] |
| 193 |
|
| 194 |
reactor = makereactor() |
| 195 |
results = list(sendframes(reactor, frames)) |
| 196 |
|
| 197 |
self.assertaction(results[-1], b'runcommand') |
| 198 |
self.assertEqual( |
| 199 |
results[-1][1], |
| 200 |
{ |
| 201 |
b'requestid': 1, |
| 202 |
b'command': b'command', |
| 203 |
b'args': { |
| 204 |
b'key': b'val', |
| 205 |
b'foo': b'bar', |
| 206 |
}, |
| 207 |
b'redirect': None, |
| 208 |
b'data': b'value1value2', |
| 209 |
}, |
| 210 |
) |
| 211 |
|
| 212 |
def testnewandcontinuation(self): |
| 213 |
result = self._sendsingleframe( |
| 214 |
makereactor(), |
| 215 |
ffs(b'1 1 stream-begin command-request new|continuation '), |
| 216 |
) |
| 217 |
self.assertaction(result, b'error') |
| 218 |
self.assertEqual( |
| 219 |
result[1], |
| 220 |
{ |
| 221 |
b'message': b'received command request frame with both new and ' |
| 222 |
b'continuation flags set', |
| 223 |
}, |
| 224 |
) |
| 225 |
|
| 226 |
def testneithernewnorcontinuation(self): |
| 227 |
result = self._sendsingleframe( |
| 228 |
makereactor(), ffs(b'1 1 stream-begin command-request 0 ') |
| 229 |
) |
| 230 |
self.assertaction(result, b'error') |
| 231 |
self.assertEqual( |
| 232 |
result[1], |
| 233 |
{ |
| 234 |
b'message': b'received command request frame with neither new nor ' |
| 235 |
b'continuation flags set', |
| 236 |
}, |
| 237 |
) |
| 238 |
|
| 239 |
def testunexpectedcommanddata(self): |
| 240 |
"""Command data frame when not running a command is an error.""" |
| 241 |
result = self._sendsingleframe( |
| 242 |
makereactor(), ffs(b'1 1 stream-begin command-data 0 ignored') |
| 243 |
) |
| 244 |
self.assertaction(result, b'error') |
| 245 |
self.assertEqual( |
| 246 |
result[1], |
| 247 |
{ |
| 248 |
b'message': b'expected sender protocol settings or command request ' |
| 249 |
b'frame; got 2', |
| 250 |
}, |
| 251 |
) |
| 252 |
|
| 253 |
def testunexpectedcommanddatareceiving(self): |
| 254 |
"""Same as above except the command is receiving.""" |
| 255 |
results = list( |
| 256 |
sendframes( |
| 257 |
makereactor(), |
| 258 |
[ |
| 259 |
ffs( |
| 260 |
b'1 1 stream-begin command-request new|more ' |
| 261 |
b"cbor:{b'name': b'ignored'}" |
| 262 |
), |
| 263 |
ffs(b'1 1 0 command-data eos ignored'), |
| 264 |
], |
| 265 |
) |
| 266 |
) |
| 267 |
|
| 268 |
self.assertaction(results[0], b'wantframe') |
| 269 |
self.assertaction(results[1], b'error') |
| 270 |
self.assertEqual( |
| 271 |
results[1][1], |
| 272 |
{ |
| 273 |
b'message': b'received command data frame for request that is not ' |
| 274 |
b'expecting data: 1', |
| 275 |
}, |
| 276 |
) |
| 277 |
|
| 278 |
def testconflictingrequestidallowed(self): |
| 279 |
"""Multiple fully serviced commands with same request ID is allowed.""" |
| 280 |
reactor = makereactor() |
| 281 |
results = [] |
| 282 |
outstream = reactor.makeoutputstream() |
| 283 |
results.append( |
| 284 |
self._sendsingleframe( |
| 285 |
reactor, |
| 286 |
ffs( |
| 287 |
b'1 1 stream-begin command-request new ' |
| 288 |
b"cbor:{b'name': b'command'}" |
| 289 |
), |
| 290 |
) |
| 291 |
) |
| 292 |
result = reactor.oncommandresponsereadyobjects( |
| 293 |
outstream, 1, [b'response1'] |
| 294 |
) |
| 295 |
self.assertaction(result, b'sendframes') |
| 296 |
list(result[1][b'framegen']) |
| 297 |
results.append( |
| 298 |
self._sendsingleframe( |
| 299 |
reactor, |
| 300 |
ffs( |
| 301 |
b'1 1 stream-begin command-request new ' |
| 302 |
b"cbor:{b'name': b'command'}" |
| 303 |
), |
| 304 |
) |
| 305 |
) |
| 306 |
result = reactor.oncommandresponsereadyobjects( |
| 307 |
outstream, 1, [b'response2'] |
| 308 |
) |
| 309 |
self.assertaction(result, b'sendframes') |
| 310 |
list(result[1][b'framegen']) |
| 311 |
results.append( |
| 312 |
self._sendsingleframe( |
| 313 |
reactor, |
| 314 |
ffs( |
| 315 |
b'1 1 stream-begin command-request new ' |
| 316 |
b"cbor:{b'name': b'command'}" |
| 317 |
), |
| 318 |
) |
| 319 |
) |
| 320 |
result = reactor.oncommandresponsereadyobjects( |
| 321 |
outstream, 1, [b'response3'] |
| 322 |
) |
| 323 |
self.assertaction(result, b'sendframes') |
| 324 |
list(result[1][b'framegen']) |
| 325 |
|
| 326 |
for i in range(3): |
| 327 |
self.assertaction(results[i], b'runcommand') |
| 328 |
self.assertEqual( |
| 329 |
results[i][1], |
| 330 |
{ |
| 331 |
b'requestid': 1, |
| 332 |
b'command': b'command', |
| 333 |
b'args': {}, |
| 334 |
b'redirect': None, |
| 335 |
b'data': None, |
| 336 |
}, |
| 337 |
) |
| 338 |
|
| 339 |
def testconflictingrequestid(self): |
| 340 |
"""Request ID for new command matching in-flight command is illegal.""" |
| 341 |
results = list( |
| 342 |
sendframes( |
| 343 |
makereactor(), |
| 344 |
[ |
| 345 |
ffs( |
| 346 |
b'1 1 stream-begin command-request new|more ' |
| 347 |
b"cbor:{b'name': b'command'}" |
| 348 |
), |
| 349 |
ffs( |
| 350 |
b'1 1 0 command-request new ' |
| 351 |
b"cbor:{b'name': b'command1'}" |
| 352 |
), |
| 353 |
], |
| 354 |
) |
| 355 |
) |
| 356 |
|
| 357 |
self.assertaction(results[0], b'wantframe') |
| 358 |
self.assertaction(results[1], b'error') |
| 359 |
self.assertEqual( |
| 360 |
results[1][1], |
| 361 |
{ |
| 362 |
b'message': b'request with ID 1 already received', |
| 363 |
}, |
| 364 |
) |
| 365 |
|
| 366 |
def testinterleavedcommands(self): |
| 367 |
cbor1 = b''.join( |
| 368 |
cborutil.streamencode( |
| 369 |
{ |
| 370 |
b'name': b'command1', |
| 371 |
b'args': { |
| 372 |
b'foo': b'bar', |
| 373 |
b'key1': b'val', |
| 374 |
}, |
| 375 |
} |
| 376 |
) |
| 377 |
) |
| 378 |
cbor3 = b''.join( |
| 379 |
cborutil.streamencode( |
| 380 |
{ |
| 381 |
b'name': b'command3', |
| 382 |
b'args': { |
| 383 |
b'biz': b'baz', |
| 384 |
b'key': b'val', |
| 385 |
}, |
| 386 |
} |
| 387 |
) |
| 388 |
) |
| 389 |
|
| 390 |
results = list( |
| 391 |
sendframes( |
| 392 |
makereactor(), |
| 393 |
[ |
| 394 |
ffs( |
| 395 |
b'1 1 stream-begin command-request new|more %s' |
| 396 |
% cbor1[0:6] |
| 397 |
), |
| 398 |
ffs(b'3 1 0 command-request new|more %s' % cbor3[0:10]), |
| 399 |
ffs( |
| 400 |
b'1 1 0 command-request continuation|more %s' |
| 401 |
% cbor1[6:9] |
| 402 |
), |
| 403 |
ffs( |
| 404 |
b'3 1 0 command-request continuation|more %s' |
| 405 |
% cbor3[10:13] |
| 406 |
), |
| 407 |
ffs(b'3 1 0 command-request continuation %s' % cbor3[13:]), |
| 408 |
ffs(b'1 1 0 command-request continuation %s' % cbor1[9:]), |
| 409 |
], |
| 410 |
) |
| 411 |
) |
| 412 |
|
| 413 |
self.assertEqual( |
| 414 |
[t[0] for t in results], |
| 415 |
[ |
| 416 |
b'wantframe', |
| 417 |
b'wantframe', |
| 418 |
b'wantframe', |
| 419 |
b'wantframe', |
| 420 |
b'runcommand', |
| 421 |
b'runcommand', |
| 422 |
], |
| 423 |
) |
| 424 |
|
| 425 |
self.assertEqual( |
| 426 |
results[4][1], |
| 427 |
{ |
| 428 |
b'requestid': 3, |
| 429 |
b'command': b'command3', |
| 430 |
b'args': {b'biz': b'baz', b'key': b'val'}, |
| 431 |
b'redirect': None, |
| 432 |
b'data': None, |
| 433 |
}, |
| 434 |
) |
| 435 |
self.assertEqual( |
| 436 |
results[5][1], |
| 437 |
{ |
| 438 |
b'requestid': 1, |
| 439 |
b'command': b'command1', |
| 440 |
b'args': {b'foo': b'bar', b'key1': b'val'}, |
| 441 |
b'redirect': None, |
| 442 |
b'data': None, |
| 443 |
}, |
| 444 |
) |
| 445 |
|
| 446 |
def testmissingcommanddataframe(self): |
| 447 |
# The reactor doesn't currently handle partially received commands. |
| 448 |
# So this test is failing to do anything with request 1. |
| 449 |
frames = [ |
| 450 |
ffs( |
| 451 |
b'1 1 stream-begin command-request new|have-data ' |
| 452 |
b"cbor:{b'name': b'command1'}" |
| 453 |
), |
| 454 |
ffs(b'3 1 0 command-request new ' b"cbor:{b'name': b'command2'}"), |
| 455 |
] |
| 456 |
results = list(sendframes(makereactor(), frames)) |
| 457 |
self.assertEqual(len(results), 2) |
| 458 |
self.assertaction(results[0], b'wantframe') |
| 459 |
self.assertaction(results[1], b'runcommand') |
| 460 |
|
| 461 |
def testmissingcommanddataframeflags(self): |
| 462 |
frames = [ |
| 463 |
ffs( |
| 464 |
b'1 1 stream-begin command-request new|have-data ' |
| 465 |
b"cbor:{b'name': b'command1'}" |
| 466 |
), |
| 467 |
ffs(b'1 1 0 command-data 0 data'), |
| 468 |
] |
| 469 |
results = list(sendframes(makereactor(), frames)) |
| 470 |
self.assertEqual(len(results), 2) |
| 471 |
self.assertaction(results[0], b'wantframe') |
| 472 |
self.assertaction(results[1], b'error') |
| 473 |
self.assertEqual( |
| 474 |
results[1][1], |
| 475 |
{ |
| 476 |
b'message': b'command data frame without flags', |
| 477 |
}, |
| 478 |
) |
| 479 |
|
| 480 |
def testframefornonreceivingrequest(self): |
| 481 |
"""Receiving a frame for a command that is not receiving is illegal.""" |
| 482 |
results = list( |
| 483 |
sendframes( |
| 484 |
makereactor(), |
| 485 |
[ |
| 486 |
ffs( |
| 487 |
b'1 1 stream-begin command-request new ' |
| 488 |
b"cbor:{b'name': b'command1'}" |
| 489 |
), |
| 490 |
ffs( |
| 491 |
b'3 1 0 command-request new|have-data ' |
| 492 |
b"cbor:{b'name': b'command3'}" |
| 493 |
), |
| 494 |
ffs(b'5 1 0 command-data eos ignored'), |
| 495 |
], |
| 496 |
) |
| 497 |
) |
| 498 |
self.assertaction(results[2], b'error') |
| 499 |
self.assertEqual( |
| 500 |
results[2][1], |
| 501 |
{ |
| 502 |
b'message': b'received frame for request that is not receiving: 5', |
| 503 |
}, |
| 504 |
) |
| 505 |
|
| 506 |
def testsimpleresponse(self): |
| 507 |
"""Bytes response to command sends result frames.""" |
| 508 |
reactor = makereactor() |
| 509 |
instream = framing.stream(1) |
| 510 |
list(sendcommandframes(reactor, instream, 1, b'mycommand', {})) |
| 511 |
|
| 512 |
outstream = reactor.makeoutputstream() |
| 513 |
result = reactor.oncommandresponsereadyobjects( |
| 514 |
outstream, 1, [b'response'] |
| 515 |
) |
| 516 |
self.assertaction(result, b'sendframes') |
| 517 |
self.assertframesequal( |
| 518 |
result[1][b'framegen'], |
| 519 |
[ |
| 520 |
b'1 2 stream-begin stream-settings eos cbor:b"identity"', |
| 521 |
b'1 2 encoded command-response continuation %s' % OK, |
| 522 |
b'1 2 encoded command-response continuation cbor:b"response"', |
| 523 |
b'1 2 0 command-response eos ', |
| 524 |
], |
| 525 |
) |
| 526 |
|
| 527 |
def testmultiframeresponse(self): |
| 528 |
"""Bytes response spanning multiple frames is handled.""" |
| 529 |
first = b'x' * framing.DEFAULT_MAX_FRAME_SIZE |
| 530 |
second = b'y' * 100 |
| 531 |
|
| 532 |
reactor = makereactor() |
| 533 |
instream = framing.stream(1) |
| 534 |
list(sendcommandframes(reactor, instream, 1, b'mycommand', {})) |
| 535 |
|
| 536 |
outstream = reactor.makeoutputstream() |
| 537 |
result = reactor.oncommandresponsereadyobjects( |
| 538 |
outstream, 1, [first + second] |
| 539 |
) |
| 540 |
self.assertaction(result, b'sendframes') |
| 541 |
self.assertframesequal( |
| 542 |
result[1][b'framegen'], |
| 543 |
[ |
| 544 |
b'1 2 stream-begin stream-settings eos cbor:b"identity"', |
| 545 |
b'1 2 encoded command-response continuation %s' % OK, |
| 546 |
b'1 2 encoded command-response continuation Y\x80d', |
| 547 |
b'1 2 encoded command-response continuation %s' % first, |
| 548 |
b'1 2 encoded command-response continuation %s' % second, |
| 549 |
b'1 2 0 command-response eos ', |
| 550 |
], |
| 551 |
) |
| 552 |
|
| 553 |
def testservererror(self): |
| 554 |
reactor = makereactor() |
| 555 |
instream = framing.stream(1) |
| 556 |
list(sendcommandframes(reactor, instream, 1, b'mycommand', {})) |
| 557 |
|
| 558 |
outstream = reactor.makeoutputstream() |
| 559 |
result = reactor.onservererror(outstream, 1, b'some message') |
| 560 |
self.assertaction(result, b'sendframes') |
| 561 |
self.assertframesequal( |
| 562 |
result[1][b'framegen'], |
| 563 |
[ |
| 564 |
b"1 2 stream-begin error-response 0 " |
| 565 |
b"cbor:{b'type': b'server', " |
| 566 |
b"b'message': [{b'msg': b'some message'}]}", |
| 567 |
], |
| 568 |
) |
| 569 |
|
| 570 |
def test1commanddeferresponse(self): |
| 571 |
"""Responses when in deferred output mode are delayed until EOF.""" |
| 572 |
reactor = makereactor(deferoutput=True) |
| 573 |
instream = framing.stream(1) |
| 574 |
results = list( |
| 575 |
sendcommandframes(reactor, instream, 1, b'mycommand', {}) |
| 576 |
) |
| 577 |
self.assertEqual(len(results), 1) |
| 578 |
self.assertaction(results[0], b'runcommand') |
| 579 |
|
| 580 |
outstream = reactor.makeoutputstream() |
| 581 |
result = reactor.oncommandresponsereadyobjects( |
| 582 |
outstream, 1, [b'response'] |
| 583 |
) |
| 584 |
self.assertaction(result, b'noop') |
| 585 |
result = reactor.oninputeof() |
| 586 |
self.assertaction(result, b'sendframes') |
| 587 |
self.assertframesequal( |
| 588 |
result[1][b'framegen'], |
| 589 |
[ |
| 590 |
b'1 2 stream-begin stream-settings eos cbor:b"identity"', |
| 591 |
b'1 2 encoded command-response continuation %s' % OK, |
| 592 |
b'1 2 encoded command-response continuation cbor:b"response"', |
| 593 |
b'1 2 0 command-response eos ', |
| 594 |
], |
| 595 |
) |
| 596 |
|
| 597 |
def testmultiplecommanddeferresponse(self): |
| 598 |
reactor = makereactor(deferoutput=True) |
| 599 |
instream = framing.stream(1) |
| 600 |
list(sendcommandframes(reactor, instream, 1, b'command1', {})) |
| 601 |
list(sendcommandframes(reactor, instream, 3, b'command2', {})) |
| 602 |
|
| 603 |
outstream = reactor.makeoutputstream() |
| 604 |
result = reactor.oncommandresponsereadyobjects( |
| 605 |
outstream, 1, [b'response1'] |
| 606 |
) |
| 607 |
self.assertaction(result, b'noop') |
| 608 |
result = reactor.oncommandresponsereadyobjects( |
| 609 |
outstream, 3, [b'response2'] |
| 610 |
) |
| 611 |
self.assertaction(result, b'noop') |
| 612 |
result = reactor.oninputeof() |
| 613 |
self.assertaction(result, b'sendframes') |
| 614 |
self.assertframesequal( |
| 615 |
result[1][b'framegen'], |
| 616 |
[ |
| 617 |
b'1 2 stream-begin stream-settings eos cbor:b"identity"', |
| 618 |
b'1 2 encoded command-response continuation %s' % OK, |
| 619 |
b'1 2 encoded command-response continuation cbor:b"response1"', |
| 620 |
b'1 2 0 command-response eos ', |
| 621 |
b'3 2 encoded command-response continuation %s' % OK, |
| 622 |
b'3 2 encoded command-response continuation cbor:b"response2"', |
| 623 |
b'3 2 0 command-response eos ', |
| 624 |
], |
| 625 |
) |
| 626 |
|
| 627 |
def testrequestidtracking(self): |
| 628 |
reactor = makereactor(deferoutput=True) |
| 629 |
instream = framing.stream(1) |
| 630 |
list(sendcommandframes(reactor, instream, 1, b'command1', {})) |
| 631 |
list(sendcommandframes(reactor, instream, 3, b'command2', {})) |
| 632 |
list(sendcommandframes(reactor, instream, 5, b'command3', {})) |
| 633 |
|
| 634 |
# Register results for commands out of order. |
| 635 |
outstream = reactor.makeoutputstream() |
| 636 |
reactor.oncommandresponsereadyobjects(outstream, 3, [b'response3']) |
| 637 |
reactor.oncommandresponsereadyobjects(outstream, 1, [b'response1']) |
| 638 |
reactor.oncommandresponsereadyobjects(outstream, 5, [b'response5']) |
| 639 |
|
| 640 |
result = reactor.oninputeof() |
| 641 |
self.assertaction(result, b'sendframes') |
| 642 |
self.assertframesequal( |
| 643 |
result[1][b'framegen'], |
| 644 |
[ |
| 645 |
b'3 2 stream-begin stream-settings eos cbor:b"identity"', |
| 646 |
b'3 2 encoded command-response continuation %s' % OK, |
| 647 |
b'3 2 encoded command-response continuation cbor:b"response3"', |
| 648 |
b'3 2 0 command-response eos ', |
| 649 |
b'1 2 encoded command-response continuation %s' % OK, |
| 650 |
b'1 2 encoded command-response continuation cbor:b"response1"', |
| 651 |
b'1 2 0 command-response eos ', |
| 652 |
b'5 2 encoded command-response continuation %s' % OK, |
| 653 |
b'5 2 encoded command-response continuation cbor:b"response5"', |
| 654 |
b'5 2 0 command-response eos ', |
| 655 |
], |
| 656 |
) |
| 657 |
|
| 658 |
def testduplicaterequestonactivecommand(self): |
| 659 |
"""Receiving a request ID that matches a request that isn't finished.""" |
| 660 |
reactor = makereactor() |
| 661 |
stream = framing.stream(1) |
| 662 |
list(sendcommandframes(reactor, stream, 1, b'command1', {})) |
| 663 |
results = list(sendcommandframes(reactor, stream, 1, b'command1', {})) |
| 664 |
|
| 665 |
self.assertaction(results[0], b'error') |
| 666 |
self.assertEqual( |
| 667 |
results[0][1], |
| 668 |
{ |
| 669 |
b'message': b'request with ID 1 is already active', |
| 670 |
}, |
| 671 |
) |
| 672 |
|
| 673 |
def testduplicaterequestonactivecommandnosend(self): |
| 674 |
"""Same as above but we've registered a response but haven't sent it.""" |
| 675 |
reactor = makereactor() |
| 676 |
instream = framing.stream(1) |
| 677 |
list(sendcommandframes(reactor, instream, 1, b'command1', {})) |
| 678 |
outstream = reactor.makeoutputstream() |
| 679 |
reactor.oncommandresponsereadyobjects(outstream, 1, [b'response']) |
| 680 |
|
| 681 |
# We've registered the response but haven't sent it. From the |
| 682 |
# perspective of the reactor, the command is still active. |
| 683 |
|
| 684 |
results = list(sendcommandframes(reactor, instream, 1, b'command1', {})) |
| 685 |
self.assertaction(results[0], b'error') |
| 686 |
self.assertEqual( |
| 687 |
results[0][1], |
| 688 |
{ |
| 689 |
b'message': b'request with ID 1 is already active', |
| 690 |
}, |
| 691 |
) |
| 692 |
|
| 693 |
def testduplicaterequestaftersend(self): |
| 694 |
"""We can use a duplicate request ID after we've sent the response.""" |
| 695 |
reactor = makereactor() |
| 696 |
instream = framing.stream(1) |
| 697 |
list(sendcommandframes(reactor, instream, 1, b'command1', {})) |
| 698 |
outstream = reactor.makeoutputstream() |
| 699 |
res = reactor.oncommandresponsereadyobjects(outstream, 1, [b'response']) |
| 700 |
list(res[1][b'framegen']) |
| 701 |
|
| 702 |
results = list(sendcommandframes(reactor, instream, 1, b'command1', {})) |
| 703 |
self.assertaction(results[0], b'runcommand') |
| 704 |
|
| 705 |
def testprotocolsettingsnoflags(self): |
| 706 |
result = self._sendsingleframe( |
| 707 |
makereactor(), ffs(b'0 1 stream-begin sender-protocol-settings 0 ') |
| 708 |
) |
| 709 |
self.assertaction(result, b'error') |
| 710 |
self.assertEqual( |
| 711 |
result[1], |
| 712 |
{ |
| 713 |
b'message': b'sender protocol settings frame must have ' |
| 714 |
b'continuation or end of stream flag set', |
| 715 |
}, |
| 716 |
) |
| 717 |
|
| 718 |
def testprotocolsettingsconflictflags(self): |
| 719 |
result = self._sendsingleframe( |
| 720 |
makereactor(), |
| 721 |
ffs(b'0 1 stream-begin sender-protocol-settings continuation|eos '), |
| 722 |
) |
| 723 |
self.assertaction(result, b'error') |
| 724 |
self.assertEqual( |
| 725 |
result[1], |
| 726 |
{ |
| 727 |
b'message': b'sender protocol settings frame cannot have both ' |
| 728 |
b'continuation and end of stream flags set', |
| 729 |
}, |
| 730 |
) |
| 731 |
|
| 732 |
def testprotocolsettingsemptypayload(self): |
| 733 |
result = self._sendsingleframe( |
| 734 |
makereactor(), |
| 735 |
ffs(b'0 1 stream-begin sender-protocol-settings eos '), |
| 736 |
) |
| 737 |
self.assertaction(result, b'error') |
| 738 |
self.assertEqual( |
| 739 |
result[1], |
| 740 |
{ |
| 741 |
b'message': b'sender protocol settings frame did not contain CBOR ' |
| 742 |
b'data', |
| 743 |
}, |
| 744 |
) |
| 745 |
|
| 746 |
def testprotocolsettingsmultipleobjects(self): |
| 747 |
result = self._sendsingleframe( |
| 748 |
makereactor(), |
| 749 |
ffs( |
| 750 |
b'0 1 stream-begin sender-protocol-settings eos ' |
| 751 |
b'\x46foobar\x43foo' |
| 752 |
), |
| 753 |
) |
| 754 |
self.assertaction(result, b'error') |
| 755 |
self.assertEqual( |
| 756 |
result[1], |
| 757 |
{ |
| 758 |
b'message': b'sender protocol settings frame contained multiple ' |
| 759 |
b'CBOR values', |
| 760 |
}, |
| 761 |
) |
| 762 |
|
| 763 |
def testprotocolsettingscontentencodings(self): |
| 764 |
reactor = makereactor() |
| 765 |
|
| 766 |
result = self._sendsingleframe( |
| 767 |
reactor, |
| 768 |
ffs( |
| 769 |
b'0 1 stream-begin sender-protocol-settings eos ' |
| 770 |
b'cbor:{b"contentencodings": [b"a", b"b"]}' |
| 771 |
), |
| 772 |
) |
| 773 |
self.assertaction(result, b'wantframe') |
| 774 |
|
| 775 |
self.assertEqual(reactor._state, b'idle') |
| 776 |
self.assertEqual( |
| 777 |
reactor._sendersettings[b'contentencodings'], [b'a', b'b'] |
| 778 |
) |
| 779 |
|
| 780 |
def testprotocolsettingsmultipleframes(self): |
| 781 |
reactor = makereactor() |
| 782 |
|
| 783 |
data = b''.join( |
| 784 |
cborutil.streamencode( |
| 785 |
{ |
| 786 |
b'contentencodings': [b'value1', b'value2'], |
| 787 |
} |
| 788 |
) |
| 789 |
) |
| 790 |
|
| 791 |
results = list( |
| 792 |
sendframes( |
| 793 |
reactor, |
| 794 |
[ |
| 795 |
ffs( |
| 796 |
b'0 1 stream-begin sender-protocol-settings continuation %s' |
| 797 |
% data[0:5] |
| 798 |
), |
| 799 |
ffs(b'0 1 0 sender-protocol-settings eos %s' % data[5:]), |
| 800 |
], |
| 801 |
) |
| 802 |
) |
| 803 |
|
| 804 |
self.assertEqual(len(results), 2) |
| 805 |
|
| 806 |
self.assertaction(results[0], b'wantframe') |
| 807 |
self.assertaction(results[1], b'wantframe') |
| 808 |
|
| 809 |
self.assertEqual(reactor._state, b'idle') |
| 810 |
self.assertEqual( |
| 811 |
reactor._sendersettings[b'contentencodings'], [b'value1', b'value2'] |
| 812 |
) |
| 813 |
|
| 814 |
def testprotocolsettingsbadcbor(self): |
| 815 |
result = self._sendsingleframe( |
| 816 |
makereactor(), |
| 817 |
ffs(b'0 1 stream-begin sender-protocol-settings eos badvalue'), |
| 818 |
) |
| 819 |
self.assertaction(result, b'error') |
| 820 |
|
| 821 |
def testprotocolsettingsnoninitial(self): |
| 822 |
# Cannot have protocol settings frames as non-initial frames. |
| 823 |
reactor = makereactor() |
| 824 |
|
| 825 |
stream = framing.stream(1) |
| 826 |
results = list(sendcommandframes(reactor, stream, 1, b'mycommand', {})) |
| 827 |
self.assertEqual(len(results), 1) |
| 828 |
self.assertaction(results[0], b'runcommand') |
| 829 |
|
| 830 |
result = self._sendsingleframe( |
| 831 |
reactor, ffs(b'0 1 0 sender-protocol-settings eos ') |
| 832 |
) |
| 833 |
self.assertaction(result, b'error') |
| 834 |
self.assertEqual( |
| 835 |
result[1], |
| 836 |
{ |
| 837 |
b'message': b'expected command request frame; got 8', |
| 838 |
}, |
| 839 |
) |
| 840 |
|
| 841 |
|
| 842 |
if __name__ == '__main__': |
| 843 |
import silenttestrunner |
| 844 |
|
| 845 |
silenttestrunner.main(__name__) |