|
5 | 5 | import struct
|
6 | 6 | import sys
|
7 | 7 | import unittest
|
| 8 | +from multiprocessing import Process |
8 | 9 | from test.support import (verbose, TESTFN, unlink, run_unittest, import_module,
|
9 | 10 | cpython_only)
|
10 | 11 |
|
11 | 12 | # Skip test if no fcntl module.
|
12 | 13 | fcntl = import_module('fcntl')
|
13 | 14 |
|
14 | 15 |
|
15 |
| -# TODO - Write tests for flock() and lockf(). |
16 | 16 |
|
17 | 17 | def get_lockdata():
|
18 | 18 | try:
|
@@ -138,6 +138,33 @@ def test_flock(self):
|
138 | 138 | self.assertRaises(ValueError, fcntl.flock, -1, fcntl.LOCK_SH)
|
139 | 139 | self.assertRaises(TypeError, fcntl.flock, 'spam', fcntl.LOCK_SH)
|
140 | 140 |
|
| 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 | + |
141 | 168 | @cpython_only
|
142 | 169 | def test_flock_overflow(self):
|
143 | 170 | import _testcapi
|
|
0 commit comments