-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathtest_routing.py
More file actions
660 lines (528 loc) · 25.8 KB
/
test_routing.py
File metadata and controls
660 lines (528 loc) · 25.8 KB
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Copyright (c) 2002-2016 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# Licensed 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 mock import patch
from threading import Semaphore, Thread
from time import sleep
from unittest import TestCase
from neo4j.v1 import basic_auth, connect
from neo4j.v1.exceptions import ProtocolError, ServiceUnavailable
from neo4j.v1.routing import RoundRobinSet, RoutingTable, RoutingConnectionPool
from test.util import ServerTestCase, StubCluster
VALID_ROUTING_RECORD = {
"ttl": 300,
"servers": [
{"role": "ROUTE", "addresses": ["127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"]},
{"role": "READ", "addresses": ["127.0.0.1:9004", "127.0.0.1:9005"]},
{"role": "WRITE", "addresses": ["127.0.0.1:9006"]},
],
}
VALID_ROUTING_RECORD_WITH_EXTRA_ROLE = {
"ttl": 300,
"servers": [
{"role": "ROUTE", "addresses": ["127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"]},
{"role": "READ", "addresses": ["127.0.0.1:9004", "127.0.0.1:9005"]},
{"role": "WRITE", "addresses": ["127.0.0.1:9006"]},
{"role": "MAGIC", "addresses": ["127.0.0.1:9007"]},
],
}
INVALID_ROUTING_RECORD = {
"X": 1,
}
def connector(address):
return connect(address, auth=basic_auth("neotest", "neotest"))
class RoundRobinSetTestCase(TestCase):
def test_should_repr_as_set(self):
rrs = RoundRobinSet([1, 2, 3])
assert repr(rrs) == "{1, 2, 3}"
def test_should_contain_element(self):
rrs = RoundRobinSet([1, 2, 3])
assert 2 in rrs
def test_should_not_contain_non_element(self):
rrs = RoundRobinSet([1, 2, 3])
assert 4 not in rrs
def test_should_be_able_to_get_next_if_empty(self):
rrs = RoundRobinSet([])
assert next(rrs) is None
def test_should_be_able_to_get_next_repeatedly(self):
rrs = RoundRobinSet([1, 2, 3])
assert next(rrs) == 1
assert next(rrs) == 2
assert next(rrs) == 3
assert next(rrs) == 1
def test_should_be_able_to_get_next_repeatedly_via_old_method(self):
rrs = RoundRobinSet([1, 2, 3])
assert rrs.next() == 1
assert rrs.next() == 2
assert rrs.next() == 3
assert rrs.next() == 1
def test_should_be_iterable(self):
rrs = RoundRobinSet([1, 2, 3])
assert list(iter(rrs)) == [1, 2, 3]
def test_should_have_length(self):
rrs = RoundRobinSet([1, 2, 3])
assert len(rrs) == 3
def test_should_be_able_to_add_new(self):
rrs = RoundRobinSet([1, 2, 3])
rrs.add(4)
assert list(rrs) == [1, 2, 3, 4]
def test_should_be_able_to_add_existing(self):
rrs = RoundRobinSet([1, 2, 3])
rrs.add(2)
assert list(rrs) == [1, 2, 3]
def test_should_be_able_to_clear(self):
rrs = RoundRobinSet([1, 2, 3])
rrs.clear()
assert list(rrs) == []
def test_should_be_able_to_discard_existing(self):
rrs = RoundRobinSet([1, 2, 3])
rrs.discard(2)
assert list(rrs) == [1, 3]
def test_should_be_able_to_discard_non_existing(self):
rrs = RoundRobinSet([1, 2, 3])
rrs.discard(4)
assert list(rrs) == [1, 2, 3]
def test_should_be_able_to_remove_existing(self):
rrs = RoundRobinSet([1, 2, 3])
rrs.remove(2)
assert list(rrs) == [1, 3]
def test_should_not_be_able_to_remove_non_existing(self):
rrs = RoundRobinSet([1, 2, 3])
with self.assertRaises(ValueError):
rrs.remove(4)
def test_should_be_able_to_update(self):
rrs = RoundRobinSet([1, 2, 3])
rrs.update([3, 4, 5])
assert list(rrs) == [1, 2, 3, 4, 5]
def test_should_be_able_to_replace(self):
rrs = RoundRobinSet([1, 2, 3])
rrs.replace([3, 4, 5])
assert list(rrs) == [3, 4, 5]
class RoutingTableConstructionTestCase(TestCase):
def test_should_be_initially_stale(self):
table = RoutingTable()
assert not table.is_fresh()
class RoutingTableParseAddressTestCase(ServerTestCase):
def test_should_parse_ip_address_and_port(self):
parsed = RoutingTable.parse_address("127.0.0.1:7687")
assert parsed == ("127.0.0.1", 7687)
def test_should_parse_host_name_and_port(self):
parsed = RoutingTable.parse_address("localhost:7687")
assert parsed == ("localhost", 7687)
def test_should_fail_on_missing_port(self):
with self.assertRaises(ValueError):
_ = RoutingTable.parse_address("127.0.0.1")
def test_should_fail_on_empty_port(self):
with self.assertRaises(ValueError):
_ = RoutingTable.parse_address("127.0.0.1:")
def test_should_fail_on_non_numeric_port(self):
with self.assertRaises(ValueError):
_ = RoutingTable.parse_address("127.0.0.1:X")
class RoutingTableParseRoutingInfoTestCase(ServerTestCase):
def test_should_return_routing_table_on_valid_record(self):
table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD])
assert table.routers == {('127.0.0.1', 9001), ('127.0.0.1', 9002), ('127.0.0.1', 9003)}
assert table.readers == {('127.0.0.1', 9004), ('127.0.0.1', 9005)}
assert table.writers == {('127.0.0.1', 9006)}
assert table.ttl == 300
def test_should_return_routing_table_on_valid_record_with_extra_role(self):
table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD_WITH_EXTRA_ROLE])
assert table.routers == {('127.0.0.1', 9001), ('127.0.0.1', 9002), ('127.0.0.1', 9003)}
assert table.readers == {('127.0.0.1', 9004), ('127.0.0.1', 9005)}
assert table.writers == {('127.0.0.1', 9006)}
assert table.ttl == 300
def test_should_fail_on_invalid_record(self):
with self.assertRaises(ProtocolError):
_ = RoutingTable.parse_routing_info([INVALID_ROUTING_RECORD])
def test_should_fail_on_zero_records(self):
with self.assertRaises(ProtocolError):
_ = RoutingTable.parse_routing_info([])
def test_should_fail_on_multiple_records(self):
with self.assertRaises(ProtocolError):
_ = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD, VALID_ROUTING_RECORD])
class RoutingTableFreshnessTestCase(TestCase):
VALID_ROUTING_RECORD = {
"ttl": 300,
"servers": [
{"role": "ROUTE", "addresses": ["127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"]},
{"role": "READ", "addresses": ["127.0.0.1:9004", "127.0.0.1:9005"]},
{"role": "WRITE", "addresses": ["127.0.0.1:9006"]},
],
}
def test_should_be_fresh_after_update(self):
table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD])
assert table.is_fresh()
def test_should_become_stale_on_expiry(self):
table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD])
table.ttl = 0
assert not table.is_fresh()
def test_should_become_stale_if_no_readers(self):
table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD])
table.readers.clear()
assert not table.is_fresh()
def test_should_become_stale_if_no_writers(self):
table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD])
table.writers.clear()
assert not table.is_fresh()
class RoutingTableUpdateTestCase(TestCase):
def setUp(self):
self.table = RoutingTable(
[("192.168.1.1", 7687), ("192.168.1.2", 7687)], [("192.168.1.3", 7687)], [], 0)
self.new_table = RoutingTable(
[("127.0.0.1", 9001), ("127.0.0.1", 9002), ("127.0.0.1", 9003)],
[("127.0.0.1", 9004), ("127.0.0.1", 9005)], [("127.0.0.1", 9006)], 300)
def test_update_should_replace_routers(self):
self.table.update(self.new_table)
assert self.table.routers == {("127.0.0.1", 9001), ("127.0.0.1", 9002), ("127.0.0.1", 9003)}
def test_update_should_replace_readers(self):
self.table.update(self.new_table)
assert self.table.readers == {("127.0.0.1", 9004), ("127.0.0.1", 9005)}
def test_update_should_replace_writers(self):
self.table.update(self.new_table)
assert self.table.writers == {("127.0.0.1", 9006)}
def test_update_should_replace_ttl(self):
self.table.update(self.new_table)
assert self.table.ttl == 300
class RoutingConnectionPoolConstructionTestCase(ServerTestCase):
def test_should_populate_initial_router(self):
with RoutingConnectionPool(connector, ("127.0.0.1", 9001)) as pool:
assert pool.routing_table.routers == {("127.0.0.1", 9001)}
class RoutingConnectionPoolFetchRoutingInfoTestCase(ServerTestCase):
def test_should_get_info_from_router(self):
with StubCluster({9001: "router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector) as pool:
result = pool.fetch_routing_info(address)
assert len(result) == 1
record = result[0]
assert record["ttl"] == 300
assert record["servers"] == [
{"role": "ROUTE", "addresses": ["127.0.0.1:9001", "127.0.0.1:9002",
"127.0.0.1:9003"]},
{"role": "READ", "addresses": ["127.0.0.1:9004", "127.0.0.1:9005"]},
{"role": "WRITE", "addresses": ["127.0.0.1:9006"]},
]
def test_should_remove_router_if_cannot_connect(self):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
assert address in pool.routing_table.routers
_ = pool.fetch_routing_info(address)
assert address not in pool.routing_table.routers
def test_should_remove_router_if_connection_drops(self):
with StubCluster({9001: "rude_router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
assert address in pool.routing_table.routers
_ = pool.fetch_routing_info(address)
assert address not in pool.routing_table.routers
def test_should_not_fail_if_cannot_connect_but_router_already_removed(self):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector) as pool:
assert address not in pool.routing_table.routers
_ = pool.fetch_routing_info(address)
assert address not in pool.routing_table.routers
def test_should_not_fail_if_connection_drops_but_router_already_removed(self):
with StubCluster({9001: "rude_router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector) as pool:
assert address not in pool.routing_table.routers
_ = pool.fetch_routing_info(address)
assert address not in pool.routing_table.routers
def test_should_return_none_if_cannot_connect(self):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
result = pool.fetch_routing_info(address)
assert result is None
def test_should_return_none_if_connection_drops(self):
with StubCluster({9001: "rude_router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
result = pool.fetch_routing_info(address)
assert result is None
def test_should_fail_for_non_router(self):
with StubCluster({9001: "non_router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
with self.assertRaises(ServiceUnavailable):
_ = pool.fetch_routing_info(address)
def test_should_fail_if_database_error(self):
with StubCluster({9001: "broken_router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
with self.assertRaises(ServiceUnavailable):
_ = pool.fetch_routing_info(address)
class RoutingConnectionPoolFetchRoutingTableTestCase(ServerTestCase):
def test_should_get_table_from_router(self):
with StubCluster({9001: "router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector) as pool:
table = pool.fetch_routing_table(address)
assert table.routers == {("127.0.0.1", 9001), ("127.0.0.1", 9002),
("127.0.0.1", 9003)}
assert table.readers == {("127.0.0.1", 9004), ("127.0.0.1", 9005)}
assert table.writers == {("127.0.0.1", 9006)}
assert table.ttl == 300
def test_null_info_should_return_null_table(self):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector) as pool:
table = pool.fetch_routing_table(address)
assert table is None
def test_no_routers_should_raise_protocol_error(self):
with StubCluster({9001: "router_no_routers.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector) as pool:
with self.assertRaises(ProtocolError):
_ = pool.fetch_routing_table(address)
def test_no_readers_should_raise_protocol_error(self):
with StubCluster({9001: "router_no_readers.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector) as pool:
with self.assertRaises(ProtocolError):
_ = pool.fetch_routing_table(address)
def test_no_writers_and_one_router_should_raise_signal_service_unavailable(self):
with StubCluster({9001: "router_no_writers_one_router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector) as pool:
with self.assertRaises(ServiceUnavailable):
_ = pool.fetch_routing_table(address)
def test_no_writers_and_multiple_routers_should_return_null_table(self):
with StubCluster({9001: "router_no_writers.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector) as pool:
table = pool.fetch_routing_table(address)
assert table is None
class RoutingConnectionPoolUpdateRoutingTableTestCase(ServerTestCase):
scenarios = {
(None,): ServiceUnavailable,
(RoutingTable,): RoutingTable,
(ServiceUnavailable,): ServiceUnavailable,
(None, None): ServiceUnavailable,
(None, RoutingTable): RoutingTable,
(None, ServiceUnavailable): ServiceUnavailable,
(None, None, None): ServiceUnavailable,
(None, None, RoutingTable): RoutingTable,
(None, None, ServiceUnavailable): ServiceUnavailable,
}
def test_update_with_no_routers_should_signal_service_unavailable(self):
with RoutingConnectionPool(connector) as pool:
with self.assertRaises(ServiceUnavailable):
pool.update_routing_table()
def test_update_scenarios(self):
for server_outcomes, overall_outcome in self.scenarios.items():
self._test_server_outcome(server_outcomes, overall_outcome)
def _test_server_outcome(self, server_outcomes, overall_outcome):
print("%r -> %r" % (server_outcomes, overall_outcome))
servers = {}
routers = []
for port, outcome in enumerate(server_outcomes, 9001):
if outcome is None:
servers[port] = "router_no_writers.script"
elif outcome is RoutingTable:
servers[port] = "router.script"
elif outcome is ServiceUnavailable:
servers[port] = "non_router.script"
else:
assert False, "Unexpected server outcome %r" % outcome
routers.append(("127.0.0.1", port))
with StubCluster(servers):
with RoutingConnectionPool(connector, *routers) as pool:
if overall_outcome is RoutingTable:
pool.update_routing_table()
table = pool.routing_table
assert table.routers == {("127.0.0.1", 9001), ("127.0.0.1", 9002),
("127.0.0.1", 9003)}
assert table.readers == {("127.0.0.1", 9004), ("127.0.0.1", 9005)}
assert table.writers == {("127.0.0.1", 9006)}
assert table.ttl == 300
elif overall_outcome is ServiceUnavailable:
with self.assertRaises(ServiceUnavailable):
pool.update_routing_table()
else:
assert False, "Unexpected overall outcome %r" % overall_outcome
class RoutingConnectionPoolRefreshRoutingTableTestCase(ServerTestCase):
def test_should_update_if_stale(self):
with StubCluster({9001: "router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
first_updated_time = pool.routing_table.last_updated_time
pool.routing_table.ttl = 0
pool.refresh_routing_table()
second_updated_time = pool.routing_table.last_updated_time
assert second_updated_time != first_updated_time
def test_should_not_update_if_fresh(self):
with StubCluster({9001: "router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
pool.refresh_routing_table()
first_updated_time = pool.routing_table.last_updated_time
pool.refresh_routing_table()
second_updated_time = pool.routing_table.last_updated_time
assert second_updated_time == first_updated_time
def test_concurrent_refreshes_should_not_block_if_fresh(self):
address = ("127.0.0.1", 9001)
table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD])
with RoutingConnectionPool(connector, address) as pool:
semaphore = Semaphore()
class Refresher(Thread):
refreshed = None
def run(self):
self.refreshed = pool.refresh_routing_table()
class BlockingRefresher(Refresher):
@classmethod
def blocking_update(cls):
pool.routing_table.update(table)
semaphore.acquire()
semaphore.release()
return table
def run(self):
with patch.object(RoutingConnectionPool, "update_routing_table",
side_effect=self.blocking_update):
super(BlockingRefresher, self).run()
first = BlockingRefresher()
second = Refresher()
assert not pool.routing_table.is_fresh()
semaphore.acquire()
first.start()
second.start()
sleep(1)
assert not second.is_alive() # second call should return immediately without blocking
second.join()
semaphore.release()
first.join()
assert first.refreshed
assert not second.refreshed
assert pool.routing_table.is_fresh()
def test_concurrent_refreshes_should_block_if_stale(self):
address = ("127.0.0.1", 9001)
table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD])
with RoutingConnectionPool(connector, address) as pool:
semaphore = Semaphore()
class Refresher(Thread):
refreshed = None
def run(self):
self.refreshed = pool.refresh_routing_table()
class BlockingRefresher(Refresher):
@classmethod
def blocking_update(cls):
semaphore.acquire()
semaphore.release()
pool.routing_table.update(table)
return table
def run(self):
with patch.object(RoutingConnectionPool, "update_routing_table",
side_effect=self.blocking_update):
super(BlockingRefresher, self).run()
first = BlockingRefresher()
second = Refresher()
assert not pool.routing_table.is_fresh()
semaphore.acquire()
first.start()
second.start()
sleep(1)
assert second.is_alive() # second call should block
semaphore.release()
second.join()
first.join()
assert first.refreshed
assert not second.refreshed
assert pool.routing_table.is_fresh()
class RoutingConnectionPoolAcquireForReadTestCase(ServerTestCase):
def test_should_refresh(self):
with StubCluster({9001: "router.script", 9004: "empty.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
assert not pool.routing_table.is_fresh()
_ = pool.acquire_for_read()
assert pool.routing_table.is_fresh()
def test_connected_to_reader(self):
with StubCluster({9001: "router.script", 9004: "empty.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
assert not pool.routing_table.is_fresh()
connection = pool.acquire_for_read()
assert connection.server.address in pool.routing_table.readers
def test_should_retry_if_first_reader_fails(self):
with StubCluster({9001: "router.script",
9004: "fail_on_init.script",
9005: "empty.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
assert not pool.routing_table.is_fresh()
_ = pool.acquire_for_read()
assert ("127.0.0.1", 9004) not in pool.routing_table.readers
assert ("127.0.0.1", 9005) in pool.routing_table.readers
class RoutingConnectionPoolAcquireForWriteTestCase(ServerTestCase):
def test_should_refresh(self):
with StubCluster({9001: "router.script", 9006: "empty.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
assert not pool.routing_table.is_fresh()
_ = pool.acquire_for_write()
assert pool.routing_table.is_fresh()
def test_connected_to_writer(self):
with StubCluster({9001: "router.script", 9006: "empty.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
assert not pool.routing_table.is_fresh()
connection = pool.acquire_for_write()
assert connection.server.address in pool.routing_table.writers
def test_should_retry_if_first_writer_fails(self):
with StubCluster({9001: "router_with_multiple_writers.script",
9006: "fail_on_init.script",
9007: "empty.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
assert not pool.routing_table.is_fresh()
_ = pool.acquire_for_write()
assert ("127.0.0.1", 9006) not in pool.routing_table.writers
assert ("127.0.0.1", 9007) in pool.routing_table.writers
class RoutingConnectionPoolRemoveTestCase(ServerTestCase):
def test_should_remove_router_from_routing_table_if_present(self):
with StubCluster({9001: "router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
pool.refresh_routing_table()
target = ("127.0.0.1", 9001)
assert target in pool.routing_table.routers
pool.remove(target)
assert target not in pool.routing_table.routers
def test_should_remove_reader_from_routing_table_if_present(self):
with StubCluster({9001: "router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
pool.refresh_routing_table()
target = ("127.0.0.1", 9004)
assert target in pool.routing_table.readers
pool.remove(target)
assert target not in pool.routing_table.readers
def test_should_remove_writer_from_routing_table_if_present(self):
with StubCluster({9001: "router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
pool.refresh_routing_table()
target = ("127.0.0.1", 9006)
assert target in pool.routing_table.writers
pool.remove(target)
assert target not in pool.routing_table.writers
def test_should_not_fail_if_absent(self):
with StubCluster({9001: "router.script"}):
address = ("127.0.0.1", 9001)
with RoutingConnectionPool(connector, address) as pool:
pool.refresh_routing_table()
target = ("127.0.0.1", 9007)
pool.remove(target)