[Python-checkins] bpo-22367: Add tests for fcntl.lockf(). (GH-17010)

Miss Islington (bot) webhook-mailer at python.org
Sat Nov 9 06:12:40 EST 2019


https://github.com/python/cpython/commit/917dbe350a762ed6d75b7d074f3fb87975ba717b
commit: 917dbe350a762ed6d75b7d074f3fb87975ba717b
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-11-09T03:12:35-08:00
summary:

bpo-22367: Add tests for fcntl.lockf(). (GH-17010)

(cherry picked from commit befa032d8869e0fab4732d910f3887642879d644)

Co-authored-by: Dong-hee Na <donghee.na92 at gmail.com>

files:
M Lib/test/test_fcntl.py

diff --git a/Lib/test/test_fcntl.py b/Lib/test/test_fcntl.py
index acd5c7cc58647..30eeb6dc4d02a 100644
--- a/Lib/test/test_fcntl.py
+++ b/Lib/test/test_fcntl.py
@@ -5,6 +5,7 @@
 import struct
 import sys
 import unittest
+from multiprocessing import Process
 from test.support import (verbose, TESTFN, unlink, run_unittest, import_module,
                           cpython_only)
 
@@ -12,7 +13,6 @@
 fcntl = import_module('fcntl')
 
 
-# TODO - Write tests for flock() and lockf().
 
 def get_lockdata():
     try:
@@ -138,6 +138,33 @@ def test_flock(self):
         self.assertRaises(ValueError, fcntl.flock, -1, fcntl.LOCK_SH)
         self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH)
 
+    def test_lockf_exclusive(self):
+        self.f = open(TESTFN, 'wb+')
+        cmd = fcntl.LOCK_EX | fcntl.LOCK_NB
+        def try_lockf_on_other_process():
+            self.assertRaises(BlockingIOError, fcntl.lockf, self.f, cmd)
+
+        fcntl.lockf(self.f, cmd)
+        p = Process(target=try_lockf_on_other_process)
+        p.start()
+        p.join()
+        fcntl.lockf(self.f, fcntl.LOCK_UN)
+        self.assertEqual(p.exitcode, 0)
+
+    def test_lockf_share(self):
+        self.f = open(TESTFN, 'wb+')
+        cmd = fcntl.LOCK_SH | fcntl.LOCK_NB
+        def try_lockf_on_other_process():
+            fcntl.lockf(self.f, cmd)
+            fcntl.lockf(self.f, fcntl.LOCK_UN)
+
+        fcntl.lockf(self.f, cmd)
+        p = Process(target=try_lockf_on_other_process)
+        p.start()
+        p.join()
+        fcntl.lockf(self.f, fcntl.LOCK_UN)
+        self.assertEqual(p.exitcode, 0)
+
     @cpython_only
     def test_flock_overflow(self):
         import _testcapi



More information about the Python-checkins mailing list