Skip to content

Commit 61232b7

Browse files
authored
PYTHON-2457 Test that clients wait 500ms between failed heartbeat checks (#524)
1 parent e95d218 commit 61232b7

File tree

1 file changed

+28
-40
lines changed

1 file changed

+28
-40
lines changed

test/test_streaming_protocol.py

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -130,54 +130,42 @@ def changed_event(event):
130130
self.assertEqual(1, len(events))
131131
self.assertGreater(events[0].new_description.round_trip_time, 0)
132132

133+
@client_context.require_version_min(4, 9, -1)
133134
@client_context.require_failCommand_appName
134135
def test_monitor_waits_after_server_check_error(self):
135-
hb_listener = HeartbeatEventListener()
136-
client = rs_or_single_client(
137-
event_listeners=[hb_listener], heartbeatFrequencyMS=500,
138-
appName='waitAfterErrorTest')
139-
self.addCleanup(client.close)
140-
# Force a connection.
141-
client.admin.command('ping')
142-
address = client.address
143-
136+
# This test implements:
137+
# https://github.com/mongodb/specifications/blob/6c5b2ac/source/server-discovery-and-monitoring/server-discovery-and-monitoring-tests.rst#monitors-sleep-at-least-minheartbeatfreqencyms-between-checks
144138
fail_ismaster = {
145-
'mode': {'times': 50},
139+
'mode': {'times': 5},
146140
'data': {
147141
'failCommands': ['isMaster'],
148-
'closeConnection': False,
149-
'errorCode': 91,
150-
# This can be uncommented after SERVER-49220 is fixed.
151-
# 'appName': 'waitAfterErrorTest',
142+
'errorCode': 1234,
143+
'appName': 'SDAMMinHeartbeatFrequencyTest',
152144
},
153145
}
154146
with self.fail_point(fail_ismaster):
155-
time.sleep(2)
156-
157-
# Server should be selectable.
158-
client.admin.command('ping')
159-
160-
def hb_started(event):
161-
return (isinstance(event, monitoring.ServerHeartbeatStartedEvent)
162-
and event.connection_id == address)
163-
164-
hb_started_events = hb_listener.matching(hb_started)
165-
# Explanation of the expected heartbeat events:
166-
# Time: event
167-
# 0ms: create MongoClient
168-
# 1ms: run monitor handshake, 1
169-
# 2ms: run awaitable isMaster, 2
170-
# 3ms: run configureFailPoint
171-
# 502ms: isMaster fails for the first time with command error
172-
# 1002ms: run monitor handshake, 3
173-
# 1502ms: run monitor handshake, 4
174-
# 2002ms: run monitor handshake, 5
175-
# 2003ms: disable configureFailPoint
176-
# 2004ms: isMaster succeeds, 6
177-
# 2004ms: awaitable isMaster, 7
178-
self.assertGreater(len(hb_started_events), 7)
179-
# This can be reduced to ~15 after SERVER-49220 is fixed.
180-
self.assertLess(len(hb_started_events), 40)
147+
start = time.time()
148+
client = single_client(
149+
appName='SDAMMinHeartbeatFrequencyTest',
150+
serverSelectionTimeoutMS=5000)
151+
self.addCleanup(client.close)
152+
# Force a connection.
153+
client.admin.command('ping')
154+
duration = time.time() - start
155+
# Explanation of the expected events:
156+
# 0ms: run configureFailPoint
157+
# 1ms: create MongoClient
158+
# 2ms: failed monitor handshake, 1
159+
# 502ms: failed monitor handshake, 2
160+
# 1002ms: failed monitor handshake, 3
161+
# 1502ms: failed monitor handshake, 4
162+
# 2002ms: failed monitor handshake, 5
163+
# 2502ms: monitor handshake succeeds
164+
# 2503ms: run awaitable isMaster
165+
# 2504ms: application handshake succeeds
166+
# 2505ms: ping command succeeds
167+
self.assertGreaterEqual(duration, 2)
168+
self.assertLessEqual(duration, 3.5)
181169

182170
@client_context.require_failCommand_appName
183171
def test_heartbeat_awaited_flag(self):

0 commit comments

Comments
 (0)