/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1553.5.12 by Martin Pool
New LockDir locking mechanism
1
# Copyright (C) 2006 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1553.5.12 by Martin Pool
New LockDir locking mechanism
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1553.5.12 by Martin Pool
New LockDir locking mechanism
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1553.5.12 by Martin Pool
New LockDir locking mechanism
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
1687.1.5 by Robert Collins
Add break_lock utility function to LockDir.
19
from cStringIO import StringIO
1551.10.3 by Aaron Bentley
Lock attempts don't treat permission problems as lock contention
20
import os
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
21
from threading import Thread, Lock
1553.5.12 by Martin Pool
New LockDir locking mechanism
22
import time
23
1687.1.5 by Robert Collins
Add break_lock utility function to LockDir.
24
import bzrlib
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
25
from bzrlib import (
2055.2.1 by John Arbash Meinel
Make LockDir less sensitive to invalid configuration of email
26
    config,
1551.10.3 by Aaron Bentley
Lock attempts don't treat permission problems as lock contention
27
    errors,
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
28
    osutils,
1551.10.4 by Aaron Bentley
Update to skip on win32
29
    tests,
2555.3.9 by Martin Pool
Add test and fix for locking robustly when rename of directories doesn't act as a mutex (thank John)
30
    transport,
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
31
    )
1553.5.27 by Martin Pool
Confirm that only the intended holder of a lock was broken.
32
from bzrlib.errors import (
33
        LockBreakMismatch,
34
        LockContention, LockError, UnlockableTransport,
1553.5.23 by Martin Pool
Start LockDir.confirm method and LockBroken exception
35
        LockNotHeld, LockBroken
1553.5.27 by Martin Pool
Confirm that only the intended holder of a lock was broken.
36
        )
2381.1.4 by Robert Collins
Unbreak lockdir tests after adding fast lockdir timeouts to the test suite default environment.
37
from bzrlib.lockdir import LockDir
1553.5.33 by Martin Pool
LockDir review comment fixes
38
from bzrlib.tests import TestCaseWithTransport
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
39
from bzrlib.trace import note
1553.5.12 by Martin Pool
New LockDir locking mechanism
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
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
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
1553.5.12 by Martin Pool
New LockDir locking mechanism
51
class TestLockDir(TestCaseWithTransport):
52
    """Test LockDir operations"""
53
1957.1.1 by John Arbash Meinel
Report to the user when we are spinning on a lock
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
1553.5.12 by Martin Pool
New LockDir locking mechanism
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')
1553.5.13 by Martin Pool
New Transport.rename that mustn't overwrite
65
        self.assertFalse(lf.is_held)
1553.5.12 by Martin Pool
New LockDir locking mechanism
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
1687.1.3 by Robert Collins
Make LockDir.unlock check the lock is still intact.
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
1553.5.12 by Martin Pool
New LockDir locking mechanism
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')
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
96
        lf.create()
1553.5.12 by Martin Pool
New LockDir locking mechanism
97
        lf.attempt_lock()
98
        try:
1553.5.13 by Martin Pool
New Transport.rename that mustn't overwrite
99
            self.assertTrue(lf.is_held)
1553.5.12 by Martin Pool
New LockDir locking mechanism
100
        finally:
101
            lf.unlock()
1553.5.13 by Martin Pool
New Transport.rename that mustn't overwrite
102
            self.assertFalse(lf.is_held)
1553.5.12 by Martin Pool
New LockDir locking mechanism
103
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
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):
1553.5.12 by Martin Pool
New LockDir locking mechanism
111
        """Fail to lock on readonly transport"""
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
112
        lf = LockDir(self.get_transport(), 'test_lock')
113
        lf.create()
114
        lf = LockDir(self.get_readonly_transport(), 'test_lock')
1553.5.12 by Martin Pool
New LockDir locking mechanism
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')
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
121
        lf1.create()
1553.5.12 by Martin Pool
New LockDir locking mechanism
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')
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
139
        lf1.create()
1553.5.12 by Martin Pool
New LockDir locking mechanism
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')
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
156
        lf1.create()
1553.5.12 by Martin Pool
New LockDir locking mechanism
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')
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
173
        lf1.create()
1553.5.12 by Martin Pool
New LockDir locking mechanism
174
        lf2 = LockDir(t, 'test_lock')
1957.1.1 by John Arbash Meinel
Report to the user when we are spinning on a lock
175
        self.setup_log_reporter(lf2)
