/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lockdir.py

  • Committer: John Arbash Meinel
  • Date: 2008-06-05 16:27:16 UTC
  • mfrom: (3475 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3476.
  • Revision ID: john@arbash-meinel.com-20080605162716-a3hn238tnctbfd8j
merge bzr.dev, resolve NEWS

Show diffs side-by-side

added added

removed removed

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