36
36
class _TestLockableFiles_mixin(object):
39
# Reduce the default timeout, so that if tests fail, they will do so
41
orig_timeout = lockdir._DEFAULT_TIMEOUT_SECONDS
43
lockdir._DEFAULT_TIMEOUT_SECONDS = orig_timeout
44
self.addCleanup(resetTimeout)
45
lockdir._DEFAULT_TIMEOUT_SECONDS = 3
39
self.reduceLockdirTimeout()
47
41
def test_read_write(self):
48
42
self.assertRaises(NoSuchFile, self.lockable.get, 'foo')
148
142
self.lockable.unlock()
144
def test_lock_write_returns_token_when_given_token(self):
145
token = self.lockable.lock_write()
148
# This test does not apply, because this lockable refuses
151
new_lockable = self.get_lockable()
152
token_from_new_lockable = new_lockable.lock_write(token=token)
154
self.assertEqual(token, token_from_new_lockable)
156
new_lockable.unlock()
158
self.lockable.unlock()
150
160
def test_lock_write_raises_on_token_mismatch(self):
151
161
token = self.lockable.lock_write()
233
243
# Relock with a token.
234
self.lockable.lock_write(token=token)
235
self.lockable.unlock()
244
token_from_reentry = self.lockable.lock_write(token=token)
246
self.assertEqual(token, token_from_reentry)
248
self.lockable.unlock()
237
250
self.lockable.unlock()
238
251
# The lock should be unlocked on disk. Verify that with a new lock
242
255
new_lockable.lock_write()
243
256
new_lockable.unlock()
258
def test_leave_in_place(self):
259
token = self.lockable.lock_write()
262
# This test does not apply, because this lockable refuses
265
self.lockable.leave_in_place()
267
self.lockable.unlock()
268
# At this point, the lock is still in place on disk
269
self.assertRaises(errors.LockContention, self.lockable.lock_write)
270
# But should be relockable with a token.
271
self.lockable.lock_write(token=token)
272
self.lockable.unlock()
274
def test_dont_leave_in_place(self):
275
token = self.lockable.lock_write()
278
# This test does not apply, because this lockable refuses
281
self.lockable.leave_in_place()
283
self.lockable.unlock()
284
# At this point, the lock is still in place on disk.
285
# Acquire the existing lock with the token, and ask that it is removed
286
# when this object unlocks, and unlock to trigger that removal.
287
new_lockable = self.get_lockable()
288
new_lockable.lock_write(token=token)
289
new_lockable.dont_leave_in_place()
290
new_lockable.unlock()
291
# At this point, the lock is no longer on disk, so we can lock it.
292
third_lockable = self.get_lockable()
293
third_lockable.lock_write()
294
third_lockable.unlock()