~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lockdir.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-30 13:54:37 UTC
  • mto: (1946.2.6 reduce-knit-churn)
  • mto: This revision was merged to the branch mainline in revision 1898.
  • Revision ID: john@arbash-meinel.com-20060730135437-1d722abdb14bff76
(jelmer) Install new intertree tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Tests for LockDir"""
18
18
 
19
19
from cStringIO import StringIO
20
 
from threading import Thread, Lock
 
20
from threading import Thread
21
21
import time
22
22
 
23
23
import bzrlib
24
 
from bzrlib import (
25
 
    config,
26
 
    osutils,
27
 
    )
28
24
from bzrlib.errors import (
29
25
        LockBreakMismatch,
30
26
        LockContention, LockError, UnlockableTransport,
31
27
        LockNotHeld, LockBroken
32
28
        )
33
 
from bzrlib.lockdir import LockDir, _DEFAULT_TIMEOUT_SECONDS
 
29
from bzrlib.lockdir import LockDir
34
30
from bzrlib.tests import TestCaseWithTransport
35
 
from bzrlib.trace import note
36
31
 
37
32
# These tests sometimes use threads to test the behaviour of lock files with
38
33
# concurrent actors.  This is not a typical (or necessarily supported) use;
47
42
class TestLockDir(TestCaseWithTransport):
48
43
    """Test LockDir operations"""
49
44
 
50
 
    def logging_report_function(self, fmt, *args):
51
 
        self._logged_reports.append((fmt, args))
52
 
 
53
 
    def setup_log_reporter(self, lock_dir):
54
 
        self._logged_reports = []
55
 
        lock_dir._report_function = self.logging_report_function
56
 
 
57
45
    def test_00_lock_creation(self):
58
46
        """Creation of lock file on a transport"""
59
47
        t = self.get_transport()
168
156
        lf1 = LockDir(t, 'test_lock')
169
157
        lf1.create()
170
158
        lf2 = LockDir(t, 'test_lock')
171
 
        self.setup_log_reporter(lf2)
172
159
        lf1.attempt_lock()
173
160
        try:
174
161
            before = time.time()
181
168
                    "took %f seconds to detect lock contention" % (after - before))
182
169
        finally:
183
170
            lf1.unlock()
184
 
        lock_base = lf2.transport.abspath(lf2.path)
185
 
        self.assertEqual(1, len(self._logged_reports))
186
 
        self.assertEqual('%s %s\n'
187
 
                         '%s\n%s\n'
188
 
                         'Will continue to try until %s\n',
189
 
                         self._logged_reports[0][0])
190
 
        args = self._logged_reports[0][1]
191
 
        self.assertEqual('Unable to obtain', args[0])
192
 
        self.assertEqual('lock %s' % (lock_base,), args[1])
193
 
        self.assertStartsWith(args[2], 'held by ')
194
 
        self.assertStartsWith(args[3], 'locked ')
195
 
        self.assertEndsWith(args[3], ' ago')
196
 
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
197
171
 
198
172
    def test_31_lock_wait_easy(self):
199
173
        """Succeed when waiting on a lock with no contention.
201
175
        t = self.get_transport()
202
176
        lf1 = LockDir(t, 'test_lock')
203
177
        lf1.create()
204
 
        self.setup_log_reporter(lf1)
205
178
        try:
206
179
            before = time.time()
207
180
            lf1.wait_lock(timeout=0.4, poll=0.1)
209
182
            self.assertTrue(after - before <= 1.0)
210
183
        finally:
211
184
            lf1.unlock()
212
 
        self.assertEqual([], self._logged_reports)
213
185
 
214
186
    def test_32_lock_wait_succeed(self):
215
187
        """Succeed when trying to acquire a lock that gets released
229
201
        unlocker.start()
230
202
        try:
231
203
            lf2 = LockDir(t, 'test_lock')
232
 
            self.setup_log_reporter(lf2)
233
204
            before = time.time()
234
205
            # wait and then lock
235
206
            lf2.wait_lock(timeout=0.4, poll=0.1)
238
209
        finally:
239
210
            unlocker.join()
240
211
 
241
 
        # There should be only 1 report, even though it should have to
242
 
        # wait for a while
243
 
        lock_base = lf2.transport.abspath(lf2.path)
244
 
        self.assertEqual(1, len(self._logged_reports))
245
 
        self.assertEqual('%s %s\n'
246
 
                         '%s\n%s\n'
247
 
                         'Will continue to try until %s\n',
248
 
                         self._logged_reports[0][0])
249
 
        args = self._logged_reports[0][1]
250
 
        self.assertEqual('Unable to obtain', args[0])
251
 
        self.assertEqual('lock %s' % (lock_base,), args[1])
252
 
        self.assertStartsWith(args[2], 'held by ')
253
 
        self.assertStartsWith(args[3], 'locked ')
254
 
        self.assertEndsWith(args[3], ' ago')
255
 
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
256
 
 
257
212
    def test_33_wait(self):
258
213
        """Succeed when waiting on a lock that gets released
