Skip to content

Commit 01eb25b

Browse files
committed
PYTHON-979 - Update fsyncUnlock helper for command monitoring
1 parent cfa2348 commit 01eb25b

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

pymongo/mongo_client.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,18 +1092,19 @@ def fsync(self, **kwargs):
10921092
def unlock(self):
10931093
"""Unlock a previously locked server.
10941094
"""
1095+
cmd = {"fsyncUnlock": 1}
10951096
with self._socket_for_writes() as sock_info:
10961097
if sock_info.max_wire_version >= 4:
10971098
try:
1098-
sock_info.command("admin", {"fsyncUnlock": 1})
1099+
sock_info.command("admin", cmd)
10991100
except OperationFailure as exc:
11001101
# Ignore "DB not locked" to replicate old behavior
11011102
if exc.code != 125:
11021103
raise
11031104
else:
11041105
helpers._first_batch(sock_info, "admin.$cmd.sys.unlock",
1105-
{}, -1, True, self.codec_options, ReadPreference.PRIMARY)
1106-
1106+
{}, -1, True, self.codec_options,
1107+
ReadPreference.PRIMARY, cmd, self._event_listeners)
11071108

11081109
def __enter__(self):
11091110
return self

test/test_monitoring.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from pymongo.read_preferences import ReadPreference
3131
from pymongo.write_concern import WriteConcern
3232
from test import unittest, client_context, client_knobs
33-
from test.utils import single_client
33+
from test.utils import single_client, wait_until
3434

3535

3636
class EventListener(monitoring.CommandListener):
@@ -1248,6 +1248,29 @@ def test_first_batch_helper(self):
12481248
self.assertEqual(started.request_id, succeeded.request_id)
12491249
self.assertEqual(started.connection_id, succeeded.connection_id)
12501250

1251+
if not client_context.is_mongos:
1252+
self.client.fsync(lock=True)
1253+
self.listener.results.clear()
1254+
self.client.unlock()
1255+
# Wait for async unlock...
1256+
wait_until(
1257+
lambda: not self.client.is_locked, "unlock the database")
1258+
started = results['started'][0]
1259+
succeeded = results['succeeded'][0]
1260+
self.assertEqual(0, len(results['failed']))
1261+
self.assertIsInstance(started, monitoring.CommandStartedEvent)
1262+
expected = {'fsyncUnlock': 1}
1263+
self.assertEqual(expected, started.command)
1264+
self.assertEqual('admin', started.database_name)
1265+
self.assertEqual('fsyncUnlock', started.command_name)
1266+
self.assertIsInstance(started.request_id, int)
1267+
self.assertEqual(self.client.address, started.connection_id)
1268+
self.assertIsInstance(succeeded, monitoring.CommandSucceededEvent)
1269+
self.assertIsInstance(succeeded.duration_micros, int)
1270+
self.assertEqual(started.command_name, succeeded.command_name)
1271+
self.assertEqual(started.request_id, succeeded.request_id)
1272+
self.assertEqual(started.connection_id, succeeded.connection_id)
1273+
12511274
def test_sensitive_commands(self):
12521275
listeners = self.client._event_listeners
12531276

0 commit comments

Comments
 (0)