~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lockdir.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-08-01 17:14:51 UTC
  • mfrom: (2662.1.1 bzr.easy_install)
  • Revision ID: pqm@pqm.ubuntu.com-20070801171451-en3tds1hzlru2j83
allow ``easy_install bzr`` runs without fatal errors. (#125521, bialix,
 r=mbp)

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