Skip to content

Commit befa032

Browse files
corona10serhiy-storchaka
authored andcommitted
bpo-22367: Add tests for fcntl.lockf(). (GH-17010)
1 parent 6cbc84f commit befa032

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
@@ -5,14 +5,14 @@
55
import struct
66
import sys
77
import unittest
8+
from multiprocessing import Process
89
from test.support import (verbose, TESTFN, unlink, run_unittest, import_module,
910
cpython_only)
1011

1112
# Skip test if no fcntl module.
1213
fcntl = import_module('fcntl')
1314

1415

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

1717
def get_lockdata():
1818
try:
@@ -138,6 +138,33 @@ def test_flock(self):
138138
self.assertRaises(ValueError, fcntl.flock, -1, fcntl.LOCK_SH)
139139
self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH)
140140

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

0 commit comments

Comments
 (0)