1553.5.12 by Martin Pool
New LockDir locking mechanism
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()
1704.2.1 by Martin Pool
Fix time-dependency in LockDir tests -- allow more margin for error in time to detect lock contention
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))
1553.5.12 by Martin Pool
New LockDir locking mechanism
186
        finally:
187
            lf1.unlock()
1957.1.1 by John Arbash Meinel
Report to the user when we are spinning on a lock
188
        lock_base = lf2.transport.abspath(lf2.path)
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
189
        self.assertEqual(1, len(self._logged_reports))
1957.1.9 by John Arbash Meinel
Change default timeouts, and report differently the first failure
190
        self.assertEqual('%s %s\n'
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
191
                         '%s\n%s\n'
1957.1.13 by John Arbash Meinel
Change to reporting the time when we will stop trying to grab the lock
192
                         'Will continue to try until %s\n',
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
193
                         self._logged_reports[0][0])
194
        args = self._logged_reports[0][1]
1957.1.9 by John Arbash Meinel
Change default timeouts, and report differently the first failure
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')
1957.1.13 by John Arbash Meinel
Change to reporting the time when we will stop trying to grab the lock
200
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
1553.5.12 by Martin Pool
New LockDir locking mechanism
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()
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
206
        lf1 = LockDir(t, 'test_lock')
207
        lf1.create()
1957.1.1 by John Arbash Meinel
Report to the user when we are spinning on a lock
208
        self.setup_log_reporter(lf1)
1553.5.12 by Martin Pool
New LockDir locking mechanism
209
        try:
210
            before = time.time()
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
211
            lf1.wait_lock(timeout=0.4, poll=0.1)
1553.5.12 by Martin Pool
New LockDir locking mechanism
212
            after = time.time()
213
            self.assertTrue(after - before <= 1.0)
214
        finally:
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
215
            lf1.unlock()
1957.1.1 by John Arbash Meinel
Report to the user when we are spinning on a lock
216
        self.assertEqual([], self._logged_reports)
1553.5.12 by Martin Pool
New LockDir locking mechanism
217
1551.15.18 by Aaron Bentley
Skip itermittently-failing test instead of deleting it
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
        """
1551.15.22 by Aaron Bentley
Redo test skip
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")
1551.15.18 by Aaron Bentley
Skip itermittently-failing test instead of deleting it
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
1957.1.2 by John Arbash Meinel
Switch the default from instantly aborting, to waiting as long as 1 minute (down from 5 minutes)
271
    def test_34_lock_write_waits(self):
272
        """LockDir.lock_write() will wait for the lock.""" 
2381.1.4 by Robert Collins
Unbreak lockdir tests after adding fast lockdir timeouts to the test suite default environment.
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.
2631.1.4 by Aaron Bentley
Fix test skip
275
        raise tests.TestSkipped('Timing-sensitive test')
2381.1.4 by Robert Collins
Unbreak lockdir tests after adding fast lockdir timeouts to the test suite default environment.
276
        bzrlib.lockdir._DEFAULT_TIMEOUT_SECONDS = 300
1957.1.2 by John Arbash Meinel
Switch the default from instantly aborting, to waiting as long as 1 minute (down from 5 minutes)
277
        t = self.get_transport()
278
        lf1 = LockDir(t, 'test_lock')
279
        lf1.create()
280
        lf1.attempt_lock()
281
282
        def wait_and_unlock():
283
            time.sleep(0.1)
284
            lf1.unlock()
285
        unlocker = Thread(target=wait_and_unlock)
286
        unlocker.start()
287
        try:
288
            lf2 = LockDir(t, 'test_lock')
289
            self.setup_log_reporter(lf2)
290
            before = time.time()
291
            # wait and then lock
292
            lf2.lock_write()
293
            after = time.time()
294
        finally:
295
            unlocker.join()
296
297
        # There should be only 1 report, even though it should have to
298
        # wait for a while
299
        lock_base = lf2.transport.abspath(lf2.path)
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
300
        self.assertEqual(1, len(self._logged_reports))
1957.1.9 by John Arbash Meinel
Change default timeouts, and report differently the first failure
301
        self.assertEqual('%s %s\n'
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
302
                         '%s\n%s\n'
1957.1.13 by John Arbash Meinel
Change to reporting the time when we will stop trying to grab the lock
303
                         'Will continue to try until %s\n',
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
304
                         self._logged_reports[0][0])
305
        args = self._logged_reports[0][1]
1957.1.9 by John Arbash Meinel
Change default timeouts, and report differently the first failure
306
        self.assertEqual('Unable to obtain', args[0])
307
        self.assertEqual('lock %s' % (lock_base,), args[1])
308
        self.assertStartsWith(args[2], 'held by ')
309
        self.assertStartsWith(args[3], 'locked ')
310
        self.assertEndsWith(args[3], ' ago')
1957.1.13 by John Arbash Meinel
Change to reporting the time when we will stop trying to grab the lock
311
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
312
313
    def test_35_wait_lock_changing(self):
314
        """LockDir.wait_lock() will report if the lock changes underneath.
