/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_lock.py

  • Committer: John Arbash Meinel
  • Date: 2006-04-25 15:05:42 UTC
  • mfrom: (1185.85.85 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060425150542-c7b518dca9928691
[merge] the old bzr-encoding changes, reparenting them on bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2011 Canonical Ltd
2
 
#
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.
7
 
#
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.
12
 
#
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
16
 
 
17
 
"""Tests for OS Locks."""
18
 
 
19
 
 
20
 
from .. import (
21
 
    debug,
22
 
    errors,
23
 
    lock,
24
 
    tests,
25
 
    )
26
 
from .scenarios import load_tests_apply_scenarios
27
 
 
28
 
 
29
 
load_tests = load_tests_apply_scenarios
30
 
 
31
 
 
32
 
class TestOSLock(tests.TestCaseInTempDir):
33
 
 
34
 
    scenarios = [(
35
 
        name, {
36
 
            'write_lock': write_lock,
37
 
            'read_lock': read_lock})
38
 
        for name, write_lock, read_lock in lock._lock_classes]
39
 
 
40
 
    read_lock = None
41
 
    write_lock = None
42
 
 
43
 
    def setUp(self):
44
 
        super(TestOSLock, self).setUp()
45
 
        self.build_tree(['a-lock-file'])
46
 
 
47
 
    def test_create_read_lock(self):
48
 
        r_lock = self.read_lock('a-lock-file')
49
 
        r_lock.unlock()
50
 
 
51
 
    def test_create_write_lock(self):
52
 
        w_lock = self.write_lock('a-lock-file')
53
 
        w_lock.unlock()
54
 
 
55
 
    def test_read_locks_share(self):
56
 
        r_lock = self.read_lock('a-lock-file')
57
 
        try:
58
 
            lock2 = self.read_lock('a-lock-file')
59
 
            lock2.unlock()
60
 
        finally:
61
 
            r_lock.unlock()
62
 
 
63
 
    def test_write_locks_are_exclusive(self):
64
 
        w_lock = self.write_lock('a-lock-file')
65
 
        try:
66
 
            self.assertRaises(errors.LockContention,
67
 
                              self.write_lock, 'a-lock-file')
68
 
        finally:
69
 
            w_lock.unlock()
70
 
 
71
 
    def test_read_locks_block_write_locks(self):
72
 
        r_lock = self.read_lock('a-lock-file')
73
 
        try:
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')
79
 
                # But not without it
80
 
                debug.debug_flags.remove('strict_locks')
81
 
                try:
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')
86
 
                else:
87
 
                    w_lock.unlock()
88
 
                    self.knownFailure('fcntl read locks don\'t'
89
 
                                      ' block write locks without -Dlock')
90
 
            else:
91
 
                self.assertRaises(errors.LockContention,
92
 
                                  self.write_lock, 'a-lock-file')
93
 
        finally:
94
 
            r_lock.unlock()
95
 
 
96
 
    def test_write_locks_block_read_lock(self):
97
 
        w_lock = self.write_lock('a-lock-file')
98
 
        try:
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')
104
 
                # But not without it
105
 
                debug.debug_flags.remove('strict_locks')
106
 
                try:
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')
111
 
                else:
112
 
                    r_lock.unlock()
113
 
                    self.knownFailure('fcntl write locks don\'t'
114
 
                                      ' block read locks without -Dlock')
115
 
            else:
116
 
                self.assertRaises(errors.LockContention,
117
 
                                  self.read_lock, 'a-lock-file')
118
 
        finally:
119
 
            w_lock.unlock()
120
 
 
121
 
    def test_temporary_write_lock(self):
122
 
        r_lock = self.read_lock('a-lock-file')
123
 
        try:
124
 
            status, w_lock = r_lock.temporary_write_lock()
125
 
            self.assertTrue(status)
126
 
            # This should block another write lock
127
 
            try:
128
 
                self.assertRaises(errors.LockContention,
129
 
                                  self.write_lock, 'a-lock-file')
130
 
            finally:
131
 
                r_lock = w_lock.restore_read_lock()
132
 
            # We should be able to take a read lock now
133
 
            r_lock2 = self.read_lock('a-lock-file')
134
 
            r_lock2.unlock()
135
 
        finally:
136
 
            r_lock.unlock()
137
 
 
138
 
    def test_temporary_write_lock_fails(self):
139
 
        r_lock = self.read_lock('a-lock-file')
140
 
        try:
141
 
            r_lock2 = self.read_lock('a-lock-file')
142
 
            try:
143
 
                status, w_lock = r_lock.temporary_write_lock()
144
 
                self.assertFalse(status)
145
 
                # Taking out the lock requires unlocking and locking again, so
146
 
                # we have to replace the original object
147
 
                r_lock = w_lock
148
 
            finally:
149
 
                r_lock2.unlock()
150
 
            # We should be able to take a read lock now
151
 
            r_lock2 = self.read_lock('a-lock-file')
152
 
            r_lock2.unlock()
153
 
        finally:
154
 
            r_lock.unlock()