~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lockable_files.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-03-16 14:01:20 UTC
  • mfrom: (3280.2.5 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080316140120-i3yq8yr1l66m11h7
Start 1.4 development

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
24
24
    )
25
25
from bzrlib.errors import BzrBadParameterNotString, NoSuchFile, ReadOnlyError
26
26
from bzrlib.lockable_files import LockableFiles, TransportLock
27
 
from bzrlib.symbol_versioning import (
28
 
    deprecated_in,
29
 
    )
30
 
from bzrlib.tests import (
31
 
    TestCaseInTempDir,
32
 
    TestNotApplicable,
33
 
    )
 
27
from bzrlib.tests import TestCaseInTempDir
34
28
from bzrlib.tests.test_smart import TestCaseWithSmartMedium
35
29
from bzrlib.tests.test_transactions import DummyWeave
36
30
from bzrlib.transactions import (PassThroughTransaction,
41
35
 
42
36
 
43
37
# these tests are applied in each parameterized suite for LockableFiles
44
 
#
45
 
# they use an old style of parameterization, but we want to remove this class
46
 
# so won't modernize them now. - mbp 20080430
47
38
class _TestLockableFiles_mixin(object):
48
39
 
49
40
    def test_read_write(self):
50
 
        self.assertRaises(NoSuchFile,
51
 
            self.applyDeprecated,
52
 
            deprecated_in((1, 5, 0)),
53
 
            self.lockable.get, 'foo')
54
 
        self.assertRaises(NoSuchFile,
55
 
            self.applyDeprecated,
56
 
            deprecated_in((1, 5, 0)),
57
 
            self.lockable.get_utf8, 'foo')
 
41
        self.assertRaises(NoSuchFile, self.lockable.get, 'foo')
 
42
        self.assertRaises(NoSuchFile, self.lockable.get_utf8, 'foo')
58
43
        self.lockable.lock_write()
59
44
        try:
60
45
            unicode_string = u'bar\u1234'
61
46
            self.assertEqual(4, len(unicode_string))
62
47
            byte_string = unicode_string.encode('utf-8')
63
48
            self.assertEqual(6, len(byte_string))
64
 
            self.assertRaises(UnicodeEncodeError,
65
 
                self.applyDeprecated,
66
 
                deprecated_in((1, 6, 0)),
67
 
                self.lockable.put, 'foo',
68
 
                StringIO(unicode_string))
69
 
            self.applyDeprecated(
70
 
                deprecated_in((1, 6, 0)),
71
 
                self.lockable.put,
72
 
                'foo', StringIO(byte_string))
73
 
            byte_stream = self.applyDeprecated(
74
 
                deprecated_in((1, 5, 0)),
75
 
                self.lockable.get,
76
 
                'foo')
77
 
            self.assertEqual(byte_string, byte_stream.read())
78
 
            unicode_stream = self.applyDeprecated(
79
 
                deprecated_in((1, 5, 0)),
80
 
                self.lockable.get_utf8,
81
 
                'foo')
 
49
            self.assertRaises(UnicodeEncodeError, self.lockable.put, 'foo',
 
50
                              StringIO(unicode_string))
 
51
            self.lockable.put('foo', StringIO(byte_string))
 
52
            self.assertEqual(byte_string,
 
53
                             self.lockable.get('foo').read())
82
54
            self.assertEqual(unicode_string,
83
 
                unicode_stream.read())
 
55
                             self.lockable.get_utf8('foo').read())
84
56
            self.assertRaises(BzrBadParameterNotString,
85
 
                self.applyDeprecated,
86
 
                deprecated_in((1, 6, 0)),
87
 
                self.lockable.put_utf8,
88
 
                'bar',
89
 
                StringIO(unicode_string))
90
 
            self.applyDeprecated(
91
 
                deprecated_in((1, 6, 0)),
92
 
                self.lockable.put_utf8,
93
 
                'bar',
94
 
                unicode_string)
95
 
            unicode_stream = self.applyDeprecated(
96
 
                deprecated_in((1, 5, 0)),
97
 
                self.lockable.get_utf8,
98
 
                'bar')
 
57
                              self.lockable.put_utf8,
 
58
                              'bar',
 
59
                              StringIO(unicode_string)
 
60
                              )
 
61
            self.lockable.put_utf8('bar', unicode_string)
99
62
            self.assertEqual(unicode_string,
100
 
                unicode_stream.read())
101
 
            byte_stream = self.applyDeprecated(
102
 
                deprecated_in((1, 5, 0)),
103
 
                self.lockable.get,
104
 
                'bar')
105
 
            self.assertEqual(byte_string, byte_stream.read())
106
 
            self.applyDeprecated(
107
 
                deprecated_in((1, 6, 0)),
108
 
                self.lockable.put_bytes,
109
 
                'raw', 'raw\xffbytes')
110
 
            byte_stream = self.applyDeprecated(
111
 
                deprecated_in((1, 5, 0)),
112
 
                self.lockable.get,
113
 
                'raw')
114
 
            self.assertEqual('raw\xffbytes', byte_stream.read())
 
63
                             self.lockable.get_utf8('bar').read())
 
64
            self.assertEqual(byte_string,
 
65
                             self.lockable.get('bar').read())
 
66
            self.lockable.put_bytes('raw', 'raw\xffbytes')
 
67
            self.assertEqual('raw\xffbytes',
 
68
                             self.lockable.get('raw').read())
115
69
        finally:
116
70
            self.lockable.unlock()
117
71
 
118
72
    def test_locks(self):
119
73
        self.lockable.lock_read()
120
74
        try:
121
 
            self.assertRaises(ReadOnlyError, self.lockable.put, 'foo',
 
75
            self.assertRaises(ReadOnlyError, self.lockable.put, 'foo', 
122
76
                              StringIO('bar\u1234'))
123
77
        finally:
124
78
            self.lockable.unlock()
157
111
        except NotImplementedError:
158
112
            # this lock cannot be broken
159
113
            self.lockable.unlock()
160
 
            raise TestNotApplicable("%r is not breakable" % (self.lockable,))
 
114
            return
161
115
        l2 = self.get_lockable()
162
116
        orig_factory = bzrlib.ui.ui_factory
163
117
        # silent ui - no need for stdout
180
134
            if token is not None:
181
135
                # This test does not apply, because this lockable supports
182
136
                # tokens.
183
 
                raise TestNotApplicable("%r uses tokens" % (self.lockable,))
 
137
                return
184
138
            self.assertRaises(errors.TokenLockingNotSupported,
185
139
                              self.lockable.lock_write, token='token')
186
140
        finally:
374
328
        super(TestLockableFiles_TransportLock, self).tearDown()
375
329
        # free the subtransport so that we do not get a 5 second
376
330
        # timeout due to the SFTP connection cache.
377
 
        try:
378
 
            del self.sub_transport
379
 
        except AttributeError:
380
 
            pass
 
331
        del self.sub_transport
381
332
 
382
333
    def get_lockable(self):
383
334
        return LockableFiles(self.sub_transport, 'my-lock', TransportLock)