7
class ComplianceTest(object):
9
self.saved_class = lockfile.LockFile
12
"""Return platform-appropriate file. Helper for tests."""
14
return os.path.join(tempfile.gettempdir(), 'trash-%s' % os.getpid())
17
lockfile.LockFile = self.class_to_test
23
elif os.path.isfile(tf):
25
lockfile.LockFile = self.saved_class
27
def _test_acquire_helper(self, tbool):
28
# As simple as it gets.
29
lock = lockfile.LockFile(self._testfile(), threaded=tbool)
31
assert lock.is_locked()
33
assert not lock.is_locked()
35
def test_acquire_basic_threaded(self):
36
self._test_acquire_helper(True)
38
def test_acquire_basic_unthreaded(self):
39
self._test_acquire_helper(False)
41
def _test_acquire_no_timeout_helper(self, tbool):
43
e1, e2 = threading.Event(), threading.Event()
44
t = _in_thread(self._lock_wait_unlock, e1, e2)
45
e1.wait() # wait for thread t to acquire lock
46
lock2 = lockfile.LockFile(self._testfile(), threaded=tbool)
47
assert lock2.is_locked()
48
assert not lock2.i_am_locking()
51
lock2.acquire(timeout=-1)
52
except lockfile.AlreadyLocked:
56
raise AssertionError("did not raise AlreadyLocked in"
58
threading.current_thread().get_name())
60
e2.set() # tell thread t to release lock
63
def test_acquire_no_timeout_threaded(self):
64
self._test_acquire_no_timeout_helper(True)
66
def test_acquire_no_timeout_unthreaded(self):
67
self._test_acquire_no_timeout_helper(False)
69
def _test_acquire_timeout_helper(self, tbool):
71
e1, e2 = threading.Event(), threading.Event()
72
t = _in_thread(self._lock_wait_unlock, e1, e2)
73
e1.wait() # wait for thread t to acquire lock
74
lock2 = lockfile.LockFile(self._testfile(), threaded=tbool)
75
assert lock2.is_locked()
77
lock2.acquire(timeout=0.1)
78
except lockfile.LockTimeout:
82
raise AssertionError("did not raise LockTimeout in thread %s" %
83
threading.current_thread().get_name())
88
def test_acquire_timeout_threaded(self):
89
self._test_acquire_timeout_helper(True)
91
def test_acquire_timeout_unthreaded(self):
92
self._test_acquire_timeout_helper(False)
94
def _test_release_basic_helper(self, tbool):
95
lock = lockfile.LockFile(self._testfile(), threaded=tbool)
97
assert lock.is_locked()
99
assert not lock.is_locked()
100
assert not lock.i_am_locking()
103
except lockfile.NotLocked:
105
except lockfile.NotMyLock:
106
raise AssertionError('unexpected exception: %s' %
109
raise AssertionError('erroneously unlocked file')
111
def test_release_basic_threaded(self):
112
self._test_release_basic_helper(True)
114
def test_release_basic_unthreaded(self):
115
self._test_release_basic_helper(False)
117
def _test_release_from_thread_helper(self, tbool):
118
e1, e2 = threading.Event(), threading.Event()
119
t = _in_thread(self._lock_wait_unlock, e1, e2)
121
lock2 = lockfile.LockFile(self._testfile(), threaded=tbool)
122
assert lock2.is_locked()
123
assert not lock2.i_am_locking()
126
except lockfile.NotMyLock:
129
raise AssertionError('erroneously unlocked a file locked'
130
' by another thread.')
134
def test_release_from_thread_threaded(self):
135
self._test_release_from_thread_helper(True)
137
def test_release_from_thread_unthreaded(self):
138
self._test_release_from_thread_helper(False)
140
def _test_is_locked_helper(self, tbool):
141
lock = lockfile.LockFile(self._testfile(), threaded=tbool)
143
assert lock.is_locked()
145
assert not lock.is_locked()
147
def test_is_locked_threaded(self):
148
self._test_is_locked_helper(True)
150
def test_is_locked_unthreaded(self):
151
self._test_is_locked_helper(False)
153
def test_i_am_locking(self):
154
lock1 = lockfile.LockFile(self._testfile(), threaded=False)
157
assert lock1.is_locked()
158
lock2 = lockfile.LockFile(self._testfile())
160
assert lock1.i_am_locking()
161
assert not lock2.i_am_locking()
163
lock2.acquire(timeout=2)
164
except lockfile.LockTimeout:
166
assert not lock2.is_locked()
167
assert not lock1.is_locked()
170
raise AssertionError('expected LockTimeout...')
171
assert not lock1.i_am_locking()
172
assert lock2.i_am_locking()
174
if lock2.i_am_locking():
177
if lock1.i_am_locking():
180
def _test_break_lock_helper(self, tbool):
181
lock = lockfile.LockFile(self._testfile(), threaded=tbool)
183
assert lock.is_locked()
184
lock2 = lockfile.LockFile(self._testfile(), threaded=tbool)
185
assert lock2.is_locked()
187
assert not lock2.is_locked()
190
except lockfile.NotLocked:
193
raise AssertionError('break lock failed')
195
def test_break_lock_threaded(self):
196
self._test_break_lock_helper(True)
198
def test_break_lock_unthreaded(self):
199
self._test_break_lock_helper(False)
201
def _lock_wait_unlock(self, event1, event2):
202
"""Lock from another thread. Helper for tests."""
203
l = lockfile.LockFile(self._testfile())
206
event1.set() # we're in,
207
event2.wait() # wait for boss's permission to leave
211
def test_enter(self):
212
lock = lockfile.LockFile(self._testfile())
215
assert lock.is_locked(), "Not locked after acquire!"
218
assert not lock.is_locked(), "still locked after release!"
220
def _in_thread(func, *args, **kwargs):
221
"""Execute func(*args, **kwargs) after dt seconds. Helper for tests."""
223
func(*args, **kwargs)
224
t = threading.Thread(target=_f, name='/*/*')