1
# Copyright (C) 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for LockDir"""
19
from threading import Thread
22
from bzrlib.errors import LockContention, LockError, UnlockableTransport
23
from bzrlib.lockdir import LockDir
24
from bzrlib.tests import TestCaseInTempDir, TestCaseWithTransport
26
# These tests sometimes use threads to test the behaviour of lock files with
27
# concurrent actors. This is not a typical (or necessarily supported) use;
28
# they're really meant for guarding between processes.
30
class TestLockDir(TestCaseWithTransport):
31
"""Test LockDir operations"""
33
def test_00_lock_creation(self):
34
"""Creation of lock file on a transport"""
35
t = self.get_transport()
36
lf = LockDir(t, 'test_lock')
37
self.assertFalse(lf.is_held())
39
def test_01_lock_repr(self):
40
"""Lock string representation"""
41
lf = LockDir(self.get_transport(), 'test_lock')
43
self.assertContainsRe(r, r'^LockDir\(.*/test_lock\)$')
45
def test_02_unlocked_peek(self):
46
lf = LockDir(self.get_transport(), 'test_lock')
47
self.assertEqual(lf.peek(), None)
49
def test_03_readonly_peek(self):
50
lf = LockDir(self.get_readonly_transport(), 'test_lock')
51
self.assertEqual(lf.peek(), None)
53
def test_10_lock_uncontested(self):
54
"""Acquire and release a lock"""
55
t = self.get_transport()
56
lf = LockDir(t, 'test_lock')
59
self.assertTrue(lf.is_held())
62
self.assertFalse(lf.is_held())
64
def test_11_lock_readonly_transport(self):
65
"""Fail to lock on readonly transport"""
66
t = self.get_readonly_transport()
67
lf = LockDir(t, 'test_lock')
68
self.assertRaises(UnlockableTransport, lf.attempt_lock)
70
def test_20_lock_contested(self):
71
"""Contention to get a lock"""
72
t = self.get_transport()
73
lf1 = LockDir(t, 'test_lock')
75
lf2 = LockDir(t, 'test_lock')
77
# locking is between LockDir instances; aliases within
78
# a single process are not detected
80
self.fail('Failed to detect lock collision')
81
except LockContention, e:
82
self.assertEqual(e.lock, lf2)
83
self.assertContainsRe(str(e),
84
r'^Could not acquire.*test_lock.*$')
87
def test_20_lock_peek(self):
88
"""Peek at the state of a lock"""
89
t = self.get_transport()
90
lf1 = LockDir(t, 'test_lock')
92
# lock is held, should get some info on it
94
self.assertEqual(set(info1.keys()),
95
set(['user', 'nonce', 'hostname', 'pid', 'start_time']))
96
# should get the same info if we look at it through a different
98
info2 = LockDir(t, 'test_lock').peek()
99
self.assertEqual(info1, info2)
100
# locks which are never used should be not-held
101
self.assertEqual(LockDir(t, 'other_lock').peek(), None)
103
def test_21_peek_readonly(self):
104
"""Peek over a readonly transport"""
105
t = self.get_transport()
106
lf1 = LockDir(t, 'test_lock')
107
lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
108
self.assertEqual(lf2.peek(), None)
111
self.assertTrue(info2)
112
self.assertEqual(info2['nonce'], lf1.nonce)
114
def test_30_lock_wait_fail(self):
115
"""Wait on a lock, then fail
117
We ask to wait up to 400ms; this should fail within at most one
118
second. (Longer times are more realistic but we don't want the test
119
suite to take too long, and this should do for now.)
121
t = self.get_transport()
122
lf1 = LockDir(t, 'test_lock')
123
lf2 = LockDir(t, 'test_lock')
127
self.assertRaises(LockContention, lf2.wait_lock,
128
timeout=0.4, poll=0.1)
130
self.assertTrue(after - before <= 1.0)
134
def test_31_lock_wait_easy(self):
135
"""Succeed when waiting on a lock with no contention.
137
t = self.get_transport()
138
lf2 = LockDir(t, 'test_lock')
141
lf2.wait_lock(timeout=0.4, poll=0.1)
143
self.assertTrue(after - before <= 1.0)
147
def test_32_lock_wait_succeed(self):
148
"""Succeed when trying to acquire a lock that gets released
150
One thread holds on a lock and then releases it; another tries to lock it.
152
t = self.get_transport()
153
lf1 = LockDir(t, 'test_lock')
156
def wait_and_unlock():
159
unlocker = Thread(target=wait_and_unlock)
162
lf2 = LockDir(t, 'test_lock')
165
lf2.wait_lock(timeout=0.4, poll=0.1)
167
self.assertTrue(after - before <= 1.0)
171
def test_33_wait(self):
172
"""Succeed when waiting on a lock that gets released
174
The difference from test_32_lock_wait_succeed is that the second
175
caller does not actually acquire the lock, but just waits for it
176
to be released. This is done over a readonly transport.
178
t = self.get_transport()
179
lf1 = LockDir(t, 'test_lock')
182
def wait_and_unlock():
185
unlocker = Thread(target=wait_and_unlock)
188
lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
190
# wait but don't lock
191
lf2.wait(timeout=0.4, poll=0.1)
193
self.assertTrue(after - before <= 1.0)