Skip to content

Commit a20ff68

Browse files
authored
PYTHON-3390 Test for encrypted client post-fork (#1037)
1 parent c0dadcb commit a20ff68

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

test/test_encryption.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,25 @@ def test_use_after_close(self):
329329
with self.assertRaisesRegex(InvalidOperation, "Cannot use MongoClient after close"):
330330
client.admin.command("ping")
331331

332+
# Not available for versions of Python without "register_at_fork"
333+
@unittest.skipIf(
334+
not hasattr(os, "register_at_fork"),
335+
"register_at_fork not available in this version of Python",
336+
)
337+
def test_fork(self):
338+
opts = AutoEncryptionOpts(KMS_PROVIDERS, "keyvault.datakeys")
339+
client = rs_or_single_client(auto_encryption_opts=opts)
340+
341+
lock_pid = os.fork()
342+
if lock_pid == 0:
343+
client.admin.command("ping")
344+
client.close()
345+
os._exit(0)
346+
else:
347+
self.assertEqual(0, os.waitpid(lock_pid, 0)[1])
348+
client.admin.command("ping")
349+
client.close()
350+
332351

333352
class TestEncryptedBulkWrite(BulkTestBase, EncryptionIntegrationTest):
334353
def test_upsert_uuid_standard_encrypt(self):

test/test_fork.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def exit_cond():
5353
if lock_pid == 0:
5454
os._exit(exit_cond())
5555
else:
56-
self.assertEqual(0, os.waitpid(lock_pid, 0)[1] >> 8)
56+
self.assertEqual(0, os.waitpid(lock_pid, 0)[1])
5757

5858
def test_lock_object_id(self):
5959
"""
@@ -67,7 +67,7 @@ def test_lock_object_id(self):
6767
if lock_pid == 0:
6868
os._exit(int(ObjectId._inc_lock.locked()))
6969
else:
70-
self.assertEqual(0, os.waitpid(lock_pid, 0)[1] >> 8)
70+
self.assertEqual(0, os.waitpid(lock_pid, 0)[1])
7171

7272
def test_topology_reset(self):
7373
"""
@@ -92,7 +92,7 @@ def test_topology_reset(self):
9292
)
9393
os._exit(0)
9494
else: # Parent
95-
self.assertEqual(0, os.waitpid(lock_pid, 0)[1] >> 8)
95+
self.assertEqual(0, os.waitpid(lock_pid, 0)[1])
9696
self.assertEqual(self.client._topology._pid, init_id)
9797
child_id = parent_conn.recv()
9898
self.assertNotEqual(child_id, init_id)

0 commit comments

Comments
 (0)