-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_openlock.py
More file actions
196 lines (171 loc) · 5.68 KB
/
test_openlock.py
File metadata and controls
196 lines (171 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from __future__ import annotations
import logging # noqa: F401
import os
import platform
import subprocess
import sys
import time
import unittest
from pathlib import Path
from typing import Any
from openlock import (
FileLock,
InvalidLockFile,
InvalidOption,
InvalidRelease,
Timeout,
get_defaults,
logger,
set_defaults,
)
if sys.version_info >= (3, 11):
from openlock import Defaults
logging.basicConfig(format="%(asctime)s:%(levelname)s:%(name)s:%(process)s:%(message)s")
logger.setLevel(logging.DEBUG)
IS_MACOS = "darwin" in platform.system().lower()
IS_WINDOWS = "windows" in platform.system().lower()
lock_file = "test.lock"
other_lock_file = "test1.lock"
defaults = get_defaults()
def show(mc: Any) -> None:
exception = mc.exception
logger.debug(f"{exception.__class__.__name__}: {str(exception)}")
class TestOpenLock(unittest.TestCase):
def setUp(self) -> None:
logging.disable(logging.DEBUG)
for L in (lock_file, other_lock_file):
try:
os.remove(L)
except OSError:
pass
set_defaults(**defaults)
def test_acquire_release(self) -> None:
r = FileLock(lock_file)
self.assertFalse(r.locked())
r.acquire(timeout=0)
self.assertTrue(os.path.exists(lock_file))
self.assertTrue(r.locked())
self.assertTrue(r.getpid() == os.getpid())
r.release()
self.assertFalse(os.path.exists(lock_file))
self.assertFalse(r.locked())
def test_double_acquire(self) -> None:
r = FileLock(lock_file)
r.acquire(timeout=0)
with self.assertRaises(Timeout):
r.acquire(timeout=0)
def test_invalid_release(self) -> None:
r = FileLock(lock_file)
with self.assertRaises(InvalidRelease):
r.release()
r.acquire(timeout=0)
r.release()
with self.assertRaises(InvalidRelease):
r.release()
def test_invalid_lock_file(self) -> None:
with open(lock_file, "w") as f:
pass
r = FileLock(lock_file)
r.acquire(timeout=0)
r.release()
with open(lock_file, "w") as f:
f.write(f"{os.getpid()}\ndummy.py\n")
r.acquire(timeout=0)
self.assertTrue(os.getpid() == r.getpid())
r.release()
with open(lock_file, "w") as f:
f.write("1\ntest_openlock.py\n")
r.acquire(timeout=0)
self.assertTrue(os.getpid() == r.getpid())
r.release()
def test_timeout(self) -> None:
r = FileLock(lock_file)
t = time.time()
r.acquire(timeout=0)
with self.assertRaises(Timeout):
r.acquire(timeout=2)
self.assertTrue(time.time() - t >= 2)
def test_different_lock_files(self) -> None:
r = FileLock(lock_file)
s = FileLock(other_lock_file)
r.acquire(timeout=0)
s.acquire(timeout=0)
self.assertTrue(r.locked())
self.assertTrue(s.locked())
def test_second_process(self) -> None:
r = FileLock(lock_file)
r.acquire(timeout=0)
p = subprocess.run(
[sys.executable, "_helper.py", lock_file, "1"], stdout=subprocess.PIPE
)
self.assertTrue(p.stdout.decode().strip() == "1")
r.release()
p2 = subprocess.Popen(
[sys.executable, "_helper.py", lock_file, "2"], stdout=subprocess.PIPE
)
time.sleep(1)
with self.assertRaises(Timeout):
r.acquire(timeout=0)
out, err = p2.communicate()
self.assertTrue(out.decode().strip() == "2")
r.acquire(timeout=0)
def test_invalid_exception(self) -> None:
with open(lock_file, "w") as f:
f.write("1\ntest_openlock.py\n")
set_defaults(tries=0)
r = FileLock(lock_file)
with self.assertRaises(InvalidLockFile):
r.acquire(timeout=0)
def test_options(self) -> None:
option_keys = set(get_defaults().keys())
self.assertTrue(option_keys == {"tries", "retry_period", "race_delay"})
options: Defaults = {
"tries": 5,
"retry_period": 100.0,
"race_delay": 100,
}
set_defaults(**options)
options_ = get_defaults()
self.assertTrue(options == options_)
option_keys = set(options_)
self.assertTrue(option_keys == {"tries", "retry_period", "race_delay"})
def test_slow_system(self) -> None:
r = FileLock(lock_file)
r.acquire(timeout=0)
r.release()
set_defaults(race_delay=0)
r = FileLock(lock_file)
with self.assertWarns(UserWarning):
with open(lock_file, "w") as f:
f.write("1\ntest_openlock.py\n")
r.acquire(timeout=0)
def test_invalid_option(self) -> None:
with self.assertRaises(InvalidOption) as e:
set_defaults(tris=1) # type: ignore
self.assertTrue("tris" in str(e.exception))
def test_default_lock_file(self) -> None:
r = FileLock()
self.assertTrue(r.lock_file == Path("openlock.lock"))
def test_latency(self) -> None:
set_defaults(race_delay=1.0)
r = FileLock(lock_file)
t = time.time()
r.acquire()
tt = time.time()
self.assertTrue(tt - t < 0.2)
t = time.time()
try:
r.acquire(timeout=0)
except Timeout:
pass
tt = time.time()
self.assertTrue(tt - t < 0.2)
r.release()
with open(lock_file, "w") as f:
f.write("1\ntest_openlock.py\n")
t = time.time()
r.acquire()
tt = time.time()
self.assertTrue(tt - t > 0.8)
if __name__ == "__main__":
unittest.main(verbosity=2)