~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lockdir.py

  • Committer: Robert Collins
  • Date: 2005-09-27 06:15:35 UTC
  • mto: (1092.3.4)
  • mto: This revision was merged to the branch mainline in revision 1391.
  • Revision ID: robertc@robertcollins.net-20050927061535-6321f7191439d108
print test case exceptions after the test log

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
"""Tests for LockDir"""
18
 
 
19
 
from cStringIO import StringIO
20
 
import os
21
 
from threading import Thread, Lock
22
 
import time
23
 
 
24
 
import bzrlib
25
 
from bzrlib import (
26
 
    config,
27
 
    errors,
28
 
    osutils,
29
 
    tests,
30
 
    )
31
 
from bzrlib.errors import (
32
 
        LockBreakMismatch,
33
 
        LockContention, LockError, UnlockableTransport,
34
 
        LockNotHeld, LockBroken
35
 
        )
36
 
from bzrlib.lockdir import LockDir
37
 
from bzrlib.tests import TestCaseWithTransport
38
 
from bzrlib.trace import note
39
 
 
40
 
# These tests sometimes use threads to test the behaviour of lock files with
41
 
# concurrent actors.  This is not a typical (or necessarily supported) use;
42
 
# they're really meant for guarding between processes.
43
 
 
44
 
# These tests are run on the default transport provided by the test framework
45
 
# (typically a local disk transport).  That can be changed by the --transport
46
 
# option to bzr selftest.  The required properties of the transport
47
 
# implementation are tested separately.  (The main requirement is just that
48
 
# they don't allow overwriting nonempty directories.)
49
 
 
50
 
class TestLockDir(TestCaseWithTransport):
51
 
    """Test LockDir operations"""
52
 
 
53
 
    def logging_report_function(self, fmt, *args):
54
 
        self._logged_reports.append((fmt, args))
55
 
 
56
 
    def setup_log_reporter(self, lock_dir):
57
 
        self._logged_reports = []
58
 
        lock_dir._report_function = self.logging_report_function
59
 
 
60
 
    def test_00_lock_creation(self):
61
 
        """Creation of lock file on a transport"""
62
 
        t = self.get_transport()
63
 
        lf = LockDir(t, 'test_lock')
64
 
        self.assertFalse(lf.is_held)
65
 
 
66
 
    def test_01_lock_repr(self):
67
 
        """Lock string representation"""
68
 
        lf = LockDir(self.get_transport(), 'test_lock')
69
 
        r = repr(lf)
70
 
        self.assertContainsRe(r, r'^LockDir\(.*/test_lock\)$')
71
 
 
72
 
    def test_02_unlocked_peek(self):
73
 
        lf = LockDir(self.get_transport(), 'test_lock')
74
 
        self.assertEqual(lf.peek(), None)
75
 
 
76
 
    def get_lock(self):
77
 
        return LockDir(self.get_transport(), 'test_lock')
78
 
 
79
 
    def test_unlock_after_break_raises(self):
80
 
        ld = self.get_lock()
81
 
        ld2 = self.get_lock()
82
 
        ld.create()
83
 
        ld.attempt_lock()
84
 
        ld2.force_break(ld2.peek())
85
 
        self.assertRaises(LockBroken, ld.unlock)
86
 
 
87
 
    def test_03_readonly_peek(self):
88
 
        lf = LockDir(self.get_readonly_transport(), 'test_lock')
89
 
        self.assertEqual(lf.peek(), None)
90
 
 
91
 
    def test_10_lock_uncontested(self):
92
 
        """Acquire and release a lock"""
93
 
        t = self.get_transport()
94
 
        lf = LockDir(t, 'test_lock')
95
 
        lf.create()
96
 
        lf.attempt_lock()
97
 
        try:
98
 
            self.assertTrue(lf.is_held)
99
 
        finally:
100
 
            lf.unlock()
101
 
            self.assertFalse(lf.is_held)