259
214
 
281
236
        finally:
282
237
            unlocker.join()
283
238
 
284
 
    def test_34_lock_write_waits(self):
285
 
        """LockDir.lock_write() will wait for the lock.""" 
286
 
        t = self.get_transport()
287
 
        lf1 = LockDir(t, 'test_lock')
288
 
        lf1.create()
289
 
        lf1.attempt_lock()
290
 
 
291
 
        def wait_and_unlock():
292
 
            time.sleep(0.1)
293
 
            lf1.unlock()
294
 
        unlocker = Thread(target=wait_and_unlock)
295
 
        unlocker.start()
296
 
        try:
297
 
            lf2 = LockDir(t, 'test_lock')
298
 
            self.setup_log_reporter(lf2)
299
 
            before = time.time()
300
 
            # wait and then lock
301
 
            lf2.lock_write()
302
 
            after = time.time()
303
 
        finally:
304
 
            unlocker.join()
305
 
 
306
 
        # There should be only 1 report, even though it should have to
307
 
        # wait for a while
308
 
        lock_base = lf2.transport.abspath(lf2.path)
309
 
        self.assertEqual(1, len(self._logged_reports))
310
 
        self.assertEqual('%s %s\n'
311
 
                         '%s\n%s\n'
312
 
                         'Will continue to try until %s\n',
313
 
                         self._logged_reports[0][0])
314
 
        args = self._logged_reports[0][1]
315
 
        self.assertEqual('Unable to obtain', args[0])
316
 
        self.assertEqual('lock %s' % (lock_base,), args[1])
317
 
        self.assertStartsWith(args[2], 'held by ')
318
 
        self.assertStartsWith(args[3], 'locked ')
319
 
        self.assertEndsWith(args[3], ' ago')
320
 
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
321
 
 
322
 
    def test_35_wait_lock_changing(self):
323
 
        """LockDir.wait_lock() will report if the lock changes underneath.
324
 
        
325
 
        This is the stages we want to happen:
326
 
 
327
 
        0) Synchronization locks are created and locked.
328
 
        1) Lock1 obtains the lockdir, and releases the 'check' lock.
329
 
        2) Lock2 grabs the 'check' lock, and checks the lockdir.
330
 
           It sees the lockdir is already acquired, reports the fact, 
331
 
           and unsets the 'checked' lock.
332
 
        3) Thread1 blocks on acquiring the 'checked' lock, and then tells
333
 
           Lock1 to release and acquire the lockdir. This resets the 'check'
334
 
           lock.
335
 
        4) Lock2 acquires the 'check' lock, and checks again. It notices
336
 
           that the holder of the lock has changed, and so reports a new 
337
 
           lock holder.
338
 
        5) Thread1 blocks on the 'checked' lock, this time, it completely
339
 
           unlocks the lockdir, allowing Lock2 to acquire the lock.
340
 
        """
341
 
 
342
 
        wait_to_check_lock = Lock()
343
 
        wait_until_checked_lock = Lock()
344
 
 
345
 
        wait_to_check_lock.acquire()
346
 
        wait_until_checked_lock.acquire()
347
 
        note('locked check and checked locks')
348
 
 
349
 
        class LockDir1(LockDir):
350
 
            """Use the synchronization points for the first lock."""
351
 
 
352
 
            def attempt_lock(self):
353
 
                # Once we have acquired the lock, it is okay for
354
 
                # the other lock to check it
355
 
                try:
356
 
                    return super(LockDir1, self).attempt_lock()
357
 
                finally:
358
 
                    note('lock1: releasing check lock')
359
 
                    wait_to_check_lock.release()
360
 
 
361
 
        class LockDir2(LockDir):
362
 
            """Use the synchronization points for the second lock."""
363
 
 
364
 
            def attempt_lock(self):
365
 
                note('lock2: waiting for check lock')
366
 
                wait_to_check_lock.acquire()
367
 
                note('lock2: acquired check lock')
368
 
                try:
369
 
                    return super(LockDir2, self).attempt_lock()
370
 
                finally:
371
 
                    note('lock2: releasing checked lock')
372
 
                    wait_until_checked_lock.release()
373
 
 
374
 
        t = self.get_transport()
375
 
        lf1 = LockDir1(t, 'test_lock')
376
 
        lf1.create()
377
 
 
378
 
        lf2 = LockDir2(t, 'test_lock')
379
 
        self.setup_log_reporter(lf2)
380
 
 
381
 
        def wait_and_switch():
382
 
            lf1.attempt_lock()
383
 
            # Block until lock2 has had a chance to check
384
 
            note('lock1: waiting 1 for checked lock')
385
 
            wait_until_checked_lock.acquire()
386
 
            note('lock1: acquired for checked lock')
387
 
            note('lock1: released lockdir')