315
        
316
        This is the stages we want to happen:
317
318
        0) Synchronization locks are created and locked.
319
        1) Lock1 obtains the lockdir, and releases the 'check' lock.
320
        2) Lock2 grabs the 'check' lock, and checks the lockdir.
321
           It sees the lockdir is already acquired, reports the fact, 
1957.1.11 by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable
322
           and unsets the 'checked' lock.
323
        3) Thread1 blocks on acquiring the 'checked' lock, and then tells
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
324
           Lock1 to release and acquire the lockdir. This resets the 'check'
325
           lock.
326
        4) Lock2 acquires the 'check' lock, and checks again. It notices
327
           that the holder of the lock has changed, and so reports a new 
328
           lock holder.
1957.1.11 by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable
329
        5) Thread1 blocks on the 'checked' lock, this time, it completely
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
330
           unlocks the lockdir, allowing Lock2 to acquire the lock.
331
        """
332
333
        wait_to_check_lock = Lock()
1957.1.11 by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable
334
        wait_until_checked_lock = Lock()
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
335
336
        wait_to_check_lock.acquire()
1957.1.11 by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable
337
        wait_until_checked_lock.acquire()
338
        note('locked check and checked locks')
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
339
340
        class LockDir1(LockDir):
341
            """Use the synchronization points for the first lock."""
342
343
            def attempt_lock(self):
344
                # Once we have acquired the lock, it is okay for
345
                # the other lock to check it
346
                try:
347
                    return super(LockDir1, self).attempt_lock()
348
                finally:
349
                    note('lock1: releasing check lock')
350
                    wait_to_check_lock.release()
351
352
        class LockDir2(LockDir):
353
            """Use the synchronization points for the second lock."""
354
1957.1.11 by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable
355
            def attempt_lock(self):
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
356
                note('lock2: waiting for check lock')
357
                wait_to_check_lock.acquire()
358
                note('lock2: acquired check lock')
359
                try:
1957.1.11 by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable
360
                    return super(LockDir2, self).attempt_lock()
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
361
                finally:
1957.1.11 by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable
362
                    note('lock2: releasing checked lock')
363
                    wait_until_checked_lock.release()
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
364
365
        t = self.get_transport()
366
        lf1 = LockDir1(t, 'test_lock')
367
        lf1.create()
368
369
        lf2 = LockDir2(t, 'test_lock')
370
        self.setup_log_reporter(lf2)
371
372
        def wait_and_switch():
373
            lf1.attempt_lock()
1957.1.11 by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable
374
            # Block until lock2 has had a chance to check
375
            note('lock1: waiting 1 for checked lock')
376
            wait_until_checked_lock.acquire()
377
            note('lock1: acquired for checked lock')
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
378
            note('lock1: released lockdir')
379
            lf1.unlock()
380
            note('lock1: acquiring lockdir')
381
            # Create a new nonce, so the lock looks different.
382
            lf1.nonce = osutils.rand_chars(20)
383
            lf1.lock_write()
384
            note('lock1: acquired lockdir')
385
386
            # Block until lock2 has peeked again
1957.1.11 by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable
387
            note('lock1: waiting 2 for checked lock')
388
            wait_until_checked_lock.acquire()
389
            note('lock1: acquired for checked lock')
390
            # Now unlock, and let lock 2 grab the lock
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
391
            lf1.unlock()
392
            wait_to_check_lock.release()
393
394
        unlocker = Thread(target=wait_and_switch)
395
        unlocker.start()
396
        try:
1957.1.11 by John Arbash Meinel
Switch from locking the peek() to locking attempt_lock(), which is much more stable
397
            # Wait and play against the other thread
2709.1.1 by Martin Pool
Make TestLockDir.test_35_wait_lock_changing less timing-sensitive
398
            lf2.wait_lock(timeout=20.0, poll=0.01)
1957.1.7 by John Arbash Meinel
Add the ability to report if the lock changes from underneath you
399
        finally:
400
            unlocker.join()
401
        lf2.unlock()
402
403
        # There should be 2 reports, because the lock changed
404
        lock_base = lf2.transport.abspath(lf2.path)
405
        self.assertEqual(2, len(self._logged_reports))
1957.1.13 by John Arbash Meinel
Change to reporting the time when we will stop trying to grab the lock
406
1957.1.15 by John Arbash Meinel
Review feedback from Robert
407
        self.assertEqual('%s %s\n'
408
                         '%s\n%s\n'
409
                         'Will continue to try until %s\n',
410
                         self._logged_reports[0][0])
411
        args = self._logged_reports[0][1]
412
        self.assertEqual('Unable to obtain', args[0])
413
        self.assertEqual('lock %s' % (lock_base,), args[1])
414
        self.assertStartsWith(args[2], 'held by ')
415
        self.assertStartsWith(args[3], 'locked ')
416
        self.assertEndsWith(args[3], ' ago')
417
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
1957.1.13 by John Arbash Meinel
Change to reporting the time when we will stop trying to grab the lock
418
1957.1.15 by John Arbash Meinel
Review feedback from Robert
419
        self.assertEqual('%s %s\n'
420
                         '%s\n%s\n'
421
                         'Will continue to try until %s\n',
422
                         self._logged_reports[1][0])
423
        args = self._logged_reports[1][1]
424
        self.assertEqual('Lock owner changed for', args[0])
425
        self.assertEqual('lock %s' % (lock_base,), args[1])
426
        self.assertStartsWith(args[2], 'held by ')
427
        self.assertStartsWith(args[3], 'locked ')
428
        self.assertEndsWith(args[3], ' ago')
429
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
1957.1.2 by John Arbash Meinel
Switch the default from instantly aborting, to waiting as long as 1 minute (down from 5 minutes)
430
1553.5.20 by Martin Pool
Start adding LockDir.confirm() method
431
    def test_40_confirm_easy(self):
432
        """Confirm a lock that's already held"""
