1
# Copyright (C) 2007 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 OS level locks."""
24
from bzrlib.tests.per_lock import TestCaseWithLock
27
class TestLock(TestCaseWithLock):
30
super(TestLock, self).setUp()
31
self.build_tree(['a-file'])
33
def test_read_lock(self):
34
"""Smoke test for read locks."""
35
a_lock = self.read_lock('a-file')
36
self.addCleanup(a_lock.unlock)
37
# The lock file should be opened for reading
39
self.assertEqual('contents of a-file\n', txt)
41
def test_create_if_needed_read(self):
42
"""We will create the file if it doesn't exist yet."""
43
a_lock = self.read_lock('other-file')
44
self.addCleanup(a_lock.unlock)
46
self.assertEqual('', txt)
48
def test_create_if_needed_write(self):
49
"""We will create the file if it doesn't exist yet."""
50
a_lock = self.write_lock('other-file')
51
self.addCleanup(a_lock.unlock)
53
self.assertEqual('', txt)
55
a_lock.f.write('foo\n')
58
self.assertEqual('foo\n', txt)
60
def test_readonly_file(self):
61
"""If the file is readonly, we can take a read lock.
63
But we shouldn't be able to take a write lock.
65
osutils.make_readonly('a-file')
66
# Make sure the file is read-only (on all platforms)
67
self.assertRaises(IOError, open, 'a-file', 'rb+')
68
a_lock = self.read_lock('a-file')
71
self.assertRaises(errors.ReadOnlyLockError, self.write_lock, 'a-file')
73
def test_write_lock(self):
74
"""Smoke test for write locks."""
75
a_lock = self.write_lock('a-file')
76
self.addCleanup(a_lock.unlock)
77
# You should be able to read and write to the lock file.
79
self.assertEqual('contents of a-file\n', txt)
80
# Win32 requires that you call seek() when switching between a read
81
# operation and a write operation.
83
a_lock.f.write('more content\n')
86
self.assertEqual('contents of a-file\nmore content\n', txt)
88
def test_multiple_read_locks(self):
89
"""You can take out more than one read lock on the same file."""
90
a_lock = self.read_lock('a-file')
91
self.addCleanup(a_lock.unlock)
92
b_lock = self.read_lock('a-file')
93
self.addCleanup(b_lock.unlock)
95
def test_multiple_write_locks_exclude(self):
96
"""Taking out more than one write lock should fail."""
97
a_lock = self.write_lock('a-file')
98
self.addCleanup(a_lock.unlock)
99
# Taking out a lock on a locked file should raise LockContention
100
self.assertRaises(errors.LockContention, self.write_lock, 'a-file')
102
def _disabled_test_read_then_write_excludes(self):
103
"""If a file is read-locked, taking out a write lock should fail."""
104
a_lock = self.read_lock('a-file')
105
self.addCleanup(a_lock.unlock)
106
# Taking out a lock on a locked file should raise LockContention
107
self.assertRaises(errors.LockContention, self.write_lock, 'a-file')
109
def test_read_unlock_write(self):
110
"""Make sure that unlocking allows us to lock write"""
111
a_lock = self.read_lock('a-file')
113
a_lock = self.write_lock('a-file')
116
# TODO: jam 20070319 fcntl read locks are not currently fully
117
# mutually exclusive with write locks. This will be fixed
118
# in the next release.
119
def _disabled_test_write_then_read_excludes(self):
120
"""If a file is write-locked, taking out a read lock should fail.
122
The file is exclusively owned by the write lock, so we shouldn't be
123
able to take out a shared read lock.
125
a_lock = self.write_lock('a-file')
126
self.addCleanup(a_lock.unlock)
127
# Taking out a lock on a locked file should raise LockContention
128
self.assertRaises(errors.LockContention, self.read_lock, 'a-file')
130
# TODO: jam 20070319 fcntl write locks are not currently fully
131
# mutually exclusive with read locks. This will be fixed
132
# in the next release.
133
def _disabled_test_write_unlock_read(self):
134
"""If we have removed the write lock, we can grab a read lock."""
135
a_lock = self.write_lock('a-file')
137
a_lock = self.read_lock('a-file')
140
def _disabled_test_multiple_read_unlock_write(self):
141
"""We can only grab a write lock if all read locks are done."""
142
a_lock = b_lock = c_lock = None
144
a_lock = self.read_lock('a-file')
145
b_lock = self.read_lock('a-file')
146
self.assertRaises(errors.LockContention, self.write_lock, 'a-file')
149
self.assertRaises(errors.LockContention, self.write_lock, 'a-file')
152
c_lock = self.write_lock('a-file')
157
if a_lock is not None:
159
if b_lock is not None:
161
if c_lock is not None: