27
27
from bzrlib.symbol_versioning import (
30
from bzrlib.tests import TestCaseInTempDir
30
from bzrlib.tests import (
31
34
from bzrlib.tests.test_smart import TestCaseWithSmartMedium
32
35
from bzrlib.tests.test_transactions import DummyWeave
33
36
from bzrlib.transactions import (PassThroughTransaction,
44
47
class _TestLockableFiles_mixin(object):
46
49
def test_read_write(self):
47
self.assertRaises(NoSuchFile, self.lockable.get, 'foo')
50
self.assertRaises(NoSuchFile,
52
deprecated_in((1, 5, 0)),
53
self.lockable.get, 'foo')
48
54
self.assertRaises(NoSuchFile,
49
55
self.applyDeprecated,
50
56
deprecated_in((1, 5, 0)),
55
61
self.assertEqual(4, len(unicode_string))
56
62
byte_string = unicode_string.encode('utf-8')
57
63
self.assertEqual(6, len(byte_string))
58
self.assertRaises(UnicodeEncodeError, self.lockable.put, 'foo',
59
StringIO(unicode_string))
60
self.lockable.put('foo', StringIO(byte_string))
61
self.assertEqual(byte_string,
62
self.lockable.get('foo').read())
64
self.assertRaises(UnicodeEncodeError,
66
deprecated_in((1, 6, 0)),
67
self.lockable.put, 'foo',
68
StringIO(unicode_string))
70
deprecated_in((1, 6, 0)),
72
'foo', StringIO(byte_string))
73
byte_stream = self.applyDeprecated(
74
deprecated_in((1, 5, 0)),
77
self.assertEqual(byte_string, byte_stream.read())
63
78
unicode_stream = self.applyDeprecated(
64
79
deprecated_in((1, 5, 0)),
65
80
self.lockable.get_utf8,
67
82
self.assertEqual(unicode_string,
68
83
unicode_stream.read())
69
84
self.assertRaises(BzrBadParameterNotString,
70
self.lockable.put_utf8,
72
StringIO(unicode_string)
74
self.lockable.put_utf8('bar', unicode_string)
86
deprecated_in((1, 6, 0)),
87
self.lockable.put_utf8,
89
StringIO(unicode_string))
91
deprecated_in((1, 6, 0)),
92
self.lockable.put_utf8,
75
95
unicode_stream = self.applyDeprecated(
76
96
deprecated_in((1, 5, 0)),
77
97
self.lockable.get_utf8,
79
99
self.assertEqual(unicode_string,
80
100
unicode_stream.read())
81
self.assertEqual(byte_string,
82
self.lockable.get('bar').read())
83
self.lockable.put_bytes('raw', 'raw\xffbytes')
84
self.assertEqual('raw\xffbytes',
85
self.lockable.get('raw').read())
101
byte_stream = self.applyDeprecated(
102
deprecated_in((1, 5, 0)),
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)),
114
self.assertEqual('raw\xffbytes', byte_stream.read())
87
116
self.lockable.unlock()
89
118
def test_locks(self):
90
119
self.lockable.lock_read()
92
self.assertRaises(ReadOnlyError, self.lockable.put, 'foo',
121
self.assertRaises(ReadOnlyError, self.lockable.put, 'foo',
93
122
StringIO('bar\u1234'))
95
124
self.lockable.unlock()
128
157
except NotImplementedError:
129
158
# this lock cannot be broken
130
159
self.lockable.unlock()
160
raise TestNotApplicable("%r is not breakable" % (self.lockable,))
132
161
l2 = self.get_lockable()
133
162
orig_factory = bzrlib.ui.ui_factory
134
163
# silent ui - no need for stdout
151
180
if token is not None:
152
181
# This test does not apply, because this lockable supports
183
raise TestNotApplicable("%r uses tokens" % (self.lockable,))
155
184
self.assertRaises(errors.TokenLockingNotSupported,
156
185
self.lockable.lock_write, token='token')
345
374
super(TestLockableFiles_TransportLock, self).tearDown()
346
375
# free the subtransport so that we do not get a 5 second
347
376
# timeout due to the SFTP connection cache.
348
del self.sub_transport
378
del self.sub_transport
379
except AttributeError:
350
382
def get_lockable(self):
351
383
return LockableFiles(self.sub_transport, 'my-lock', TransportLock)