388
 
            lf1.unlock()
389
 
            note('lock1: acquiring lockdir')
390
 
            # Create a new nonce, so the lock looks different.
391
 
            lf1.nonce = osutils.rand_chars(20)
392
 
            lf1.lock_write()
393
 
            note('lock1: acquired lockdir')
394
 
 
395
 
            # Block until lock2 has peeked again
396
 
            note('lock1: waiting 2 for checked lock')
397
 
            wait_until_checked_lock.acquire()
398
 
            note('lock1: acquired for checked lock')
399
 
            # Now unlock, and let lock 2 grab the lock
400
 
            lf1.unlock()
401
 
            wait_to_check_lock.release()
402
 
 
403
 
        unlocker = Thread(target=wait_and_switch)
404
 
        unlocker.start()
405
 
        try:
406
 
            # Wait and play against the other thread
407
 
            lf2.wait_lock(timeout=1.0, poll=0.01)
408
 
        finally:
409
 
            unlocker.join()
410
 
        lf2.unlock()
411
 
 
412
 
        # There should be 2 reports, because the lock changed
413
 
        lock_base = lf2.transport.abspath(lf2.path)
414
 
        self.assertEqual(2, len(self._logged_reports))
415
 
 
416
 
        self.assertEqual('%s %s\n'
417
 
                         '%s\n%s\n'
418
 
                         'Will continue to try until %s\n',
419
 
                         self._logged_reports[0][0])
420
 
        args = self._logged_reports[0][1]
421
 
        self.assertEqual('Unable to obtain', args[0])
422
 
        self.assertEqual('lock %s' % (lock_base,), args[1])
423
 
        self.assertStartsWith(args[2], 'held by ')
424
 
        self.assertStartsWith(args[3], 'locked ')
425
 
        self.assertEndsWith(args[3], ' ago')
426
 
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
427
 
 
428
 
        self.assertEqual('%s %s\n'
429
 
                         '%s\n%s\n'
430
 
                         'Will continue to try until %s\n',
431
 
                         self._logged_reports[1][0])
432
 
        args = self._logged_reports[1][1]
433
 
        self.assertEqual('Lock owner changed for', args[0])
434
 
        self.assertEqual('lock %s' % (lock_base,), args[1])
435
 
        self.assertStartsWith(args[2], 'held by ')
436
 
        self.assertStartsWith(args[3], 'locked ')
437
 
        self.assertEndsWith(args[3], ' ago')
438
 
        self.assertContainsRe(args[4], r'\d\d:\d\d:\d\d')
439
 
 
440
239
    def test_40_confirm_easy(self):
441
240
        """Confirm a lock that's already held"""
442
241
        t = self.get_transport()
553
352
            self.assertRaises(LockBroken, ld1.unlock)
554
353
        finally:
555
354
            bzrlib.ui.ui_factory = orig_factory
556
 
 
557
 
    def test_create_missing_base_directory(self):
558
 
        """If LockDir.path doesn't exist, it can be created
559
 
 
560
 
        Some people manually remove the entire lock/ directory trying
561
 
        to unlock a stuck repository/branch/etc. Rather than failing
562
 
        after that, just create the lock directory when needed.
563
 
        """
564
 
        t = self.get_transport()
565
 
        lf1 = LockDir(t, 'test_lock')
566
 
 
567
 
        lf1.create()
568
 
        self.failUnless(t.has('test_lock'))
569
 
 
570
 
        t.rmdir('test_lock')
571
 
        self.failIf(t.has('test_lock'))
572
 
 
573
 
        # This will create 'test_lock' if it needs to
574
 
        lf1.lock_write()
575
 
        self.failUnless(t.has('test_lock'))
576
 
        self.failUnless(t.has('test_lock/held/info'))
577
 
 
578
 
        lf1.unlock()
579
 
        self.failIf(t.has('test_lock/held/info'))
580
 
 
581
 
    def test__format_lock_info(self):
582
 
        ld1 = self.get_lock()
583
 
        ld1.create()
584
 
        ld1.lock_write()
585
 
        try:
586
 
            info_list = ld1._format_lock_info(ld1.peek())
587
 
        finally:
588
 
            ld1.unlock()
589
 
        self.assertEqual('lock %s' % (ld1.transport.abspath(ld1.path),),
590
 
                         info_list[0])
591
 
        self.assertContainsRe(info_list[1],
592
 
                              r'^held by .* on host .* \[process #\d*\]$')
593
 
        self.assertContainsRe(info_list[2], r'locked \d+ seconds? ago$')
594
 
 
595
 
    def test_lock_without_email(self):
596
 
        global_config = config.GlobalConfig()
597
 
        # Intentionally has no email address
598
 
        global_config.set_user_option('email', 'User Identity')
599
 
        ld1 = self.get_lock()
600
 
        ld1.create()
601
 
        ld1.lock_write()
602
 
        ld1.unlock()