102
 
 
103
 
    def test_11_create_readonly_transport(self):
104
 
        """Fail to create lock on readonly transport"""
105
 
        t = self.get_readonly_transport()
106
 
        lf = LockDir(t, 'test_lock')
107
 
        self.assertRaises(UnlockableTransport, lf.create)
108
 
 
109
 
    def test_12_lock_readonly_transport(self):
110
 
        """Fail to lock on readonly transport"""
111
 
        lf = LockDir(self.get_transport(), 'test_lock')
112
 
        lf.create()
113
 
        lf = LockDir(self.get_readonly_transport(), 'test_lock')
114
 
        self.assertRaises(UnlockableTransport, lf.attempt_lock)
115
 
 
116
 
    def test_20_lock_contested(self):
117
 
        """Contention to get a lock"""
118
 
        t = self.get_transport()
119
 
        lf1 = LockDir(t, 'test_lock')
120
 
        lf1.create()
121
 
        lf1.attempt_lock()
122
 
        lf2 = LockDir(t, 'test_lock')
123
 
        try:
124
 
            # locking is between LockDir instances; aliases within 
125
 
            # a single process are not detected
126
 
            lf2.attempt_lock()
127
 
            self.fail('Failed to detect lock collision')
128
 
        except LockContention, e:
129
 
            self.assertEqual(e.lock, lf2)
130
 
            self.assertContainsRe(str(e),
131
 
                    r'^Could not acquire.*test_lock.*$')
132
 
        lf1.unlock()
133
 
 
134
 
    def test_20_lock_peek(self):
135
 
        """Peek at the state of a lock"""
136
 
        t = self.get_transport()
137
 
        lf1 = LockDir(t, 'test_lock')
138
 
        lf1.create()
139
 
        lf1.attempt_lock()
140
 
        # lock is held, should get some info on it
141
 
        info1 = lf1.peek()
142
 
        self.assertEqual(set(info1.keys()),
143
 
                         set(['user', 'nonce', 'hostname', 'pid', 'start_time']))
144
 
        # should get the same info if we look at it through a different
145
 
        # instance
146
 
        info2 = LockDir(t, 'test_lock').peek()
147
 
        self.assertEqual(info1, info2)
148
 
        # locks which are never used should be not-held
149
 
        self.assertEqual(LockDir(t, 'other_lock').peek(), None)
150
 
 
151
 
    def test_21_peek_readonly(self):
152
 
        """Peek over a readonly transport"""
153
 
        t = self.get_transport()
154
 
        lf1 = LockDir(t, 'test_lock')
155
 
        lf1.create()
156
 
        lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
157
 
        self.assertEqual(lf2.peek(), None)
158
 
        lf1.attempt_lock()
159
 
        info2 = lf2.peek()
160
 
        self.assertTrue(info2)
161
 
        self.assertEqual(info2['nonce'], lf1.nonce)
162
 
 
163
 
    def test_30_lock_wait_fail(self):
164
 
        """Wait on a lock, then fail
165
 
        
166
 
        We ask to wait up to 400ms; this should fail within at most one
167
 
        second.  (Longer times are more realistic but we don't want the test
168
 
        suite to take too long, and this should do for now.)
169
 
        """
170
 
        t = self.get_transport()
171
 
        lf1 = LockDir(t, 'test_lock')
172
 
        lf1.create()
173
 
        lf2 = LockDir(t, 'test_lock')
174
 
        self.setup_log_reporter(lf2)
175
 
        lf1.attempt_lock()
176
 
        try:
177
 
            before = time.time()
178
 
            self.assertRaises(LockContention, lf2.wait_lock,
179
 
                              timeout=0.4, poll=0.1)
180
 
            after = time.time()
181
 
            # it should only take about 0.4 seconds, but we allow more time in
182
 
            # case the machine is heavily loaded
183
 
            self.assertTrue(after - before <= 8.0, 
184
 
                    "took %f seconds to detect lock contention" % (after - before))