433
        t = self.get_transport()
434
        lf1 = LockDir(t, 'test_lock')
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
435
        lf1.create()
1553.5.20 by Martin Pool
Start adding LockDir.confirm() method
436
        lf1.attempt_lock()
437
        lf1.confirm()
438
439
    def test_41_confirm_not_held(self):
440
        """Confirm a lock that's already held"""
441
        t = self.get_transport()
442
        lf1 = LockDir(t, 'test_lock')
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
443
        lf1.create()
1553.5.20 by Martin Pool
Start adding LockDir.confirm() method
444
        self.assertRaises(LockNotHeld, lf1.confirm)
1553.5.23 by Martin Pool
Start LockDir.confirm method and LockBroken exception
445
446
    def test_42_confirm_broken_manually(self):
447
        """Confirm a lock broken by hand"""
448
        t = self.get_transport()
449
        lf1 = LockDir(t, 'test_lock')
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
450
        lf1.create()
1553.5.23 by Martin Pool
Start LockDir.confirm method and LockBroken exception
451
        lf1.attempt_lock()
452
        t.move('test_lock', 'lock_gone_now')
453
        self.assertRaises(LockBroken, lf1.confirm)
1553.5.25 by Martin Pool
New LockDir.force_break and simple test case
454
455
    def test_43_break(self):
456
        """Break a lock whose caller has forgotten it"""
457
        t = self.get_transport()
458
        lf1 = LockDir(t, 'test_lock')
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
459
        lf1.create()
1553.5.25 by Martin Pool
New LockDir.force_break and simple test case
460
        lf1.attempt_lock()
461
        # we incorrectly discard the lock object without unlocking it
462
        del lf1
463
        # someone else sees it's still locked
464
        lf2 = LockDir(t, 'test_lock')
1553.5.27 by Martin Pool
Confirm that only the intended holder of a lock was broken.
465
        holder_info = lf2.peek()
466
        self.assertTrue(holder_info)
467
        lf2.force_break(holder_info)
1553.5.25 by Martin Pool
New LockDir.force_break and simple test case
468
        # now we should be able to take it
469
        lf2.attempt_lock()
470
        lf2.confirm()
1553.5.26 by Martin Pool
Breaking an already-released lock should just succeed
471
472
    def test_44_break_already_released(self):
