/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 bzrlib/tests/test_lockable_files.py

Merge and cleanup pre-external-reference-repository tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
from StringIO import StringIO
18
18
 
27
27
from bzrlib.symbol_versioning import (
28
28
    deprecated_in,
29
29
    )
30
 
from bzrlib.tests import (
31
 
    TestCaseInTempDir,
32
 
    TestNotApplicable,
33
 
    )
 
30
from bzrlib.tests import TestCaseInTempDir
34
31
from bzrlib.tests.test_smart import TestCaseWithSmartMedium
35
32
from bzrlib.tests.test_transactions import DummyWeave
36
33
from bzrlib.transactions import (PassThroughTransaction,
46
43
# so won't modernize them now. - mbp 20080430
47
44
class _TestLockableFiles_mixin(object):
48
45
 
 
46
    def test_read_write(self):
 
47
        self.assertRaises(NoSuchFile,
 
48
            self.applyDeprecated,
 
49
            deprecated_in((1, 5, 0)),
 
50
            self.lockable.get, 'foo')
 
51
        self.assertRaises(NoSuchFile,
 
52
            self.applyDeprecated,
 
53
            deprecated_in((1, 5, 0)),
 
54
            self.lockable.get_utf8, 'foo')
 
55
        self.lockable.lock_write()
 
56
        try:
 
57
            unicode_string = u'bar\u1234'
 
58
            self.assertEqual(4, len(unicode_string))
 
59
            byte_string = unicode_string.encode('utf-8')
 
60
            self.assertEqual(6, len(byte_string))
 
61
            self.assertRaises(UnicodeEncodeError,
 
62
                self.applyDeprecated,
 
63
                deprecated_in((1, 6, 0)),
 
64
                self.lockable.put, 'foo',
 
65
                StringIO(unicode_string))
 
66
            self.applyDeprecated(
 
67
                deprecated_in((1, 6, 0)),
 
68
                self.lockable.put,
 
69
                'foo', StringIO(byte_string))
 
70
            byte_stream = self.applyDeprecated(
 
71
                deprecated_in((1, 5, 0)),
 
72
                self.lockable.get,
 
73
                'foo')
 
74
            self.assertEqual(byte_string, byte_stream.read())
 
75
            unicode_stream = self.applyDeprecated(
 
76
                deprecated_in((1, 5, 0)),
 
77
                self.lockable.get_utf8,
 
78
                'foo')
 
79
            self.assertEqual(unicode_string,
 
80
                unicode_stream.read())
 
81
            self.assertRaises(BzrBadParameterNotString,
 
82
                self.applyDeprecated,
 
83
                deprecated_in((1, 6, 0)),
 
84
                self.lockable.put_utf8,
 
85
                'bar',
 
86
                StringIO(unicode_string))
 
87
            self.applyDeprecated(
 
88
                deprecated_in((1, 6, 0)),
 
89
                self.lockable.put_utf8,
 
90
                'bar',
 
91
                unicode_string)
 
92
            unicode_stream = self.applyDeprecated(
 
93
                deprecated_in((1, 5, 0)),
 
94
                self.lockable.get_utf8,
 
95
                'bar')
 
96
            self.assertEqual(unicode_string,
 
97
                unicode_stream.read())
 
98
            byte_stream = self.applyDeprecated(
 
99
                deprecated_in((1, 5, 0)),
 
100
                self.lockable.get,
 
101
                'bar')
 
102
            self.assertEqual(byte_string, byte_stream.read())
 
103
            self.applyDeprecated(
 
104
                deprecated_in((1, 6, 0)),
 
105
                self.lockable.put_bytes,
 
106
                'raw', 'raw\xffbytes')
 
107
            byte_stream = self.applyDeprecated(
 
108
                deprecated_in((1, 5, 0)),
 
109
                self.lockable.get,
 
110
                'raw')
 
111
            self.assertEqual('raw\xffbytes', byte_stream.read())
 
112
        finally:
 
113
            self.lockable.unlock()
 
114
 
 
115
    def test_locks(self):
 
116
        self.lockable.lock_read()
 
117
        try:
 
118
            self.assertRaises(ReadOnlyError, self.lockable.put, 'foo',
 
119
                              StringIO('bar\u1234'))
 
120
        finally:
 
121
            self.lockable.unlock()
 
122
 
49
123
    def test_transactions(self):
50
124
        self.assertIs(self.lockable.get_transaction().__class__,
51
125
                      PassThroughTransaction)
68
142
 
69
143
    def test__escape(self):
70
144
        self.assertEqual('%25', self.lockable._escape('%'))
71
 
 
 
145
        
72
146
    def test__escape_empty(self):
73
147
        self.assertEqual('', self.lockable._escape(''))
74
148
 
80
154
        except NotImplementedError:
81
155
            # this lock cannot be broken
82
156
            self.lockable.unlock()
83
 
            raise TestNotApplicable("%r is not breakable" % (self.lockable,))
 
157
            return
84
158
        l2 = self.get_lockable()
85
159
        orig_factory = bzrlib.ui.ui_factory
86
160
        # silent ui - no need for stdout
87
 
        bzrlib.ui.ui_factory = bzrlib.ui.CannedInputUIFactory([True])
 
161
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
 
162
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
88
163
        try:
89
164
            l2.break_lock()
90
165
        finally:
98
173
 
99
174
    def test_lock_write_returns_None_refuses_token(self):
100
175
        token = self.lockable.lock_write()
101
 
        self.addCleanup(self.lockable.unlock)
102
 
        if token is not None:
103
 
            # This test does not apply, because this lockable supports
104
 
            # tokens.
105
 
            raise TestNotApplicable("%r uses tokens" % (self.lockable,))
106
 
        self.assertRaises(errors.TokenLockingNotSupported,
107
 
                          self.lockable.lock_write, token='token')
 
176
        try:
 
177
            if token is not None:
 
178
                # This test does not apply, because this lockable supports
 
179
                # tokens.
 
180
                return
 
181
            self.assertRaises(errors.TokenLockingNotSupported,
 
182
                              self.lockable.lock_write, token='token')
 
183
        finally:
 
184
            self.lockable.unlock()
108
185
 
109
186
    def test_lock_write_returns_token_when_given_token(self):
110
187
        token = self.lockable.lock_write()
111
 
        self.addCleanup(self.lockable.unlock)
112
 
        if token is None:
113
 
            # This test does not apply, because this lockable refuses
114
 
            # tokens.
115
 
            return
116
 
        new_lockable = self.get_lockable()
117
 
        token_from_new_lockable = new_lockable.lock_write(token=token)
118
 
        self.addCleanup(new_lockable.unlock)
119
 
        self.assertEqual(token, token_from_new_lockable)
 
188
        try:
 
189
            if token is None:
 
190
                # This test does not apply, because this lockable refuses
 
191
                # tokens.
 
192
                return
 
193
            new_lockable = self.get_lockable()
 
194
            token_from_new_lockable = new_lockable.lock_write(token=token)
 
195
            try:
 
196
                self.assertEqual(token, token_from_new_lockable)
 
197
            finally:
 
198
                new_lockable.unlock()
 
199
        finally:
 
200
            self.lockable.unlock()
120
201
 
121
202
    def test_lock_write_raises_on_token_mismatch(self):
122
203
        token = self.lockable.lock_write()
123
 
        self.addCleanup(self.lockable.unlock)
124
 
        if token is None:
125
 
            # This test does not apply, because this lockable refuses
126
 
            # tokens.
127
 
            return
128
 
        different_token = token + 'xxx'
129
 
        # Re-using the same lockable instance with a different token will
130
 
        # raise TokenMismatch.
131
 
        self.assertRaises(errors.TokenMismatch,
132
 
                          self.lockable.lock_write, token=different_token)
133
 
        # A separate instance for the same lockable will also raise
134
 
        # TokenMismatch.
135
 
        # This detects the case where a caller claims to have a lock (via
136
 
        # the token) for an external resource, but doesn't (the token is
137
 
        # different).  Clients need a separate lock object to make sure the
138
 
        # external resource is probed, whereas the existing lock object
139
 
        # might cache.
140
 
        new_lockable = self.get_lockable()
141
 
        self.assertRaises(errors.TokenMismatch,
142
 
                          new_lockable.lock_write, token=different_token)
 
204
        try:
 
205
            if token is None:
 
206
                # This test does not apply, because this lockable refuses
 
207
                # tokens.
 
208
                return
 
209
            different_token = token + 'xxx'
 
210
            # Re-using the same lockable instance with a different token will
 
211
            # raise TokenMismatch.
 
212
            self.assertRaises(errors.TokenMismatch,
 
213
                              self.lockable.lock_write, token=different_token)
 
214
            # A seperate instance for the same lockable will also raise
 
215
            # TokenMismatch.
 
216
            # This detects the case where a caller claims to have a lock (via
 
217
            # the token) for an external resource, but doesn't (the token is
 
218
            # different).  Clients need a seperate lock object to make sure the
 
219
            # external resource is probed, whereas the existing lock object
 
220
            # might cache.
 
221
            new_lockable = self.get_lockable()
 
222
            self.assertRaises(errors.TokenMismatch,
 
223
                              new_lockable.lock_write, token=different_token)
 
224
        finally:
 
225
            self.lockable.unlock()
143
226
 
144
227
    def test_lock_write_with_matching_token(self):
145
228
        # If the token matches, so no exception is raised by lock_write.
146
229
        token = self.lockable.lock_write()
147
 
        self.addCleanup(self.lockable.unlock)
148
 
        if token is None:
149
 
            # This test does not apply, because this lockable refuses
150
 
            # tokens.
151
 
            return
152
 
        # The same instance will accept a second lock_write if the specified
153
 
        # token matches.
154
 
        self.lockable.lock_write(token=token)
155
 
        self.lockable.unlock()
156
 
        # Calling lock_write on a new instance for the same lockable will
157
 
        # also succeed.
158
 
        new_lockable = self.get_lockable()
159
 
        new_lockable.lock_write(token=token)
160
 
        new_lockable.unlock()
 
230
        try:
 
231
            if token is None:
 
232
                # This test does not apply, because this lockable refuses
 
233
                # tokens.
 
234
                return
 
235
            # The same instance will accept a second lock_write if the specified
 
236
            # token matches.
 
237
            self.lockable.lock_write(token=token)
 
238
            self.lockable.unlock()
 
239
            # Calling lock_write on a new instance for the same lockable will
 
240
            # also succeed.
 
241
            new_lockable = self.get_lockable()
 
242
            new_lockable.lock_write(token=token)
 
243
            new_lockable.unlock()
 
244
        finally:
 
245
            self.lockable.unlock()
161
246
 
162
247
    def test_unlock_after_lock_write_with_token(self):
163
248
        # If lock_write did not physically acquire the lock (because it was
164
249
        # passed a token), then unlock should not physically release it.
165
250
        token = self.lockable.lock_write()
166
 
        self.addCleanup(self.lockable.unlock)
167
 
        if token is None:
168
 
            # This test does not apply, because this lockable refuses
169
 
            # tokens.
170
 
            return
171
 
        new_lockable = self.get_lockable()
172
 
        new_lockable.lock_write(token=token)
173
 
        new_lockable.unlock()
174
 
        self.assertTrue(self.lockable.get_physical_lock_status())
 
251
        try:
 
252
            if token is None:
 
253
                # This test does not apply, because this lockable refuses
 
254
                # tokens.
 
255
                return
 
256
            new_lockable = self.get_lockable()
 
257
            new_lockable.lock_write(token=token)
 
258
            new_lockable.unlock()
 
259
            self.assertTrue(self.lockable.get_physical_lock_status())
 
260
        finally:
 
261
            self.lockable.unlock()
175
262
 
176
263
    def test_lock_write_with_token_fails_when_unlocked(self):
177
264
        # Lock and unlock to get a superficially valid token.  This mimics a
242
329
        # But should be relockable with a token.
243
330
        self.lockable.lock_write(token=token)
244
331
        self.lockable.unlock()
245
 
        # Cleanup: we should still be able to get the lock, but we restore the
246
 
        # behavior to clearing the lock when unlocking.
247
 
        self.lockable.lock_write(token=token)
248
 
        self.lockable.dont_leave_in_place()
249
 
        self.lockable.unlock()
250
332
 
251
333
    def test_dont_leave_in_place(self):
252
334
        token = self.lockable.lock_write()
271
353
        third_lockable.unlock()
272
354
 
273
355
 
274
 
# This method of adapting tests to parameters is different to
275
 
# the TestProviderAdapters used elsewhere, but seems simpler for this
276
 
# case.
 
356
# This method of adapting tests to parameters is different to 
 
357
# the TestProviderAdapters used elsewhere, but seems simpler for this 
 
358
# case.  
277
359
class TestLockableFiles_TransportLock(TestCaseInTempDir,
278
360
                                      _TestLockableFiles_mixin):
279
361
 
285
367
        self.lockable = self.get_lockable()
286
368
        self.lockable.create_lock()
287
369
 
288
 
    def stop_server(self):
289
 
        super(TestLockableFiles_TransportLock, self).stop_server()
 
370
    def tearDown(self):
 
371
        super(TestLockableFiles_TransportLock, self).tearDown()
290
372
        # free the subtransport so that we do not get a 5 second
291
373
        # timeout due to the SFTP connection cache.
292
 
        try:
293
 
            del self.sub_transport
294
 
        except AttributeError:
295
 
            pass
 
374
        del self.sub_transport
296
375
 
297
376
    def get_lockable(self):
298
377
        return LockableFiles(self.sub_transport, 'my-lock', TransportLock)
299
 
 
 
378
        
300
379
 
301
380
class TestLockableFiles_LockDir(TestCaseInTempDir,
302
381
                              _TestLockableFiles_mixin):
306
385
        TestCaseInTempDir.setUp(self)
307
386
        self.transport = get_transport('.')
308
387
        self.lockable = self.get_lockable()
309
 
        # the lock creation here sets mode - test_permissions on branch
310
 
        # tests that implicitly, but it might be a good idea to factor
 
388
        # the lock creation here sets mode - test_permissions on branch 
 
389
        # tests that implicitly, but it might be a good idea to factor 
311
390
        # out the mode checking logic and have it applied to loackable files
312
391
        # directly. RBC 20060418
313
392
        self.lockable.create_lock()