185
 
        finally:
186
 
            lf1.unlock()
187
 
        lock_base = lf2.transport.abspath(lf2.path)
188
 
        self.assertEqual(1, len(self._logged_reports))
189
 
        self.assertEqual('%s %s\n'
190
 
                         '%s\n%s\n'
191
 
                         'Will continue to try until %s\n',
192
 
                         self._logged_reports[0][0])
193
 
        args = self._logged_reports[0][1]
194
 
        self.assertEqual('Unable to obtain', args[0])
195
 
        self.assertEqual('lock %s' % (lock_base,), args[1])
196
 
        self.assertStartsWith(args[2], 'held by ')
197
 
        self.assertStartsWith(args[3], 'locked ')
198
 
        self.assertEndsWith(args[3], ' ago')
199
 
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
200
 
 
201
 
    def test_31_lock_wait_easy(self):
202
 
        """Succeed when waiting on a lock with no contention.
203
 
        """
204
 
        t = self.get_transport()
205
 
        lf1 = LockDir(t, 'test_lock')
206
 
        lf1.create()
207
 
        self.setup_log_reporter(lf1)
208
 
        try:
209
 
            before = time.time()
210
 
            lf1.wait_lock(timeout=0.4, poll=0.1)
211
 
            after = time.time()
212
 
            self.assertTrue(after - before <= 1.0)
213
 
        finally:
214
 
            lf1.unlock()
215
 
        self.assertEqual([], self._logged_reports)
216
 
 
217
 
    def test_32_lock_wait_succeed(self):
218
 
        """Succeed when trying to acquire a lock that gets released
219
 
 
220
 
        One thread holds on a lock and then releases it; another 
221
 
        tries to lock it.
222
 
        """
223
 
        t = self.get_transport()
224
 
        lf1 = LockDir(t, 'test_lock')
225
 
        lf1.create()
226
 
        lf1.attempt_lock()
227
 
 
228
 
        def wait_and_unlock():
229
 
            time.sleep(0.1)
230
 
            lf1.unlock()
231
 
        unlocker = Thread(target=wait_and_unlock)
232
 
        unlocker.start()
233
 
        try:
234
 
            lf2 = LockDir(t, 'test_lock')
235
 
            self.setup_log_reporter(lf2)
236
 
            before = time.time()
237
 
            # wait and then lock
238
 
            lf2.wait_lock(timeout=0.4, poll=0.1)
239
 
            after = time.time()
240
 
            self.assertTrue(after - before <= 1.0)
241
 
        finally:
242
 
            unlocker.join()
243
 
 
244
 
        # There should be only 1 report, even though it should have to
245
 
        # wait for a while
246
 
        lock_base = lf2.transport.abspath(lf2.path)
247
 
        self.assertEqual(1, len(self._logged_reports))
248
 
        self.assertEqual('%s %s\n'
249
 
                         '%s\n%s\n'
250
 
                         'Will continue to try until %s\n',
251
 
                         self._logged_reports[0][0])
252
 
        args = self._logged_reports[0][1]
253
 
        self.assertEqual('Unable to obtain', args[0])
254
 
        self.assertEqual('lock %s' % (lock_base,), args[1])
255
 
        self.assertStartsWith(args[2], 'held by ')
256
 
        self.assertStartsWith(args[3], 'locked ')
257
 
        self.assertEndsWith(args[3], ' ago')
258
 
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
259
 
 
260
 
    def test_33_wait(self):
261
 
        """Succeed when waiting on a lock that gets released
262
 
 
263
 
        The difference from test_32_lock_wait_succeed is that the second 
264
 
        caller does not actually acquire the lock, but just waits for it
265
 
        to be released.  This is done over a readonly transport.
266
 
        """
267
 
        t = self.get_transport()
268
 
        lf1 = LockDir(t, 'test_lock')
269
 
        lf1.create()
