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
|
# Copyright (C) 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Tests for bzrlib.counted_lock"""
from bzrlib.counted_lock import CountedLock
from bzrlib.errors import (
LockError,
ReadOnlyError,
)
from bzrlib.tests import TestCase
class DummyLock(object):
"""Lock that just records what's been done to it."""
def __init__(self):
self._calls = []
self._lock_mode = None
def is_locked(self):
return self._lock_mode is not None
def lock_read(self):
self._assert_not_locked()
self._lock_mode = 'r'
self._calls.append('lock_read')
def lock_write(self):
self._assert_not_locked()
self._lock_mode = 'w'
self._calls.append('lock_write')
def unlock(self):
self._assert_locked()
self._lock_mode = None
self._calls.append('unlock')
def break_lock(self):
self._lock_mode = None
self._calls.append('break')
def _assert_locked(self):
if not self._lock_mode:
raise LockError("%s is not locked" % (self,))
def _assert_not_locked(self):
if self._lock_mode:
raise LockError("%s is already locked in mode %r" %
(self, self._lock_mode))
class TestDummyLock(TestCase):
def test_lock_initially_not_held(self):
l = DummyLock()
self.assertFalse(l.is_locked())
def test_lock_not_reentrant(self):
# can't take the underlying lock twice
l = DummyLock()
l.lock_read()
self.assertRaises(LockError, l.lock_read)
def test_detect_underlock(self):
l = DummyLock()
self.assertRaises(LockError, l.unlock)
def test_basic_locking(self):
# dummy lock works like a basic non reentrant lock
real_lock = DummyLock()
self.assertFalse(real_lock.is_locked())
# lock read and unlock
real_lock.lock_read()
self.assertTrue(real_lock.is_locked())
real_lock.unlock()
self.assertFalse(real_lock.is_locked())
# lock write and unlock
real_lock.lock_write()
self.assertTrue(real_lock.is_locked())
real_lock.unlock()
self.assertFalse(real_lock.is_locked())
# check calls
self.assertEqual(
['lock_read', 'unlock', 'lock_write', 'unlock'],
real_lock._calls)
def test_break_lock(self):
l = DummyLock()
l.lock_write()
l.break_lock()
self.assertFalse(l.is_locked())
self.assertEqual(
['lock_write', 'break'],
l._calls)
class TestCountedLock(TestCase):
def test_lock_unlock(self):
# Lock and unlock a counted lock
real_lock = DummyLock()
l = CountedLock(real_lock)
self.assertFalse(l.is_locked())
# can lock twice, although this isn't allowed on the underlying lock
l.lock_read()
l.lock_read()
self.assertTrue(l.is_locked())
# and release
l.unlock()
self.assertTrue(l.is_locked())
l.unlock()
self.assertFalse(l.is_locked())
self.assertEquals(
['lock_read', 'unlock'],
real_lock._calls)
def test_unlock_not_locked(self):
real_lock = DummyLock()
l = CountedLock(real_lock)
self.assertRaises(LockError, l.unlock)
def test_read_lock_while_write_locked(self):
real_lock = DummyLock()
l = CountedLock(real_lock)
l.lock_write()
l.lock_read()
l.lock_write()
l.unlock()
l.unlock()
l.unlock()
self.assertFalse(l.is_locked())
self.assertEquals(
['lock_write', 'unlock'],
real_lock._calls)
def test_write_lock_while_read_locked(self):
real_lock = DummyLock()
l = CountedLock(real_lock)
l.lock_read()
self.assertRaises(ReadOnlyError, l.lock_write)
self.assertRaises(ReadOnlyError, l.lock_write)
l.unlock()
self.assertFalse(l.is_locked())
self.assertEquals(
['lock_read', 'unlock'],
real_lock._calls)
def test_break_lock(self):
real_lock = DummyLock()
l = CountedLock(real_lock)
l.lock_write()
l.lock_write()
self.assertTrue(real_lock.is_locked())
l.break_lock()
self.assertFalse(l.is_locked())
self.assertFalse(real_lock.is_locked())
|