forked from skupperproject/skupper-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_tests_distribution.py
1555 lines (1356 loc) · 60.6 KB
/
system_tests_distribution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
from time import sleep
from proton import Message, Delivery
from proton.handlers import MessagingHandler
from proton.reactor import Container, LinkOption
from system_test import TestCase, Qdrouterd, main_module, TIMEOUT, TestTimeout
from system_test import unittest, Logger
# ------------------------------------------------
# Helper classes for all tests.
# ------------------------------------------------
class AddressCheckResponse:
"""
Convenience class for the responses returned by an AddressChecker.
"""
def __init__(self, status_code, status_description, attrs):
self.status_code = status_code
self.status_description = status_description
self.attrs = attrs
def __getattr__(self, key):
return self.attrs[key]
def __str__(self):
return "Address Check Response: status=%s desc=%s attrs=%s" % \
(self.status_code, self.status_description, self.attrs)
class AddressChecker:
"""
Format address-query messages and parse the responses.
"""
def __init__(self, reply_addr):
self.reply_addr = reply_addr
def parse_address_query_response(self, msg):
ap = msg.properties
return AddressCheckResponse(ap['statusCode'], ap['statusDescription'], msg.body)
def make_address_query(self, name):
ap = {'operation': 'READ', 'type': 'org.apache.qpid.dispatch.router.address', 'name': name}
return Message(properties=ap, reply_to=self.reply_addr)
def make_addresses_query(self):
ap = {'operation': 'QUERY', 'type': 'org.apache.qpid.dispatch.router.address'}
return Message(properties=ap, reply_to=self.reply_addr)
class AddressCheckerTimeout:
def __init__(self, parent):
self.parent = parent
def on_timer_task(self, event):
self.parent.address_check_timeout()
class DistributionSkipMapper:
# 1 means skip that test.
skip = {'test_01': 0,
'test_02': 0,
'test_03': 0,
'test_04': 0,
'test_05': 0,
'test_06': 0,
'test_07': 0,
'test_08': 0,
'test_09': 0,
'test_10': 0,
'test_11': 0,
'test_12': 0,
'test_13': 0,
'test_14': 0,
'test_15': 0,
'test_16': 1,
'test_17': 1,
'test_18': 1,
'test_19': 1,
'test_20': 1,
'test_21': 1,
'test_22': 1,
'test_23': 0,
'test_24': 0,
'test_25': 0
}
# ------------------------------------------------
# END Helper classes for all tests.
# ------------------------------------------------
# ================================================================
# Setup
# ================================================================
class DistributionTests(TestCase):
@classmethod
def setUpClass(cls):
"""
Create a router topology that is a superset of the topologies we will
need for various tests. So far, we have only two types of tests:
3-router linear, and 3-router triangular. The various tests simply
attach their senders and receivers appropriately to 'see' their
desired topology.
"""
super(DistributionTests, cls).setUpClass()
# -----------------------------------------------------
# Here are some chunks of configuration that will be
# the same on all routers.
# -----------------------------------------------------
def router(name, more_config):
config = [('router', {'mode': 'interior', 'id': name}),
('address', {'prefix': 'closest', 'distribution': 'closest'}),
('address', {'prefix': 'balanced', 'distribution': 'balanced'}),
('address', {'prefix': 'multicast', 'distribution': 'multicast'})
] \
+ more_config
config = Qdrouterd.Config(config)
cls.routers.append(cls.tester.qdrouterd(name, config, wait=True))
cls.routers = []
#
# Connection picture
#
# 1 1
# A <-------- B <------ C
# ^ 2 ^ 2
# \ /
# \ /
# \ /
# \ /
# D
#
# Note: in the above picture, an arrow from, i.e., B to A
# means that B initiates the connection from itself to A.
# So if you see "B ----> A" in the picture, you should also
# see a connector block in the configuration of B that
# connects to an inter-router port on A.
#
A_client_port = cls.tester.get_port()
B_client_port = cls.tester.get_port()
C_client_port = cls.tester.get_port()
D_client_port = cls.tester.get_port()
A_inter_router_port_1 = cls.tester.get_port()
A_inter_router_port_2 = cls.tester.get_port()
B_inter_router_port_1 = cls.tester.get_port()
B_inter_router_port_2 = cls.tester.get_port()
# "Route-container port" does not mean that the port
# contains a route. It means that any client that
# connectsd to the port is considered to be a route-
# container.
A_route_container_port = cls.tester.get_port()
B_route_container_port = cls.tester.get_port()
C_route_container_port = cls.tester.get_port()
D_route_container_port = cls.tester.get_port()
# Costs for balanced tests. The 'balanced' distribution
# takes these costs into account in its algorithm.
# Costs are associated not with routers, but with the
# connections between routers. In the config, they may
# be attached to the inter-router listener, or the connector,
# or both. If both the inter-router listener and the
# connector have associated costs, the higher of the two
# will be used.
cls.A_B_cost = 10
cls.B_C_cost = 20
cls.A_D_cost = 50
cls.B_D_cost = 100
router('A',
[
('listener',
{'port': A_client_port,
'role': 'normal',
'stripAnnotations': 'no'
}
),
('listener',
{'role': 'inter-router',
'port': A_inter_router_port_1
}
),
('listener',
{'role': 'inter-router',
'port': A_inter_router_port_2
}
),
('listener',
{'port': A_route_container_port, # route-container is listener number 3
'stripAnnotations': 'no',
'role': 'route-container'
}
)
]
)
router('B',
[
('listener',
{'port': B_client_port,
'role': 'normal',
'stripAnnotations': 'no'
}
),
('listener',
{'role': 'inter-router',
'port': B_inter_router_port_1
}
),
('listener',
{'role': 'inter-router',
'port': B_inter_router_port_2
}
),
('listener',
{'port': B_route_container_port, # route-container is number 3
'stripAnnotations': 'no',
'role': 'route-container'
}
),
('connector',
{'name': 'connectorToA',
'role': 'inter-router',
'port': A_inter_router_port_1,
'cost': cls.A_B_cost
}
)
]
)
router('C',
[
('listener',
{'port': C_client_port,
'role': 'normal',
'stripAnnotations': 'no'
}
),
('listener',
{'port': C_route_container_port, # route-container is number 1
'stripAnnotations': 'no',
'role': 'route-container'
}
),
('connector',
{'name': 'connectorToB',
'role': 'inter-router',
'port': B_inter_router_port_1,
'cost' : cls.B_C_cost
}
)
]
)
router('D',
[
('listener',
{'port': D_client_port,
'role': 'normal',
'stripAnnotations': 'no'
}
),
('listener',
{'port': D_route_container_port, # route-container is number 1
'stripAnnotations': 'no',
'role': 'route-container'
}
),
('connector',
{'name': 'connectorToA',
'role': 'inter-router',
'port': A_inter_router_port_2,
'cost' : cls.A_D_cost
}
),
('connector',
{'name': 'connectorToB',
'role': 'inter-router',
'port': B_inter_router_port_2,
'cost' : cls.B_D_cost
}
)
]
)
router_A = cls.routers[0]
router_B = cls.routers[1]
router_C = cls.routers[2]
router_D = cls.routers[3]
cls.A_route_container_addr = router_A.addresses[3]
cls.B_route_container_addr = router_B.addresses[3]
cls.C_route_container_addr = router_C.addresses[1]
cls.D_route_container_addr = router_D.addresses[1]
router_A.wait_router_connected('B')
router_A.wait_router_connected('C')
router_A.wait_router_connected('D')
cls.A_addr = router_A.addresses[0]
cls.B_addr = router_B.addresses[0]
cls.C_addr = router_C.addresses[0]
cls.D_addr = router_D.addresses[0]
@unittest.skipIf(DistributionSkipMapper.skip['test_01'], 'Test skipped during development.')
def test_01_targeted_sender_AC(self):
name = 'test_01'
test = TargetedSenderTest(name, self.A_addr, self.C_addr, "closest/01")
test.run()
self.assertIsNone(test.error)
@unittest.skipIf(DistributionSkipMapper.skip['test_02'], 'Test skipped during development.')
def test_02_targeted_sender_DC(self):
name = 'test_02'
test = TargetedSenderTest(name, self.D_addr, self.C_addr, "closest/02")
test.run()
self.assertIsNone(test.error)
@unittest.skipIf(DistributionSkipMapper.skip['test_03'], 'Test skipped during development.')
def test_03_anonymous_sender_AC(self):
name = 'test_03'
test = AnonymousSenderTest(name, self.A_addr, self.C_addr)
test.run()
self.assertIsNone(test.error)
@unittest.skipIf(DistributionSkipMapper.skip['test_04'], 'Test skipped during development.')
def test_04_anonymous_sender_DC(self):
name = 'test_04'
test = AnonymousSenderTest(name, self.D_addr, self.C_addr)
test.run()
self.assertIsNone(test.error)
@unittest.skipIf(DistributionSkipMapper.skip['test_05'], 'Test skipped during development.')
def test_05_dynamic_reply_to_AC(self):
name = 'test_05'
test = DynamicReplyTo(name, self.A_addr, self.C_addr)
test.run()
self.assertIsNone(test.error)
@unittest.skipIf(DistributionSkipMapper.skip['test_06'], 'Test skipped during development.')
def test_06_dynamic_reply_to_DC(self):
name = 'test_06'
test = DynamicReplyTo(name, self.D_addr, self.C_addr)
test.run()
self.assertIsNone(test.error)
@unittest.skipIf(DistributionSkipMapper.skip['test_09'], 'Test skipped during development.')
def test_09_closest_linear(self):
name = 'test_09'
test = ClosestTest(name,
self.A_addr,
self.B_addr,
self.C_addr,
"addr_09",
print_debug=False
)
test.run()
self.assertIsNone(test.error)
@unittest.skipIf(DistributionSkipMapper.skip['test_10'], 'Test skipped during development.')
def test_10_closest_mesh(self):
name = 'test_10'
test = ClosestTest(name,
self.A_addr,
self.B_addr,
self.D_addr,
"addr_10"
)
test.run()
self.assertIsNone(test.error)
#
# Cost picture for balanced distribution tests.
#
# 10 20
# A <-------- B <------ C
# ^ ^
# \ /
# 50 \ / 100
# \ /
# \ /
# D
#
#
#
# Here is how the message balancing should work for
# various total number of messages, up to 100:
#
# NOTE: remember these messages are all unsettled.
# And will stay that way. This is not a realistic
# usage scenario, but it the best way to test the
# balanced distribution algorithm.
#
# 1. Messages start flowing in at A. They will all
# be used by A (sent to its receiver) until the
# total == cost ( A, B ).
#
# 2. At that point, A will start sharing with B,
# one-for-me-one-for-you. (So A will go to 11 before
# B gets its first message.)
#
# 3. A and B will count up until B reaches
# cost ( B, C )
# B will then start sharings its messages with C,
# one-for-me-one-for-you. (So B will go to 21 before
# C gets its first message.)
#
# 4. However note: it is NOT round-robin at this point.
# A is still taking every other message, B is only getting
# A's overflow, and now B is sharing half of that with C.
# So at this point B will start falling farther behind A.
#
# 5. The totals here are completely deterministic, so we pass
# to the test a 'slop' amount of 0.
#
# total near --10--> mid ---20--> far
#
# 1 1 0 0
# 10 10 0 0
# 11 11 0 0
# 12 11 1 0
# 13 12 1 0
# 14 12 2 0
# ...
# 50 30 20 0
# 51 31 20 0
# 52 31 21 0
# 53 32 21 0
# 54 32 21 1
# 55 33 21 1
# 56 33 22 1
# 57 34 22 1
# 58 34 22 2
# 59 35 22 2
# 60 35 23 2
# ...
# 100 55 33 12
#
@unittest.skipIf(DistributionSkipMapper.skip['test_11'], 'Test skipped during development.')
def test_11_balanced_linear(self):
name = 'test_11'
# slop is how much the second two values may diverge from
# the expected. But they still must sum to total - A.
total = 100
expected_A = 55
expected_B = 33
expected_C = 12
# FIXME - or investigate -- I believe this slop
# should not be necessary -- the distribution
# algorithm should be perfectly deterministic.
# But -- without it, I am getting 0.3% failure rate on this test.
slop = 1
omit_middle_receiver = False
test = BalancedTest(name,
self.A_addr,
self.B_addr,
self.C_addr,
"addr_11",
total,
expected_A,
expected_B,
expected_C,
slop,
omit_middle_receiver
)
test.run()
self.assertIsNone(test.error)
@unittest.skipIf(DistributionSkipMapper.skip['test_12'], 'Test skipped during development.')
def test_12_balanced_linear_omit_middle_receiver(self):
name = 'test_12'
# If we omit the middle receiver, then router A will count
# up to cost ( A, B ) and the keep counting up a further
# cost ( B, C ) before it starts to spill over.
# That is, it will count up to
# cost ( A, B ) + cost ( B, C ) == 30
# After that it will start sharing downstream (router C)
# one-for-me-one-for-you. So when the number of total messages
# is odd, A will be 31 ahead of C. When total message count is
# even, A will be 30 ahead.
# As in the other linear scenario, there is no 'slop' here.
total = 100
expected_A = 65
expected_B = 0
expected_C = 35
# FIXME - or investigate -- I believe this slop
# should not be necessary -- the distribution
# algorithm should be perfectly deterministic.
# But -- without it, I am getting 0.2% failure rate on this test.
slop = 1
omit_middle_receiver = True
test = BalancedTest(name,
self.A_addr,
self.B_addr,
self.C_addr,
"addr_12",
total,
expected_A,
expected_B,
expected_C,
slop,
omit_middle_receiver
)
test.run()
self.assertIsNone(test.error)
# Reasoning for the triangular balanced case:
#
# Cost picture
#
# 10 20
# A <-------- B <------ C
# ^ ^
# \ /
# 50 \ / 100
# \ /
# \ /
# D
#
# We are doing ( A, B, D ), with the sender attached at A.
# All these messages are unsettled, which is what allows us to
# see how the balanced distribution algorithm works.
#
# 1. total unsettled msgs at A cannot be more than B_cost + 1,
# and also cannot be more than D_cost + 1
#
# 2. A will always keep the message for itself (for its own receiver)
# if it can do so without violating rule (1).
#
# 3. So, A will count up to 11, and then it will start alternating
# with B.
#
# 4. When A counts up to 51, it must also start sharing with D.
# It will alternate between B and D.
#
# 5. As long as B does not yet have 100 messages, it will not
# share with D.
#
# 6. So! at 100 messages total, A must be above both of its
# neighbors by that neighbor's cost, or 1 more -- and the total
# of all 3 must sum to 100.
#
# A = B + 10 B = A - 10
# A = D + 50 D = A - 50
# A + B + D == 100
# -->
# A + (A - 10) + (A - 50) == 100
# 3A - 60 == 100
# A == 53.333...
# A == 54
#
# so B + D == 46
# A is 10 or 11 > B --> B == 44 or 43
# A is 50 or 51 > D --> D == 4 or 3
# B == 43 and D == 3
#
# So pass these values in to the test: (54, 43, 3)
# and test that:
# 1. A is exactly that value.
# 2. B and D sum to 100 - A
# 3. B and D are both with 1 of their expected values.
#
@unittest.skipIf(DistributionSkipMapper.skip['test_13'], 'Test skipped during development.')
def test_13_balanced_mesh(self):
name = 'test_13'
total = 100
expected_A = 54
expected_B = 43
expected_D = 3
slop = 1
omit_middle_receiver = False
test = BalancedTest(name,
self.A_addr,
self.B_addr,
self.D_addr,
"addr_13",
total,
expected_A,
expected_B,
expected_D,
slop,
omit_middle_receiver
)
test.run()
self.assertIsNone(test.error)
@unittest.skipIf(DistributionSkipMapper.skip['test_14'], 'Test skipped during development.')
def test_14_multicast_linear(self):
name = 'test_14'
test = MulticastTest(name,
self.A_addr,
self.B_addr,
self.C_addr,
"addr_14"
)
test.run()
self.assertIsNone(test.error)
@unittest.skipIf(DistributionSkipMapper.skip['test_15'], 'Test skipped during development.')
def test_15_multicast_mesh(self):
name = 'test_15'
test = MulticastTest(name,
self.A_addr,
self.B_addr,
self.D_addr,
"addr_15"
)
test.run()
self.assertIsNone(test.error)
# ================================================================
# Tests
# ================================================================
next_link_sequence = 1
def link_name(suffix=None):
global next_link_sequence
suffix = suffix or "name"
name = "link-%s.%d" % (suffix, next_link_sequence)
next_link_sequence += 1
return name
class TargetedSenderTest (MessagingHandler):
"""
A 'targeted' sender is one in which we tell the router what
address we want to send to. (As opposed to letting the router
pass back an address to us.)
"""
def __init__(self, test_name, send_addr, recv_addr, destination):
super(TargetedSenderTest, self).__init__(prefetch=0)
self.send_addr = send_addr
self.recv_addr = recv_addr
self.dest = destination
self.error = None
self.sender = None
self.receiver = None
self.n_expected = 10
self.n_sent = 0
self.n_received = 0
self.n_accepted = 0
self.test_name = test_name
def timeout(self):
self.error = "Timeout Expired: n_sent=%d n_received=%d n_accepted=%d" % \
(self.n_sent, self.n_received, self.n_accepted)
self.send_conn.close()
self.recv_conn.close()
def on_start(self, event):
self.timer = event.reactor.schedule(TIMEOUT, TestTimeout(self))
self.send_conn = event.container.connect(self.send_addr)
self.recv_conn = event.container.connect(self.recv_addr)
self.sender = event.container.create_sender(self.send_conn, self.dest)
self.receiver = event.container.create_receiver(self.recv_conn, self.dest)
self.receiver.flow(self.n_expected)
def send(self):
while self.sender.credit > 0 and self.n_sent < self.n_expected:
msg = Message(body=self.n_sent)
self.sender.send(msg)
self.n_sent += 1
def on_sendable(self, event):
if self.n_sent < self.n_expected:
self.send()
def on_accepted(self, event):
self.n_accepted += 1
def on_message(self, event):
self.n_received += 1
if self.n_received == self.n_expected:
self.receiver.close()
self.send_conn.close()
self.recv_conn.close()
self.timer.cancel()
def run(self):
Container(self).run()
class DynamicTarget(LinkOption):
def apply(self, link):
link.target.dynamic = True
link.target.address = None
class AnonymousSenderTest (MessagingHandler):
"""
An 'anonymous' sender is one in which we let the router tell
us what address the sender should use. It will pass back this
information to us when we get the on_link_opened event.
"""
def __init__(self, test_name, send_addr, recv_addr):
super(AnonymousSenderTest, self).__init__()
self.send_addr = send_addr
self.recv_addr = recv_addr
self.error = None
self.recv_conn = None
self.send_conn = None
self.sender = None
self.receiver = None
self.address = None
self.expected = 10
self.n_sent = 0
self.n_received = 0
self.n_accepted = 0
self.test_name = test_name
def timeout(self):
self.error = "Timeout Expired: n_sent=%d n_received=%d n_accepted=%d" % \
(self.n_sent, self.n_received, self.n_accepted)
self.send_conn.close()
self.recv_conn.close()
def on_start(self, event):
self.timer = event.reactor.schedule(TIMEOUT, TestTimeout(self))
self.send_conn = event.container.connect(self.send_addr)
self.recv_conn = event.container.connect(self.recv_addr)
self.sender = event.container.create_sender(self.send_conn, options=DynamicTarget())
def send(self):
while self.sender.credit > 0 and self.n_sent < self.expected:
self.n_sent += 1
m = Message(address=self.address, body="Message %d of %d" % (self.n_sent, self.expected))
self.sender.send(m)
def on_link_opened(self, event):
if event.sender == self.sender:
# Here we are told the address that we will use for the sender.
self.address = self.sender.remote_target.address
self.receiver = event.container.create_receiver(self.recv_conn, self.address)
def on_sendable(self, event):
self.send()
def on_message(self, event):
if event.receiver == self.receiver:
self.n_received += 1
def on_accepted(self, event):
self.n_accepted += 1
if self.n_accepted == self.expected:
self.send_conn.close()
self.recv_conn.close()
self.timer.cancel()
def run(self):
Container(self).run()
# =======================================================================
# =======================================================================
class DynamicReplyTo(MessagingHandler):
"""
In this test we have a separate 'client' and 'server' with separate
connections. The client sends requests to the server, and embeds in
them its desired reply-to address. The server uses that address to
send back messages. The tests ends with success if the client receives
the expected number of replies, or with failure if we time out before
that happens.
"""
def __init__(self, test_name, client_addr, server_addr):
super(DynamicReplyTo, self).__init__(prefetch=10)
self.client_addr = client_addr
self.server_addr = server_addr
self.dest = "closest.dynamicRequestResponse"
self.error = None
self.server_receiver = None
self.client_receiver = None
self.client_sender = None
self.server_sender = None
self.n_expected = 10
self.n_sent = 0
self.received_by_server = 0
self.received_by_client = 0
self.test_name = test_name
self.server_receiver_ready = False
self.client_receiver_ready = False
self.reply_to_addr = None
self.senders_created = False
self.addr_check_timer = None
self.addr_check_sender = None
self.container = None
self.num_attempts = 0
self.addr_check_receiver = None
def timeout(self):
self.error = "Timeout Expired: n_sent=%d received_by_server=%d received_by_client=%d" % \
(self.n_sent, self.received_by_server, self.received_by_client)
self.client_connection.close()
self.server_connection.close()
def address_check_timeout(self):
self.addr_check_sender.send(self.addr_checker.make_address_query("M" + self.dest))
def bail(self):
self.timer.cancel()
self.server_receiver.close()
self.client_receiver.close()
self.addr_check_sender.close()
self.addr_check_receiver.close()
self.server_sender.close()
self.client_sender.close()
self.client_connection.close()
self.server_connection.close()
def on_start(self, event):
self.timer = event.reactor.schedule(TIMEOUT, TestTimeout(self))
# separate connections to simulate client and server.
self.client_connection = event.container.connect(self.client_addr)
self.server_connection = event.container.connect(self.server_addr)
self.server_receiver = event.container.create_receiver(self.server_connection, self.dest)
self.client_receiver = event.container.create_receiver(self.client_connection, None, dynamic=True)
self.addr_check_sender = event.container.create_sender(self.client_connection, "$management")
self.container = event.container
self.addr_check_receiver = event.container.create_receiver(self.client_connection, dynamic=True)
def create_senders(self):
if not self.senders_created:
self.senders_created = True
self.client_sender = self.container.create_sender(self.client_connection, self.dest)
self.server_sender = self.container.create_sender(self.server_connection, None)
def on_link_opened(self, event):
if event.receiver == self.addr_check_receiver:
self.addr_checker = AddressChecker(self.addr_check_receiver.remote_source.address)
if not self.server_receiver_ready and event.receiver == self.server_receiver:
self.server_receiver_ready = True
if not self.client_receiver_ready and event.receiver == self.client_receiver:
self.client_receiver_ready = True
if self.server_receiver_ready and self.client_receiver_ready:
if self.num_attempts == 0:
self.reply_to_addr = self.client_receiver.remote_source.address
self.num_attempts += 1
self.addr_check_timer = event.reactor.schedule(3, AddressCheckerTimeout(self))
def on_sendable(self, event):
if self.reply_to_addr is None:
return
if event.sender == self.client_sender:
while event.sender.credit > 0 and self.n_sent < self.n_expected:
# We send to server, and tell it how to reply to the client.
request = Message(body=self.n_sent,
address=self.dest,
reply_to=self.reply_to_addr)
event.sender.send(request)
self.n_sent += 1
def on_message(self, event):
if event.receiver == self.addr_check_receiver:
response = self.addr_checker.parse_address_query_response(event.message)
# Create the senders if the address has propagated.
if response.status_code == 200 and response.remoteCount == 1:
self.create_senders()
else:
if self.num_attempts < 2:
self.num_attempts += 1
self.addr_check_timer = event.reactor.schedule(3, AddressCheckerTimeout(self))
else:
self.error = "Address %s did not propagate to the router to which the sender is attached" % self.dest
self.bail()
return
# Server gets a request and responds to
# the address that is embedded in the message.
if event.receiver == self.server_receiver :
self.server_sender.send(Message(address=event.message.reply_to,
body="Reply hazy, try again later."))
self.received_by_server += 1
# Client gets a response and counts it.
elif event.receiver == self.client_receiver :
self.received_by_client += 1
if self.received_by_client == self.n_expected:
self.bail()
def run(self):
Container(self).run()
class ClosestTest (MessagingHandler):
"""
Test whether distance-based message routing works in a 3-router
network. The network may be linear or mesh, depending on which routers the
caller gives us.
(Illustration is a linear network.)
sender -----> Router_1 -----> Router_2 -----> Router_3
| | |
v v v
rcvr_1 rcvr_2 rcvr_3
With a linear network of 3 routers, set up a sender on router_1, and then 1
receiver each on all 3 routers. Requirement: router 2 is closer than
router 3 by one hop.
Once the closest pair of receivers has received the required amount of
messages they are closed. Neither of the other receivers should have
received any messages, as they were not the closest receivers.
Repeat until all three routers have received messages.
The test is set up in phases to ensure there are no races between fast/slow
clients and routers:
Phase 1: bring up all connections and create receivers, wait until all
receivers can finished link setup.
Phase 2: poll routers until the subscriber count shows all receivers are
ready.
Phase 3: start the sender, wait until on_sendable triggers
Phase 4: send test messages, verify distribution.
Once a batch of messages has completely arrived at the current closest
receiver, close that receiver. Note that there can be a few seconds before
that loss of link propagates to all three routers. During that time any
sent messages may fail with outcome RELEASED - that is expected.
"""
def __init__(self, test_name, router_1, router_2, router_3, addr_suffix,
print_debug=False):
super(ClosestTest, self).__init__(prefetch=0)
self.test_name = test_name
self.error = None
self.router_1 = router_1
self.router_2 = router_2
self.router_3 = router_3
self.addr_suffix = addr_suffix
self.dest = "closest/" + addr_suffix
# after send_batch sent messages are accepted, verify the closest
# receivers have consumed the batch.
self.send_batch = 4
self.n_sent = 0
self.n_received = 0
self.sender = None
self.rx_opened = 0
self.rx_count_1 = 0
self.rx_count_2 = 0
self.rx_count_3 = 0
self.closest_rx = None
# for checking the number of subscribers to the destination address
self.addr_checker = None
self.addr_check_receiver = None
self.addr_check_sender = None