~lifeless/bzr/index.range_map

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_counted_lock.py

  • Committer: Robert Collins
  • Date: 2008-06-19 01:17:19 UTC
  • mfrom: (3218.1.277 +trunk)
  • Revision ID: robertc@robertcollins.net-20080619011719-1c4g4uxzzhdls2wf
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
19
19
from bzrlib.counted_lock import CountedLock
20
20
from bzrlib.errors import (
21
21
    LockError,
 
22
    LockNotHeld,
22
23
    ReadOnlyError,
 
24
    TokenMismatch,
23
25
    )
24
26
from bzrlib.tests import TestCase
25
27
 
39
41
        self._lock_mode = 'r'
40
42
        self._calls.append('lock_read')
41
43
 
42
 
    def lock_write(self):
 
44
    def lock_write(self, token=None):
 
45
        if token not in (None, 'token'):
 
46
            raise TokenMismatch(token, 'token')
43
47
        self._assert_not_locked()
44
48
        self._lock_mode = 'w'
45
49
        self._calls.append('lock_write')
 
50
        return 'token'
46
51
 
47
52
    def unlock(self):
48
53
        self._assert_locked()
62
67
            raise LockError("%s is already locked in mode %r" %
63
68
                (self, self._lock_mode))
64
69
 
 
70
    def validate_token(self, token):
 
71
        if token == 'token':
 
72
            # already held by this caller
 
73
            return 'token'
 
74
        elif token is None:
 
75
            return
 
76
        else:
 
77
            raise TokenMismatch(token, 'token')
 
78
 
65
79
 
66
80
class TestDummyLock(TestCase):
67
81
 
131
145
    def test_unlock_not_locked(self):
132
146
        real_lock = DummyLock()
133
147
        l = CountedLock(real_lock)
134
 
        self.assertRaises(LockError, l.unlock)
 
148
        self.assertRaises(LockNotHeld, l.unlock)
135
149
 
136
150
    def test_read_lock_while_write_locked(self):
137
151
        real_lock = DummyLock()