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."""
25
class TestLock(tests.TestCaseInTempDir):
28
super(TestLock, self).setUp()
29
self.build_tree(['a-file'])
31
def test_read_lock(self):
32
"""Smoke test for read locks."""
33
a_lock = lock.ReadLock('a-file')
34
self.addCleanup(a_lock.unlock)
35
# The lock file should be opened for reading
37
self.assertEqual('contents of a-file\n', txt)
39
def test_create_if_needed_read(self):
40
"""We will create the file if it doesn't exist yet."""
41
a_lock = lock.ReadLock('other-file')
42
self.addCleanup(a_lock.unlock)
44
self.assertEqual('', txt)
46
def test_create_if_needed_write(self):
47
"""We will create the file if it doesn't exist yet."""
48
a_lock = lock.WriteLock('other-file')
49
self.addCleanup(a_lock.unlock)
51
self.assertEqual('', txt)
52
a_lock.f.write('foo\n')
55
self.assertEqual('foo\n', txt)
57
def test_write_lock(self):
58
"""Smoke test for write locks."""
59
a_lock = lock.WriteLock('a-file')
60
self.addCleanup(a_lock.unlock)
61
# You should be able to read and write to the lock file.
63
self.assertEqual('contents of a-file\n', txt)
64
a_lock.f.write('more content\n')
67
self.assertEqual('contents of a-file\nmore content\n', txt)
69
def test_multiple_read_locks(self):
70
"""You can take out more than one read lock on the same file."""
71
a_lock = lock.ReadLock('a-file')
72
self.addCleanup(a_lock.unlock)
73
b_lock = lock.ReadLock('a-file')
74
self.addCleanup(b_lock.unlock)