48
48
"X" : 1 ,
49
49
}
50
50
51
+ UNREACHABLE_ADDRESS = ("127.0.0.1" , 8080 )
51
52
52
53
def connector (address ):
53
54
return connect (address , auth = basic_auth ("neotest" , "neotest" ))
@@ -58,7 +59,7 @@ class RoutingConnectionPoolFetchRoutingInfoTestCase(StubTestCase):
58
59
def test_should_get_info_from_router (self ):
59
60
with StubCluster ({9001 : "router.script" }):
60
61
address = ("127.0.0.1" , 9001 )
61
- with RoutingConnectionPool (connector ) as pool :
62
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS ) as pool :
62
63
result = pool .fetch_routing_info (address )
63
64
assert len (result ) == 1
64
65
record = result [0 ]
@@ -72,58 +73,58 @@ def test_should_get_info_from_router(self):
72
73
73
74
def test_should_remove_router_if_cannot_connect (self ):
74
75
address = ("127.0.0.1" , 9001 )
75
- with RoutingConnectionPool (connector , address ) as pool :
76
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
76
77
assert address in pool .routing_table .routers
77
78
_ = pool .fetch_routing_info (address )
78
79
assert address not in pool .routing_table .routers
79
80
80
81
def test_should_remove_router_if_connection_drops (self ):
81
82
with StubCluster ({9001 : "rude_router.script" }):
82
83
address = ("127.0.0.1" , 9001 )
83
- with RoutingConnectionPool (connector , address ) as pool :
84
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
84
85
assert address in pool .routing_table .routers
85
86
_ = pool .fetch_routing_info (address )
86
87
assert address not in pool .routing_table .routers
87
88
88
89
def test_should_not_fail_if_cannot_connect_but_router_already_removed (self ):
89
90
address = ("127.0.0.1" , 9001 )
90
- with RoutingConnectionPool (connector ) as pool :
91
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS ) as pool :
91
92
assert address not in pool .routing_table .routers
92
93
_ = pool .fetch_routing_info (address )
93
94
assert address not in pool .routing_table .routers
94
95
95
96
def test_should_not_fail_if_connection_drops_but_router_already_removed (self ):
96
97
with StubCluster ({9001 : "rude_router.script" }):
97
98
address = ("127.0.0.1" , 9001 )
98
- with RoutingConnectionPool (connector ) as pool :
99
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS ) as pool :
99
100
assert address not in pool .routing_table .routers
100
101
_ = pool .fetch_routing_info (address )
101
102
assert address not in pool .routing_table .routers
102
103
103
104
def test_should_return_none_if_cannot_connect (self ):
104
105
address = ("127.0.0.1" , 9001 )
105
- with RoutingConnectionPool (connector , address ) as pool :
106
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
106
107
result = pool .fetch_routing_info (address )
107
108
assert result is None
108
109
109
110
def test_should_return_none_if_connection_drops (self ):
110
111
with StubCluster ({9001 : "rude_router.script" }):
111
112
address = ("127.0.0.1" , 9001 )
112
- with RoutingConnectionPool (connector , address ) as pool :
113
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
113
114
result = pool .fetch_routing_info (address )
114
115
assert result is None
115
116
116
117
def test_should_fail_for_non_router (self ):
117
118
with StubCluster ({9001 : "non_router.script" }):
118
119
address = ("127.0.0.1" , 9001 )
119
- with RoutingConnectionPool (connector , address ) as pool :
120
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
120
121
with self .assertRaises (ServiceUnavailable ):
121
122
_ = pool .fetch_routing_info (address )
122
123
123
124
def test_should_fail_if_database_error (self ):
124
125
with StubCluster ({9001 : "broken_router.script" }):
125
126
address = ("127.0.0.1" , 9001 )
126
- with RoutingConnectionPool (connector , address ) as pool :
127
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
127
128
with self .assertRaises (ServiceUnavailable ):
128
129
_ = pool .fetch_routing_info (address )
129
130
@@ -133,7 +134,7 @@ class RoutingConnectionPoolFetchRoutingTableTestCase(StubTestCase):
133
134
def test_should_get_table_from_router (self ):
134
135
with StubCluster ({9001 : "router.script" }):
135
136
address = ("127.0.0.1" , 9001 )
136
- with RoutingConnectionPool (connector ) as pool :
137
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS ) as pool :
137
138
table = pool .fetch_routing_table (address )
138
139
assert table .routers == {("127.0.0.1" , 9001 ), ("127.0.0.1" , 9002 ),
139
140
("127.0.0.1" , 9003 )}
@@ -143,28 +144,28 @@ def test_should_get_table_from_router(self):
143
144
144
145
def test_null_info_should_return_null_table (self ):
145
146
address = ("127.0.0.1" , 9001 )
146
- with RoutingConnectionPool (connector ) as pool :
147
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS ) as pool :
147
148
table = pool .fetch_routing_table (address )
148
149
assert table is None
149
150
150
151
def test_no_routers_should_raise_protocol_error (self ):
151
152
with StubCluster ({9001 : "router_no_routers.script" }):
152
153
address = ("127.0.0.1" , 9001 )
153
- with RoutingConnectionPool (connector ) as pool :
154
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS ) as pool :
154
155
with self .assertRaises (ProtocolError ):
155
156
_ = pool .fetch_routing_table (address )
156
157
157
158
def test_no_readers_should_raise_protocol_error (self ):
158
159
with StubCluster ({9001 : "router_no_readers.script" }):
159
160
address = ("127.0.0.1" , 9001 )
160
- with RoutingConnectionPool (connector ) as pool :
161
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS ) as pool :
161
162
with self .assertRaises (ProtocolError ):
162
163
_ = pool .fetch_routing_table (address )
163
164
164
165
def test_no_writers_should_return_null_table (self ):
165
166
with StubCluster ({9001 : "router_no_writers.script" }):
166
167
address = ("127.0.0.1" , 9001 )
167
- with RoutingConnectionPool (connector ) as pool :
168
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS ) as pool :
168
169
table = pool .fetch_routing_table (address )
169
170
assert table is None
170
171
@@ -183,8 +184,21 @@ class RoutingConnectionPoolUpdateRoutingTableTestCase(StubTestCase):
183
184
(None , None , ServiceUnavailable ): ServiceUnavailable ,
184
185
}
185
186
187
+ def test_roll_back_to_initial_server_if_failed_update_with_existing_routers (self ):
188
+ with StubCluster ({9001 : "router.script" }):
189
+ initial_address = ("127.0.0.1" , 9001 ) # roll back addresses
190
+ routers = [("127.0.0.1" , 9002 ), ("127.0.0.1" , 9003 )] # not reachable servers
191
+ with RoutingConnectionPool (connector , initial_address , * routers ) as pool :
192
+ pool .update_routing_table ()
193
+ table = pool .routing_table
194
+ assert table .routers == {("127.0.0.1" , 9001 ), ("127.0.0.1" , 9002 ),
195
+ ("127.0.0.1" , 9003 )}
196
+ assert table .readers == {("127.0.0.1" , 9004 ), ("127.0.0.1" , 9005 )}
197
+ assert table .writers == {("127.0.0.1" , 9006 )}
198
+ assert table .ttl == 300
199
+
186
200
def test_update_with_no_routers_should_signal_service_unavailable (self ):
187
- with RoutingConnectionPool (connector ) as pool :
201
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS ) as pool :
188
202
with self .assertRaises (ServiceUnavailable ):
189
203
pool .update_routing_table ()
190
204
@@ -207,7 +221,7 @@ def _test_server_outcome(self, server_outcomes, overall_outcome):
207
221
assert False , "Unexpected server outcome %r" % outcome
208
222
routers .append (("127.0.0.1" , port ))
209
223
with StubCluster (servers ):
210
- with RoutingConnectionPool (connector , * routers ) as pool :
224
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , * routers ) as pool :
211
225
if overall_outcome is RoutingTable :
212
226
pool .update_routing_table ()
213
227
table = pool .routing_table
@@ -228,7 +242,7 @@ class RoutingConnectionPoolRefreshRoutingTableTestCase(StubTestCase):
228
242
def test_should_update_if_stale (self ):
229
243
with StubCluster ({9001 : "router.script" }):
230
244
address = ("127.0.0.1" , 9001 )
231
- with RoutingConnectionPool (connector , address ) as pool :
245
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
232
246
first_updated_time = pool .routing_table .last_updated_time
233
247
pool .routing_table .ttl = 0
234
248
pool .refresh_routing_table ()
@@ -238,7 +252,7 @@ def test_should_update_if_stale(self):
238
252
def test_should_not_update_if_fresh (self ):
239
253
with StubCluster ({9001 : "router.script" }):
240
254
address = ("127.0.0.1" , 9001 )
241
- with RoutingConnectionPool (connector , address ) as pool :
255
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
242
256
pool .refresh_routing_table ()
243
257
first_updated_time = pool .routing_table .last_updated_time
244
258
pool .refresh_routing_table ()
@@ -250,7 +264,7 @@ def test_should_not_update_if_fresh(self):
250
264
# address = ("127.0.0.1", 9001)
251
265
# table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD])
252
266
#
253
- # with RoutingConnectionPool(connector, address) as pool:
267
+ # with RoutingConnectionPool(connector, not_reachable_address, address) as pool:
254
268
# semaphore = Semaphore()
255
269
#
256
270
# class Refresher(Thread):
@@ -297,7 +311,7 @@ def test_should_not_update_if_fresh(self):
297
311
# address = ("127.0.0.1", 9001)
298
312
# table = RoutingTable.parse_routing_info([VALID_ROUTING_RECORD])
299
313
#
300
- # with RoutingConnectionPool(connector, address) as pool:
314
+ # with RoutingConnectionPool(connector, not_reachable_address, address) as pool:
301
315
# semaphore = Semaphore()
302
316
#
303
317
# class Refresher(Thread):
@@ -345,15 +359,15 @@ class RoutingConnectionPoolAcquireForReadTestCase(StubTestCase):
345
359
def test_should_refresh (self ):
346
360
with StubCluster ({9001 : "router.script" , 9004 : "empty.script" }):
347
361
address = ("127.0.0.1" , 9001 )
348
- with RoutingConnectionPool (connector , address ) as pool :
362
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
349
363
assert not pool .routing_table .is_fresh ()
350
364
_ = pool .acquire (access_mode = READ_ACCESS )
351
365
assert pool .routing_table .is_fresh ()
352
366
353
367
def test_connected_to_reader (self ):
354
368
with StubCluster ({9001 : "router.script" , 9004 : "empty.script" }):
355
369
address = ("127.0.0.1" , 9001 )
356
- with RoutingConnectionPool (connector , address ) as pool :
370
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
357
371
assert not pool .routing_table .is_fresh ()
358
372
connection = pool .acquire (access_mode = READ_ACCESS )
359
373
assert connection .server .address in pool .routing_table .readers
@@ -363,7 +377,7 @@ def test_should_retry_if_first_reader_fails(self):
363
377
9004 : "fail_on_init.script" ,
364
378
9005 : "empty.script" }):
365
379
address = ("127.0.0.1" , 9001 )
366
- with RoutingConnectionPool (connector , address ) as pool :
380
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
367
381
assert not pool .routing_table .is_fresh ()
368
382
_ = pool .acquire (access_mode = READ_ACCESS )
369
383
assert ("127.0.0.1" , 9004 ) not in pool .routing_table .readers
@@ -375,15 +389,15 @@ class RoutingConnectionPoolAcquireForWriteTestCase(StubTestCase):
375
389
def test_should_refresh (self ):
376
390
with StubCluster ({9001 : "router.script" , 9006 : "empty.script" }):
377
391
address = ("127.0.0.1" , 9001 )
378
- with RoutingConnectionPool (connector , address ) as pool :
392
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
379
393
assert not pool .routing_table .is_fresh ()
380
394
_ = pool .acquire (access_mode = WRITE_ACCESS )
381
395
assert pool .routing_table .is_fresh ()
382
396
383
397
def test_connected_to_writer (self ):
384
398
with StubCluster ({9001 : "router.script" , 9006 : "empty.script" }):
385
399
address = ("127.0.0.1" , 9001 )
386
- with RoutingConnectionPool (connector , address ) as pool :
400
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
387
401
assert not pool .routing_table .is_fresh ()
388
402
connection = pool .acquire (access_mode = WRITE_ACCESS )
389
403
assert connection .server .address in pool .routing_table .writers
@@ -393,7 +407,7 @@ def test_should_retry_if_first_writer_fails(self):
393
407
9006 : "fail_on_init.script" ,
394
408
9007 : "empty.script" }):
395
409
address = ("127.0.0.1" , 9001 )
396
- with RoutingConnectionPool (connector , address ) as pool :
410
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
397
411
assert not pool .routing_table .is_fresh ()
398
412
_ = pool .acquire (access_mode = WRITE_ACCESS )
399
413
assert ("127.0.0.1" , 9006 ) not in pool .routing_table .writers
@@ -405,7 +419,7 @@ class RoutingConnectionPoolRemoveTestCase(StubTestCase):
405
419
def test_should_remove_router_from_routing_table_if_present (self ):
406
420
with StubCluster ({9001 : "router.script" }):
407
421
address = ("127.0.0.1" , 9001 )
408
- with RoutingConnectionPool (connector , address ) as pool :
422
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
409
423
pool .refresh_routing_table ()
410
424
target = ("127.0.0.1" , 9001 )
411
425
assert target in pool .routing_table .routers
@@ -415,7 +429,7 @@ def test_should_remove_router_from_routing_table_if_present(self):
415
429
def test_should_remove_reader_from_routing_table_if_present (self ):
416
430
with StubCluster ({9001 : "router.script" }):
417
431
address = ("127.0.0.1" , 9001 )
418
- with RoutingConnectionPool (connector , address ) as pool :
432
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
419
433
pool .refresh_routing_table ()
420
434
target = ("127.0.0.1" , 9004 )
421
435
assert target in pool .routing_table .readers
@@ -425,7 +439,7 @@ def test_should_remove_reader_from_routing_table_if_present(self):
425
439
def test_should_remove_writer_from_routing_table_if_present (self ):
426
440
with StubCluster ({9001 : "router.script" }):
427
441
address = ("127.0.0.1" , 9001 )
428
- with RoutingConnectionPool (connector , address ) as pool :
442
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
429
443
pool .refresh_routing_table ()
430
444
target = ("127.0.0.1" , 9006 )
431
445
assert target in pool .routing_table .writers
@@ -435,7 +449,7 @@ def test_should_remove_writer_from_routing_table_if_present(self):
435
449
def test_should_not_fail_if_absent (self ):
436
450
with StubCluster ({9001 : "router.script" }):
437
451
address = ("127.0.0.1" , 9001 )
438
- with RoutingConnectionPool (connector , address ) as pool :
452
+ with RoutingConnectionPool (connector , UNREACHABLE_ADDRESS , address ) as pool :
439
453
pool .refresh_routing_table ()
440
454
target = ("127.0.0.1" , 9007 )
441
455
pool .remove (target )
0 commit comments