270
 
        lf1.attempt_lock()
271
 
 
272
 
        def wait_and_unlock():
273
 
            time.sleep(0.1)
274
 
            lf1.unlock()
275
 
        unlocker = Thread(target=wait_and_unlock)
276
 
        unlocker.start()
277
 
        try:
278
 
            lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
279
 
            before = time.time()
280
 
            # wait but don't lock
281
 
            lf2.wait(timeout=0.4, poll=0.1)
282
 
            after = time.time()
283
 
            self.assertTrue(after - before <= 1.0)
284
 
        finally:
285
 
            unlocker.join()
286
 
 
287
 
    def test_34_lock_write_waits(self):
288
 
        """LockDir.lock_write() will wait for the lock.""" 
289
 
        # the test suite sets the default to 0 to make deadlocks fail fast.
290
 
        # change it for this test, as we want to try a manual deadlock.
291
 
        bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = 300
292
 
        t = self.get_transport()
293
 
        lf1 = LockDir(t, 'test_lock')
294
 
        lf1.create()
295
 
        lf1.attempt_lock()
296
 
 
297
 
        def wait_and_unlock():
298
 
            time.sleep(0.1)
299
 
            lf1.unlock()
300
 
        unlocker = Thread(target=wait_and_unlock)
301
 
        unlocker.start()
302
 
        try:
303
 
            lf2 = LockDir(t, 'test_lock')
304
 
            self.setup_log_reporter(lf2)
305
 
            before = time.time()
306
 
            # wait and then lock
307
 
            lf2.lock_write()
308
 
            after = time.time()
309
 
        finally:
310
 
            unlocker.join()
311
 
 
312
 
        # There should be only 1 report, even though it should have to
313
 
        # wait for a while
314
 
        lock_base = lf2.transport.abspath(lf2.path)
315
 
        self.assertEqual(1, len(self._logged_reports))
316
 
        self.assertEqual('%s %s\n'
317
 
                         '%s\n%s\n'
318
 
                         'Will continue to try until %s\n',
319
 
                         self._logged_reports[0][0])
320
 
        args = self._logged_reports[0][1]
321
 
        self.assertEqual('Unable to obtain', args[0])
322
 
        self.assertEqual('lock %s' % (lock_base,), args[1])
323
 
        self.assertStartsWith(args[2], 'held by ')
324
 
        self.assertStartsWith(args[3], 'locked ')
325
 
        self.assertEndsWith(args[3], ' ago')
326
 
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
327
 
 
328
 
    def test_35_wait_lock_changing(self):
329
 
        """LockDir.wait_lock() will report if the lock changes underneath.
330
 
        
331
 
        This is the stages we want to happen:
332
 
 
333
 
        0) Synchronization locks are created and locked.
334
 
        1) Lock1 obtains the lockdir, and releases the 'check' lock.
335
 
        2) Lock2 grabs the 'check' lock, and checks the lockdir.
336
 
           It sees the lockdir is already acquired, reports the fact, 
337
 
           and unsets the 'checked' lock.
338
 
        3) Thread1 blocks on acquiring the 'checked' lock, and then tells
339
 
           Lock1 to release and acquire the lockdir. This resets the 'check'
340
 
           lock.
341
 
        4) Lock2 acquires the 'check' lock, and checks again. It notices
342
 
           that the holder of the lock has changed, and so reports a new 
343
 
           lock holder.
344
 
        5) Thread1 blocks on the 'checked' lock, this time, it completely
345
 
           unlocks the lockdir, allowing Lock2 to acquire the lock.
346
 
        """
347
 
 
348
 
        wait_to_check_lock = Lock()
349
 
        wait_until_checked_lock = Lock()
350
 
 
351
 
        wait_to_check_lock.acquire()
352
 
        wait_until_checked_lock.acquire()
353
 
        note('locked check and checked locks')
354
 
 
355
 
        class LockDir1(LockDir):
