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