473
        """Lock break races with regular release"""
474
        t = self.get_transport()
475
        lf1 = LockDir(t, 'test_lock')
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
476
        lf1.create()
1553.5.26 by Martin Pool
Breaking an already-released lock should just succeed
477
        lf1.attempt_lock()
478
        # someone else sees it's still locked
479
        lf2 = LockDir(t, 'test_lock')
480
        holder_info = lf2.peek()
481
        # in the interim the lock is released
482
        lf1.unlock()
483
        # break should succeed
1553.5.27 by Martin Pool
Confirm that only the intended holder of a lock was broken.
484
        lf2.force_break(holder_info)
1553.5.26 by Martin Pool
Breaking an already-released lock should just succeed
485
        # now we should be able to take it
486
        lf2.attempt_lock()
487
        lf2.confirm()
488
1553.5.27 by Martin Pool
Confirm that only the intended holder of a lock was broken.
489
    def test_45_break_mismatch(self):
490
        """Lock break races with someone else acquiring it"""
491
        t = self.get_transport()
492
        lf1 = LockDir(t, 'test_lock')
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
493
        lf1.create()
1553.5.27 by Martin Pool
Confirm that only the intended holder of a lock was broken.
494
        lf1.attempt_lock()
495
        # someone else sees it's still locked
496
        lf2 = LockDir(t, 'test_lock')
497
        holder_info = lf2.peek()
498
        # in the interim the lock is released
499
        lf1.unlock()
500
        lf3 = LockDir(t, 'test_lock')
501
        lf3.attempt_lock()
502
        # break should now *fail*
503
        self.assertRaises(LockBreakMismatch, lf2.force_break,
504
                          holder_info)
505
        lf3.unlock()
1553.5.54 by Martin Pool
Add LockDir.read_lock fake method
506
507
    def test_46_fake_read_lock(self):
508
        t = self.get_transport()
509
        lf1 = LockDir(t, 'test_lock')
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
510
        lf1.create()
1553.5.54 by Martin Pool
Add LockDir.read_lock fake method
511
        lf1.lock_read()
512
        lf1.unlock()
1553.5.58 by Martin Pool
Change LockDirs to format "lock-name/held/info"
513
514
    def test_50_lockdir_representation(self):
515
        """Check the on-disk representation of LockDirs is as expected.
516
517
        There should always be a top-level directory named by the lock.
518
        When the lock is held, there should be a lockname/held directory 
519
        containing an info file.
520
        """
521
        t = self.get_transport()
522
        lf1 = LockDir(t, 'test_lock')
523
        lf1.create()
524
        self.assertTrue(t.has('test_lock'))
525
        lf1.lock_write()
526
        self.assertTrue(t.has('test_lock/held/info'))
527
        lf1.unlock()
528
        self.assertFalse(t.has('test_lock/held/info'))
1687.1.5 by Robert Collins
Add break_lock utility function to LockDir.
529
530
    def test_break_lock(self):
531
        # the ui based break_lock routine should Just Work (tm)
532
        ld1 = self.get_lock()
533
        ld2 = self.get_lock()
534
        ld1.create()
535
        ld1.lock_write()
1687.1.6 by Robert Collins
Extend LockableFiles to support break_lock() calls.
536
        # do this without IO redirection to ensure it doesn't prompt.
537
        self.assertRaises(AssertionError, ld1.break_lock)
1687.1.5 by Robert Collins
Add break_lock utility function to LockDir.
538
        orig_factory = bzrlib.ui.ui_factory
539
        # silent ui - no need for stdout
540
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
541
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
542
        try:
543
            ld2.break_lock()
544
            self.assertRaises(LockBroken, ld1.unlock)
545
        finally:
546
            bzrlib.ui.ui_factory = orig_factory
1955.1.1 by John Arbash Meinel
LockDir can create the root directory if it fails to create a pending directory due to NoSuchFile.
547
548
    def test_create_missing_base_directory(self):
549
        """If LockDir.path doesn't exist, it can be created
550
551
        Some people manually remove the entire lock/ directory trying
552
        to unlock a stuck repository/branch/etc. Rather than failing
553
        after that, just create the lock directory when needed.
554
        """
555
        t = self.get_transport()
556
        lf1 = LockDir(t, 'test_lock')
557
558
        lf1.create()
559
        self.failUnless(t.has('test_lock'))
560
561
        t.rmdir('test_lock')
562
        self.failIf(t.has('test_lock'))