356
 
            """Use the synchronization points for the first lock."""
357
 
 
358
 
            def attempt_lock(self):
359
 
                # Once we have acquired the lock, it is okay for
360
 
                # the other lock to check it
361
 
                try:
362
 
                    return super(LockDir1, self).attempt_lock()
363
 
                finally:
364
 
                    note('lock1: releasing check lock')
365
 
                    wait_to_check_lock.release()
366
 
 
367
 
        class LockDir2(LockDir):
368
 
            """Use the synchronization points for the second lock."""
369
 
 
370
 
            def attempt_lock(self):
371
 
                note('lock2: waiting for check lock')
372
 
                wait_to_check_lock.acquire()
373
 
                note('lock2: acquired check lock')
374
 
                try:
375
 
                    return super(LockDir2, self).attempt_lock()
376
 
                finally:
377
 
                    note('lock2: releasing checked lock')
378
 
                    wait_until_checked_lock.release()
379
 
 
380
 
        t = self.get_transport()
381
 
        lf1 = LockDir1(t, 'test_lock')
382
 
        lf1.create()
383
 
 
384
 
        lf2 = LockDir2(t, 'test_lock')
385
 
        self.setup_log_reporter(lf2)
386
 
 
387
 
        def wait_and_switch():
388
 
            lf1.attempt_lock()
389
 
            # Block until lock2 has had a chance to check
390
 
            note('lock1: waiting 1 for checked lock')
391
 
            wait_until_checked_lock.acquire()
392
 
            note('lock1: acquired for checked lock')
393
 
            note('lock1: released lockdir')
394
 
            lf1.unlock()
395
 
            note('lock1: acquiring lockdir')
396
 
            # Create a new nonce, so the lock looks different.
397
 
            lf1.nonce = osutils.rand_chars(20)
398
 
            lf1.lock_write()
399
 
            note('lock1: acquired lockdir')
400
 
 
401
 
            # Block until lock2 has peeked again
402
 
            note('lock1: waiting 2 for checked lock')
403
 
            wait_until_checked_lock.acquire()
404
 
            note('lock1: acquired for checked lock')
405
 
            # Now unlock, and let lock 2 grab the lock
406
 
            lf1.unlock()
407
 
            wait_to_check_lock.release()
408
 
 
409
 
        unlocker = Thread(target=wait_and_switch)
410
 
        unlocker.start()
411
 
        try:
412
 
            # Wait and play against the other thread
413
 
            lf2.wait_lock(timeout=1.0, poll=0.01)
414
 
        finally:
415
 
            unlocker.join()
416
 
        lf2.unlock()
417
 
 
418
 
        # There should be 2 reports, because the lock changed
419
 
        lock_base = lf2.transport.abspath(lf2.path)
420
 
        self.assertEqual(2, len(self._logged_reports))
421
 
 
422
 
        self.assertEqual('%s %s\n'
423
 
                         '%s\n%s\n'
424
 
                         'Will continue to try until %s\n',
425
 
                         self._logged_reports[0][0])
426
 
        args = self._logged_reports[0][1]
427
 
        self.assertEqual('Unable to obtain', args[0])
428
 
        self.assertEqual('lock %s' % (lock_base,), args[1])
429
 
        self.assertStartsWith(args[2], 'held by ')
430
 
        self.assertStartsWith(args[3], 'locked ')
431
 
        self.assertEndsWith(args[3], ' ago')
432
 
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
433
 
 
434
 
        self.assertEqual('%s %s\n'
435
 
                         '%s\n%s\n'
436
 
                         'Will continue to try until %s\n',
437
 
                         self._logged_reports[1][0])
438
 
        args = self._logged_reports[1][1]
439
 
        self.assertEqual('Lock owner changed for', args[0])
440
 
        self.assertEqual('lock %s' % (lock_base,), args[1])
441
 
        self.assertStartsWith(args[2], 'held by ')
