Debian Codegraph @

python/tests/proton_tests/engine.py

raw
1 #
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
18 #
19
20 from __future__ import absolute_import
21
22 import os
23 import gc
24 from time import time, sleep
25 from proton import *
26 from proton.reactor import Container
27 from . import common
28 from .common import pump, Skipped
29
30
31 # older versions of gc do not provide the garbage list
32 if not hasattr(gc, "garbage"):
33 gc.garbage = []
34
35 # future test areas
36 # + different permutations of setup
37 # - creating deliveries and calling input/output before opening the session/link
38 # + shrinking output_size down to something small? should the engine buffer?
39 # + resuming
40 # - locally and remotely created deliveries with the same tag
41
42
43 OUTPUT_SIZE = 10 * 1024
44
45
46 class Test(common.Test):
47
48 def __init__(self, *args):
49 common.Test.__init__(self, *args)
50 self._wires = []
51
52 def connection(self):
53 c1 = Connection()
54 c2 = Connection()
55 t1 = Transport()
56 t1.bind(c1)
57 t2 = Transport()
58 t2.bind(c2)
59 self._wires.append((c1, t1, c2, t2))
60
61 mask1 = 0
62 mask2 = 0
63
64 for cat in ("TRACE_FRM", "TRACE_RAW"):
65 trc = os.environ.get("PN_%s" % cat)
66 if trc and trc.lower() in ("1", "2", "yes", "true"):
67 mask1 = mask1 | getattr(Transport, cat)
68 if trc == "2":
69 mask2 = mask2 | getattr(Transport, cat)
70 t1.trace(mask1)
71 t2.trace(mask2)
72
73 return c1, c2
74
75 def link(self, name, max_frame=None, idle_timeout=None):
76 c1, c2 = self.connection()
77 if max_frame:
78 c1.transport.max_frame_size = max_frame[0]
79 c2.transport.max_frame_size = max_frame[1]
80 if idle_timeout:
81 # idle_timeout in seconds expressed as float
82 c1.transport.idle_timeout = idle_timeout[0]
83 c2.transport.idle_timeout = idle_timeout[1]
84 c1.open()
85 c2.open()
86 ssn1 = c1.session()
87 ssn1.open()
88 self.pump()
89 ssn2 = c2.session_head(Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_ACTIVE)
90 ssn2.open()
91 self.pump()
92 snd = ssn1.sender(name)
93 rcv = ssn2.receiver(name)
94 return snd, rcv
95
96 def cleanup(self):
97 self._wires = []
98
99 def pump(self, buffer_size=OUTPUT_SIZE):
100 for c1, t1, c2, t2 in self._wires:
101 pump(t1, t2, buffer_size)
102
103
104 class ConnectionTest(Test):
105
106 def setUp(self):
107 gc.enable()
108 self.c1, self.c2 = self.connection()
109
110 def cleanup(self):
111 # release resources created by this class
112 super(ConnectionTest, self).cleanup()
113 self.c1 = None
114 self.c2 = None
115
116 def tearDown(self):
117 self.cleanup()
118 gc.collect()
119 assert not gc.garbage
120
121 def test_open_close(self):
122 assert self.c1.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
123 assert self.c2.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
124
125 self.c1.open()
126 self.pump()
127
128 assert self.c1.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_UNINIT
129 assert self.c2.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_ACTIVE
130
131 self.c2.open()
132 self.pump()
133
134 assert self.c1.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
135 assert self.c2.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
136
137 self.c1.close()
138 self.pump()
139
140 assert self.c1.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
141 assert self.c2.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED
142
143 self.c2.close()
144 self.pump()
145
146 assert self.c1.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
147 assert self.c2.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
148
149 def test_simultaneous_open_close(self):
150 assert self.c1.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
151 assert self.c2.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
152
153 self.c1.open()
154 self.c2.open()
155
156 self.pump()
157
158 assert self.c1.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
159 assert self.c2.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
160
161 self.c1.close()
162 self.c2.close()
163
164 self.pump()
165
166 assert self.c1.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
167 assert self.c2.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
168
169 def test_capabilities_array(self):
170 self.c1.offered_capabilities = Array(UNDESCRIBED, Data.SYMBOL,
171 symbol("O_one"),
172 symbol("O_two"),
173 symbol("O_three"))
174
175 self.c1.desired_capabilities = Array(UNDESCRIBED, Data.SYMBOL,
176 symbol("D_one"),
177 symbol("D_two"),
178 symbol("D_three"))
179 self.c1.open()
180
181 assert self.c2.remote_offered_capabilities is None
182 assert self.c2.remote_desired_capabilities is None
183
184 self.pump()
185
186 assert self.c2.remote_offered_capabilities == self.c1.offered_capabilities, \
187 (self.c2.remote_offered_capabilities, self.c1.offered_capabilities)
188 assert self.c2.remote_desired_capabilities == self.c1.desired_capabilities, \
189 (self.c2.remote_desired_capabilities, self.c1.desired_capabilities)
190
191 def test_capabilities_symbol_list(self):
192 self.c1.offered_capabilities = SymbolList(['O_one', 'O_two', symbol('O_three')])
193 self.c1.desired_capabilities = SymbolList([symbol('D_one'), 'D_two', 'D_three'])
194 self.c1.open()
195
196 assert self.c2.remote_offered_capabilities is None
197 assert self.c2.remote_desired_capabilities is None
198
199 self.pump()
200
201 assert self.c2.remote_offered_capabilities == self.c1.offered_capabilities, \
202 (self.c2.remote_offered_capabilities, self.c1.offered_capabilities)
203 assert self.c2.remote_desired_capabilities == self.c1.desired_capabilities, \
204 (self.c2.remote_desired_capabilities, self.c1.desired_capabilities)
205
206 def test_condition(self):
207 self.c1.open()
208 self.c2.open()
209 self.pump()
210 assert self.c1.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
211 assert self.c2.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
212
213 cond = Condition("blah:bleh", "this is a description", {symbol("foo"): "bar"})
214 self.c1.condition = cond
215 self.c1.close()
216
217 self.pump()
218
219 assert self.c1.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
220 assert self.c2.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED
221
222 rcond = self.c2.remote_condition
223 assert rcond == cond, (rcond, cond)
224
225 def test_properties(self, p1=PropertyDict(key=symbol("value")), p2=None):
226 self.c1.properties = p1
227 self.c2.properties = p2
228 self.c1.open()
229 self.c2.open()
230 self.pump()
231
232 assert self.c2.remote_properties == p1, (self.c2.remote_properties, p1)
233 assert self.c1.remote_properties == p2, (self.c1.remote_properties, p2)
234
235 # The proton implementation limits channel_max to 32767.
236 # If I set the application's limit lower than that, I should
237 # get my wish. If I set it higher -- not.
238 def test_channel_max_low(self, value=1234):
239 self.c1.transport.channel_max = value
240 self.c1.open()
241 self.pump()
242 assert self.c1.transport.channel_max == value, (self.c1.transport.channel_max, value)
243
244 def test_channel_max_high(self, value=65535):
245 self.c1.transport.channel_max = value
246 self.c1.open()
247 self.pump()
248 assert self.c1.transport.channel_max == 32767, (self.c1.transport.channel_max, value)
249
250 def test_channel_max_raise_and_lower(self):
251 upper_limit = 32767
252
253 # It's OK to lower the max below upper_limit.
254 self.c1.transport.channel_max = 12345
255 assert self.c1.transport.channel_max == 12345
256
257 # But it won't let us raise the limit above PN_IMPL_CHANNEL_MAX.
258 self.c1.transport.channel_max = 65535
259 assert self.c1.transport.channel_max == upper_limit
260
261 # send the OPEN frame
262 self.c1.open()
263 self.pump()
264
265 # Now it's too late to make any change, because
266 # we have already sent the OPEN frame.
267 try:
268 self.c1.transport.channel_max = 666
269 assert False, "expected session exception"
270 except:
271 pass
272
273 assert self.c1.transport.channel_max == upper_limit
274
275 # TODO: Currently failing test - PROTON-1759 - skip
276
277 def test_channel_max_limits_sessions(self):
278 raise Skipped('Test fails - PROTON-1759')
279 # This is an index -- so max number of channels should be 1.
280 self.c1.transport.channel_max = 0
281 self.c1.open()
282 self.c2.open()
283 ssn_0 = self.c2.session()
284 assert ssn_0 is not None
285 ssn_0.open()
286 self.pump()
287 try:
288 ssn_1 = self.c2.session()
289 ssn_1.open()
290 self.pump()
291 assert False, "expected session exception"
292 except SessionException:
293 pass
294
295 def test_cleanup(self):
296 self.c1.open()
297 self.c2.open()
298 self.pump()
299 assert self.c1.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
300 assert self.c2.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
301 t1 = self.c1.transport
302 t2 = self.c2.transport
303 c2 = self.c2
304 self.c1.close()
305 # release all references to C1, except that held by the transport
306 self.cleanup()
307 gc.collect()
308 # transport should flush last state from C1:
309 pump(t1, t2)
310 assert c2.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED
311
312 def test_user_config(self):
313 self.c1.user = "vindaloo"
314 self.c1.password = "secret"
315 self.c1.open()
316 self.pump()
317
318 self.c2.user = "leela"
319 self.c2.password = "trustno1"
320 self.c2.open()
321 self.pump()
322
323 assert self.c1.user == "vindaloo", self.c1.user
324 assert self.c1.password is None, self.c1.password
325 assert self.c2.user == "leela", self.c2.user
326 assert self.c2.password is None, self.c2.password
327
328
329 class SessionTest(Test):
330
331 def setUp(self):
332 gc.enable()
333 self.c1, self.c2 = self.connection()
334 self.ssn = self.c1.session()
335 self.c1.open()
336 self.c2.open()
337
338 def cleanup(self):
339 # release resources created by this class
340 super(SessionTest, self).cleanup()
341 self.c1 = None
342 self.c2 = None
343 self.ssn = None
344
345 def tearDown(self):
346 self.cleanup()
347 gc.collect()
348 assert not gc.garbage
349
350 def test_open_close(self):
351 assert self.ssn.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
352
353 self.ssn.open()
354
355 assert self.ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_UNINIT
356
357 self.pump()
358
359 assert self.ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_UNINIT
360
361 ssn = self.c2.session_head(Endpoint.REMOTE_ACTIVE | Endpoint.LOCAL_UNINIT)
362
363 assert ssn is not None
364 assert ssn.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_ACTIVE
365 assert self.ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_UNINIT
366
367 ssn.open()
368
369 assert ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
370 assert self.ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_UNINIT
371
372 self.pump()
373
374 assert ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
375 assert self.ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
376
377 ssn.close()
378
379 assert ssn.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
380 assert self.ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
381
382 self.pump()
383
384 assert ssn.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
385 assert self.ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED
386
387 self.ssn.close()
388
389 assert ssn.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
390 assert self.ssn.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
391
392 self.pump()
393
394 assert ssn.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
395 assert self.ssn.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
396
397 def test_simultaneous_close(self):
398 self.ssn.open()
399 self.pump()
400 ssn = self.c2.session_head(Endpoint.REMOTE_ACTIVE | Endpoint.LOCAL_UNINIT)
401 assert ssn is not None
402 ssn.open()
403 self.pump()
404
405 assert self.ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
406 assert ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
407
408 self.ssn.close()
409 ssn.close()
410
411 assert self.ssn.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
412 assert ssn.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
413
414 self.pump()
415
416 assert self.ssn.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
417 assert ssn.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
418
419 def test_closing_connection(self):
420 self.ssn.open()
421 self.pump()
422 self.c1.close()
423 self.pump()
424 self.ssn.close()
425 self.pump()
426
427 def test_condition(self):
428 self.ssn.open()
429 self.pump()
430 ssn = self.c2.session_head(Endpoint.REMOTE_ACTIVE | Endpoint.LOCAL_UNINIT)
431 assert ssn is not None
432 ssn.open()
433 self.pump()
434
435 assert self.ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
436 assert ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
437
438 cond = Condition("blah:bleh", "this is a description", {symbol("foo"): "bar"})
439 self.ssn.condition = cond
440 self.ssn.close()
441
442 self.pump()
443
444 assert self.ssn.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
445 assert ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED
446
447 rcond = ssn.remote_condition
448 assert rcond == cond, (rcond, cond)
449
450 def test_cleanup(self):
451 snd, rcv = self.link("test-link")
452 snd.open()
453 rcv.open()
454 self.pump()
455 snd_ssn = snd.session
456 rcv_ssn = rcv.session
457 assert rcv_ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
458 self.ssn = None
459 snd_ssn.close()
460 snd_ssn.free()
461 del snd_ssn
462 gc.collect()
463 self.pump()
464 assert rcv_ssn.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED
465
466 def test_reopen_on_same_session_without_free(self):
467 """
468 confirm that a link is correctly opened when attaching to a previously
469 closed link *that has not been freed yet* on the same session
470 """
471 self.ssn.open()
472 self.pump()
473
474 ssn2 = self.c2.session_head(Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_ACTIVE)
475 ssn2.open()
476 self.pump()
477 snd = self.ssn.sender("test-link")
478 rcv = ssn2.receiver("test-link")
479
480 assert snd.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
481 assert rcv.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
482
483 snd.open()
484 rcv.open()
485 self.pump()
486
487 assert snd.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
488 assert rcv.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
489
490 snd.close()
491 rcv.close()
492 self.pump()
493
494 assert snd.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
495 assert rcv.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
496
497 snd = self.ssn.sender("test-link")
498 rcv = ssn2.receiver("test-link")
499 assert snd.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
500 assert rcv.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
501
502 snd.open()
503 rcv.open()
504 self.pump()
505
506 assert snd.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
507 assert rcv.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
508
509 def test_set_get_outgoing_window(self):
510 assert self.ssn.outgoing_window == 2147483647
511
512 self.ssn.outgoing_window = 1024
513 assert self.ssn.outgoing_window == 1024
514
515
516 class LinkTest(Test):
517
518 def setUp(self):
519 gc.enable()
520 self.snd, self.rcv = self.link("test-link")
521
522 def cleanup(self):
523 # release resources created by this class
524 super(LinkTest, self).cleanup()
525 self.snd = None
526 self.rcv = None
527
528 def tearDown(self):
529 self.cleanup()
530 gc.collect()
531 assert not gc.garbage, gc.garbage
532
533 def test_open_close(self):
534 assert self.snd.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
535 assert self.rcv.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
536
537 self.snd.open()
538
539 assert self.snd.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_UNINIT
540 assert self.rcv.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
541
542 self.pump()
543
544 assert self.snd.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_UNINIT
545 assert self.rcv.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_ACTIVE
546
547 self.rcv.open()
548
549 assert self.snd.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_UNINIT
550 assert self.rcv.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
551
552 self.pump()
553
554 assert self.snd.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
555 assert self.rcv.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
556
557 self.snd.close()
558
559 assert self.snd.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
560 assert self.rcv.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
561
562 self.pump()
563
564 assert self.snd.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
565 assert self.rcv.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED
566
567 self.rcv.close()
568
569 assert self.snd.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
570 assert self.rcv.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
571
572 self.pump()
573
574 assert self.snd.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
575 assert self.rcv.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
576
577 def test_simultaneous_open_close(self):
578 assert self.snd.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
579 assert self.rcv.state == Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_UNINIT
580
581 self.snd.open()
582 self.rcv.open()
583
584 assert self.snd.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_UNINIT
585 assert self.rcv.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_UNINIT
586
587 self.pump()
588
589 assert self.snd.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
590 assert self.rcv.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
591
592 self.snd.close()
593 self.rcv.close()
594
595 assert self.snd.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
596 assert self.rcv.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
597
598 self.pump()
599
600 assert self.snd.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
601 assert self.rcv.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_CLOSED
602
603 def test_multiple(self):
604 rcv = self.snd.session.receiver("second-rcv")
605 assert rcv.name == "second-rcv"
606 self.snd.open()
607 rcv.open()
608 self.pump()
609 c2 = self.rcv.session.connection
610 l = c2.link_head(Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_ACTIVE)
611 while l:
612 l.open()
613 l = l.next(Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_ACTIVE)
614 self.pump()
615
616 assert self.snd
617 assert rcv
618 self.snd.close()
619 rcv.close()
620 ssn = rcv.session
621 conn = ssn.connection
622 ssn.close()
623 conn.close()
624 self.pump()
625
626 def test_closing_session(self):
627 self.snd.open()
628 self.rcv.open()
629 ssn1 = self.snd.session
630 self.pump()
631 ssn1.close()
632 self.pump()
633 self.snd.close()
634 self.pump()
635
636 def test_closing_connection(self):
637 self.snd.open()
638 self.rcv.open()
639 ssn1 = self.snd.session
640 c1 = ssn1.connection
641 self.pump()
642 c1.close()
643 self.pump()
644 self.snd.close()
645 self.pump()
646
647 def assertEqualTermini(self, t1, t2):
648 assert t1.type == t2.type, (t1.type, t2.type)
649 assert t1.address == t2.address, (t1.address, t2.address)
650 assert t1.durability == t2.durability, (t1.durability, t2.durability)
651 assert t1.expiry_policy == t2.expiry_policy, (t1.expiry_policy, t2.expiry_policy)
652 assert t1.timeout == t2.timeout, (t1.timeout, t2.timeout)
653 assert t1.dynamic == t2.dynamic, (t1.dynamic, t2.dynamic)
654 for attr in ["properties", "capabilities", "outcomes", "filter"]:
655 d1 = getattr(t1, attr)
656 d2 = getattr(t2, attr)
657 assert d1.format() == d2.format(), (attr, d1.format(), d2.format())
658
659 def _test_source_target(self, config_source, config_target):
660 if config_source is None:
661 self.snd.source.type = Terminus.UNSPECIFIED
662 else:
663 config_source(self.snd.source)
664 if config_target is None:
665 self.snd.target.type = Terminus.UNSPECIFIED
666 else:
667 config_target(self.snd.target)
668 self.snd.open()
669 self.pump()
670 self.assertEqualTermini(self.rcv.remote_source, self.snd.source)
671 self.assertEqualTermini(self.rcv.remote_target, self.snd.target)
672 self.rcv.target.copy(self.rcv.remote_target)
673 self.rcv.source.copy(self.rcv.remote_source)
674 self.rcv.open()
675 self.pump()
676 self.assertEqualTermini(self.snd.remote_target, self.snd.target)
677 self.assertEqualTermini(self.snd.remote_source, self.snd.source)
678
679 def test_source_target(self):
680 self._test_source_target(TerminusConfig(address="source"),
681 TerminusConfig(address="target"))
682
683 def test_source(self):
684 self._test_source_target(TerminusConfig(address="source"), None)
685
686 def test_target(self):
687 self._test_source_target(None, TerminusConfig(address="target"))
688
689 def test_coordinator(self):
690 caps = Array(UNDESCRIBED, Data.SYMBOL, symbol("amqp:local-transactions"))
691 self._test_source_target(None, TerminusConfig(type=Terminus.COORDINATOR,
692 capabilities=caps))
693
694 def test_source_target_full(self):
695 self._test_source_target(TerminusConfig(address="source",
696 timeout=3,
697 dist_mode=Terminus.DIST_MODE_MOVE,
698 filter=[("int", 1), ("symbol", "two"), ("string", "three")],
699 capabilities=["one", "two", "three"]),
700 TerminusConfig(address="source",
701 timeout=7,
702 capabilities=None))
703
704 def test_distribution_mode(self):
705 self._test_source_target(TerminusConfig(address="source",
706 dist_mode=Terminus.DIST_MODE_COPY),
707 TerminusConfig(address="target"))
708 assert self.rcv.remote_source.distribution_mode == Terminus.DIST_MODE_COPY
709 assert self.rcv.remote_target.distribution_mode == Terminus.DIST_MODE_UNSPECIFIED
710
711 def test_dynamic_link(self):
712 self._test_source_target(TerminusConfig(address=None, dynamic=True), None)
713 assert self.rcv.remote_source.dynamic
714 assert self.rcv.remote_source.address is None
715
716 def test_condition(self):
717 self.snd.open()
718 self.rcv.open()
719 self.pump()
720
721 assert self.snd.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
722 assert self.rcv.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
723
724 cond = Condition("blah:bleh", "this is a description", {symbol("foo"): "bar"})
725 self.snd.condition = cond
726 self.snd.close()
727
728 self.pump()
729
730 assert self.snd.state == Endpoint.LOCAL_CLOSED | Endpoint.REMOTE_ACTIVE
731 assert self.rcv.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED
732
733 rcond = self.rcv.remote_condition
734 assert rcond == cond, (rcond, cond)
735
736 def test_settle_mode(self):
737 self.snd.snd_settle_mode = Link.SND_UNSETTLED
738 assert self.snd.snd_settle_mode == Link.SND_UNSETTLED
739 self.rcv.rcv_settle_mode = Link.RCV_SECOND
740 assert self.rcv.rcv_settle_mode == Link.RCV_SECOND
741
742 assert self.snd.remote_rcv_settle_mode != Link.RCV_SECOND
743 assert self.rcv.remote_snd_settle_mode != Link.SND_UNSETTLED
744
745 self.snd.open()
746 self.rcv.open()
747 self.pump()
748
749 assert self.snd.remote_rcv_settle_mode == Link.RCV_SECOND
750 assert self.rcv.remote_snd_settle_mode == Link.SND_UNSETTLED
751
752 def test_max_message_size(self):
753 assert self.snd.max_message_size == 0
754 assert self.rcv.remote_max_message_size == 0
755 self.snd.max_message_size = 13579
756 self.snd.open()
757 self.rcv.open()
758 self.pump()
759 assert self.rcv.remote_max_message_size == 13579
760
761 def test_properties(self):
762 sender_props = {symbol('key1'): 'value1',
763 symbol('key2'): 'value2'}
764 self.snd.properties = sender_props
765 self.snd.open()
766 self.rcv.open()
767 self.pump()
768
769 assert self.rcv.remote_properties == sender_props, (self.rcv.remote_properties, sender_props)
770 assert self.snd.remote_properties is None, (self.snd.remote_properties, None)
771
772 def test_cleanup(self):
773 snd, rcv = self.link("test-link")
774 snd.open()
775 rcv.open()
776 self.pump()
777 assert rcv.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
778 snd.close()
779 snd.free()
780 del snd
781 gc.collect()
782 self.pump()
783 assert rcv.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED
784
785
786 class TerminusConfig:
787
788 def __init__(self, type=None, address=None, timeout=None, durability=None,
789 filter=None, capabilities=None, dynamic=False, dist_mode=None):
790 self.address = address
791 self.timeout = timeout
792 self.durability = durability
793 self.filter = filter
794 self.capabilities = capabilities
795 self.dynamic = dynamic
796 self.dist_mode = dist_mode
797 self.type = type
798
799 def __call__(self, terminus):
800 if self.type is not None:
801 terminus.type = self.type
802 if self.address is not None:
803 terminus.address = self.address
804 if self.timeout is not None:
805 terminus.timeout = self.timeout
806 if self.durability is not None:
807 terminus.durability = self.durability
808 if self.capabilities is not None:
809 terminus.capabilities.put_array(False, Data.SYMBOL)
810 terminus.capabilities.enter()
811 for c in self.capabilities:
812 terminus.capabilities.put_symbol(c)
813 if self.filter is not None:
814 terminus.filter.put_map()
815 terminus.filter.enter()
816 for (t, v) in self.filter:
817 setter = getattr(terminus.filter, "put_%s" % t)
818 setter(v)
819 if self.dynamic:
820 terminus.dynamic = True
821 if self.dist_mode is not None:
822 terminus.distribution_mode = self.dist_mode
823
824
825 class TransferTest(Test):
826
827 def setUp(self):
828 gc.enable()
829 self.snd, self.rcv = self.link("test-link")
830 self.c1 = self.snd.session.connection
831 self.c2 = self.rcv.session.connection
832 self.snd.open()
833 self.rcv.open()
834 self.pump()
835
836 def cleanup(self):
837 # release resources created by this class
838 super(TransferTest, self).cleanup()
839 self.c1 = None
840 self.c2 = None
841 self.snd = None
842 self.rcv = None
843
844 def tearDown(self):
845 self.cleanup()
846 gc.collect()
847 assert not gc.garbage
848
849 def test_work_queue(self):
850 assert self.c1.work_head is None
851 self.snd.delivery("tag")
852 assert self.c1.work_head is None
853 self.rcv.flow(1)
854 self.pump()
855 d = self.c1.work_head
856 assert d is not None
857 tag = d.tag
858 assert tag == "tag", tag
859 assert d.writable
860
861 n = self.snd.send(b"this is a test")
862 assert self.snd.advance()
863 assert self.c1.work_head is None
864
865 self.pump()
866
867 d = self.c2.work_head
868 assert d.tag == "tag"
869 assert d.readable
870
871 def test_multiframe(self):
872 self.rcv.flow(1)
873 self.snd.delivery("tag")
874 msg = b"this is a test"
875 n = self.snd.send(msg)
876 assert n == len(msg)
877
878 self.pump()
879
880 d = self.rcv.current
881 assert d
882 assert d.tag == "tag", repr(d.tag)
883 assert d.readable
884
885 binary = self.rcv.recv(1024)
886 assert binary == msg, (binary, msg)
887
888 binary = self.rcv.recv(1024)
889 assert binary == b""
890
891 msg = b"this is more"
892 n = self.snd.send(msg)
893 assert n == len(msg)
894 assert self.snd.advance()
895
896 self.pump()
897
898 binary = self.rcv.recv(1024)
899 assert binary == msg, (binary, msg)
900
901 binary = self.rcv.recv(1024)
902 assert binary is None
903
904 def test_multiframe_abort(self):
905 self.rcv.flow(1)
906 sd = self.snd.delivery("tag")
907 msg = b"this is a test"
908 n = self.snd.send(msg)
909 assert n == len(msg)
910
911 self.pump()
912
913 binary = self.rcv.recv(1024)
914 assert binary == msg, (binary, msg)
915
916 msg = b"this is more. Error if not discarded."
917 n = self.snd.send(msg)
918 assert n == len(msg)
919 sd.abort()
920 assert sd.aborted
921
922 # Confirm abort discards the sender's buffered content, i.e. no data in generated transfer frame.
923 # We want:
924 # @transfer(20) [handle=0, delivery-id=0, delivery-tag=b"tag", message-format=0, settled=true, aborted=true]
925 # wanted = b"\x00\x00\x00%\x02\x00\x00\x00\x00S\x14\xd0\x00\x00\x00\x15\x00\x00\x00\nR\x00R\x00\xa0\x03tagR\x00A@@@@A"
926 # wanted = b"\x00\x00\x00\x26\x02\x00\x00\x00\x00S\x14\xd0\x00\x00\x00\x16\x00\x00\x00\x0bR\x00R\x00\xa0\x03tagR\x00A@@@@A@"
927 # wanted = b'\x00\x00\x00\x20\x02\x00\x00\x00\x00S\x14\xc0\x13\x0bR\x00R\x00\xa0\x03tagR\x00A@@@@A@'
928 # wanted = b'\x00\x00\x00"\x02\x00\x00\x00\x00S\x14\xd0\x00\x00\x00\x12\x00\x00\x00\nCC\xa0\x03tagCA@@@@A'
929 # wanted = b'\x00\x00\x00\x1d\x02\x00\x00\x00\x00S\x14\xc0\x10\x0bCC\xa0\x03tagCA@@@@A@'
930 wanted = b'\x00\x00\x00\x1c\x02\x00\x00\x00\x00S\x14\xc0\x0f\x0aCC\xa0\x03tagCA@@@@A'
931 t = self.snd.transport
932 wire_bytes = t.peek(1024)
933 assert wanted == wire_bytes, wire_bytes
934
935 self.pump()
936 assert self.rcv.current.aborted
937
938 def test_multiframe_last_empty(self):
939 self.rcv.flow(1)
940 sd = self.snd.delivery("tag")
941 msg_p1 = b"this is a test"
942 n = self.snd.send(msg_p1)
943 assert n == len(msg_p1)
944
945 self.pump()
946
947 assert len(msg_p1) == self.rcv.current.pending
948 assert self.rcv.current.partial
949 msg_p2 = b"this is more."
950 n = self.snd.send(msg_p2)
951 assert n == len(msg_p2)
952
953 self.pump()
954
955 msg = msg_p1 + msg_p2
956 assert len(msg) == self.rcv.current.pending
957 assert self.rcv.current.partial
958 # Advance. Should send empty xfer frame with more flag false.
959 assert self.snd.advance()
960
961 self.pump()
962
963 assert len(msg) == self.rcv.current.pending
964 assert not self.rcv.current.partial
965 binary = self.rcv.recv(self.rcv.current.pending)
966 assert binary == msg
967
968 def test_disposition(self):
969 self.rcv.flow(1)
970
971 self.pump()
972
973 sd = self.snd.delivery("tag")
974 msg = b"this is a test"
975 n = self.snd.send(msg)
976 assert n == len(msg)
977 assert self.snd.advance()
978
979 self.pump()
980
981 rd = self.rcv.current
982 assert rd is not None
983 assert rd.tag == sd.tag
984 rmsg = self.rcv.recv(1024)
985 assert rmsg == msg
986 rd.update(Delivery.ACCEPTED)
987
988 self.pump()
989
990 rdisp = sd.remote_state
991 ldisp = rd.local_state
992 assert rdisp == ldisp == Delivery.ACCEPTED, (rdisp, ldisp)
993 assert sd.updated
994
995 sd.update(Delivery.ACCEPTED)
996
997 self.pump()
998
999 assert sd.local_state == rd.remote_state == Delivery.ACCEPTED
1000 sd.settle()
1001
1002 def test_delivery_id_ordering(self):
1003 self.rcv.flow(1024)
1004 self.pump(buffer_size=64 * 1024)
1005
1006 # fill up delivery buffer on sender
1007 for m in range(1024):
1008 sd = self.snd.delivery("tag%s" % m)
1009 msg = ("message %s" % m).encode('ascii')
1010 n = self.snd.send(msg)
1011 assert n == len(msg)
1012 assert self.snd.advance()
1013
1014 self.pump(buffer_size=64 * 1024)
1015
1016 # receive a session-windows worth of messages and accept them
1017 for m in range(1024):
1018 rd = self.rcv.current
1019 assert rd is not None, m
1020 assert rd.tag == ("tag%s" % m), (rd.tag, m)
1021 msg = self.rcv.recv(1024)
1022 assert msg == ("message %s" % m).encode('ascii'), (msg, m)
1023 rd.update(Delivery.ACCEPTED)
1024 rd.settle()
1025
1026 self.pump(buffer_size=64 * 1024)
1027
1028 # add some new deliveries
1029 for m in range(1024, 1450):
1030 sd = self.snd.delivery("tag%s" % m)
1031 msg = ("message %s" % m).encode('ascii')
1032 n = self.snd.send(msg)
1033 assert n == len(msg)
1034 assert self.snd.advance()
1035
1036 # handle all disposition changes to sent messages
1037 d = self.c1.work_head
1038 while d:
1039 next_d = d.work_next
1040 if d.updated:
1041 d.update(Delivery.ACCEPTED)
1042 d.settle()
1043 d = next_d
1044
1045 # submit some more deliveries
1046 for m in range(1450, 1500):
1047 sd = self.snd.delivery("tag%s" % m)
1048 msg = ("message %s" % m).encode('ascii')
1049 n = self.snd.send(msg)
1050 assert n == len(msg)
1051 assert self.snd.advance()
1052
1053 self.pump(buffer_size=64 * 1024)
1054 self.rcv.flow(1024)
1055 self.pump(buffer_size=64 * 1024)
1056
1057 # verify remaining messages can be received and accepted
1058 for m in range(1024, 1500):
1059 rd = self.rcv.current
1060 assert rd is not None, m
1061 assert rd.tag == ("tag%s" % m), (rd.tag, m)
1062 msg = self.rcv.recv(1024)
1063 assert msg == ("message %s" % m).encode('ascii'), (msg, m)
1064 rd.update(Delivery.ACCEPTED)
1065 rd.settle()
1066
1067 def test_cleanup(self):
1068 self.rcv.flow(10)
1069 self.pump()
1070
1071 for x in range(10):
1072 self.snd.delivery("tag%d" % x)
1073 msg = b"this is a test"
1074 n = self.snd.send(msg)
1075 assert n == len(msg)
1076 assert self.snd.advance()
1077 self.snd.close()
1078 self.snd.free()
1079 self.snd = None
1080 gc.collect()
1081
1082 self.pump()
1083
1084 for x in range(10):
1085 rd = self.rcv.current
1086 assert rd is not None
1087 assert rd.tag == "tag%d" % x
1088 rmsg = self.rcv.recv(1024)
1089 assert self.rcv.advance()
1090 assert rmsg == msg
1091 # close of snd should've settled:
1092 assert rd.settled
1093 rd.settle()
1094
1095
1096 class MaxFrameTransferTest(Test):
1097
1098 def setUp(self):
1099 pass
1100
1101 def cleanup(self):
1102 # release resources created by this class
1103 super(MaxFrameTransferTest, self).cleanup()
1104 self.c1 = None
1105 self.c2 = None
1106 self.snd = None
1107 self.rcv = None
1108
1109 def tearDown(self):
1110 self.cleanup()
1111
1112 def message(self, size):
1113 parts = []
1114 for i in range(size):
1115 parts.append(str(i))
1116 return "/".join(parts)[:size].encode("utf-8")
1117
1118 def testMinFrame(self):
1119 """
1120 Configure receiver to support minimum max-frame as defined by AMQP-1.0.
1121 Verify transfer of messages larger than 512.
1122 """
1123 self.snd, self.rcv = self.link("test-link", max_frame=[0, 512])
1124 self.c1 = self.snd.session.connection
1125 self.c2 = self.rcv.session.connection
1126 self.snd.open()
1127 self.rcv.open()
1128 self.pump()
1129 assert self.rcv.session.connection.transport.max_frame_size == 512
1130 assert self.snd.session.connection.transport.remote_max_frame_size == 512
1131
1132 self.rcv.flow(1)
1133 self.snd.delivery("tag")
1134 msg = self.message(513)
1135 n = self.snd.send(msg)
1136 assert n == len(msg)
1137 assert self.snd.advance()
1138
1139 self.pump()
1140
1141 binary = self.rcv.recv(513)
1142 assert binary == msg
1143
1144 binary = self.rcv.recv(1024)
1145 assert binary is None
1146
1147 def testOddFrame(self):
1148 """
1149 Test an odd sized max limit with data that will require multiple frames to
1150 be transfered.
1151 """
1152 self.snd, self.rcv = self.link("test-link", max_frame=[0, 521])
1153 self.c1 = self.snd.session.connection
1154 self.c2 = self.rcv.session.connection
1155 self.snd.open()
1156 self.rcv.open()
1157 self.pump()
1158 assert self.rcv.session.connection.transport.max_frame_size == 521
1159 assert self.snd.session.connection.transport.remote_max_frame_size == 521
1160
1161 self.rcv.flow(2)
1162 self.snd.delivery("tag")
1163 msg = ("X" * 1699).encode('utf-8')
1164 n = self.snd.send(msg)
1165 assert n == len(msg)
1166 assert self.snd.advance()
1167
1168 self.pump()
1169
1170 binary = self.rcv.recv(1699)
1171 assert binary == msg
1172
1173 binary = self.rcv.recv(1024)
1174 assert binary is None
1175
1176 self.rcv.advance()
1177
1178 self.snd.delivery("gat")
1179 msg = self.message(1426)
1180 n = self.snd.send(msg)
1181 assert n == len(msg)
1182 assert self.snd.advance()
1183
1184 self.pump()
1185
1186 binary = self.rcv.recv(1426)
1187 assert binary == msg
1188
1189 self.pump()
1190
1191 binary = self.rcv.recv(1024)
1192 assert binary is None
1193
1194 def testSendQueuedMultiFrameMessages(self, sendSingleFrameMsg=False):
1195 """
1196 Test that multiple queued messages on the same link
1197 with multi-frame content are sent correctly. Use an
1198 odd max frame size, send enough data to use many.
1199 """
1200 self.snd, self.rcv = self.link("test-link", max_frame=[0, 517])
1201 self.c1 = self.snd.session.connection
1202 self.c2 = self.rcv.session.connection
1203 self.snd.open()
1204 self.rcv.open()
1205 self.pump()
1206 assert self.rcv.session.connection.transport.max_frame_size == 517
1207 assert self.snd.session.connection.transport.remote_max_frame_size == 517
1208
1209 self.rcv.flow(5)
1210
1211 self.pump()
1212
1213 # Send a delivery with 5 frames, all bytes as X1234
1214 self.snd.delivery("tag")
1215 msg = ("X1234" * 425).encode('utf-8')
1216 assert 2125 == len(msg)
1217 n = self.snd.send(msg)
1218 assert n == len(msg)
1219 assert self.snd.advance()
1220
1221 # Send a delivery with 5 frames, all bytes as Y5678
1222 self.snd.delivery("tag2")
1223 msg2 = ("Y5678" * 425).encode('utf-8')
1224 assert 2125 == len(msg2)
1225 n = self.snd.send(msg2)
1226 assert n == len(msg2)
1227 assert self.snd.advance()
1228
1229 self.pump()
1230
1231 if sendSingleFrameMsg:
1232 # Send a delivery with 1 frame
1233 self.snd.delivery("tag3")
1234 msg3 = ("Z").encode('utf-8')
1235 assert 1 == len(msg3)
1236 n = self.snd.send(msg3)
1237 assert n == len(msg3)
1238 assert self.snd.advance()
1239 self.pump()
1240
1241 binary = self.rcv.recv(5000)
1242 self.assertEqual(binary, msg)
1243
1244 self.rcv.advance()
1245
1246 binary2 = self.rcv.recv(5000)
1247 self.assertEqual(binary2, msg2)
1248
1249 self.rcv.advance()
1250
1251 if sendSingleFrameMsg:
1252 binary3 = self.rcv.recv(5000)
1253 self.assertEqual(binary3, msg3)
1254 self.rcv.advance()
1255
1256 self.pump()
1257
1258 def testSendQueuedMultiFrameMessagesThenSingleFrameMessage(self):
1259 self.testSendQueuedMultiFrameMessages(sendSingleFrameMsg=True)
1260
1261 def testBigMessage(self):
1262 """
1263 Test transferring a big message.
1264 """
1265 self.snd, self.rcv = self.link("test-link")
1266 self.c1 = self.snd.session.connection
1267 self.c2 = self.rcv.session.connection
1268 self.snd.open()
1269 self.rcv.open()
1270 self.pump()
1271
1272 self.rcv.flow(2)
1273 self.snd.delivery("tag")
1274 msg = self.message(1024 * 256)
1275 n = self.snd.send(msg)
1276 assert n == len(msg)
1277 assert self.snd.advance()
1278
1279 self.pump()
1280
1281 binary = self.rcv.recv(1024 * 256)
1282 assert binary == msg
1283
1284 binary = self.rcv.recv(1024)
1285 assert binary is None
1286
1287 def testMaxFrameAbort(self):
1288 self.snd, self.rcv = self.link("test-link", max_frame=[0, 512])
1289 self.c1 = self.snd.session.connection
1290 self.c2 = self.rcv.session.connection
1291 self.snd.open()
1292 self.rcv.open()
1293 self.rcv.flow(1)
1294 sndt = self.snd.transport
1295 dblfrmbytes = ("0123456789" * 52).encode('utf-8')
1296 assert 520 == len(dblfrmbytes)
1297 self.pump()
1298
1299 # Part 1: abort a delivery that would generate two frames - before they are generated.
1300 # Expect that no output is generated and credit is unaffected.
1301 assert self.snd.credit == 1
1302 sd = self.snd.delivery("tag_0")
1303 n = self.snd.send(dblfrmbytes)
1304 assert n == len(dblfrmbytes)
1305 sd.abort()
1306 generated = sndt.peek(2048)
1307 assert generated == b""
1308 assert self.snd.credit == 1
1309
1310 # Part 2: abort a streaming delivery whose last content would generate two frames.
1311 # First send some un-aborted data across the wire.
1312 sd = self.snd.delivery("tag_1")
1313 n = self.snd.send(dblfrmbytes)
1314 assert n == len(dblfrmbytes)
1315 self.pump()
1316 binary = self.rcv.recv(2048)
1317 assert binary == dblfrmbytes, (binary, dblfrmbytes)
1318 # Now send more data spanning two frames and immediately abort.
1319 # Unlike part 1, an abort frame is required to sync with peer.
1320 n = self.snd.send(dblfrmbytes)
1321 assert n == len(dblfrmbytes)
1322 sd.abort()
1323 assert sd.aborted
1324 # Expect a single abort transfer frame with no content. One credit is consumed.
1325 # @transfer(20) [handle=0, delivery-id=0, delivery-tag=b"tag_1", message-format=0, settled=true, aborted=true]
1326 # wanted = b"\x00\x00\x00\x27\x02\x00\x00\x00\x00S\x14\xd0\x00\x00\x00\x17\x00\x00\x00\nR\x00R\x00\xa0\x05tag_1R\x00A@@@@A"
1327 # wanted = b"\x00\x00\x00\x28\x02\x00\x00\x00\x00S\x14\xd0\x00\x00\x00\x18\x00\x00\x00\x0bR\x00R\x00\xa0\x05tag_1R\x00A@@@@A@"
1328 # wanted = b'\x00\x00\x00\x22\x02\x00\x00\x00\x00S\x14\xc0\x15\x0bR\x00R\x00\xa0\x05tag_1R\x00A@@@@A@'
1329 # wanted = b'\x00\x00\x00\x24\x02\x00\x00\x00\x00S\x14\xd0\x00\x00\x00\x14\x00\x00\x00\nCC\xa0\x05tag_1CA@@@@A'
1330 # wanted = b'\x00\x00\x00\x1f\x02\x00\x00\x00\x00S\x14\xc0\x12\x0bCC\xa0\x05tag_1CA@@@@A@'
1331 wanted = b'\x00\x00\x00\x1e\x02\x00\x00\x00\x00S\x14\xc0\x11\x0aCC\xa0\x05tag_1CA@@@@A'
1332 t = self.snd.transport
1333 wire_bytes = t.peek(2048)
1334 assert wanted == wire_bytes, wire_bytes
1335 assert self.snd.credit == 0
1336 self.pump()
1337 assert self.rcv.current.aborted
1338 # Confirm no lingering transfers by closing the link.
1339 self.snd.close()
1340 # Expect just the detach frame.
1341 # @detach(22) [handle=0, closed=true]
1342 # wanted = b"\x00\x00\x00\x17\x02\x00\x00\x00\x00S\x16\xd0\x00\x00\x00\x07\x00\x00\x00\x02R\x00A"
1343 # wanted = b"\x00\x00\x00\x18\x02\x00\x00\x00\x00S\x16\xd0\x00\x00\x00\x08\x00\x00\x00\x03R\x00A@"
1344 # wanted = b'\x00\x00\x00\x12\x02\x00\x00\x00\x00S\x16\xc0\x05\x03R\x00A@'
1345 # wanted = b"\x00\x00\x00\x16\x02\x00\x00\x00\x00S\x16\xd0\x00\x00\x00\x06\x00\x00\x00\x02CA"
1346 # wanted = b'\x00\x00\x00\x11\x02\x00\x00\x00\x00S\x16\xc0\x04\x03CA@'
1347 wanted = b'\x00\x00\x00\x10\x02\x00\x00\x00\x00S\x16\xc0\x03\x02CA'
1348 wire_bytes = t.peek(2048)
1349 assert wanted == wire_bytes, wire_bytes
1350
1351
1352 class IdleTimeoutTest(Test):
1353
1354 def setUp(self):
1355 pass
1356
1357 def cleanup(self):
1358 # release resources created by this class
1359 super(IdleTimeoutTest, self).cleanup()
1360 self.snd = None
1361 self.rcv = None
1362 self.c1 = None
1363 self.c2 = None
1364
1365 def tearDown(self):
1366 self.cleanup()
1367
1368 def message(self, size):
1369 parts = []
1370 for i in range(size):
1371 parts.append(str(i))
1372 return "/".join(parts)[:size]
1373
1374 def testGetSet(self):
1375 """
1376 Verify the configuration and negotiation of the idle timeout.
1377 """
1378
1379 self.snd, self.rcv = self.link("test-link", idle_timeout=[1.0, 2.0])
1380 self.c1 = self.snd.session.connection
1381 self.c2 = self.rcv.session.connection
1382 self.snd.open()
1383 self.rcv.open()
1384 self.pump()
1385 # proton advertises 1/2 the configured timeout to the peer:
1386 assert self.rcv.session.connection.transport.idle_timeout == 2.0
1387 assert self.rcv.session.connection.transport.remote_idle_timeout == 0.5
1388 assert self.snd.session.connection.transport.idle_timeout == 1.0
1389 assert self.snd.session.connection.transport.remote_idle_timeout == 1.0
1390
1391 def testTimeout(self):
1392 """
1393 Verify the AMQP Connection idle timeout.
1394 """
1395
1396 # snd will timeout the Connection if no frame is received within 1000 ticks
1397 self.snd, self.rcv = self.link("test-link", idle_timeout=[1.0, 0])
1398 self.c1 = self.snd.session.connection
1399 self.c2 = self.rcv.session.connection
1400 self.snd.open()
1401 self.rcv.open()
1402 self.pump()
1403
1404 t_snd = self.snd.session.connection.transport
1405 t_rcv = self.rcv.session.connection.transport
1406 assert t_rcv.idle_timeout == 0.0
1407 # proton advertises 1/2 the timeout (see spec)
1408 assert t_rcv.remote_idle_timeout == 0.5
1409 assert t_snd.idle_timeout == 1.0
1410 assert t_snd.remote_idle_timeout == 0.0
1411
1412 sndr_frames_in = t_snd.frames_input
1413 rcvr_frames_out = t_rcv.frames_output
1414
1415 # at t+1msec, nothing should happen:
1416 clock = 0.001
1417 assert t_snd.tick(clock) == 1.001, "deadline for remote timeout"
1418 assert t_rcv.tick(clock) == 0.251, "deadline to send keepalive"
1419 self.pump()
1420 assert sndr_frames_in == t_snd.frames_input, "unexpected received frame"
1421
1422 # at one tick from expected idle frame send, nothing should happen:
1423 clock = 0.250
1424 assert t_snd.tick(clock) == 1.001, "deadline for remote timeout"
1425 assert t_rcv.tick(clock) == 0.251, "deadline to send keepalive"
1426 self.pump()
1427 assert sndr_frames_in == t_snd.frames_input, "unexpected received frame"
1428
1429 # this should cause rcvr to expire and send a keepalive
1430 clock = 0.251
1431 assert t_snd.tick(clock) == 1.001, "deadline for remote timeout"
1432 assert t_rcv.tick(clock) == 0.501, "deadline to send keepalive"
1433 self.pump()
1434 sndr_frames_in += 1
1435 rcvr_frames_out += 1
1436 assert sndr_frames_in == t_snd.frames_input, "unexpected received frame"
1437 assert rcvr_frames_out == t_rcv.frames_output, "unexpected frame"
1438
1439 # since a keepalive was received, sndr will rebase its clock against this tick:
1440 # and the receiver should not change its deadline
1441 clock = 0.498
1442 assert t_snd.tick(clock) == 1.498, "deadline for remote timeout"
1443 assert t_rcv.tick(clock) == 0.501, "deadline to send keepalive"
1444 self.pump()
1445 assert sndr_frames_in == t_snd.frames_input, "unexpected received frame"
1446
1447 # now expire sndr
1448 clock = 1.499
1449 t_snd.tick(clock)
1450 self.pump()
1451 assert self.c2.state & Endpoint.REMOTE_CLOSED
1452 assert self.c2.remote_condition.name == "amqp:resource-limit-exceeded"
1453
1454
1455 class CreditTest(Test):
1456
1457 def setUp(self):
1458 self.snd, self.rcv = self.link("test-link", max_frame=(16 * 1024, 16 * 1024))
1459 self.c1 = self.snd.session.connection
1460 self.c2 = self.rcv.session.connection
1461 self.snd.open()
1462 self.rcv.open()
1463 self.pump()
1464
1465 def cleanup(self):
1466 # release resources created by this class
1467 super(CreditTest, self).cleanup()
1468 self.c1 = None
1469 self.snd = None
1470 self.c2 = None
1471 self.rcv2 = None
1472 self.snd2 = None
1473
1474 def tearDown(self):
1475 self.cleanup()
1476
1477 def testCreditSender(self, count=1024):
1478 credit = self.snd.credit
1479 assert credit == 0, credit
1480 self.rcv.flow(10)
1481 self.pump()
1482 credit = self.snd.credit
1483 assert credit == 10, credit
1484
1485 self.rcv.flow(count)
1486 self.pump()
1487 credit = self.snd.credit
1488 assert credit == 10 + count, credit
1489
1490 def testCreditReceiver(self):
1491 self.rcv.flow(10)
1492 self.pump()
1493 assert self.rcv.credit == 10, self.rcv.credit
1494
1495 d = self.snd.delivery("tag")
1496 assert d
1497 assert self.snd.advance()
1498 self.pump()
1499 assert self.rcv.credit == 10, self.rcv.credit
1500 assert self.rcv.queued == 1, self.rcv.queued
1501 c = self.rcv.current
1502 assert c.tag == "tag", c.tag
1503 assert self.rcv.advance()
1504 assert self.rcv.credit == 9, self.rcv.credit
1505 assert self.rcv.queued == 0, self.rcv.queued
1506
1507 def _testBufferingOnClose(self, a, b):
1508 for i in range(10):
1509 d = self.snd.delivery("tag-%s" % i)
1510 assert d
1511 d.settle()
1512 self.pump()
1513 assert self.snd.queued == 10
1514
1515 endpoints = {"connection": (self.c1, self.c2),
1516 "session": (self.snd.session, self.rcv.session),
1517 "link": (self.snd, self.rcv)}
1518
1519 local_a, remote_a = endpoints[a]
1520 local_b, remote_b = endpoints[b]
1521
1522 remote_b.close()
1523 self.pump()
1524 assert local_b.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED
1525 local_a.close()
1526 self.pump()
1527 assert remote_a.state & Endpoint.REMOTE_CLOSED
1528 assert self.snd.queued == 10
1529
1530 def testBufferingOnCloseLinkLink(self):
1531 self._testBufferingOnClose("link", "link")
1532
1533 def testBufferingOnCloseLinkSession(self):
1534 self._testBufferingOnClose("link", "session")
1535
1536 def testBufferingOnCloseLinkConnection(self):
1537 self._testBufferingOnClose("link", "connection")
1538
1539 def testBufferingOnCloseSessionLink(self):
1540 self._testBufferingOnClose("session", "link")
1541
1542 def testBufferingOnCloseSessionSession(self):
1543 self._testBufferingOnClose("session", "session")
1544
1545 def testBufferingOnCloseSessionConnection(self):
1546 self._testBufferingOnClose("session", "connection")
1547
1548 def testBufferingOnCloseConnectionLink(self):
1549 self._testBufferingOnClose("connection", "link")
1550
1551 def testBufferingOnCloseConnectionSession(self):
1552 self._testBufferingOnClose("connection", "session")
1553
1554 def testBufferingOnCloseConnectionConnection(self):
1555 self._testBufferingOnClose("connection", "connection")
1556
1557 def testFullDrain(self):
1558 assert self.rcv.credit == 0
1559 assert self.snd.credit == 0
1560 self.rcv.drain(10)
1561 assert self.rcv.draining()
1562 assert self.rcv.credit == 10
1563 assert self.snd.credit == 0
1564 self.pump()
1565 assert self.rcv.credit == 10
1566 assert self.snd.credit == 10
1567 assert self.rcv.draining()
1568 self.snd.drained()
1569 assert self.rcv.credit == 10
1570 assert self.snd.credit == 0
1571 assert self.rcv.draining()
1572 self.pump()
1573 assert self.rcv.credit == 0
1574 assert self.snd.credit == 0
1575 assert not self.rcv.draining()
1576 drained = self.rcv.drained()
1577 assert drained == 10, drained
1578
1579 def testPartialDrain(self):
1580 self.rcv.drain(2)
1581 assert self.rcv.draining()
1582 self.pump()
1583
1584 d = self.snd.delivery("tag")
1585 assert d
1586 assert self.snd.advance()
1587 self.snd.drained()
1588 assert self.rcv.draining()
1589 self.pump()
1590 assert not self.rcv.draining()
1591
1592 c = self.rcv.current
1593 assert self.rcv.queued == 1, self.rcv.queued
1594 assert c.tag == d.tag, c.tag
1595 assert self.rcv.advance()
1596 assert not self.rcv.current
1597 assert self.rcv.credit == 0, self.rcv.credit
1598 assert not self.rcv.draining()
1599 drained = self.rcv.drained()
1600 assert drained == 1, drained
1601
1602 def testDrainFlow(self):
1603 assert self.rcv.credit == 0
1604 assert self.snd.credit == 0
1605 self.rcv.drain(10)
1606 assert self.rcv.credit == 10
1607 assert self.snd.credit == 0
1608 self.pump()
1609 assert self.rcv.credit == 10
1610 assert self.snd.credit == 10
1611 self.snd.drained()
1612 assert self.rcv.credit == 10
1613 assert self.snd.credit == 0
1614 self.pump()
1615 assert self.rcv.credit == 0
1616 assert self.snd.credit == 0
1617 self.rcv.flow(10)
1618 assert self.rcv.credit == 10
1619 assert self.snd.credit == 0
1620 self.pump()
1621 assert self.rcv.credit == 10
1622 assert self.snd.credit == 10
1623 self.snd.drained()
1624 assert self.rcv.credit == 10
1625 assert self.snd.credit == 10
1626 self.pump()
1627 assert self.rcv.credit == 10
1628 assert self.snd.credit == 10
1629 drained = self.rcv.drained()
1630 assert drained == 10, drained
1631
1632 def testNegative(self):
1633 assert self.snd.credit == 0
1634 d = self.snd.delivery("tag")
1635 assert d
1636 assert self.snd.advance()
1637 self.pump()
1638
1639 assert self.rcv.credit == 0
1640 assert self.rcv.queued == 0
1641
1642 self.rcv.flow(1)
1643 assert self.rcv.credit == 1
1644 assert self.rcv.queued == 0
1645 self.pump()
1646 assert self.rcv.credit == 1
1647 assert self.rcv.queued == 1, self.rcv.queued
1648
1649 c = self.rcv.current
1650 assert c
1651 assert c.tag == "tag"
1652 assert self.rcv.advance()
1653 assert self.rcv.credit == 0
1654 assert self.rcv.queued == 0
1655
1656 def testDrainZero(self):
1657 assert self.snd.credit == 0
1658 assert self.rcv.credit == 0
1659 assert self.rcv.queued == 0
1660 drained = self.rcv.drained()
1661 assert drained == 0
1662
1663 self.rcv.flow(10)
1664 self.pump()
1665 assert self.snd.credit == 10
1666 assert self.rcv.credit == 10
1667 assert self.rcv.queued == 0
1668
1669 self.snd.drained()
1670 self.pump()
1671 assert self.snd.credit == 10
1672 assert self.rcv.credit == 10
1673 assert self.rcv.queued == 0
1674 drained = self.rcv.drained()
1675 assert drained == 0
1676
1677 self.rcv.drain(0)
1678 assert self.snd.credit == 10
1679 assert self.rcv.credit == 10
1680 assert self.rcv.queued == 0
1681
1682 self.pump()
1683
1684 assert self.snd.credit == 10
1685 assert self.rcv.credit == 10
1686 assert self.rcv.queued == 0
1687
1688 self.snd.drained()
1689 assert self.snd.credit == 0
1690 assert self.rcv.credit == 10
1691 assert self.rcv.queued == 0
1692 drained = self.rcv.drained()
1693 assert drained == 0
1694 self.pump()
1695
1696 assert self.snd.credit == 0
1697 assert self.rcv.credit == 0
1698 assert self.rcv.queued == 0
1699 drained = self.rcv.drained()
1700 assert drained == 10
1701
1702 def testDrainOrder(self):
1703 """ Verify drain/drained works regardless of ordering. See PROTON-401
1704 """
1705 assert self.snd.credit == 0
1706 assert self.rcv.credit == 0
1707 assert self.rcv.queued == 0
1708
1709 # self.rcv.session.connection.transport.trace(Transport.TRACE_FRM)
1710 # self.snd.session.connection.transport.trace(Transport.TRACE_FRM)
1711
1712 # verify that a sender that has reached the drain state will respond
1713 # promptly to a drain issued by the peer.
1714 self.rcv.flow(10)
1715 self.pump()
1716 assert self.snd.credit == 10, self.snd.credit
1717 assert self.rcv.credit == 10, self.rcv.credit
1718
1719 sd = self.snd.delivery("tagA")
1720 assert sd
1721 n = self.snd.send(b"A")
1722 assert n == 1
1723 self.pump()
1724 self.snd.advance()
1725
1726 # done sending, so signal that we are drained:
1727 self.snd.drained()
1728 self.pump()
1729 assert self.snd.credit == 9, self.snd.credit
1730 assert self.rcv.credit == 10, self.rcv.credit
1731
1732 self.rcv.drain(0)
1733 self.pump()
1734 assert self.snd.credit == 9, self.snd.credit
1735 assert self.rcv.credit == 10, self.rcv.credit
1736
1737 data = self.rcv.recv(10)
1738 assert data == b"A", data
1739 self.rcv.advance()
1740 self.pump()
1741 assert self.snd.credit == 9, self.snd.credit
1742 assert self.rcv.credit == 9, self.rcv.credit
1743
1744 self.snd.drained()
1745 self.pump()
1746 assert self.snd.credit == 0, self.snd.credit
1747 assert self.rcv.credit == 0, self.rcv.credit
1748
1749 # verify that a drain requested by the peer is not "acknowledged" until
1750 # after the sender has completed sending its pending messages
1751
1752 self.rcv.flow(10)
1753 self.pump()
1754 assert self.snd.credit == 10, self.snd.credit
1755 assert self.rcv.credit == 10, self.rcv.credit
1756
1757 sd = self.snd.delivery("tagB")
1758 assert sd
1759 n = self.snd.send(b"B")
1760 assert n == 1
1761 self.snd.advance()
1762 self.pump()
1763 assert self.snd.credit == 9, self.snd.credit
1764 assert self.rcv.credit == 10, self.rcv.credit
1765
1766 self.rcv.drain(0)
1767 self.pump()
1768 assert self.snd.credit == 9, self.snd.credit
1769 assert self.rcv.credit == 10, self.rcv.credit
1770
1771 sd = self.snd.delivery("tagC")
1772 assert sd
1773 n = self.snd.send(b"C")
1774 assert n == 1
1775 self.snd.advance()
1776 self.pump()
1777 assert self.snd.credit == 8, self.snd.credit
1778 assert self.rcv.credit == 10, self.rcv.credit
1779
1780 # now that the sender has finished sending everything, it can signal
1781 # drained
1782 self.snd.drained()
1783 self.pump()
1784 assert self.snd.credit == 0, self.snd.credit
1785 assert self.rcv.credit == 2, self.rcv.credit
1786
1787 data = self.rcv.recv(10)
1788 assert data == b"B", data
1789 self.rcv.advance()
1790 data = self.rcv.recv(10)
1791 assert data == b"C", data
1792 self.rcv.advance()
1793 self.pump()
1794 assert self.snd.credit == 0, self.snd.credit
1795 assert self.rcv.credit == 0, self.rcv.credit
1796
1797 def testPushback(self, count=10):
1798 assert self.snd.credit == 0
1799 assert self.rcv.credit == 0
1800
1801 self.rcv.flow(count)
1802 self.pump()
1803
1804 for i in range(count):
1805 d = self.snd.delivery("tag%s" % i)
1806 assert d
1807 self.snd.advance()
1808
1809 assert self.snd.queued == count
1810 assert self.rcv.queued == 0
1811 self.pump()
1812 assert self.snd.queued == 0
1813 assert self.rcv.queued == count
1814
1815 d = self.snd.delivery("extra")
1816 self.snd.advance()
1817
1818 assert self.snd.queued == 1
1819 assert self.rcv.queued == count
1820 self.pump()
1821 assert self.snd.queued == 1
1822 assert self.rcv.queued == count
1823
1824 def testHeadOfLineBlocking(self):
1825 self.snd2 = self.snd.session.sender("link-2")
1826 self.rcv2 = self.rcv.session.receiver("link-2")
1827 self.snd2.open()
1828 self.rcv2.open()
1829 self.pump()
1830 assert self.snd2.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
1831 assert self.rcv2.state == Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE
1832
1833 self.rcv.flow(5)
1834 self.rcv2.flow(10)
1835 self.pump()
1836
1837 assert self.snd.credit == 5
1838 assert self.snd2.credit == 10
1839
1840 for i in range(10):
1841 tag = "test %d" % i
1842 self.snd.delivery(tag)
1843 self.snd.send(tag.encode("ascii"))
1844 assert self.snd.advance()
1845 self.snd2.delivery(tag)
1846 self.snd2.send(tag.encode("ascii"))
1847 assert self.snd2.advance()
1848
1849 self.pump()
1850
1851 for i in range(5):
1852 b = self.rcv.recv(512)
1853 assert self.rcv.advance()
1854 b = self.rcv2.recv(512)
1855 assert self.rcv2.advance()
1856
1857 for i in range(5):
1858 b = self.rcv2.recv(512)
1859 assert self.rcv2.advance()
1860
1861
1862 class SessionCreditTest(Test):
1863
1864 def tearDown(self):
1865 self.cleanup()
1866
1867 def testBuffering(self, count=32, size=1024, capacity=16 * 1024, max_frame=1024):
1868 snd, rcv = self.link("test-link", max_frame=(max_frame, max_frame))
1869 rcv.session.incoming_capacity = capacity
1870 snd.open()
1871 rcv.open()
1872 rcv.flow(count)
1873 self.pump()
1874
1875 assert count > 0
1876
1877 total_bytes = count * size
1878
1879 assert snd.session.outgoing_bytes == 0, snd.session.outgoing_bytes
1880 assert rcv.session.incoming_bytes == 0, rcv.session.incoming_bytes
1881 assert snd.queued == 0, snd.queued
1882 assert rcv.queued == 0, rcv.queued
1883
1884 data = bytes(bytearray(size))
1885 idx = 0
1886 while snd.credit:
1887 d = snd.delivery("tag%s" % idx)
1888 assert d
1889 n = snd.send(data)
1890 assert n == size, (n, size)
1891 assert snd.advance()
1892 self.pump()
1893 idx += 1
1894
1895 assert idx == count, (idx, count)
1896
1897 assert snd.session.outgoing_bytes < total_bytes, (snd.session.outgoing_bytes, total_bytes)
1898 assert rcv.session.incoming_bytes < capacity, (rcv.session.incoming_bytes, capacity)
1899 assert snd.session.outgoing_bytes + rcv.session.incoming_bytes == total_bytes, \
1900 (snd.session.outgoing_bytes, rcv.session.incoming_bytes, total_bytes)
1901 if snd.session.outgoing_bytes > 0:
1902 available = rcv.session.incoming_capacity - rcv.session.incoming_bytes
1903 assert available < max_frame, (available, max_frame)
1904
1905 for i in range(count):
1906 d = rcv.current
1907 assert d, i
1908 pending = d.pending
1909 before = rcv.session.incoming_bytes
1910 assert rcv.advance()
1911 after = rcv.session.incoming_bytes
1912 assert before - after == pending, (before, after, pending)
1913 snd_before = snd.session.incoming_bytes
1914 self.pump()
1915 snd_after = snd.session.incoming_bytes
1916
1917 assert rcv.session.incoming_bytes < capacity
1918 if snd_before > 0:
1919 assert capacity - after <= max_frame
1920 assert snd_before > snd_after
1921 if snd_after > 0:
1922 available = rcv.session.incoming_capacity - rcv.session.incoming_bytes
1923 assert available < max_frame, available
1924
1925 def testBufferingSize16(self):
1926 self.testBuffering(size=16)
1927
1928 def testBufferingSize256(self):
1929 self.testBuffering(size=256)
1930
1931 def testBufferingSize512(self):
1932 self.testBuffering(size=512)
1933
1934 def testBufferingSize2048(self):
1935 self.testBuffering(size=2048)
1936
1937 def testBufferingSize1025(self):
1938 self.testBuffering(size=1025)
1939
1940 def testBufferingSize1023(self):
1941 self.testBuffering(size=1023)
1942
1943 def testBufferingSize989(self):
1944 self.testBuffering(size=989)
1945
1946 def testBufferingSize1059(self):
1947 self.testBuffering(size=1059)
1948
1949 def testCreditWithBuffering(self):
1950 snd, rcv = self.link("test-link", max_frame=(1024, 1024))
1951 rcv.session.incoming_capacity = 64 * 1024
1952 snd.open()
1953 rcv.open()
1954 rcv.flow(128)
1955 self.pump()
1956
1957 assert snd.credit == 128, snd.credit
1958 assert rcv.queued == 0, rcv.queued
1959
1960 idx = 0
1961 while snd.credit:
1962 d = snd.delivery("tag%s" % idx)
1963 snd.send(("x" * 1024).encode('ascii'))
1964 assert d
1965 assert snd.advance()
1966 self.pump()
1967 idx += 1
1968
1969 assert idx == 128, idx
1970 assert rcv.queued < 128, rcv.queued
1971
1972 rcv.flow(1)
1973 self.pump()
1974 assert snd.credit == 1, snd.credit
1975
1976
1977 class SettlementTest(Test):
1978
1979 def setUp(self):
1980 self.snd, self.rcv = self.link("test-link")
1981 self.c1 = self.snd.session.connection
1982 self.c2 = self.rcv.session.connection
1983 self.snd.open()
1984 self.rcv.open()
1985 self.pump()
1986
1987 def cleanup(self):
1988 # release resources created by this class
1989 super(SettlementTest, self).cleanup()
1990 self.c1 = None
1991 self.snd = None
1992 self.c2 = None
1993 self.rcv2 = None
1994 self.snd2 = None
1995
1996 def tearDown(self):
1997 self.cleanup()
1998
1999 def testSettleCurrent(self):
2000 self.rcv.flow(10)
2001 self.pump()
2002
2003 assert self.snd.credit == 10, self.snd.credit
2004 d = self.snd.delivery("tag")
2005 e = self.snd.delivery("tag2")
2006 assert d
2007 assert e
2008 c = self.snd.current
2009 assert c.tag == "tag", c.tag
2010 c.settle()
2011 c = self.snd.current
2012 assert c.tag == "tag2", c.tag
2013 c.settle()
2014 c = self.snd.current
2015 assert not c
2016 self.pump()
2017
2018 c = self.rcv.current
2019 assert c
2020 assert c.tag == "tag", c.tag
2021 assert c.settled
2022 c.settle()
2023 c = self.rcv.current
2024 assert c
2025 assert c.tag == "tag2", c.tag
2026 assert c.settled
2027 c.settle()
2028 c = self.rcv.current
2029 assert not c
2030
2031 def testUnsettled(self):
2032 self.rcv.flow(10)
2033 self.pump()
2034
2035 assert self.snd.unsettled == 0, self.snd.unsettled
2036 assert self.rcv.unsettled == 0, self.rcv.unsettled
2037
2038 d = self.snd.delivery("tag")
2039 assert d
2040 assert self.snd.unsettled == 1, self.snd.unsettled
2041 assert self.rcv.unsettled == 0, self.rcv.unsettled
2042 assert self.snd.advance()
2043 self.pump()
2044
2045 assert self.snd.unsettled == 1, self.snd.unsettled
2046 assert self.rcv.unsettled == 1, self.rcv.unsettled
2047
2048 c = self.rcv.current
2049 assert c
2050 c.settle()
2051
2052 assert self.snd.unsettled == 1, self.snd.unsettled
2053 assert self.rcv.unsettled == 0, self.rcv.unsettled
2054
2055 def testMultipleUnsettled(self, count=1024, size=1024):
2056 self.rcv.flow(count)
2057 self.pump()
2058
2059 assert self.snd.unsettled == 0, self.snd.unsettled
2060 assert self.rcv.unsettled == 0, self.rcv.unsettled
2061
2062 unsettled = []
2063
2064 for i in range(count):
2065 sd = self.snd.delivery("tag%s" % i)
2066 assert sd
2067 n = self.snd.send(("x" * size).encode('ascii'))
2068 assert n == size, n
2069 assert self.snd.advance()
2070 self.pump()
2071
2072 rd = self.rcv.current
2073 assert rd, "did not receive delivery %s" % i
2074 n = rd.pending
2075 b = self.rcv.recv(n)
2076 assert len(b) == n, (b, n)
2077 rd.update(Delivery.ACCEPTED)
2078 assert self.rcv.advance()
2079 self.pump()
2080 unsettled.append(rd)
2081
2082 assert self.rcv.unsettled == count
2083
2084 for rd in unsettled:
2085 rd.settle()
2086
2087 def testMultipleUnsettled2K1K(self):
2088 self.testMultipleUnsettled(2048, 1024)
2089
2090 def testMultipleUnsettled4K1K(self):
2091 self.testMultipleUnsettled(4096, 1024)
2092
2093 def testMultipleUnsettled1K2K(self):
2094 self.testMultipleUnsettled(1024, 2048)
2095
2096 def testMultipleUnsettled2K2K(self):
2097 self.testMultipleUnsettled(2048, 2048)
2098
2099 def testMultipleUnsettled4K2K(self):
2100 self.testMultipleUnsettled(4096, 2048)
2101
2102
2103 class PipelineTest(Test):
2104
2105 def setUp(self):
2106 self.c1, self.c2 = self.connection()
2107
2108 def cleanup(self):
2109 # release resources created by this class
2110 super(PipelineTest, self).cleanup()
2111 self.c1 = None
2112 self.c2 = None
2113
2114 def tearDown(self):
2115 self.cleanup()
2116
2117 def test(self):
2118 ssn = self.c1.session()
2119 snd = ssn.sender("sender")
2120 self.c1.open()
2121 ssn.open()
2122 snd.open()
2123
2124 for i in range(10):
2125 t = "delivery-%s" % i
2126 d = snd.delivery(t)
2127 snd.send(t.encode('ascii'))
2128 d.settle()
2129
2130 snd.close()
2131 ssn.close()
2132 self.c1.close()
2133
2134 self.pump()
2135
2136 state = self.c2.state
2137 assert state == (Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_ACTIVE), "%x" % state
2138 ssn2 = self.c2.session_head(Endpoint.LOCAL_UNINIT)
2139 assert ssn2
2140 state == ssn2.state
2141 assert state == (Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_ACTIVE), "%x" % state
2142 rcv = self.c2.link_head(Endpoint.LOCAL_UNINIT)
2143 assert rcv
2144 state = rcv.state
2145 assert state == (Endpoint.LOCAL_UNINIT | Endpoint.REMOTE_ACTIVE), "%x" % state
2146
2147 self.c2.open()
2148 ssn2.open()
2149 rcv.open()
2150 rcv.flow(10)
2151 assert rcv.queued == 0, rcv.queued
2152
2153 self.pump()
2154
2155 assert rcv.queued == 10, rcv.queued
2156 state = rcv.state
2157 assert state == (Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED), "%x" % state
2158 state = ssn2.state
2159 assert state == (Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED), "%x" % state
2160 state = self.c2.state
2161 assert state == (Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_CLOSED), "%x" % state
2162
2163 for i in range(rcv.queued):
2164 d = rcv.current
2165 assert d
2166 assert d.tag == "delivery-%s" % i
2167 d.settle()
2168
2169 assert rcv.queued == 0, rcv.queued
2170
2171
2172 class ServerTest(Test):
2173
2174 def testKeepalive(self):
2175 """ Verify that idle frames are sent to keep a Connection alive
2176 """
2177 idle_timeout = self.delay
2178 server = common.TestServer()
2179 server.start()
2180
2181 class Program:
2182
2183 def on_reactor_init(self, event):
2184 self.conn = event.reactor.connection()
2185 self.conn.hostname = "%s:%s" % (server.host, server.port)
2186 self.conn.open()
2187 self.old_count = None
2188 event.reactor.schedule(3 * idle_timeout, self)
2189
2190 def on_connection_bound(self, event):
2191 event.transport.idle_timeout = idle_timeout
2192
2193 def on_connection_remote_open(self, event):
2194 self.old_count = event.transport.frames_input
2195
2196 def on_timer_task(self, event):
2197 assert self.conn.state == (Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE), "Connection terminated"
2198 assert self.conn.transport.frames_input > self.old_count, "No idle frames received"
2199 self.conn.close()
2200
2201 Container(Program()).run()
2202 server.stop()
2203
2204 def testIdleTimeout(self):
2205 """ Verify that a Connection is terminated properly when Idle frames do not
2206 arrive in a timely manner.
2207 """
2208 idle_timeout = self.delay
2209 server = common.TestServer(idle_timeout=idle_timeout)
2210 server.start()
2211
2212 class Program:
2213
2214 def on_reactor_init(self, event):
2215 self.conn = event.reactor.connection()
2216 self.conn.hostname = "%s:%s" % (server.host, server.port)
2217 self.conn.open()
2218 self.remote_condition = None
2219 self.old_count = None
2220 # verify the connection stays up even if we don't explicitly send stuff
2221 # wait up to 3x the idle timeout
2222 event.reactor.schedule(3 * idle_timeout, self)
2223
2224 def on_connection_bound(self, event):
2225 self.transport = event.transport
2226
2227 def on_connection_remote_open(self, event):
2228 self.old_count = event.transport.frames_output
2229
2230 def on_connection_remote_close(self, event):
2231 assert self.conn.remote_condition
2232 assert self.conn.remote_condition.name == "amqp:resource-limit-exceeded"
2233 self.remote_condition = self.conn.remote_condition
2234
2235 def on_timer_task(self, event):
2236 assert self.conn.state == (Endpoint.LOCAL_ACTIVE | Endpoint.REMOTE_ACTIVE), "Connection terminated"
2237 assert self.conn.transport.frames_output > self.old_count, "No idle frames sent"
2238
2239 # now wait to explicitly cause the other side to expire:
2240 suspend_time = 3 * idle_timeout
2241 if os.name == "nt":
2242 # On windows, the full delay gets too close to the graceful/hard close tipping point
2243 suspend_time = 2.5 * idle_timeout
2244 sleep(suspend_time)
2245
2246 p = Program()
2247 Container(p).run()
2248 assert p.remote_condition
2249 assert p.remote_condition.name == "amqp:resource-limit-exceeded"
2250 server.stop()
2251
2252
2253 class NoValue:
2254
2255 def __init__(self):
2256 pass
2257
2258 def apply(self, dlv):
2259 pass
2260
2261 def check(self, dlv):
2262 assert dlv.data is None
2263 assert dlv.section_number == 0
2264 assert dlv.section_offset == 0
2265 assert dlv.condition is None
2266 assert dlv.failed == False
2267 assert dlv.undeliverable == False
2268 assert dlv.annotations is None
2269
2270
2271 class RejectValue:
2272 def __init__(self, condition):
2273 self.condition = condition
2274
2275 def apply(self, dlv):
2276 dlv.condition = self.condition
2277
2278 def check(self, dlv):
2279 assert dlv.data is None, dlv.data
2280 assert dlv.section_number == 0
2281 assert dlv.section_offset == 0
2282 assert dlv.condition == self.condition, (dlv.condition, self.condition)
2283 assert dlv.failed == False
2284 assert dlv.undeliverable == False
2285 assert dlv.annotations is None
2286
2287
2288 class ReceivedValue:
2289 def __init__(self, section_number, section_offset):
2290 self.section_number = section_number
2291 self.section_offset = section_offset
2292
2293 def apply(self, dlv):
2294 dlv.section_number = self.section_number
2295 dlv.section_offset = self.section_offset
2296
2297 def check(self, dlv):
2298 assert dlv.data is None, dlv.data
2299 assert dlv.section_number == self.section_number, (dlv.section_number, self.section_number)
2300 assert dlv.section_offset == self.section_offset
2301 assert dlv.condition is None
2302 assert dlv.failed == False
2303 assert dlv.undeliverable == False
2304 assert dlv.annotations is None
2305
2306
2307 class ModifiedValue:
2308 def __init__(self, failed, undeliverable, annotations):
2309 self.failed = failed
2310 self.undeliverable = undeliverable
2311 self.annotations = annotations
2312
2313 def apply(self, dlv):
2314 dlv.failed = self.failed
2315 dlv.undeliverable = self.undeliverable
2316 dlv.annotations = self.annotations
2317
2318 def check(self, dlv):
2319 assert dlv.data is None, dlv.data
2320 assert dlv.section_number == 0
2321 assert dlv.section_offset == 0
2322 assert dlv.condition is None
2323 assert dlv.failed == self.failed
2324 assert dlv.undeliverable == self.undeliverable
2325 assert dlv.annotations == self.annotations, (dlv.annotations, self.annotations)
2326
2327
2328 class CustomValue:
2329 def __init__(self, data):
2330 self.data = data
2331
2332 def apply(self, dlv):
2333 dlv.data = self.data
2334
2335 def check(self, dlv):
2336 assert dlv.data == self.data, (dlv.data, self.data)
2337 assert dlv.section_number == 0
2338 assert dlv.section_offset == 0
2339 assert dlv.condition is None
2340 assert dlv.failed == False
2341 assert dlv.undeliverable == False
2342 assert dlv.annotations is None
2343
2344
2345 class DeliveryTest(Test):
2346
2347 def tearDown(self):
2348 self.cleanup()
2349
2350 def testDisposition(self, count=1, tag="tag%i", type=Delivery.ACCEPTED, value=NoValue()):
2351 snd, rcv = self.link("test-link")
2352 snd.open()
2353 rcv.open()
2354
2355 snd_deliveries = []
2356 for i in range(count):
2357 d = snd.delivery(tag % i)
2358 snd_deliveries.append(d)
2359 snd.advance()
2360
2361 rcv.flow(count)
2362 self.pump()
2363
2364 rcv_deliveries = []
2365 for i in range(count):
2366 d = rcv.current
2367 assert d.tag == (tag % i)
2368 rcv_deliveries.append(d)
2369 rcv.advance()
2370
2371 for d in rcv_deliveries:
2372 value.apply(d.local)
2373 d.update(type)
2374
2375 self.pump()
2376
2377 for d in snd_deliveries:
2378 assert d.remote_state == type
2379 assert d.remote.type == type
2380 value.check(d.remote)
2381 value.apply(d.local)
2382 d.update(type)
2383
2384 self.pump()
2385
2386 for d in rcv_deliveries:
2387 assert d.remote_state == type
2388 assert d.remote.type == type
2389 value.check(d.remote)
2390
2391 for d in snd_deliveries:
2392 d.settle()
2393
2394 self.pump()
2395
2396 for d in rcv_deliveries:
2397 assert d.settled, d.settled
2398 d.settle()
2399
2400 def testReceived(self):
2401 self.testDisposition(type=Disposition.RECEIVED, value=ReceivedValue(1, 2))
2402
2403 def testRejected(self):
2404 self.testDisposition(type=Disposition.REJECTED, value=RejectValue(Condition(symbol("foo"))))
2405
2406 def testReleased(self):
2407 self.testDisposition(type=Disposition.RELEASED)
2408
2409 def testModified(self):
2410 self.testDisposition(type=Disposition.MODIFIED,
2411 value=ModifiedValue(failed=True, undeliverable=True,
2412 annotations={"key": "value"}))
2413
2414 def testCustom(self):
2415 self.testDisposition(type=0x12345, value=CustomValue([1, 2, 3]))
2416
2417
2418 class CollectorTest(Test):
2419
2420 def setUp(self):
2421 self.collector = Collector()
2422
2423 def drain(self):
2424 result = []
2425 while True:
2426 e = self.collector.peek()
2427 if e:
2428 result.append(e)
2429 self.collector.pop()
2430 else:
2431 break
2432 return result
2433
2434 def expect(self, *types):
2435 return self.expect_oneof(types)
2436
2437 def expect_oneof(self, *sequences):
2438 events = self.drain()
2439 types = tuple([e.type for e in events])
2440
2441 for alternative in sequences:
2442 if types == alternative:
2443 if len(events) == 1:
2444 return events[0]
2445 elif len(events) > 1:
2446 return events
2447 else:
2448 return
2449
2450 assert False, "actual events %s did not match any of the expected sequences: %s" % (events, sequences)
2451
2452 def expect_until(self, *types):
2453 events = self.drain()
2454 etypes = tuple([e.type for e in events[-len(types):]])
2455 assert etypes == types, "actual events %s did not end in expect sequence: %s" % (events, types)
2456
2457
2458 class EventTest(CollectorTest):
2459
2460 def tearDown(self):
2461 self.cleanup()
2462
2463 def testEndpointEvents(self):
2464 c1, c2 = self.connection()
2465 c1.collect(self.collector)
2466 self.expect(Event.CONNECTION_INIT)
2467 self.pump()
2468 self.expect()
2469 c2.open()
2470 self.pump()
2471 self.expect(Event.CONNECTION_REMOTE_OPEN)
2472 self.pump()
2473 self.expect()
2474
2475 ssn = c2.session()
2476 snd = ssn.sender("sender")
2477 ssn.open()
2478 snd.open()
2479
2480 self.expect()
2481 self.pump()
2482 self.expect(Event.SESSION_INIT, Event.SESSION_REMOTE_OPEN,
2483 Event.LINK_INIT, Event.LINK_REMOTE_OPEN)
2484
2485 c1.open()
2486 ssn2 = c1.session()
2487 ssn2.open()
2488 rcv = ssn2.receiver("receiver")
2489 rcv.open()
2490 self.pump()
2491 self.expect(Event.CONNECTION_LOCAL_OPEN, Event.TRANSPORT,
2492 Event.SESSION_INIT, Event.SESSION_LOCAL_OPEN,
2493 Event.TRANSPORT, Event.LINK_INIT, Event.LINK_LOCAL_OPEN,
2494 Event.TRANSPORT)
2495
2496 rcv.close()
2497 self.expect(Event.LINK_LOCAL_CLOSE, Event.TRANSPORT)
2498 self.pump()
2499 rcv.free()
2500 del rcv
2501 self.expect(Event.LINK_FINAL)
2502 ssn2.free()
2503 del ssn2
2504 self.pump()
2505 c1.free()
2506 c1.transport.unbind()
2507 self.expect_oneof((Event.SESSION_FINAL, Event.LINK_FINAL, Event.SESSION_FINAL,
2508 Event.CONNECTION_UNBOUND, Event.CONNECTION_FINAL),
2509 (Event.CONNECTION_UNBOUND, Event.SESSION_FINAL, Event.LINK_FINAL,
2510 Event.SESSION_FINAL, Event.CONNECTION_FINAL))
2511
2512 def testConnectionINIT_FINAL(self):
2513 c = Connection()
2514 c.collect(self.collector)
2515 self.expect(Event.CONNECTION_INIT)
2516 c.free()
2517 self.expect(Event.CONNECTION_FINAL)
2518
2519 def testSessionINIT_FINAL(self):
2520 c = Connection()
2521 c.collect(self.collector)
2522 self.expect(Event.CONNECTION_INIT)
2523 s = c.session()
2524 self.expect(Event.SESSION_INIT)
2525 s.free()
2526 self.expect(Event.SESSION_FINAL)
2527 c.free()
2528 self.expect(Event.CONNECTION_FINAL)
2529
2530 def testLinkINIT_FINAL(self):
2531 c = Connection()
2532 c.collect(self.collector)
2533 self.expect(Event.CONNECTION_INIT)
2534 s = c.session()
2535 self.expect(Event.SESSION_INIT)
2536 r = s.receiver("asdf")
2537 self.expect(Event.LINK_INIT)
2538 r.free()
2539 self.expect(Event.LINK_FINAL)
2540 c.free()
2541 self.expect(Event.SESSION_FINAL, Event.CONNECTION_FINAL)
2542
2543 def testFlowEvents(self):
2544 snd, rcv = self.link("test-link")
2545 snd.session.connection.collect(self.collector)
2546 rcv.open()
2547 rcv.flow(10)
2548 self.pump()
2549 self.expect(Event.CONNECTION_INIT, Event.SESSION_INIT,
2550 Event.LINK_INIT, Event.LINK_REMOTE_OPEN, Event.LINK_FLOW)
2551 rcv.flow(10)
2552 self.pump()
2553 self.expect(Event.LINK_FLOW)
2554 return snd, rcv
2555
2556 def testDeliveryEvents(self):
2557 snd, rcv = self.link("test-link")
2558 rcv.session.connection.collect(self.collector)
2559 rcv.open()
2560 rcv.flow(10)
2561 self.pump()
2562 self.expect(Event.CONNECTION_INIT, Event.SESSION_INIT,
2563 Event.LINK_INIT, Event.LINK_LOCAL_OPEN, Event.TRANSPORT)
2564 snd.delivery("delivery")
2565 snd.send(b"Hello World!")
2566 snd.advance()
2567 self.pump()
2568 self.expect()
2569 snd.open()
2570 self.pump()
2571 self.expect(Event.LINK_REMOTE_OPEN, Event.DELIVERY)
2572 rcv.session.connection.transport.unbind()
2573 rcv.session.connection.free()
2574 self.expect(Event.CONNECTION_UNBOUND, Event.TRANSPORT, Event.LINK_FINAL,
2575 Event.SESSION_FINAL, Event.CONNECTION_FINAL)
2576
2577 def testDeliveryEventsDisp(self):
2578 snd, rcv = self.testFlowEvents()
2579 snd.open()
2580 dlv = snd.delivery("delivery")
2581 snd.send(b"Hello World!")
2582 assert snd.advance()
2583 self.expect(Event.LINK_LOCAL_OPEN, Event.TRANSPORT)
2584 self.pump()
2585 self.expect(Event.LINK_FLOW)
2586 rdlv = rcv.current
2587 assert rdlv is not None
2588 assert rdlv.tag == "delivery"
2589 rdlv.update(Delivery.ACCEPTED)
2590 self.pump()
2591 event = self.expect(Event.DELIVERY)
2592 assert event.context == dlv, (dlv, event.context)
2593
2594 def testConnectionBOUND_UNBOUND(self):
2595 c = Connection()
2596 c.collect(self.collector)
2597 self.expect(Event.CONNECTION_INIT)
2598 t = Transport()
2599 t.bind(c)
2600 self.expect(Event.CONNECTION_BOUND)
2601 t.unbind()
2602 self.expect(Event.CONNECTION_UNBOUND, Event.TRANSPORT)
2603
2604 def testTransportERROR_CLOSE(self):
2605 c = Connection()
2606 c.collect(self.collector)
2607 self.expect(Event.CONNECTION_INIT)
2608 t = Transport()
2609 t.bind(c)
2610 self.expect(Event.CONNECTION_BOUND)
2611 assert t.condition is None
2612 t.push(b"asdf")
2613 self.expect(Event.TRANSPORT_ERROR, Event.TRANSPORT_TAIL_CLOSED)
2614 assert t.condition is not None
2615 assert t.condition.name == "amqp:connection:framing-error"
2616 assert "AMQP protocol header" in t.condition.description
2617 p = t.pending()
2618 assert p > 0
2619 t.pop(p)
2620 self.expect(Event.TRANSPORT_HEAD_CLOSED, Event.TRANSPORT_CLOSED)
2621
2622 def testTransportCLOSED(self):
2623 c = Connection()
2624 c.collect(self.collector)
2625 self.expect(Event.CONNECTION_INIT)
2626 t = Transport()
2627 t.bind(c)
2628 c.open()
2629
2630 self.expect(Event.CONNECTION_BOUND, Event.CONNECTION_LOCAL_OPEN, Event.TRANSPORT)
2631
2632 c2 = Connection()
2633 t2 = Transport()
2634 t2.bind(c2)
2635 c2.open()
2636 c2.close()
2637
2638 pump(t, t2)
2639
2640 self.expect(Event.CONNECTION_REMOTE_OPEN, Event.CONNECTION_REMOTE_CLOSE,
2641 Event.TRANSPORT_TAIL_CLOSED)
2642
2643 c.close()
2644
2645 pump(t, t2)
2646
2647 self.expect(Event.CONNECTION_LOCAL_CLOSE, Event.TRANSPORT,
2648 Event.TRANSPORT_HEAD_CLOSED, Event.TRANSPORT_CLOSED)
2649
2650 def testLinkDetach(self):
2651 c1 = Connection()
2652 c1.collect(self.collector)
2653 t1 = Transport()
2654 t1.bind(c1)
2655 c1.open()
2656 s1 = c1.session()
2657 s1.open()
2658 l1 = s1.sender("asdf")
2659 l1.open()
2660 l1.detach()
2661 self.expect_until(Event.LINK_LOCAL_DETACH, Event.TRANSPORT)
2662
2663 c2 = Connection()
2664 c2.collect(self.collector)
2665 t2 = Transport()
2666 t2.bind(c2)
2667
2668 pump(t1, t2)
2669
2670 self.expect_until(Event.LINK_REMOTE_DETACH)
2671
2672
2673 class PeerTest(CollectorTest):
2674
2675 def setUp(self):
2676 CollectorTest.setUp(self)
2677 self.connection = Connection()
2678 self.connection.collect(self.collector)
2679 self.transport = Transport()
2680 self.transport.bind(self.connection)
2681 self.peer = Connection()
2682 self.peer_transport = Transport()
2683 self.peer_transport.bind(self.peer)
2684 self.peer_transport.trace(Transport.TRACE_OFF)
2685
2686 def pump(self):
2687 pump(self.transport, self.peer_transport)
2688
2689
2690 class TeardownLeakTest(PeerTest):
2691
2692 def doLeak(self, local, remote):
2693 self.connection.open()
2694 self.expect(Event.CONNECTION_INIT, Event.CONNECTION_BOUND,
2695 Event.CONNECTION_LOCAL_OPEN, Event.TRANSPORT)
2696
2697 ssn = self.connection.session()
2698 ssn.open()
2699 self.expect(Event.SESSION_INIT, Event.SESSION_LOCAL_OPEN, Event.TRANSPORT)
2700
2701 snd = ssn.sender("sender")
2702 snd.open()
2703 self.expect(Event.LINK_INIT, Event.LINK_LOCAL_OPEN, Event.TRANSPORT)
2704
2705 self.pump()
2706
2707 self.peer.open()
2708 self.peer.session_head(0).open()
2709 self.peer.link_head(0).open()
2710
2711 self.pump()
2712 self.expect_oneof((Event.CONNECTION_REMOTE_OPEN, Event.SESSION_REMOTE_OPEN,
2713 Event.LINK_REMOTE_OPEN, Event.LINK_FLOW),
2714 (Event.CONNECTION_REMOTE_OPEN, Event.SESSION_REMOTE_OPEN,
2715 Event.LINK_REMOTE_OPEN))
2716
2717 if local:
2718 snd.close() # ha!!
2719 self.expect(Event.LINK_LOCAL_CLOSE, Event.TRANSPORT)
2720 ssn.close()
2721 self.expect(Event.SESSION_LOCAL_CLOSE, Event.TRANSPORT)
2722 self.connection.close()
2723 self.expect(Event.CONNECTION_LOCAL_CLOSE, Event.TRANSPORT)
2724
2725 if remote:
2726 self.peer.link_head(0).close() # ha!!
2727 self.peer.session_head(0).close()
2728 self.peer.close()
2729
2730 self.pump()
2731
2732 if remote:
2733 self.expect(Event.TRANSPORT_HEAD_CLOSED, Event.LINK_REMOTE_CLOSE,
2734 Event.SESSION_REMOTE_CLOSE, Event.CONNECTION_REMOTE_CLOSE,
2735 Event.TRANSPORT_TAIL_CLOSED, Event.TRANSPORT_CLOSED)
2736 else:
2737 self.expect(Event.TRANSPORT_HEAD_CLOSED, Event.SESSION_REMOTE_CLOSE,
2738 Event.CONNECTION_REMOTE_CLOSE, Event.TRANSPORT_TAIL_CLOSED,
2739 Event.TRANSPORT_CLOSED)
2740
2741 self.connection.free()
2742 self.expect(Event.LINK_FINAL, Event.SESSION_FINAL)
2743 self.transport.unbind()
2744
2745 self.expect(Event.CONNECTION_UNBOUND, Event.CONNECTION_FINAL)
2746
2747 def testLocalRemoteLeak(self):
2748 self.doLeak(True, True)
2749
2750 def testLocalLeak(self):
2751 self.doLeak(True, False)
2752
2753 def testRemoteLeak(self):
2754 self.doLeak(False, True)
2755
2756 def testLeak(self):
2757 self.doLeak(False, False)
2758
2759
2760 class IdleTimeoutEventTest(PeerTest):
2761
2762 def half_pump(self):
2763 p = self.transport.pending()
2764 if p > 0:
2765 self.transport.pop(p)
2766
2767 def testTimeoutWithZombieServer(self, expectOpenCloseFrames=True):
2768 self.transport.idle_timeout = self.delay
2769 self.connection.open()
2770 self.half_pump()
2771 t = time()
2772 self.transport.tick(t)
2773 self.transport.tick(t + self.delay * 4)
2774 self.expect(Event.CONNECTION_INIT, Event.CONNECTION_BOUND,
2775 Event.CONNECTION_LOCAL_OPEN, Event.TRANSPORT,
2776 Event.TRANSPORT_ERROR, Event.TRANSPORT_TAIL_CLOSED)
2777 assert self.transport.capacity() < 0
2778 if expectOpenCloseFrames:
2779 assert self.transport.pending() > 0
2780 self.half_pump()
2781 self.expect(Event.TRANSPORT_HEAD_CLOSED, Event.TRANSPORT_CLOSED)
2782 assert self.transport.pending() < 0
2783
2784 def testTimeoutWithZombieServerAndSASL(self):
2785 sasl = self.transport.sasl()
2786 self.testTimeoutWithZombieServer(expectOpenCloseFrames=False)
2787
2788
2789 class DeliverySegFaultTest(Test):
2790
2791 def testDeliveryAfterUnbind(self):
2792 conn = Connection()
2793 t = Transport()
2794 ssn = conn.session()
2795 snd = ssn.sender("sender")
2796 dlv = snd.delivery("tag")
2797 dlv.settle()
2798 del dlv
2799 t.bind(conn)
2800 t.unbind()
2801 dlv = snd.delivery("tag")
2802
2803
2804 class SaslEventTest(CollectorTest):
2805
2806 def testAnonymousNoInitialResponse(self):
2807 conn = Connection()
2808 conn.collect(self.collector)
2809 transport = Transport(Transport.SERVER)
2810 transport.bind(conn)
2811 self.expect(Event.CONNECTION_INIT, Event.CONNECTION_BOUND)
2812
2813 transport.push(b'AMQP\x03\x01\x00\x00\x00\x00\x00 \x02\x01\x00\x00\x00SA'
2814 b'\xd0\x00\x00\x00\x10\x00\x00\x00\x02\xa3\tANONYMOUS@'
2815 b'AMQP\x00\x01\x00\x00')
2816 self.expect(Event.TRANSPORT)
2817 for i in range(1024):
2818 p = transport.pending()
2819 self.drain()
2820 p = transport.pending()
2821 self.expect()
2822
2823 def testPipelinedServerReadFirst(self):
2824 conn = Connection()
2825 conn.collect(self.collector)
2826 transport = Transport(Transport.CLIENT)
2827 s = transport.sasl()
2828 s.allowed_mechs("ANONYMOUS PLAIN")
2829 transport.bind(conn)
2830 self.expect(Event.CONNECTION_INIT, Event.CONNECTION_BOUND)
2831 transport.push(
2832 # SASL
2833 b'AMQP\x03\x01\x00\x00'
2834 # @sasl-mechanisms(64) [sasl-server-mechanisms=@PN_SYMBOL[:ANONYMOUS]]
2835 b'\x00\x00\x00\x1c\x02\x01\x00\x00\x00S@\xc0\x0f\x01\xe0\x0c\x01\xa3\tANONYMOUS'
2836 # @sasl-outcome(68) [code=0]
2837 b'\x00\x00\x00\x10\x02\x01\x00\x00\x00SD\xc0\x03\x01P\x00'
2838 # AMQP
2839 b'AMQP\x00\x01\x00\x00'
2840 )
2841 self.expect(Event.TRANSPORT)
2842 p = transport.pending()
2843 bytes = transport.peek(p)
2844 transport.pop(p)
2845
2846 server = Transport(Transport.SERVER)
2847 server.push(bytes)
2848 assert s.outcome == SASL.OK
2849 assert server.sasl().outcome == SASL.OK
2850
2851 def testPipelinedServerWriteFirst(self):
2852 conn = Connection()
2853 conn.collect(self.collector)
2854 transport = Transport(Transport.CLIENT)
2855 s = transport.sasl()
2856 s.allowed_mechs("ANONYMOUS")
2857 transport.bind(conn)
2858 p = transport.pending()
2859 bytes = transport.peek(p)
2860 transport.pop(p)
2861 self.expect(Event.CONNECTION_INIT, Event.CONNECTION_BOUND)
2862 transport.push(
2863 # SASL
2864 b'AMQP\x03\x01\x00\x00'
2865 # @sasl-mechanisms(64) [sasl-server-mechanisms=@PN_SYMBOL[:ANONYMOUS]]
2866 b'\x00\x00\x00\x1c\x02\x01\x00\x00\x00S@\xc0\x0f\x01\xe0\x0c\x01\xa3\tANONYMOUS'
2867 # @sasl-outcome(68) [code=0]
2868 b'\x00\x00\x00\x10\x02\x01\x00\x00\x00SD\xc0\x03\x01P\x00'
2869 # AMQP
2870 b'AMQP\x00\x01\x00\x00'
2871 )
2872 self.expect(Event.TRANSPORT)
2873 p = transport.pending()
2874 bytes = transport.peek(p)
2875 transport.pop(p)
2876 assert s.outcome == SASL.OK
2877 # XXX: the bytes above appear to be correct, but we don't get any
2878 # sort of event indicating that the transport is authenticated