~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lock.py

Define an explicit error when trying to grab a write lock on a readonly file.
Add some future tests that ensure write and read locks exclude eachother.
Currently fcntl locks do not exclude.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from bzrlib import (
20
20
    errors,
21
21
    lock,
 
22
    osutils,
22
23
    tests,
23
24
    )
24
25
 
55
56
        txt = a_lock.f.read()
56
57
        self.assertEqual('foo\n', txt)
57
58
 
 
59
    def test_readonly_file(self):
 
60
        """If the file is readonly, we can take a read lock.
 
61
 
 
62
        But we shouldn't be able to take a write lock.
 
63
        """
 
64
        osutils.make_readonly('a-file')
 
65
        # Make sure the file is read-only (on all platforms)
 
66
        self.assertRaises(IOError, open, 'a-file', 'rb+')
 
67
        a_lock = lock.ReadLock('a-file')
 
68
        a_lock.unlock()
 
69
 
 
70
        # TODO: jam 20070313 This should be a specific subclass
 
71
        self.assertRaises(errors.ReadOnlyLockError, lock.WriteLock, 'a-file')
 
72
 
58
73
    def test_write_lock(self):
59
74
        """Smoke test for write locks."""
60
75
        a_lock = lock.WriteLock('a-file')
80
95
        self.addCleanup(a_lock.unlock)
81
96
        # Taking out a lock on a locked file should raise LockContention
82
97
        self.assertRaises(errors.LockContention, lock.WriteLock, 'a-file')
 
98
 
 
99
    def _disabled_test_read_then_write_excludes(self):
 
100
        """If a file is read-locked, taking out a write lock should fail."""
 
101
        a_lock = lock.ReadLock('a-file')
 
102
        self.addCleanup(a_lock.unlock)
 
103
        # Taking out a lock on a locked file should raise LockContention
 
104
        self.assertRaises(errors.LockContention, lock.WriteLock, 'a-file')
 
105
 
 
106
    def _disabled_test_write_then_read_excludes(self):
 
107
        """If a file is write-locked, taking out a read lock should fail.
 
108
 
 
109
        The file is exclusively owned by the write lock, so we shouldn't be
 
110
        able to take out a shared read lock.
 
111
        """
 
112
        a_lock = lock.WriteLock('a-file')
 
113
        self.addCleanup(a_lock.unlock)
 
114
        # Taking out a lock on a locked file should raise LockContention
 
115
        self.assertRaises(errors.LockContention, lock.ReadLock, 'a-file')