Skip to content

Commit a9eb79d

Browse files
committed
bpo-22367: Add test_lockf
1 parent e471e72 commit a9eb79d

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Lib/test/test_fcntl.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
fcntl = import_module('fcntl')
1313

1414

15-
# TODO - Write tests for flock() and lockf().
1615

1716
def get_lockdata():
1817
try:
@@ -138,6 +137,34 @@ def test_flock(self):
138137
self.assertRaises(ValueError, fcntl.flock, -1, fcntl.LOCK_SH)
139138
self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH)
140139

140+
def test_lockf(self):
141+
from multiprocessing import Process
142+
self.f = open(TESTFN, 'wb+')
143+
self.f.write(b'testpython')
144+
ex_cmd = fcntl.LOCK_EX | fcntl.LOCK_NB
145+
sh_cmd = fcntl.LOCK_SH | fcntl.LOCK_NB
146+
def try_lock_f_on_other_process(cmd):
147+
if cmd == ex_cmd:
148+
self.assertRaises(BlockingIOError, fcntl.lockf, self.f, cmd, 1, 0)
149+
if cmd == sh_cmd:
150+
fcntl.lockf(self.f, cmd, 1, 0)
151+
fcntl.lockf(self.f, cmd, 1, 1)
152+
153+
fcntl.lockf(self.f, ex_cmd, 1, 0)
154+
p = Process(target=try_lock_f_on_other_process, args=(ex_cmd,))
155+
p.start()
156+
p.join()
157+
fcntl.lockf(self.f, fcntl.LOCK_UN)
158+
self.assertEqual(p.exitcode, 0)
159+
160+
fcntl.lockf(self.f, sh_cmd, 1, 0)
161+
p = Process(target=try_lock_f_on_other_process, args=(sh_cmd,))
162+
p.start()
163+
p.join()
164+
fcntl.lockf(self.f, fcntl.LOCK_UN)
165+
self.assertEqual(p.exitcode, 0)
166+
167+
141168
@cpython_only
142169
def test_flock_overflow(self):
143170
import _testcapi

0 commit comments

Comments
 (0)