~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_lock/test_lock.py

  • Committer: John Arbash Meinel
  • Date: 2007-03-14 20:15:52 UTC
  • mto: (2353.4.2 locking)
  • mto: This revision was merged to the branch mainline in revision 2360.
  • Revision ID: john@arbash-meinel.com-20070314201552-bjtfua57456dviep
Update the lock code and test code so that if more than one
lock implementation is available, they will both be tested.

It is quite a bit of overhead, for a case where we are likely to only have 1
real lock implementation per platform, but hey, for now we have 2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
from bzrlib import (
20
20
    errors,
21
 
    lock,
22
21
    osutils,
23
 
    tests,
24
22
    )
25
23
 
26
 
 
27
 
class TestLock(tests.TestCaseInTempDir):
 
24
from bzrlib.tests.per_lock import TestCaseWithLock
 
25
 
 
26
 
 
27
class TestLock(TestCaseWithLock):
28
28
 
29
29
    def setUp(self):
30
30
        super(TestLock, self).setUp()
32
32
 
33
33
    def test_read_lock(self):
34
34
        """Smoke test for read locks."""
35
 
        a_lock = lock.ReadLock('a-file')
 
35
        a_lock = self.read_lock('a-file')
36
36
        self.addCleanup(a_lock.unlock)
37
37
        # The lock file should be opened for reading
38
38
        txt = a_lock.f.read()
40
40
 
41
41
    def test_create_if_needed_read(self):
42
42
        """We will create the file if it doesn't exist yet."""
43
 
        a_lock = lock.ReadLock('other-file')
 
43
        a_lock = self.read_lock('other-file')
44
44
        self.addCleanup(a_lock.unlock)
45
45
        txt = a_lock.f.read()
46
46
        self.assertEqual('', txt)
47
47
 
48
48
    def test_create_if_needed_write(self):
49
49
        """We will create the file if it doesn't exist yet."""
50
 
        a_lock = lock.WriteLock('other-file')
 
50
        a_lock = self.write_lock('other-file')
51
51
        self.addCleanup(a_lock.unlock)
52
52
        txt = a_lock.f.read()
53
53
        self.assertEqual('', txt)
64
64
        osutils.make_readonly('a-file')
65
65
        # Make sure the file is read-only (on all platforms)
66
66
        self.assertRaises(IOError, open, 'a-file', 'rb+')
67
 
        a_lock = lock.ReadLock('a-file')
 
67
        a_lock = self.read_lock('a-file')
68
68
        a_lock.unlock()
69
69
 
70
70
        # TODO: jam 20070313 This should be a specific subclass
71
 
        self.assertRaises(errors.ReadOnlyLockError, lock.WriteLock, 'a-file')
 
71
        self.assertRaises(errors.ReadOnlyLockError, self.write_lock, 'a-file')
72
72
 
73
73
    def test_write_lock(self):
74
74
        """Smoke test for write locks."""
75
 
        a_lock = lock.WriteLock('a-file')
 
75
        a_lock = self.write_lock('a-file')
76
76
        self.addCleanup(a_lock.unlock)
77
77
        # You should be able to read and write to the lock file.
78
78
        txt = a_lock.f.read()
87
87
 
88
88
    def test_multiple_read_locks(self):
89
89
        """You can take out more than one read lock on the same file."""
90
 
        a_lock = lock.ReadLock('a-file')
 
90
        a_lock = self.read_lock('a-file')
91
91
        self.addCleanup(a_lock.unlock)
92
 
        b_lock = lock.ReadLock('a-file')
 
92
        b_lock = self.read_lock('a-file')
93
93
        self.addCleanup(b_lock.unlock)
94
94
 
95
95
    def test_multiple_write_locks_exclude(self):
96
96
        """Taking out more than one write lock should fail."""
97
 
        a_lock = lock.WriteLock('a-file')
 
97
        a_lock = self.write_lock('a-file')
98
98
        self.addCleanup(a_lock.unlock)
99
99
        # Taking out a lock on a locked file should raise LockContention
100
 
        self.assertRaises(errors.LockContention, lock.WriteLock, 'a-file')
 
100
        self.assertRaises(errors.LockContention, self.write_lock, 'a-file')
101
101
 
102
102
    def _disabled_test_read_then_write_excludes(self):
103
103
        """If a file is read-locked, taking out a write lock should fail."""
104
 
        a_lock = lock.ReadLock('a-file')
 
104
        a_lock = self.read_lock('a-file')
105
105
        self.addCleanup(a_lock.unlock)
106
106
        # Taking out a lock on a locked file should raise LockContention
107
 
        self.assertRaises(errors.LockContention, lock.WriteLock, 'a-file')
 
107
        self.assertRaises(errors.LockContention, self.write_lock, 'a-file')
108
108
 
109
109
    def _disabled_test_write_then_read_excludes(self):
110
110
        """If a file is write-locked, taking out a read lock should fail.
112
112
        The file is exclusively owned by the write lock, so we shouldn't be
113
113
        able to take out a shared read lock.
114
114
        """
115
 
        a_lock = lock.WriteLock('a-file')
 
115
        a_lock = self.write_lock('a-file')
116
116
        self.addCleanup(a_lock.unlock)
117
117
        # Taking out a lock on a locked file should raise LockContention
118
 
        self.assertRaises(errors.LockContention, lock.ReadLock, 'a-file')
 
118
        self.assertRaises(errors.LockContention, self.read_lock, 'a-file')