22
22
from bzrlib import decorators
23
from bzrlib.decorators import needs_read_lock, needs_write_lock
24
23
from bzrlib.tests import TestCase
27
class DecoratorSample(object):
28
"""Sample class that uses decorators.
26
def create_decorator_sample(style, except_in_unlock=False):
27
"""Create a DecoratorSample object, using specific lock operators.
30
Log when requests go through lock_read()/unlock() or lock_write()/unlock.
29
:param style: The type of lock decorators to use (fast/pretty/None)
30
:param except_in_unlock: If True, raise an exception during unlock
31
:return: An instantiated DecoratorSample object.
37
self.actions.append('lock_read')
40
self.actions.append('lock_write')
43
self.actions.append('unlock')
47
"""Frob the sample object"""
48
self.actions.append('frob')
51
def bank(self, bar, biz=None):
52
"""Bank the sample, but using bar and biz."""
53
self.actions.append(('bank', bar, biz))
36
needs_read_lock = decorators.needs_read_lock
37
needs_write_lock = decorators.needs_write_lock
38
elif style == 'pretty':
39
needs_read_lock = decorators._pretty_needs_read_lock
40
needs_write_lock = decorators._pretty_needs_write_lock
42
needs_read_lock = decorators._fast_needs_read_lock
43
needs_write_lock = decorators._fast_needs_write_lock
45
class DecoratorSample(object):
46
"""Sample class that uses decorators.
48
Log when requests go through lock_read()/unlock() or
56
self.actions.append('lock_read')
59
self.actions.append('lock_write')
63
self.actions.append('unlock_fail')
64
raise KeyError('during unlock')
66
self.actions.append('unlock')
70
"""Frob the sample object"""
71
self.actions.append('frob')
75
def bank(self, bar, biz=None):
76
"""Bank the sample, but using bar and biz."""
77
self.actions.append(('bank', bar, biz))
81
def fail_during_read(self):
82
self.actions.append('fail_during_read')
83
raise TypeError('during read')
86
def fail_during_write(self):
87
self.actions.append('fail_during_write')
88
raise TypeError('during write')
90
return DecoratorSample()
93
class TestDecoratorActions(TestCase):
95
_decorator_style = None # default
97
def test_read_lock_locks_and_unlocks(self):
98
sam = create_decorator_sample(self._decorator_style)
99
self.assertEqual('newbie', sam.frob())
100
self.assertEqual(['lock_read', 'frob', 'unlock'], sam.actions)
102
def test_write_lock_locks_and_unlocks(self):
103
sam = create_decorator_sample(self._decorator_style)
104
self.assertEqual(('bar', 'bing'), sam.bank('bar', biz='bing'))
105
self.assertEqual(['lock_write', ('bank', 'bar', 'bing'), 'unlock'],
108
def test_read_lock_unlocks_during_failure(self):
109
sam = create_decorator_sample(self._decorator_style)
110
self.assertRaises(TypeError, sam.fail_during_read)
111
self.assertEqual(['lock_read', 'fail_during_read', 'unlock'],
114
def test_write_lock_unlocks_during_failure(self):
115
sam = create_decorator_sample(self._decorator_style)
116
self.assertRaises(TypeError, sam.fail_during_write)
117
self.assertEqual(['lock_write', 'fail_during_write', 'unlock'],
120
def test_read_lock_raises_original_error(self):
121
sam = create_decorator_sample(self._decorator_style,
122
except_in_unlock=True)
123
self.assertRaises(TypeError, sam.fail_during_read)
124
self.assertEqual(['lock_read', 'fail_during_read', 'unlock_fail'],
127
def test_write_lock_raises_original_error(self):
128
sam = create_decorator_sample(self._decorator_style,
129
except_in_unlock=True)
130
self.assertRaises(TypeError, sam.fail_during_write)
131
self.assertEqual(['lock_write', 'fail_during_write', 'unlock_fail'],
134
def test_read_lock_raises_unlock_error(self):
135
sam = create_decorator_sample(self._decorator_style,
136
except_in_unlock=True)
137
self.assertRaises(KeyError, sam.frob)
138
self.assertEqual(['lock_read', 'frob', 'unlock_fail'], sam.actions)
140
def test_write_lock_raises_unlock_error(self):
141
sam = create_decorator_sample(self._decorator_style,
142
except_in_unlock=True)
143
self.assertRaises(KeyError, sam.bank, 'bar', biz='bing')
144
self.assertEqual(['lock_write', ('bank', 'bar', 'bing'),
145
'unlock_fail'], sam.actions)
148
class TestFastDecoratorActions(TestDecoratorActions):
150
_decorator_style = 'fast'
153
class TestPrettyDecoratorActions(TestDecoratorActions):
155
_decorator_style = 'pretty'
56
158
class TestDecoratorDocs(TestCase):