442
 
        self.assertStartsWith(args[3], 'locked ')
443
 
        self.assertEndsWith(args[3], ' ago')
444
 
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
445
 
 
446
 
    def test_40_confirm_easy(self):
447
 
        """Confirm a lock that's already held"""
448
 
        t = self.get_transport()
449
 
        lf1 = LockDir(t, 'test_lock')
450
 
        lf1.create()
451
 
        lf1.attempt_lock()
452
 
        lf1.confirm()
453
 
 
454
 
    def test_41_confirm_not_held(self):
455
 
        """Confirm a lock that's already held"""
456
 
        t = self.get_transport()
457
 
        lf1 = LockDir(t, 'test_lock')
458
 
        lf1.create()
459
 
        self.assertRaises(LockNotHeld, lf1.confirm)
460
 
 
461
 
    def test_42_confirm_broken_manually(self):
462
 
        """Confirm a lock broken by hand"""
463
 
        t = self.get_transport()
464
 
        lf1 = LockDir(t, 'test_lock')
465
 
        lf1.create()
466
 
        lf1.attempt_lock()
467
 
        t.move('test_lock', 'lock_gone_now')
468
 
        self.assertRaises(LockBroken, lf1.confirm)
469
 
 
470
 
    def test_43_break(self):
471
 
        """Break a lock whose caller has forgotten it"""
472
 
        t = self.get_transport()
473
 
        lf1 = LockDir(t, 'test_lock')
474
 
        lf1.create()
475
 
        lf1.attempt_lock()
476
 
        # we incorrectly discard the lock object without unlocking it
477
 
        del lf1
478
 
        # someone else sees it's still locked
479
 
        lf2 = LockDir(t, 'test_lock')
480
 
        holder_info = lf2.peek()
481
 
        self.assertTrue(holder_info)
482
 
        lf2.force_break(holder_info)
483
 
        # now we should be able to take it
484
 
        lf2.attempt_lock()
485
 
        lf2.confirm()
486
 
 
487
 
    def test_44_break_already_released(self):
488
 
        """Lock break races with regular release"""
489
 
        t = self.get_transport()
490
 
        lf1 = LockDir(t, 'test_lock')
491
 
        lf1.create()
492
 
        lf1.attempt_lock()
493
 
        # someone else sees it's still locked
494
 
        lf2 = LockDir(t, 'test_lock')
495
 
        holder_info = lf2.peek()
496
 
        # in the interim the lock is released
497
 
        lf1.unlock()
498
 
        # break should succeed
499
 
        lf2.force_break(holder_info)
500
 
        # now we should be able to take it
501
 
        lf2.attempt_lock()
502
 
        lf2.confirm()
503
 
 
504
 
    def test_45_break_mismatch(self):
505
 
        """Lock break races with someone else acquiring it"""
506
 
        t = self.get_transport()
507
 
        lf1 = LockDir(t, 'test_lock')
508
 
        lf1.create()
509
 
        lf1.attempt_lock()
510
 
        # someone else sees it's still locked
511
 
        lf2 = LockDir(t, 'test_lock')
512
 
        holder_info = lf2.peek()
513
 
        # in the interim the lock is released
514
 
        lf1.unlock()
515
 
        lf3 = LockDir(t, 'test_lock')
516
 
        lf3.attempt_lock()
517
 
        # break should now *fail*
518
 
        self.assertRaises(LockBreakMismatch, lf2.force_break,
519
 
                          holder_info)
520
 
        lf3.unlock()
521
 
 
522
 
    def test_46_fake_read_lock(self):
523
 
        t = self.get_transport()
524
 
        lf1 = LockDir(t, 'test_lock')
525
 
        lf1.create()
526
 
        lf1.lock_read()
527
 
        lf1.unlock()
528
 
 
529
 
    def test_50_lockdir_representation(self):
