Skip to content

Commit c1a7bdf

Browse files
committed
PYTHON-1026 - Make test suite resilient against elections
1 parent 8ae717d commit c1a7bdf

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

test/__init__.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ class ClientContext(object):
144144

145145
def __init__(self):
146146
"""Create a client and grab essential information from the server."""
147-
# Seed host. This may be updated further down.
148-
self.host, self.port = host, port
149147
self.connected = False
150148
self.ismaster = {}
151149
self.w = None
@@ -163,11 +161,11 @@ def __init__(self):
163161
self.ssl_certfile = False
164162
self.server_is_resolvable = is_server_resolvable()
165163
self.ssl_client_options = {}
166-
self.client = _connect(self.host, self.port)
164+
self.client = _connect(host, port)
167165

168166
if HAVE_SSL and not self.client:
169167
# Is MongoDB configured for SSL?
170-
self.client = _connect(self.host, self.port, **_SSL_OPTIONS)
168+
self.client = _connect(host, port, **_SSL_OPTIONS)
171169
if self.client:
172170
self.ssl = True
173171
self.ssl_client_options = _SSL_OPTIONS
@@ -177,28 +175,30 @@ def __init__(self):
177175

178176
if self.client:
179177
self.connected = True
180-
self.ismaster = self.client.admin.command('ismaster')
181-
self.w = len(self.ismaster.get("hosts", [])) or 1
182-
self.nodes = set([(self.host, self.port)])
183-
self.replica_set_name = self.ismaster.get('setName', '')
184-
self.version = Version.from_client(self.client)
185-
if self.replica_set_name:
178+
ismaster = self.client.admin.command('ismaster')
179+
if 'setName' in ismaster:
180+
self.replica_set_name = ismaster['setName']
186181
self.is_rs = True
182+
# It doesn't matter which member we use as the seed here.
187183
self.client = pymongo.MongoClient(
188-
self.ismaster['primary'],
184+
host,
185+
port,
189186
replicaSet=self.replica_set_name,
190187
**self.ssl_client_options)
191-
# Force connection
192-
self.client.admin.command('ismaster')
193-
self.host, self.port = self.client.primary
194-
188+
# Get the authoritative ismaster result from the primary.
189+
self.ismaster = self.client.admin.command('ismaster')
195190
nodes = [partition_node(node.lower())
196191
for node in self.ismaster.get('hosts', [])]
197192
nodes.extend([partition_node(node.lower())
198193
for node in self.ismaster.get('passives', [])])
199194
nodes.extend([partition_node(node.lower())
200195
for node in self.ismaster.get('arbiters', [])])
201196
self.nodes = set(nodes)
197+
else:
198+
self.ismaster = ismaster
199+
self.nodes = set([(host, port)])
200+
self.w = len(self.ismaster.get("hosts", [])) or 1
201+
self.version = Version.from_client(self.client)
202202

203203
try:
204204
self.cmd_line = self.client.admin.command('getCmdLineOpts')
@@ -239,6 +239,20 @@ def __init__(self):
239239
self.is_mongos = (self.ismaster.get('msg') == 'isdbgrid')
240240
self.has_ipv6 = self._server_started_with_ipv6()
241241

242+
@property
243+
def host(self):
244+
if self.is_rs:
245+
primary = self.client.primary
246+
return primary[0] if primary is not None else host
247+
return host
248+
249+
@property
250+
def port(self):
251+
if self.is_rs:
252+
primary = self.client.primary
253+
return primary[1] if primary is not None else port
254+
return port
255+
242256
@property
243257
def pair(self):
244258
return "%s:%d" % (self.host, self.port)

test/test_client.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,6 @@ def test_repr(self):
458458

459459
self.assertEqual(eval(the_repr), client)
460460

461-
@client_context.require_replica_set
462-
def test_repr_replica_set(self):
463-
self.assertIn("MongoClient(host=[", repr(self.client))
464-
self.assertIn(client_context.pair, repr(self.client))
465-
466461
def test_getters(self):
467462
wait_until(lambda: client_context.nodes == self.client.nodes,
468463
"find all nodes")

0 commit comments

Comments
 (0)