438
439
# it's still supported
439
440
a = "%d revisions pulled" % r
440
441
self.assertEqual(a, "10 revisions pulled")
445
class _StubLockable(object):
446
"""Helper for TestRunWithWriteLockedTarget."""
448
def __init__(self, calls, unlock_exc=None):
450
self.unlock_exc = unlock_exc
452
def lock_write(self):
453
self.calls.append('lock_write')
456
self.calls.append('unlock')
457
if self.unlock_exc is not None:
458
raise self.unlock_exc
461
class _ErrorFromCallable(Exception):
462
"""Helper for TestRunWithWriteLockedTarget."""
465
class _ErrorFromUnlock(Exception):
466
"""Helper for TestRunWithWriteLockedTarget."""
469
class TestRunWithWriteLockedTarget(TestCase):
470
"""Tests for _run_with_write_locked_target."""
475
def func_that_returns_ok(self):
476
self._calls.append('func called')
479
def func_that_raises(self):
480
self._calls.append('func called')
481
raise _ErrorFromCallable()
483
def test_success_unlocks(self):
484
lockable = _StubLockable(self._calls)
485
result = _run_with_write_locked_target(
486
lockable, self.func_that_returns_ok)
487
self.assertEqual('ok', result)
488
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
490
def test_exception_unlocks_and_propagates(self):
491
lockable = _StubLockable(self._calls)
492
self.assertRaises(_ErrorFromCallable,
493
_run_with_write_locked_target, lockable, self.func_that_raises)
494
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
496
def test_callable_succeeds_but_error_during_unlock(self):
497
lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
498
self.assertRaises(_ErrorFromUnlock,
499
_run_with_write_locked_target, lockable, self.func_that_returns_ok)
500
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)
502
def test_error_during_unlock_does_not_mask_original_error(self):
503
lockable = _StubLockable(self._calls, unlock_exc=_ErrorFromUnlock())
504
self.assertRaises(_ErrorFromCallable,
505
_run_with_write_locked_target, lockable, self.func_that_raises)
506
self.assertEqual(['lock_write', 'func called', 'unlock'], self._calls)