530
 
        """Check the on-disk representation of LockDirs is as expected.
531
 
 
532
 
        There should always be a top-level directory named by the lock.
533
 
        When the lock is held, there should be a lockname/held directory 
534
 
        containing an info file.
535
 
        """
536
 
        t = self.get_transport()
537
 
        lf1 = LockDir(t, 'test_lock')
538
 
        lf1.create()
539
 
        self.assertTrue(t.has('test_lock'))
540
 
        lf1.lock_write()
541
 
        self.assertTrue(t.has('test_lock/held/info'))
542
 
        lf1.unlock()
543
 
        self.assertFalse(t.has('test_lock/held/info'))
544
 
 
545
 
    def test_break_lock(self):
546
 
        # the ui based break_lock routine should Just Work (tm)
547
 
        ld1 = self.get_lock()
548
 
        ld2 = self.get_lock()
549
 
        ld1.create()
550
 
        ld1.lock_write()
551
 
        # do this without IO redirection to ensure it doesn't prompt.
552
 
        self.assertRaises(AssertionError, ld1.break_lock)
553
 
        orig_factory = bzrlib.ui.ui_factory
554
 
        # silent ui - no need for stdout
555
 
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
556
 
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
557
 
        try:
558
 
            ld2.break_lock()
559
 
            self.assertRaises(LockBroken, ld1.unlock)
560
 
        finally:
561
 
            bzrlib.ui.ui_factory = orig_factory
562
 
 
563
 
    def test_create_missing_base_directory(self):
564
 
        """If LockDir.path doesn't exist, it can be created
565
 
 
566
 
        Some people manually remove the entire lock/ directory trying
567
 
        to unlock a stuck repository/branch/etc. Rather than failing
568
 
        after that, just create the lock directory when needed.
569
 
        """
570
 
        t = self.get_transport()
571
 
        lf1 = LockDir(t, 'test_lock')
572
 
 
573
 
        lf1.create()
574
 
        self.failUnless(t.has('test_lock'))
575
 
 
576
 
        t.rmdir('test_lock')
577
 
        self.failIf(t.has('test_lock'))
578
 
 
579
 
        # This will create 'test_lock' if it needs to
580
 
        lf1.lock_write()
581
 
        self.failUnless(t.has('test_lock'))
582
 
        self.failUnless(t.has('test_lock/held/info'))
583
 
 
584
 
        lf1.unlock()
585
 
        self.failIf(t.has('test_lock/held/info'))
586
 
 
587
 
    def test__format_lock_info(self):
588
 
        ld1 = self.get_lock()
589
 
        ld1.create()
590
 
        ld1.lock_write()
591
 
        try:
592
 
            info_list = ld1._format_lock_info(ld1.peek())
593
 
        finally:
594
 
            ld1.unlock()
595
 
        self.assertEqual('lock %s' % (ld1.transport.abspath(ld1.path),),
596
 
                         info_list[0])
597
 
        self.assertContainsRe(info_list[1],
598
 
                              r'^held by .* on host .* \[process #\d*\]$')
599
 
        self.assertContainsRe(info_list[2], r'locked \d+ seconds? ago$')
600
 
 
601
 
    def test_lock_without_email(self):
602
 
        global_config = config.GlobalConfig()
603
 
        # Intentionally has no email address
604
 
        global_config.set_user_option('email', 'User Identity')
605
 
        ld1 = self.get_lock()
606
 
        ld1.create()
607
 
        ld1.lock_write()
608
 
        ld1.unlock()
609
 
 
610
 
    def test_lock_permission(self):
611
 
        if not osutils.supports_posix_readonly():
612
 
            raise tests.TestSkipped('Cannot induce a permission failure')
613
 
        ld1 = self.get_lock()
614
 
        lock_path = ld1.transport.local_abspath('test_lock')
615
 
        os.mkdir(lock_path)
616
 
        osutils.make_readonly(lock_path)
617
 
        self.assertRaises(errors.PermissionDenied, ld1.attempt_lock)