Skip to content

Commit ba6ef88

Browse files
committed
PYTHON-1026 - Single member replica set testing
1 parent 139176b commit ba6ef88

File tree

7 files changed

+37
-2
lines changed

7 files changed

+37
-2
lines changed

test/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ def port(self):
257257
def pair(self):
258258
return "%s:%d" % (self.host, self.port)
259259

260+
@property
261+
def has_secondaries(self):
262+
return bool(len(self.client.secondaries))
263+
260264
def _check_user_provided(self):
261265
try:
262266
self.client.admin.authenticate(db_user, db_pwd)
@@ -367,6 +371,15 @@ def require_replica_set(self, func):
367371
"Not connected to a replica set",
368372
func=func)
369373

374+
def require_secondaries_count(self, count):
375+
"""Run a test only if the client is connected to a replica set that has
376+
`count` secondaries.
377+
"""
378+
sec_count = len(self.client.secondaries)
379+
return self._require(sec_count >= count,
380+
"Need %d secondaries, %d available"
381+
% (count, sec_count))
382+
370383
def require_no_replica_set(self, func):
371384
"""Run a test if the client is *not* connected to a replica set."""
372385
return self._require(

test/test_collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ def test_aggregation_cursor_validation(self):
15241524
@client_context.require_version_min(2, 5, 1)
15251525
def test_aggregation_cursor(self):
15261526
db = self.db
1527-
if client_context.replica_set_name:
1527+
if client_context.has_secondaries:
15281528
# Test that getMore messages are sent to the right server.
15291529
db = self.client.get_database(
15301530
db.name,
@@ -1580,7 +1580,7 @@ def test_aggregation_cursor_alive(self):
15801580
def test_parallel_scan(self):
15811581
db = self.db
15821582
db.drop_collection("test")
1583-
if client_context.replica_set_name:
1583+
if client_context.has_secondaries:
15841584
# Test that getMore messages are sent to the right server.
15851585
db = self.client.get_database(
15861586
db.name,

test/test_gridfs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ def test_unacknowledged(self):
459459

460460
class TestGridfsReplicaSet(TestReplicaSetClientBase):
461461

462+
@classmethod
463+
@client_context.require_secondaries_count(1)
464+
def setUpClass(cls):
465+
super(TestGridfsReplicaSet, cls).setUpClass()
466+
462467
def test_gridfs_replica_set(self):
463468
rsc = rs_client(
464469
w=self.w,

test/test_gridfs_bucket.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,11 @@ def test_download_to_stream_by_name(self):
444444

445445
class TestGridfsBucketReplicaSet(TestReplicaSetClientBase):
446446

447+
@classmethod
448+
@client_context.require_secondaries_count(1)
449+
def setUpClass(cls):
450+
super(TestGridfsBucketReplicaSet, cls).setUpClass()
451+
447452
def test_gridfs_replica_set(self):
448453
rsc = rs_client(
449454
w=self.w,

test/test_monitoring.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ def test_get_more_failure(self):
386386
self.assertEqual(0, failed.failure.get("ok"))
387387

388388
@client_context.require_replica_set
389+
@client_context.require_secondaries_count(1)
389390
def test_not_master_error(self):
390391
address = next(iter(client_context.client.secondaries))
391392
client = single_client(*address, event_listeners=[self.listener])

test/test_read_preferences.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ def test_copy(self):
7777

7878
class TestReadPreferencesBase(TestReplicaSetClientBase):
7979

80+
@classmethod
81+
@client_context.require_secondaries_count(1)
82+
def setUpClass(cls):
83+
super(TestReadPreferencesBase, cls).setUpClass()
84+
8085
def setUp(self):
8186
super(TestReadPreferencesBase, self).setUp()
8287
# Insert some data so we can use cursors in read_from_which_host
@@ -158,6 +163,7 @@ def test_reads_from_secondary(self):
158163

159164

160165
class TestReadPreferences(TestReadPreferencesBase):
166+
161167
def test_mode_validation(self):
162168
for mode in (ReadPreference.PRIMARY,
163169
ReadPreference.PRIMARY_PREFERRED,
@@ -322,6 +328,7 @@ def record_a_read(self, address):
322328
class TestCommandAndReadPreference(TestReplicaSetClientBase):
323329

324330
@classmethod
331+
@client_context.require_secondaries_count(1)
325332
def setUpClass(cls):
326333
super(TestCommandAndReadPreference, cls).setUpClass()
327334
cls.c = ReadPrefTester(

test/test_replica_set_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ def test_properties(self):
160160
self.assertEqual(c.max_bson_size, 16777216)
161161
c.close()
162162

163+
@client_context.require_secondaries_count(1)
163164
def test_auto_reconnect_exception_when_read_preference_is_secondary(self):
164165
c = MongoClient(
165166
client_context.pair,
@@ -180,6 +181,7 @@ def raise_socket_error(*args, **kwargs):
180181
finally:
181182
socket.socket.sendall = old_sendall
182183

184+
@client_context.require_secondaries_count(1)
183185
def test_timeout_does_not_mark_member_down(self):
184186
# If a query times out, the client shouldn't mark the member "down".
185187

@@ -285,9 +287,11 @@ def _test_kill_cursor_explicit(self, read_pref):
285287
def test_kill_cursor_explicit_primary(self):
286288
self._test_kill_cursor_explicit(ReadPreference.PRIMARY)
287289

290+
@client_context.require_secondaries_count(1)
288291
def test_kill_cursor_explicit_secondary(self):
289292
self._test_kill_cursor_explicit(ReadPreference.SECONDARY)
290293

294+
@client_context.require_secondaries_count(1)
291295
def test_not_master_error(self):
292296
secondary_address = one(self.secondaries)
293297
direct_client = single_client(*secondary_address)

0 commit comments

Comments
 (0)