563
564
        # This will create 'test_lock' if it needs to
565
        lf1.lock_write()
566
        self.failUnless(t.has('test_lock'))
567
        self.failUnless(t.has('test_lock/held/info'))
568
569
        lf1.unlock()
570
        self.failIf(t.has('test_lock/held/info'))
1957.1.6 by John Arbash Meinel
[merge] bzr.dev 2009
571
1957.1.5 by John Arbash Meinel
Create a helper function for formatting lock information
572
    def test__format_lock_info(self):
573
        ld1 = self.get_lock()
574
        ld1.create()
575
        ld1.lock_write()
576
        try:
577
            info_list = ld1._format_lock_info(ld1.peek())
578
        finally:
579
            ld1.unlock()
580
        self.assertEqual('lock %s' % (ld1.transport.abspath(ld1.path),),
581
                         info_list[0])
582
        self.assertContainsRe(info_list[1],
583
                              r'^held by .* on host .* \[process #\d*\]$')
584
        self.assertContainsRe(info_list[2], r'locked \d+ seconds? ago$')
2055.2.1 by John Arbash Meinel
Make LockDir less sensitive to invalid configuration of email
585
586
    def test_lock_without_email(self):
587
        global_config = config.GlobalConfig()
588
        # Intentionally has no email address
589
        global_config.set_user_option('email', 'User Identity')
590
        ld1 = self.get_lock()
591
        ld1.create()
592
        ld1.lock_write()
593
        ld1.unlock()
1551.10.3 by Aaron Bentley
Lock attempts don't treat permission problems as lock contention
594
595
    def test_lock_permission(self):
1551.10.4 by Aaron Bentley
Update to skip on win32
596
        if not osutils.supports_posix_readonly():
597
            raise tests.TestSkipped('Cannot induce a permission failure')
1551.10.3 by Aaron Bentley
Lock attempts don't treat permission problems as lock contention
598
        ld1 = self.get_lock()
599
        lock_path = ld1.transport.local_abspath('test_lock')
600
        os.mkdir(lock_path)
601
        osutils.make_readonly(lock_path)
602
        self.assertRaises(errors.PermissionDenied, ld1.attempt_lock)
2555.3.5 by Martin Pool
Return token directly from LockDir.acquire to avoid unnecessary peek()
603
604
    def test_lock_by_token(self):
605
        ld1 = self.get_lock()
606
        token = ld1.lock_write()
607
        self.assertNotEqual(None, token)
608
        ld2 = self.get_lock()
609
        t2 = ld2.lock_write(token)
610
        self.assertEqual(token, t2)
2555.3.9 by Martin Pool
Add test and fix for locking robustly when rename of directories doesn't act as a mutex (thank John)
611
612
    def test_lock_with_buggy_rename(self):
613
        # test that lock acquisition handles servers which pretend they
614
        # renamed correctly but that actually fail
615
        t = transport.get_transport('brokenrename+' + self.get_url())
616
        ld1 = LockDir(t, 'test_lock')
617
        ld1.create()
618
        ld1.attempt_lock()
619
        ld2 = LockDir(t, 'test_lock')
2555.3.14 by Martin Pool
Better handling in LockDir of rename that moves one directory within another
620
        # we should fail to lock
621
        e = self.assertRaises(errors.LockContention, ld2.attempt_lock)
622
        # now the original caller should succeed in unlocking
623
        ld1.unlock()
624
        # and there should be nothing left over
625
        self.assertEquals([], t.list_dir('test_lock'))
2555.3.12 by Martin Pool
Add test for https://bugs.launchpad.net/bzr/+bug/109169 -- test_failed_lock_leaves_no_trash
626
627
    def test_failed_lock_leaves_no_trash(self):
628
        # if we fail to acquire the lock, we don't leave pending directories
629
        # behind -- https://bugs.launchpad.net/bzr/+bug/109169
630
        ld1 = self.get_lock()
631
        ld2 = self.get_lock()
632
        # should be nothing before we start
633
        ld1.create()
634
        t = self.get_transport().clone('test_lock')
635
        def check_dir(a):
636
            self.assertEquals(a, t.list_dir('.'))
637
        check_dir([])
638
        # when held, that's all we see
639
        ld1.attempt_lock()
640
        check_dir(['held'])
641
        # second guy should fail
642
        self.assertRaises(errors.LockContention, ld2.attempt_lock)
643
        # no kibble
644
        check_dir(['held'])