46
43
# so won't modernize them now. - mbp 20080430
47
44
class _TestLockableFiles_mixin(object):
46
def test_read_write(self):
47
self.assertRaises(NoSuchFile,
49
deprecated_in((1, 5, 0)),
50
self.lockable.get, 'foo')
51
self.assertRaises(NoSuchFile,
53
deprecated_in((1, 5, 0)),
54
self.lockable.get_utf8, 'foo')
55
self.lockable.lock_write()
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,
63
deprecated_in((1, 6, 0)),
64
self.lockable.put, 'foo',
65
StringIO(unicode_string))
67
deprecated_in((1, 6, 0)),
69
'foo', StringIO(byte_string))
70
byte_stream = self.applyDeprecated(
71
deprecated_in((1, 5, 0)),
74
self.assertEqual(byte_string, byte_stream.read())
75
unicode_stream = self.applyDeprecated(
76
deprecated_in((1, 5, 0)),
77
self.lockable.get_utf8,
79
self.assertEqual(unicode_string,
80
unicode_stream.read())
81
self.assertRaises(BzrBadParameterNotString,
83
deprecated_in((1, 6, 0)),
84
self.lockable.put_utf8,
86
StringIO(unicode_string))
88
deprecated_in((1, 6, 0)),
89
self.lockable.put_utf8,
92
unicode_stream = self.applyDeprecated(
93
deprecated_in((1, 5, 0)),
94
self.lockable.get_utf8,
96
self.assertEqual(unicode_string,
97
unicode_stream.read())
98
byte_stream = self.applyDeprecated(
99
deprecated_in((1, 5, 0)),
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)),
111
self.assertEqual('raw\xffbytes', byte_stream.read())
113
self.lockable.unlock()
115
def test_locks(self):
116
self.lockable.lock_read()
118
self.assertRaises(ReadOnlyError, self.lockable.put, 'foo',
119
StringIO('bar\u1234'))
121
self.lockable.unlock()
49
123
def test_transactions(self):
50
124
self.assertIs(self.lockable.get_transaction().__class__,
51
125
PassThroughTransaction)
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
105
raise TestNotApplicable("%r uses tokens" % (self.lockable,))
106
self.assertRaises(errors.TokenLockingNotSupported,
107
self.lockable.lock_write, token='token')
177
if token is not None:
178
# This test does not apply, because this lockable supports
181
self.assertRaises(errors.TokenLockingNotSupported,
182
self.lockable.lock_write, token='token')
184
self.lockable.unlock()
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)
113
# This test does not apply, because this lockable refuses
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)
190
# This test does not apply, because this lockable refuses
193
new_lockable = self.get_lockable()
194
token_from_new_lockable = new_lockable.lock_write(token=token)
196
self.assertEqual(token, token_from_new_lockable)
198
new_lockable.unlock()
200
self.lockable.unlock()
121
202
def test_lock_write_raises_on_token_mismatch(self):
122
203
token = self.lockable.lock_write()
123
self.addCleanup(self.lockable.unlock)
125
# This test does not apply, because this lockable refuses
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
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
140
new_lockable = self.get_lockable()
141
self.assertRaises(errors.TokenMismatch,
142
new_lockable.lock_write, token=different_token)
206
# This test does not apply, because this lockable refuses
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
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
221
new_lockable = self.get_lockable()
222
self.assertRaises(errors.TokenMismatch,
223
new_lockable.lock_write, token=different_token)
225
self.lockable.unlock()
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)
149
# This test does not apply, because this lockable refuses
152
# The same instance will accept a second lock_write if the specified
154
self.lockable.lock_write(token=token)
155
self.lockable.unlock()
156
# Calling lock_write on a new instance for the same lockable will
158
new_lockable = self.get_lockable()
159
new_lockable.lock_write(token=token)
160
new_lockable.unlock()
232
# This test does not apply, because this lockable refuses
235
# The same instance will accept a second lock_write if the specified
237
self.lockable.lock_write(token=token)
238
self.lockable.unlock()
239
# Calling lock_write on a new instance for the same lockable will
241
new_lockable = self.get_lockable()
242
new_lockable.lock_write(token=token)
243
new_lockable.unlock()
245
self.lockable.unlock()
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)
168
# This test does not apply, because this lockable refuses
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())
253
# This test does not apply, because this lockable refuses
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())
261
self.lockable.unlock()
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