1
# Copyright (C) 2009, 2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for OS Locks."""
26
from bzrlib.tests.scenarios import load_tests_apply_scenarios
29
load_tests = load_tests_apply_scenarios
32
class TestOSLock(tests.TestCaseInTempDir):
36
'write_lock': write_lock,
37
'read_lock': read_lock})
38
for name, write_lock, read_lock in lock._lock_classes]
44
super(TestOSLock, self).setUp()
45
self.build_tree(['a-lock-file'])
47
def test_create_read_lock(self):
48
r_lock = self.read_lock('a-lock-file')
51
def test_create_write_lock(self):
52
w_lock = self.write_lock('a-lock-file')
55
def test_read_locks_share(self):
56
r_lock = self.read_lock('a-lock-file')
58
lock2 = self.read_lock('a-lock-file')
63
def test_write_locks_are_exclusive(self):
64
w_lock = self.write_lock('a-lock-file')
66
self.assertRaises(errors.LockContention,
67
self.write_lock, 'a-lock-file')
71
def test_read_locks_block_write_locks(self):
72
r_lock = self.read_lock('a-lock-file')
74
if lock.have_fcntl and self.write_lock is lock._fcntl_WriteLock:
75
# With -Dlock, fcntl locks are properly exclusive
76
debug.debug_flags.add('strict_locks')
77
self.assertRaises(errors.LockContention,
78
self.write_lock, 'a-lock-file')
80
debug.debug_flags.remove('strict_locks')
82
w_lock = self.write_lock('a-lock-file')
83
except errors.LockContention:
84
self.fail('Unexpected success. fcntl read locks'
85
' do not usually block write locks')
88
self.knownFailure('fcntl read locks don\'t'
89
' block write locks without -Dlock')
91
self.assertRaises(errors.LockContention,
92
self.write_lock, 'a-lock-file')
96
def test_write_locks_block_read_lock(self):
97
w_lock = self.write_lock('a-lock-file')
99
if lock.have_fcntl and self.read_lock is lock._fcntl_ReadLock:
100
# With -Dlock, fcntl locks are properly exclusive
101
debug.debug_flags.add('strict_locks')
102
self.assertRaises(errors.LockContention,
103
self.read_lock, 'a-lock-file')
105
debug.debug_flags.remove('strict_locks')
107
r_lock = self.read_lock('a-lock-file')
108
except errors.LockContention:
109
self.fail('Unexpected success. fcntl write locks'
110
' do not usually block read locks')
113
self.knownFailure('fcntl write locks don\'t'
114
' block read locks without -Dlock')
116
self.assertRaises(errors.LockContention,
117
self.read_lock, 'a-lock-file')
122
def test_temporary_write_lock(self):
123
r_lock = self.read_lock('a-lock-file')
125
status, w_lock = r_lock.temporary_write_lock()
126
self.assertTrue(status)
127
# This should block another write lock
129
self.assertRaises(errors.LockContention,
130
self.write_lock, 'a-lock-file')
132
r_lock = w_lock.restore_read_lock()
133
# We should be able to take a read lock now
134
r_lock2 = self.read_lock('a-lock-file')
139
def test_temporary_write_lock_fails(self):
140
r_lock = self.read_lock('a-lock-file')
142
r_lock2 = self.read_lock('a-lock-file')
144
status, w_lock = r_lock.temporary_write_lock()
145
self.assertFalse(status)
146
# Taking out the lock requires unlocking and locking again, so
147
# we have to replace the original object
151
# We should be able to take a read lock now
152
r_lock2 = self.read_lock('a-lock-file')