27
27
from ha_tools import use_greenlets
28
28
29
29
30
- from pymongo import (MongoReplicaSetClient ,
31
- ReadPreference )
32
- from pymongo .mongo_replica_set_client import (
33
- Member , Monitor , MongoReplicaSetClient )
34
- from pymongo .mongo_client import _partition_node
35
- from pymongo .mongo_client import MongoClient
36
30
from pymongo .errors import AutoReconnect , OperationFailure , ConnectionFailure
37
- from pymongo .read_preferences import modes
31
+ from pymongo .mongo_replica_set_client import Member , Monitor
32
+ from pymongo .mongo_replica_set_client import MongoReplicaSetClient
33
+ from pymongo .mongo_client import MongoClient , _partition_node
34
+ from pymongo .read_preferences import ReadPreference , modes
38
35
39
36
from test import utils
40
37
from test .utils import one
52
49
NEAREST = ReadPreference .NEAREST
53
50
54
51
55
- class TestDirectConnection (unittest .TestCase ):
52
+ class HATestCase (unittest .TestCase ):
53
+ """A test case for connections to replica sets or mongos."""
54
+
55
+ def tearDown (self ):
56
+ ha_tools .kill_all_members ()
57
+ ha_tools .nodes .clear ()
58
+
59
+
60
+ class TestDirectConnection (HATestCase ):
56
61
57
62
def setUp (self ):
58
63
members = [{}, {}, {'arbiterOnly' : True }]
@@ -145,10 +150,10 @@ def test_secondary_connection(self):
145
150
146
151
def tearDown (self ):
147
152
self .c .close ()
148
- ha_tools . kill_all_members ()
153
+ super ( TestDirectConnection , self ). tearDown ()
149
154
150
155
151
- class TestPassiveAndHidden (unittest . TestCase ):
156
+ class TestPassiveAndHidden (HATestCase ):
152
157
153
158
def setUp (self ):
154
159
members = [{},
@@ -177,10 +182,10 @@ def test_passive_and_hidden(self):
177
182
178
183
def tearDown (self ):
179
184
self .c .close ()
180
- ha_tools . kill_all_members ()
185
+ super ( TestPassiveAndHidden , self ). tearDown ()
181
186
182
187
183
- class TestMonitorRemovesRecoveringMember (unittest . TestCase ):
188
+ class TestMonitorRemovesRecoveringMember (HATestCase ):
184
189
# Members in STARTUP2 or RECOVERING states are shown in the primary's
185
190
# isMaster response, but aren't secondaries and shouldn't be read from.
186
191
# Verify that if a secondary goes into RECOVERING mode, the Monitor removes
@@ -211,10 +216,10 @@ def test_monitor_removes_recovering_member(self):
211
216
212
217
def tearDown (self ):
213
218
self .c .close ()
214
- ha_tools . kill_all_members ()
219
+ super ( TestMonitorRemovesRecoveringMember , self ). tearDown ()
215
220
216
221
217
- class TestTriggeredRefresh (unittest . TestCase ):
222
+ class TestTriggeredRefresh (HATestCase ):
218
223
# Verify that if a secondary goes into RECOVERING mode or if the primary
219
224
# changes, the next exception triggers an immediate refresh.
220
225
@@ -282,14 +287,14 @@ def test_stepdown_triggers_refresh(self):
282
287
# We've detected the stepdown
283
288
self .assertTrue (
284
289
not c_find_one .primary
285
- or primary != _partition_node ( c_find_one .primary ) )
290
+ or _partition_node ( primary ) != c_find_one .primary )
286
291
287
292
def tearDown (self ):
288
293
Monitor ._refresh_interval = MONITOR_INTERVAL
289
- ha_tools . kill_all_members ()
294
+ super ( TestTriggeredRefresh , self ). tearDown ()
290
295
291
296
292
- class TestHealthMonitor (unittest . TestCase ):
297
+ class TestHealthMonitor (HATestCase ):
293
298
294
299
def setUp (self ):
295
300
res = ha_tools .start_replica_set ([{}, {}, {}])
@@ -357,17 +362,10 @@ def primary_changed():
357
362
358
363
ha_tools .stepdown_primary ()
359
364
self .assertTrue (primary_changed ())
360
-
361
- # There can be a delay between finding the primary and updating
362
- # secondaries
363
- sleep (5 )
364
365
self .assertNotEqual (secondaries , c .secondaries )
365
366
366
- def tearDown (self ):
367
- ha_tools .kill_all_members ()
368
-
369
367
370
- class TestWritesWithFailover (unittest . TestCase ):
368
+ class TestWritesWithFailover (HATestCase ):
371
369
372
370
def setUp (self ):
373
371
res = ha_tools .start_replica_set ([{}, {}, {}])
@@ -398,11 +396,8 @@ def try_write():
398
396
self .assertTrue (primary != c .primary )
399
397
self .assertEqual ('baz' , db .test .find_one ({'bar' : 'baz' })['bar' ])
400
398
401
- def tearDown (self ):
402
- ha_tools .kill_all_members ()
403
-
404
399
405
- class TestReadWithFailover (unittest . TestCase ):
400
+ class TestReadWithFailover (HATestCase ):
406
401
407
402
def setUp (self ):
408
403
res = ha_tools .start_replica_set ([{}, {}, {}])
@@ -435,11 +430,8 @@ def iter_cursor(cursor):
435
430
self .assertTrue (iter_cursor (cursor ))
436
431
self .assertEqual (10 , cursor ._Cursor__retrieved )
437
432
438
- def tearDown (self ):
439
- ha_tools .kill_all_members ()
440
433
441
-
442
- class TestReadPreference (unittest .TestCase ):
434
+ class TestReadPreference (HATestCase ):
443
435
def setUp (self ):
444
436
members = [
445
437
# primary
@@ -784,11 +776,10 @@ def test_pinning(self):
784
776
785
777
def tearDown (self ):
786
778
self .c .close ()
787
- ha_tools .kill_all_members ()
788
- self .clear_ping_times ()
779
+ super (TestReadPreference , self ).tearDown ()
789
780
790
781
791
- class TestReplicaSetAuth (unittest . TestCase ):
782
+ class TestReplicaSetAuth (HATestCase ):
792
783
def setUp (self ):
793
784
members = [
794
785
{},
@@ -829,10 +820,10 @@ def test_auth_during_failover(self):
829
820
830
821
def tearDown (self ):
831
822
self .c .close ()
832
- ha_tools . kill_all_members ()
823
+ super ( TestReplicaSetAuth , self ). tearDown ()
833
824
834
825
835
- class TestAlive (unittest . TestCase ):
826
+ class TestAlive (HATestCase ):
836
827
def setUp (self ):
837
828
members = [{}, {}]
838
829
self .seed , self .name = ha_tools .start_replica_set (members )
@@ -866,11 +857,8 @@ def test_alive(self):
866
857
finally :
867
858
rsc .close ()
868
859
869
- def tearDown (self ):
870
- ha_tools .kill_all_members ()
871
860
872
-
873
- class TestMongosHighAvailability (unittest .TestCase ):
861
+ class TestMongosHighAvailability (HATestCase ):
874
862
def setUp (self ):
875
863
seed_list = ha_tools .create_sharded_cluster ()
876
864
self .dbname = 'pymongo_mongos_ha'
@@ -910,10 +898,10 @@ def test_mongos_ha(self):
910
898
911
899
def tearDown (self ):
912
900
self .client .drop_database (self .dbname )
913
- ha_tools . kill_all_members ()
901
+ super ( TestMongosHighAvailability , self ). tearDown ()
914
902
915
903
916
- class TestReplicaSetRequest (unittest . TestCase ):
904
+ class TestReplicaSetRequest (HATestCase ):
917
905
def setUp (self ):
918
906
members = [{}, {}, {'arbiterOnly' : True }]
919
907
res = ha_tools .start_replica_set (members )
@@ -928,8 +916,9 @@ def test_request_during_failover(self):
928
916
self .assertTrue (self .c .auto_start_request )
929
917
self .assertTrue (self .c .in_request ())
930
918
931
- primary_pool = self .c ._MongoReplicaSetClient__members [primary ].pool
932
- secondary_pool = self .c ._MongoReplicaSetClient__members [secondary ].pool
919
+ rs_state = self .c ._MongoReplicaSetClient__rs_state
920
+ primary_pool = rs_state .get (primary ).pool
921
+ secondary_pool = rs_state .get (secondary ).pool
933
922
934
923
# Trigger start_request on primary pool
935
924
utils .assertReadFrom (self , self .c , primary , PRIMARY )
@@ -967,7 +956,7 @@ def test_request_during_failover(self):
967
956
968
957
def tearDown (self ):
969
958
self .c .close ()
970
- ha_tools . kill_all_members ()
959
+ super ( TestReplicaSetRequest , self ). tearDown ()
971
960
972
961
973
962
if __name__ == '__main__' :
0 commit comments