root / test / ganeti.locking_unittest.py @ d76167a5
History | View | Annotate | Download (39.8 kB)
1 | 162c1c1f | Guido Trotter | #!/usr/bin/python
|
---|---|---|---|
2 | 162c1c1f | Guido Trotter | #
|
3 | 162c1c1f | Guido Trotter | |
4 | 162c1c1f | Guido Trotter | # Copyright (C) 2006, 2007 Google Inc.
|
5 | 162c1c1f | Guido Trotter | #
|
6 | 162c1c1f | Guido Trotter | # This program is free software; you can redistribute it and/or modify
|
7 | 162c1c1f | Guido Trotter | # it under the terms of the GNU General Public License as published by
|
8 | 162c1c1f | Guido Trotter | # the Free Software Foundation; either version 2 of the License, or
|
9 | 162c1c1f | Guido Trotter | # (at your option) any later version.
|
10 | 162c1c1f | Guido Trotter | #
|
11 | 162c1c1f | Guido Trotter | # This program is distributed in the hope that it will be useful, but
|
12 | 162c1c1f | Guido Trotter | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 162c1c1f | Guido Trotter | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 162c1c1f | Guido Trotter | # General Public License for more details.
|
15 | 162c1c1f | Guido Trotter | #
|
16 | 162c1c1f | Guido Trotter | # You should have received a copy of the GNU General Public License
|
17 | 162c1c1f | Guido Trotter | # along with this program; if not, write to the Free Software
|
18 | 162c1c1f | Guido Trotter | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 162c1c1f | Guido Trotter | # 0.0510-1301, USA.
|
20 | 162c1c1f | Guido Trotter | |
21 | 162c1c1f | Guido Trotter | |
22 | 162c1c1f | Guido Trotter | """Script for unittesting the locking module"""
|
23 | 162c1c1f | Guido Trotter | |
24 | 162c1c1f | Guido Trotter | |
25 | 162c1c1f | Guido Trotter | import os |
26 | 162c1c1f | Guido Trotter | import unittest |
27 | 162c1c1f | Guido Trotter | import time |
28 | 162c1c1f | Guido Trotter | import Queue |
29 | 84e344d4 | Michael Hanselmann | import threading |
30 | 162c1c1f | Guido Trotter | |
31 | 162c1c1f | Guido Trotter | from ganeti import locking |
32 | a95fd5d7 | Guido Trotter | from ganeti import errors |
33 | 162c1c1f | Guido Trotter | |
34 | 162c1c1f | Guido Trotter | |
35 | 42a999d1 | Guido Trotter | # This is used to test the ssynchronize decorator.
|
36 | 42a999d1 | Guido Trotter | # Since it's passed as input to a decorator it must be declared as a global.
|
37 | 42a999d1 | Guido Trotter | _decoratorlock = locking.SharedLock() |
38 | 42a999d1 | Guido Trotter | |
39 | 4607c978 | Iustin Pop | #: List for looping tests
|
40 | 4607c978 | Iustin Pop | ITERATIONS = range(8) |
41 | 4607c978 | Iustin Pop | |
42 | 84e344d4 | Michael Hanselmann | |
43 | 4607c978 | Iustin Pop | def _Repeat(fn): |
44 | 4607c978 | Iustin Pop | """Decorator for executing a function many times"""
|
45 | 4607c978 | Iustin Pop | def wrapper(*args, **kwargs): |
46 | 4607c978 | Iustin Pop | for i in ITERATIONS: |
47 | 4607c978 | Iustin Pop | fn(*args, **kwargs) |
48 | 4607c978 | Iustin Pop | return wrapper
|
49 | 4607c978 | Iustin Pop | |
50 | 84e344d4 | Michael Hanselmann | |
51 | 4607c978 | Iustin Pop | class _ThreadedTestCase(unittest.TestCase): |
52 | 4607c978 | Iustin Pop | """Test class that supports adding/waiting on threads"""
|
53 | 4607c978 | Iustin Pop | def setUp(self): |
54 | 4607c978 | Iustin Pop | unittest.TestCase.setUp(self)
|
55 | 4607c978 | Iustin Pop | self.threads = []
|
56 | 4607c978 | Iustin Pop | |
57 | 4607c978 | Iustin Pop | def _addThread(self, *args, **kwargs): |
58 | 4607c978 | Iustin Pop | """Create and remember a new thread"""
|
59 | 84e344d4 | Michael Hanselmann | t = threading.Thread(*args, **kwargs) |
60 | 4607c978 | Iustin Pop | self.threads.append(t)
|
61 | 4607c978 | Iustin Pop | t.start() |
62 | 4607c978 | Iustin Pop | return t
|
63 | 4607c978 | Iustin Pop | |
64 | 4607c978 | Iustin Pop | def _waitThreads(self): |
65 | 4607c978 | Iustin Pop | """Wait for all our threads to finish"""
|
66 | 4607c978 | Iustin Pop | for t in self.threads: |
67 | 4607c978 | Iustin Pop | t.join(60)
|
68 | 4607c978 | Iustin Pop | self.failIf(t.isAlive())
|
69 | 4607c978 | Iustin Pop | self.threads = []
|
70 | 42a999d1 | Guido Trotter | |
71 | 4607c978 | Iustin Pop | |
72 | d76167a5 | Michael Hanselmann | class TestSingleActionPipeCondition(unittest.TestCase): |
73 | d76167a5 | Michael Hanselmann | """_SingleActionPipeCondition tests"""
|
74 | d76167a5 | Michael Hanselmann | |
75 | d76167a5 | Michael Hanselmann | def setUp(self): |
76 | d76167a5 | Michael Hanselmann | self.cond = locking._SingleActionPipeCondition()
|
77 | d76167a5 | Michael Hanselmann | |
78 | d76167a5 | Michael Hanselmann | def testInitialization(self): |
79 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._read_fd is not None) |
80 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._write_fd is not None) |
81 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._poller is not None) |
82 | d76167a5 | Michael Hanselmann | self.assertEqual(self.cond._nwaiters, 0) |
83 | d76167a5 | Michael Hanselmann | |
84 | d76167a5 | Michael Hanselmann | def testUsageCount(self): |
85 | d76167a5 | Michael Hanselmann | self.cond.StartWaiting()
|
86 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._read_fd is not None) |
87 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._write_fd is not None) |
88 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._poller is not None) |
89 | d76167a5 | Michael Hanselmann | self.assertEqual(self.cond._nwaiters, 1) |
90 | d76167a5 | Michael Hanselmann | |
91 | d76167a5 | Michael Hanselmann | # use again
|
92 | d76167a5 | Michael Hanselmann | self.cond.StartWaiting()
|
93 | d76167a5 | Michael Hanselmann | self.assertEqual(self.cond._nwaiters, 2) |
94 | d76167a5 | Michael Hanselmann | |
95 | d76167a5 | Michael Hanselmann | # there is more than one user
|
96 | d76167a5 | Michael Hanselmann | self.assert_(not self.cond.DoneWaiting()) |
97 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._read_fd is not None) |
98 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._write_fd is not None) |
99 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._poller is not None) |
100 | d76167a5 | Michael Hanselmann | self.assertEqual(self.cond._nwaiters, 1) |
101 | d76167a5 | Michael Hanselmann | |
102 | d76167a5 | Michael Hanselmann | self.assert_(self.cond.DoneWaiting()) |
103 | d76167a5 | Michael Hanselmann | self.assertEqual(self.cond._nwaiters, 0) |
104 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._read_fd is None) |
105 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._write_fd is None) |
106 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._poller is None) |
107 | d76167a5 | Michael Hanselmann | |
108 | d76167a5 | Michael Hanselmann | def testNotify(self): |
109 | d76167a5 | Michael Hanselmann | wait1 = self.cond.StartWaiting()
|
110 | d76167a5 | Michael Hanselmann | wait2 = self.cond.StartWaiting()
|
111 | d76167a5 | Michael Hanselmann | |
112 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._read_fd is not None) |
113 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._write_fd is not None) |
114 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._poller is not None) |
115 | d76167a5 | Michael Hanselmann | |
116 | d76167a5 | Michael Hanselmann | self.cond.notifyAll()
|
117 | d76167a5 | Michael Hanselmann | |
118 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._read_fd is not None) |
119 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._write_fd is None) |
120 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._poller is not None) |
121 | d76167a5 | Michael Hanselmann | |
122 | d76167a5 | Michael Hanselmann | self.assert_(not self.cond.DoneWaiting()) |
123 | d76167a5 | Michael Hanselmann | |
124 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._read_fd is not None) |
125 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._write_fd is None) |
126 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._poller is not None) |
127 | d76167a5 | Michael Hanselmann | |
128 | d76167a5 | Michael Hanselmann | self.assert_(self.cond.DoneWaiting()) |
129 | d76167a5 | Michael Hanselmann | |
130 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._read_fd is None) |
131 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._write_fd is None) |
132 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._poller is None) |
133 | d76167a5 | Michael Hanselmann | |
134 | d76167a5 | Michael Hanselmann | def testReusage(self): |
135 | d76167a5 | Michael Hanselmann | self.cond.StartWaiting()
|
136 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._read_fd is not None) |
137 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._write_fd is not None) |
138 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._poller is not None) |
139 | d76167a5 | Michael Hanselmann | |
140 | d76167a5 | Michael Hanselmann | self.assert_(self.cond.DoneWaiting()) |
141 | d76167a5 | Michael Hanselmann | |
142 | d76167a5 | Michael Hanselmann | self.assertRaises(RuntimeError, self.cond.StartWaiting) |
143 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._read_fd is None) |
144 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._write_fd is None) |
145 | d76167a5 | Michael Hanselmann | self.assert_(self.cond._poller is None) |
146 | d76167a5 | Michael Hanselmann | |
147 | d76167a5 | Michael Hanselmann | def testNotifyTwice(self): |
148 | d76167a5 | Michael Hanselmann | self.cond.notifyAll()
|
149 | d76167a5 | Michael Hanselmann | self.assertRaises(RuntimeError, self.cond.notifyAll) |
150 | d76167a5 | Michael Hanselmann | |
151 | d76167a5 | Michael Hanselmann | |
152 | 4607c978 | Iustin Pop | class TestSharedLock(_ThreadedTestCase): |
153 | d6646186 | Guido Trotter | """SharedLock tests"""
|
154 | 162c1c1f | Guido Trotter | |
155 | 162c1c1f | Guido Trotter | def setUp(self): |
156 | 4607c978 | Iustin Pop | _ThreadedTestCase.setUp(self)
|
157 | 162c1c1f | Guido Trotter | self.sl = locking.SharedLock()
|
158 | 162c1c1f | Guido Trotter | # helper threads use the 'done' queue to tell the master they finished.
|
159 | 162c1c1f | Guido Trotter | self.done = Queue.Queue(0) |
160 | 162c1c1f | Guido Trotter | |
161 | 162c1c1f | Guido Trotter | def testSequenceAndOwnership(self): |
162 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned()) |
163 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
164 | 162c1c1f | Guido Trotter | self.assert_(self.sl._is_owned()) |
165 | 162c1c1f | Guido Trotter | self.assert_(self.sl._is_owned(shared=1)) |
166 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned(shared=0)) |
167 | 162c1c1f | Guido Trotter | self.sl.release()
|
168 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned()) |
169 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
170 | 162c1c1f | Guido Trotter | self.assert_(self.sl._is_owned()) |
171 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned(shared=1)) |
172 | 162c1c1f | Guido Trotter | self.assert_(self.sl._is_owned(shared=0)) |
173 | 162c1c1f | Guido Trotter | self.sl.release()
|
174 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned()) |
175 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
176 | 162c1c1f | Guido Trotter | self.assert_(self.sl._is_owned()) |
177 | 162c1c1f | Guido Trotter | self.assert_(self.sl._is_owned(shared=1)) |
178 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned(shared=0)) |
179 | 162c1c1f | Guido Trotter | self.sl.release()
|
180 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned()) |
181 | 162c1c1f | Guido Trotter | |
182 | 162c1c1f | Guido Trotter | def testBooleanValue(self): |
183 | 162c1c1f | Guido Trotter | # semaphores are supposed to return a true value on a successful acquire
|
184 | 162c1c1f | Guido Trotter | self.assert_(self.sl.acquire(shared=1)) |
185 | 162c1c1f | Guido Trotter | self.sl.release()
|
186 | 162c1c1f | Guido Trotter | self.assert_(self.sl.acquire()) |
187 | 162c1c1f | Guido Trotter | self.sl.release()
|
188 | 162c1c1f | Guido Trotter | |
189 | 162c1c1f | Guido Trotter | def testDoubleLockingStoE(self): |
190 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
191 | 162c1c1f | Guido Trotter | self.assertRaises(AssertionError, self.sl.acquire) |
192 | 162c1c1f | Guido Trotter | |
193 | 162c1c1f | Guido Trotter | def testDoubleLockingEtoS(self): |
194 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
195 | 162c1c1f | Guido Trotter | self.assertRaises(AssertionError, self.sl.acquire, shared=1) |
196 | 162c1c1f | Guido Trotter | |
197 | 162c1c1f | Guido Trotter | def testDoubleLockingStoS(self): |
198 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
199 | 162c1c1f | Guido Trotter | self.assertRaises(AssertionError, self.sl.acquire, shared=1) |
200 | 162c1c1f | Guido Trotter | |
201 | 162c1c1f | Guido Trotter | def testDoubleLockingEtoE(self): |
202 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
203 | 162c1c1f | Guido Trotter | self.assertRaises(AssertionError, self.sl.acquire) |
204 | 162c1c1f | Guido Trotter | |
205 | 162c1c1f | Guido Trotter | # helper functions: called in a separate thread they acquire the lock, send
|
206 | 162c1c1f | Guido Trotter | # their identifier on the done queue, then release it.
|
207 | 162c1c1f | Guido Trotter | def _doItSharer(self): |
208 | a95fd5d7 | Guido Trotter | try:
|
209 | a95fd5d7 | Guido Trotter | self.sl.acquire(shared=1) |
210 | a95fd5d7 | Guido Trotter | self.done.put('SHR') |
211 | a95fd5d7 | Guido Trotter | self.sl.release()
|
212 | a95fd5d7 | Guido Trotter | except errors.LockError:
|
213 | a95fd5d7 | Guido Trotter | self.done.put('ERR') |
214 | 162c1c1f | Guido Trotter | |
215 | 162c1c1f | Guido Trotter | def _doItExclusive(self): |
216 | a95fd5d7 | Guido Trotter | try:
|
217 | a95fd5d7 | Guido Trotter | self.sl.acquire()
|
218 | a95fd5d7 | Guido Trotter | self.done.put('EXC') |
219 | a95fd5d7 | Guido Trotter | self.sl.release()
|
220 | a95fd5d7 | Guido Trotter | except errors.LockError:
|
221 | a95fd5d7 | Guido Trotter | self.done.put('ERR') |
222 | a95fd5d7 | Guido Trotter | |
223 | a95fd5d7 | Guido Trotter | def _doItDelete(self): |
224 | a95fd5d7 | Guido Trotter | try:
|
225 | 4354ab03 | Guido Trotter | self.sl.delete()
|
226 | a95fd5d7 | Guido Trotter | self.done.put('DEL') |
227 | a95fd5d7 | Guido Trotter | except errors.LockError:
|
228 | a95fd5d7 | Guido Trotter | self.done.put('ERR') |
229 | 162c1c1f | Guido Trotter | |
230 | 162c1c1f | Guido Trotter | def testSharersCanCoexist(self): |
231 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
232 | 84e344d4 | Michael Hanselmann | threading.Thread(target=self._doItSharer).start()
|
233 | 162c1c1f | Guido Trotter | self.assert_(self.done.get(True, 1)) |
234 | 162c1c1f | Guido Trotter | self.sl.release()
|
235 | 162c1c1f | Guido Trotter | |
236 | 4607c978 | Iustin Pop | @_Repeat
|
237 | 162c1c1f | Guido Trotter | def testExclusiveBlocksExclusive(self): |
238 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
239 | 4607c978 | Iustin Pop | self._addThread(target=self._doItExclusive) |
240 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
241 | 162c1c1f | Guido Trotter | self.sl.release()
|
242 | 4607c978 | Iustin Pop | self._waitThreads()
|
243 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'EXC') |
244 | 162c1c1f | Guido Trotter | |
245 | 4607c978 | Iustin Pop | @_Repeat
|
246 | a95fd5d7 | Guido Trotter | def testExclusiveBlocksDelete(self): |
247 | a95fd5d7 | Guido Trotter | self.sl.acquire()
|
248 | 4607c978 | Iustin Pop | self._addThread(target=self._doItDelete) |
249 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
250 | a95fd5d7 | Guido Trotter | self.sl.release()
|
251 | 4607c978 | Iustin Pop | self._waitThreads()
|
252 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'DEL') |
253 | 4607c978 | Iustin Pop | self.sl = locking.SharedLock()
|
254 | a95fd5d7 | Guido Trotter | |
255 | 4607c978 | Iustin Pop | @_Repeat
|
256 | 162c1c1f | Guido Trotter | def testExclusiveBlocksSharer(self): |
257 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
258 | 4607c978 | Iustin Pop | self._addThread(target=self._doItSharer) |
259 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
260 | 162c1c1f | Guido Trotter | self.sl.release()
|
261 | 4607c978 | Iustin Pop | self._waitThreads()
|
262 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'SHR') |
263 | 162c1c1f | Guido Trotter | |
264 | 4607c978 | Iustin Pop | @_Repeat
|
265 | 162c1c1f | Guido Trotter | def testSharerBlocksExclusive(self): |
266 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
267 | 4607c978 | Iustin Pop | self._addThread(target=self._doItExclusive) |
268 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
269 | 162c1c1f | Guido Trotter | self.sl.release()
|
270 | 4607c978 | Iustin Pop | self._waitThreads()
|
271 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'EXC') |
272 | 162c1c1f | Guido Trotter | |
273 | 4607c978 | Iustin Pop | @_Repeat
|
274 | a95fd5d7 | Guido Trotter | def testSharerBlocksDelete(self): |
275 | a95fd5d7 | Guido Trotter | self.sl.acquire(shared=1) |
276 | 4607c978 | Iustin Pop | self._addThread(target=self._doItDelete) |
277 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
278 | a95fd5d7 | Guido Trotter | self.sl.release()
|
279 | 4607c978 | Iustin Pop | self._waitThreads()
|
280 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'DEL') |
281 | 4607c978 | Iustin Pop | self.sl = locking.SharedLock()
|
282 | a95fd5d7 | Guido Trotter | |
283 | 4607c978 | Iustin Pop | @_Repeat
|
284 | 162c1c1f | Guido Trotter | def testWaitingExclusiveBlocksSharer(self): |
285 | e6416152 | Iustin Pop | """SKIPPED testWaitingExclusiveBlockSharer"""
|
286 | e6416152 | Iustin Pop | return
|
287 | e6416152 | Iustin Pop | |
288 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
289 | 162c1c1f | Guido Trotter | # the lock is acquired in shared mode...
|
290 | 4607c978 | Iustin Pop | self._addThread(target=self._doItExclusive) |
291 | 162c1c1f | Guido Trotter | # ...but now an exclusive is waiting...
|
292 | 4607c978 | Iustin Pop | self._addThread(target=self._doItSharer) |
293 | 162c1c1f | Guido Trotter | # ...so the sharer should be blocked as well
|
294 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
295 | 162c1c1f | Guido Trotter | self.sl.release()
|
296 | 4607c978 | Iustin Pop | self._waitThreads()
|
297 | 162c1c1f | Guido Trotter | # The exclusive passed before
|
298 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'EXC') |
299 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'SHR') |
300 | 162c1c1f | Guido Trotter | |
301 | 4607c978 | Iustin Pop | @_Repeat
|
302 | 162c1c1f | Guido Trotter | def testWaitingSharerBlocksExclusive(self): |
303 | a143be68 | Iustin Pop | """SKIPPED testWaitingSharerBlocksExclusive"""
|
304 | a143be68 | Iustin Pop | return
|
305 | a143be68 | Iustin Pop | |
306 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
307 | 162c1c1f | Guido Trotter | # the lock is acquired in exclusive mode...
|
308 | 4607c978 | Iustin Pop | self._addThread(target=self._doItSharer) |
309 | 162c1c1f | Guido Trotter | # ...but now a sharer is waiting...
|
310 | 4607c978 | Iustin Pop | self._addThread(target=self._doItExclusive) |
311 | 162c1c1f | Guido Trotter | # ...the exclusive is waiting too...
|
312 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
313 | 162c1c1f | Guido Trotter | self.sl.release()
|
314 | 4607c978 | Iustin Pop | self._waitThreads()
|
315 | 162c1c1f | Guido Trotter | # The sharer passed before
|
316 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'SHR') |
317 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'EXC') |
318 | 162c1c1f | Guido Trotter | |
319 | a95fd5d7 | Guido Trotter | def testDelete(self): |
320 | a95fd5d7 | Guido Trotter | self.sl.delete()
|
321 | a95fd5d7 | Guido Trotter | self.assertRaises(errors.LockError, self.sl.acquire) |
322 | 84152b96 | Guido Trotter | self.assertRaises(errors.LockError, self.sl.acquire, shared=1) |
323 | a95fd5d7 | Guido Trotter | self.assertRaises(errors.LockError, self.sl.delete) |
324 | a95fd5d7 | Guido Trotter | |
325 | 84152b96 | Guido Trotter | def testNoDeleteIfSharer(self): |
326 | 84152b96 | Guido Trotter | self.sl.acquire(shared=1) |
327 | 84152b96 | Guido Trotter | self.assertRaises(AssertionError, self.sl.delete) |
328 | 84152b96 | Guido Trotter | |
329 | 4607c978 | Iustin Pop | @_Repeat
|
330 | a95fd5d7 | Guido Trotter | def testDeletePendingSharersExclusiveDelete(self): |
331 | a95fd5d7 | Guido Trotter | self.sl.acquire()
|
332 | 4607c978 | Iustin Pop | self._addThread(target=self._doItSharer) |
333 | 4607c978 | Iustin Pop | self._addThread(target=self._doItSharer) |
334 | 4607c978 | Iustin Pop | self._addThread(target=self._doItExclusive) |
335 | 4607c978 | Iustin Pop | self._addThread(target=self._doItDelete) |
336 | a95fd5d7 | Guido Trotter | self.sl.delete()
|
337 | 4607c978 | Iustin Pop | self._waitThreads()
|
338 | 4607c978 | Iustin Pop | # The threads who were pending return ERR
|
339 | 4607c978 | Iustin Pop | for _ in range(4): |
340 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'ERR') |
341 | 4607c978 | Iustin Pop | self.sl = locking.SharedLock()
|
342 | a95fd5d7 | Guido Trotter | |
343 | 4607c978 | Iustin Pop | @_Repeat
|
344 | a95fd5d7 | Guido Trotter | def testDeletePendingDeleteExclusiveSharers(self): |
345 | a95fd5d7 | Guido Trotter | self.sl.acquire()
|
346 | 4607c978 | Iustin Pop | self._addThread(target=self._doItDelete) |
347 | 4607c978 | Iustin Pop | self._addThread(target=self._doItExclusive) |
348 | 4607c978 | Iustin Pop | self._addThread(target=self._doItSharer) |
349 | 4607c978 | Iustin Pop | self._addThread(target=self._doItSharer) |
350 | a95fd5d7 | Guido Trotter | self.sl.delete()
|
351 | 4607c978 | Iustin Pop | self._waitThreads()
|
352 | a95fd5d7 | Guido Trotter | # The two threads who were pending return both ERR
|
353 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'ERR') |
354 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'ERR') |
355 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'ERR') |
356 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'ERR') |
357 | 4607c978 | Iustin Pop | self.sl = locking.SharedLock()
|
358 | a95fd5d7 | Guido Trotter | |
359 | 84e344d4 | Michael Hanselmann | @_Repeat
|
360 | 84e344d4 | Michael Hanselmann | def testExclusiveAcquireTimeout(self): |
361 | 84e344d4 | Michael Hanselmann | def _LockExclusive(wait): |
362 | 84e344d4 | Michael Hanselmann | self.sl.acquire(shared=0) |
363 | 84e344d4 | Michael Hanselmann | self.done.put("A: start sleep") |
364 | 84e344d4 | Michael Hanselmann | time.sleep(wait) |
365 | 84e344d4 | Michael Hanselmann | self.done.put("A: end sleep") |
366 | 84e344d4 | Michael Hanselmann | self.sl.release()
|
367 | 84e344d4 | Michael Hanselmann | |
368 | 84e344d4 | Michael Hanselmann | for shared in [0, 1]: |
369 | 84e344d4 | Michael Hanselmann | # Start thread to hold lock for 20 ms
|
370 | 84e344d4 | Michael Hanselmann | self._addThread(target=_LockExclusive, args=(20.0 / 1000.0, )) |
371 | 84e344d4 | Michael Hanselmann | |
372 | 84e344d4 | Michael Hanselmann | # Wait up to 100 ms to get lock
|
373 | 84e344d4 | Michael Hanselmann | self.failUnless(self.sl.acquire(shared=shared, timeout=0.1)) |
374 | 84e344d4 | Michael Hanselmann | self.done.put("got 2nd") |
375 | 84e344d4 | Michael Hanselmann | self.sl.release()
|
376 | 84e344d4 | Michael Hanselmann | |
377 | 84e344d4 | Michael Hanselmann | self._waitThreads()
|
378 | 84e344d4 | Michael Hanselmann | |
379 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.done.get_nowait(), "A: start sleep") |
380 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.done.get_nowait(), "A: end sleep") |
381 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.done.get_nowait(), "got 2nd") |
382 | 84e344d4 | Michael Hanselmann | self.assertRaises(Queue.Empty, self.done.get_nowait) |
383 | 84e344d4 | Michael Hanselmann | |
384 | 84e344d4 | Michael Hanselmann | @_Repeat
|
385 | 84e344d4 | Michael Hanselmann | def testAcquireExpiringTimeout(self): |
386 | 84e344d4 | Michael Hanselmann | def _AcquireWithTimeout(shared, timeout): |
387 | 84e344d4 | Michael Hanselmann | if not self.sl.acquire(shared=shared, timeout=timeout): |
388 | 84e344d4 | Michael Hanselmann | self.done.put("timeout") |
389 | 84e344d4 | Michael Hanselmann | |
390 | 84e344d4 | Michael Hanselmann | for shared in [0, 1]: |
391 | 84e344d4 | Michael Hanselmann | # Lock exclusively
|
392 | 84e344d4 | Michael Hanselmann | self.sl.acquire()
|
393 | 84e344d4 | Michael Hanselmann | |
394 | 84e344d4 | Michael Hanselmann | # Start shared acquires with timeout between 0 and 20 ms
|
395 | 84e344d4 | Michael Hanselmann | for i in xrange(11): |
396 | 84e344d4 | Michael Hanselmann | self._addThread(target=_AcquireWithTimeout,
|
397 | 84e344d4 | Michael Hanselmann | args=(shared, i * 2.0 / 1000.0)) |
398 | 84e344d4 | Michael Hanselmann | |
399 | 84e344d4 | Michael Hanselmann | # Wait for threads to finish (makes sure the acquire timeout expires
|
400 | 84e344d4 | Michael Hanselmann | # before releasing the lock)
|
401 | 84e344d4 | Michael Hanselmann | self._waitThreads()
|
402 | 84e344d4 | Michael Hanselmann | |
403 | 84e344d4 | Michael Hanselmann | # Release lock
|
404 | 84e344d4 | Michael Hanselmann | self.sl.release()
|
405 | 84e344d4 | Michael Hanselmann | |
406 | 84e344d4 | Michael Hanselmann | for _ in xrange(11): |
407 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.done.get_nowait(), "timeout") |
408 | 84e344d4 | Michael Hanselmann | |
409 | 84e344d4 | Michael Hanselmann | self.assertRaises(Queue.Empty, self.done.get_nowait) |
410 | 84e344d4 | Michael Hanselmann | |
411 | 84e344d4 | Michael Hanselmann | @_Repeat
|
412 | 84e344d4 | Michael Hanselmann | def testSharedSkipExclusiveAcquires(self): |
413 | 84e344d4 | Michael Hanselmann | # Tests whether shared acquires jump in front of exclusive acquires in the
|
414 | 84e344d4 | Michael Hanselmann | # queue.
|
415 | 84e344d4 | Michael Hanselmann | |
416 | 84e344d4 | Michael Hanselmann | # Get exclusive lock while we fill the queue
|
417 | 84e344d4 | Michael Hanselmann | self.sl.acquire()
|
418 | 84e344d4 | Michael Hanselmann | |
419 | 84e344d4 | Michael Hanselmann | def _Acquire(shared, name): |
420 | 84e344d4 | Michael Hanselmann | if not self.sl.acquire(shared=shared): |
421 | 84e344d4 | Michael Hanselmann | return
|
422 | 84e344d4 | Michael Hanselmann | |
423 | 84e344d4 | Michael Hanselmann | self.done.put(name)
|
424 | 84e344d4 | Michael Hanselmann | self.sl.release()
|
425 | 84e344d4 | Michael Hanselmann | |
426 | 84e344d4 | Michael Hanselmann | # Start shared acquires
|
427 | 84e344d4 | Michael Hanselmann | for _ in xrange(5): |
428 | 84e344d4 | Michael Hanselmann | self._addThread(target=_Acquire, args=(1, "shared A")) |
429 | 84e344d4 | Michael Hanselmann | |
430 | 84e344d4 | Michael Hanselmann | # Start exclusive acquires
|
431 | 84e344d4 | Michael Hanselmann | for _ in xrange(3): |
432 | 84e344d4 | Michael Hanselmann | self._addThread(target=_Acquire, args=(0, "exclusive B")) |
433 | 84e344d4 | Michael Hanselmann | |
434 | 84e344d4 | Michael Hanselmann | # More shared acquires
|
435 | 84e344d4 | Michael Hanselmann | for _ in xrange(5): |
436 | 84e344d4 | Michael Hanselmann | self._addThread(target=_Acquire, args=(1, "shared C")) |
437 | 84e344d4 | Michael Hanselmann | |
438 | 84e344d4 | Michael Hanselmann | # More exclusive acquires
|
439 | 84e344d4 | Michael Hanselmann | for _ in xrange(3): |
440 | 84e344d4 | Michael Hanselmann | self._addThread(target=_Acquire, args=(0, "exclusive D")) |
441 | 84e344d4 | Michael Hanselmann | |
442 | 84e344d4 | Michael Hanselmann | # Expect 6 pending exclusive acquires and 1 for all shared acquires
|
443 | 84e344d4 | Michael Hanselmann | # together
|
444 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.sl._count_pending(), 7) |
445 | 84e344d4 | Michael Hanselmann | |
446 | 84e344d4 | Michael Hanselmann | # Release exclusive lock and wait
|
447 | 84e344d4 | Michael Hanselmann | self.sl.release()
|
448 | 84e344d4 | Michael Hanselmann | |
449 | 84e344d4 | Michael Hanselmann | self._waitThreads()
|
450 | 84e344d4 | Michael Hanselmann | |
451 | 84e344d4 | Michael Hanselmann | # Check sequence
|
452 | 84e344d4 | Michael Hanselmann | for _ in xrange(10): |
453 | 84e344d4 | Michael Hanselmann | # Shared locks aren't guaranteed to be notified in order, but they'll be
|
454 | 84e344d4 | Michael Hanselmann | # first
|
455 | 84e344d4 | Michael Hanselmann | self.assert_(self.done.get_nowait() in ("shared A", "shared C")) |
456 | 84e344d4 | Michael Hanselmann | |
457 | 84e344d4 | Michael Hanselmann | for _ in xrange(3): |
458 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.done.get_nowait(), "exclusive B") |
459 | 84e344d4 | Michael Hanselmann | |
460 | 84e344d4 | Michael Hanselmann | for _ in xrange(3): |
461 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.done.get_nowait(), "exclusive D") |
462 | 84e344d4 | Michael Hanselmann | |
463 | 84e344d4 | Michael Hanselmann | self.assertRaises(Queue.Empty, self.done.get_nowait) |
464 | 84e344d4 | Michael Hanselmann | |
465 | 84e344d4 | Michael Hanselmann | @_Repeat
|
466 | 84e344d4 | Michael Hanselmann | def testMixedAcquireTimeout(self): |
467 | 84e344d4 | Michael Hanselmann | sync = threading.Condition() |
468 | 84e344d4 | Michael Hanselmann | |
469 | 84e344d4 | Michael Hanselmann | def _AcquireShared(ev): |
470 | 84e344d4 | Michael Hanselmann | if not self.sl.acquire(shared=1, timeout=None): |
471 | 84e344d4 | Michael Hanselmann | return
|
472 | 84e344d4 | Michael Hanselmann | |
473 | 84e344d4 | Michael Hanselmann | self.done.put("shared") |
474 | 84e344d4 | Michael Hanselmann | |
475 | 84e344d4 | Michael Hanselmann | # Notify main thread
|
476 | 84e344d4 | Michael Hanselmann | ev.set() |
477 | 84e344d4 | Michael Hanselmann | |
478 | 84e344d4 | Michael Hanselmann | # Wait for notification
|
479 | 84e344d4 | Michael Hanselmann | sync.acquire() |
480 | 84e344d4 | Michael Hanselmann | try:
|
481 | 84e344d4 | Michael Hanselmann | sync.wait() |
482 | 84e344d4 | Michael Hanselmann | finally:
|
483 | 84e344d4 | Michael Hanselmann | sync.release() |
484 | 84e344d4 | Michael Hanselmann | |
485 | 84e344d4 | Michael Hanselmann | # Release lock
|
486 | 84e344d4 | Michael Hanselmann | self.sl.release()
|
487 | 84e344d4 | Michael Hanselmann | |
488 | 84e344d4 | Michael Hanselmann | acquires = [] |
489 | 84e344d4 | Michael Hanselmann | for _ in xrange(3): |
490 | 84e344d4 | Michael Hanselmann | ev = threading.Event() |
491 | 84e344d4 | Michael Hanselmann | self._addThread(target=_AcquireShared, args=(ev, ))
|
492 | 84e344d4 | Michael Hanselmann | acquires.append(ev) |
493 | 84e344d4 | Michael Hanselmann | |
494 | 84e344d4 | Michael Hanselmann | # Wait for all acquires to finish
|
495 | 84e344d4 | Michael Hanselmann | for i in acquires: |
496 | 84e344d4 | Michael Hanselmann | i.wait() |
497 | 84e344d4 | Michael Hanselmann | |
498 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.sl._count_pending(), 0) |
499 | 84e344d4 | Michael Hanselmann | |
500 | 84e344d4 | Michael Hanselmann | # Try to get exclusive lock
|
501 | 84e344d4 | Michael Hanselmann | self.failIf(self.sl.acquire(shared=0, timeout=0.02)) |
502 | 84e344d4 | Michael Hanselmann | |
503 | 84e344d4 | Michael Hanselmann | # Acquire exclusive without timeout
|
504 | 84e344d4 | Michael Hanselmann | exclsync = threading.Condition() |
505 | 84e344d4 | Michael Hanselmann | exclev = threading.Event() |
506 | 84e344d4 | Michael Hanselmann | |
507 | 84e344d4 | Michael Hanselmann | def _AcquireExclusive(): |
508 | 84e344d4 | Michael Hanselmann | if not self.sl.acquire(shared=0): |
509 | 84e344d4 | Michael Hanselmann | return
|
510 | 84e344d4 | Michael Hanselmann | |
511 | 84e344d4 | Michael Hanselmann | self.done.put("exclusive") |
512 | 84e344d4 | Michael Hanselmann | |
513 | 84e344d4 | Michael Hanselmann | # Notify main thread
|
514 | 84e344d4 | Michael Hanselmann | exclev.set() |
515 | 84e344d4 | Michael Hanselmann | |
516 | 84e344d4 | Michael Hanselmann | exclsync.acquire() |
517 | 84e344d4 | Michael Hanselmann | try:
|
518 | 84e344d4 | Michael Hanselmann | exclsync.wait() |
519 | 84e344d4 | Michael Hanselmann | finally:
|
520 | 84e344d4 | Michael Hanselmann | exclsync.release() |
521 | 84e344d4 | Michael Hanselmann | |
522 | 84e344d4 | Michael Hanselmann | self.sl.release()
|
523 | 84e344d4 | Michael Hanselmann | |
524 | 84e344d4 | Michael Hanselmann | self._addThread(target=_AcquireExclusive)
|
525 | 84e344d4 | Michael Hanselmann | |
526 | 84e344d4 | Michael Hanselmann | # Try to get exclusive lock
|
527 | 84e344d4 | Michael Hanselmann | self.failIf(self.sl.acquire(shared=0, timeout=0.02)) |
528 | 84e344d4 | Michael Hanselmann | |
529 | 84e344d4 | Michael Hanselmann | # Make all shared holders release their locks
|
530 | 84e344d4 | Michael Hanselmann | sync.acquire() |
531 | 84e344d4 | Michael Hanselmann | try:
|
532 | 84e344d4 | Michael Hanselmann | sync.notifyAll() |
533 | 84e344d4 | Michael Hanselmann | finally:
|
534 | 84e344d4 | Michael Hanselmann | sync.release() |
535 | 84e344d4 | Michael Hanselmann | |
536 | 84e344d4 | Michael Hanselmann | # Wait for exclusive acquire to succeed
|
537 | 84e344d4 | Michael Hanselmann | exclev.wait() |
538 | 84e344d4 | Michael Hanselmann | |
539 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.sl._count_pending(), 0) |
540 | 84e344d4 | Michael Hanselmann | |
541 | 84e344d4 | Michael Hanselmann | # Try to get exclusive lock
|
542 | 84e344d4 | Michael Hanselmann | self.failIf(self.sl.acquire(shared=0, timeout=0.02)) |
543 | 84e344d4 | Michael Hanselmann | |
544 | 84e344d4 | Michael Hanselmann | def _AcquireSharedSimple(): |
545 | 84e344d4 | Michael Hanselmann | if self.sl.acquire(shared=1, timeout=None): |
546 | 84e344d4 | Michael Hanselmann | self.done.put("shared2") |
547 | 84e344d4 | Michael Hanselmann | self.sl.release()
|
548 | 84e344d4 | Michael Hanselmann | |
549 | 84e344d4 | Michael Hanselmann | for _ in xrange(10): |
550 | 84e344d4 | Michael Hanselmann | self._addThread(target=_AcquireSharedSimple)
|
551 | 84e344d4 | Michael Hanselmann | |
552 | 84e344d4 | Michael Hanselmann | # Tell exclusive lock to release
|
553 | 84e344d4 | Michael Hanselmann | exclsync.acquire() |
554 | 84e344d4 | Michael Hanselmann | try:
|
555 | 84e344d4 | Michael Hanselmann | exclsync.notifyAll() |
556 | 84e344d4 | Michael Hanselmann | finally:
|
557 | 84e344d4 | Michael Hanselmann | exclsync.release() |
558 | 84e344d4 | Michael Hanselmann | |
559 | 84e344d4 | Michael Hanselmann | # Wait for everything to finish
|
560 | 84e344d4 | Michael Hanselmann | self._waitThreads()
|
561 | 84e344d4 | Michael Hanselmann | |
562 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.sl._count_pending(), 0) |
563 | 84e344d4 | Michael Hanselmann | |
564 | 84e344d4 | Michael Hanselmann | # Check sequence
|
565 | 84e344d4 | Michael Hanselmann | for _ in xrange(3): |
566 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.done.get_nowait(), "shared") |
567 | 84e344d4 | Michael Hanselmann | |
568 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.done.get_nowait(), "exclusive") |
569 | 84e344d4 | Michael Hanselmann | |
570 | 84e344d4 | Michael Hanselmann | for _ in xrange(10): |
571 | 84e344d4 | Michael Hanselmann | self.assertEqual(self.done.get_nowait(), "shared2") |
572 | 84e344d4 | Michael Hanselmann | |
573 | 84e344d4 | Michael Hanselmann | self.assertRaises(Queue.Empty, self.done.get_nowait) |
574 | 84e344d4 | Michael Hanselmann | |
575 | 162c1c1f | Guido Trotter | |
576 | 4607c978 | Iustin Pop | class TestSSynchronizedDecorator(_ThreadedTestCase): |
577 | 42a999d1 | Guido Trotter | """Shared Lock Synchronized decorator test"""
|
578 | 42a999d1 | Guido Trotter | |
579 | 42a999d1 | Guido Trotter | def setUp(self): |
580 | 4607c978 | Iustin Pop | _ThreadedTestCase.setUp(self)
|
581 | 42a999d1 | Guido Trotter | # helper threads use the 'done' queue to tell the master they finished.
|
582 | 42a999d1 | Guido Trotter | self.done = Queue.Queue(0) |
583 | 42a999d1 | Guido Trotter | |
584 | 42a999d1 | Guido Trotter | @locking.ssynchronized(_decoratorlock)
|
585 | 42a999d1 | Guido Trotter | def _doItExclusive(self): |
586 | 42a999d1 | Guido Trotter | self.assert_(_decoratorlock._is_owned())
|
587 | 42a999d1 | Guido Trotter | self.done.put('EXC') |
588 | 42a999d1 | Guido Trotter | |
589 | 42a999d1 | Guido Trotter | @locking.ssynchronized(_decoratorlock, shared=1) |
590 | 42a999d1 | Guido Trotter | def _doItSharer(self): |
591 | 42a999d1 | Guido Trotter | self.assert_(_decoratorlock._is_owned(shared=1)) |
592 | 42a999d1 | Guido Trotter | self.done.put('SHR') |
593 | 42a999d1 | Guido Trotter | |
594 | 42a999d1 | Guido Trotter | def testDecoratedFunctions(self): |
595 | 42a999d1 | Guido Trotter | self._doItExclusive()
|
596 | 42a999d1 | Guido Trotter | self.assert_(not _decoratorlock._is_owned()) |
597 | 42a999d1 | Guido Trotter | self._doItSharer()
|
598 | 42a999d1 | Guido Trotter | self.assert_(not _decoratorlock._is_owned()) |
599 | 42a999d1 | Guido Trotter | |
600 | 42a999d1 | Guido Trotter | def testSharersCanCoexist(self): |
601 | 42a999d1 | Guido Trotter | _decoratorlock.acquire(shared=1)
|
602 | 84e344d4 | Michael Hanselmann | threading.Thread(target=self._doItSharer).start()
|
603 | 42a999d1 | Guido Trotter | self.assert_(self.done.get(True, 1)) |
604 | 42a999d1 | Guido Trotter | _decoratorlock.release() |
605 | 42a999d1 | Guido Trotter | |
606 | 4607c978 | Iustin Pop | @_Repeat
|
607 | 42a999d1 | Guido Trotter | def testExclusiveBlocksExclusive(self): |
608 | 42a999d1 | Guido Trotter | _decoratorlock.acquire() |
609 | 4607c978 | Iustin Pop | self._addThread(target=self._doItExclusive) |
610 | 42a999d1 | Guido Trotter | # give it a bit of time to check that it's not actually doing anything
|
611 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
612 | 42a999d1 | Guido Trotter | _decoratorlock.release() |
613 | 4607c978 | Iustin Pop | self._waitThreads()
|
614 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'EXC') |
615 | 42a999d1 | Guido Trotter | |
616 | 4607c978 | Iustin Pop | @_Repeat
|
617 | 42a999d1 | Guido Trotter | def testExclusiveBlocksSharer(self): |
618 | 42a999d1 | Guido Trotter | _decoratorlock.acquire() |
619 | 4607c978 | Iustin Pop | self._addThread(target=self._doItSharer) |
620 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
621 | 42a999d1 | Guido Trotter | _decoratorlock.release() |
622 | 4607c978 | Iustin Pop | self._waitThreads()
|
623 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'SHR') |
624 | 42a999d1 | Guido Trotter | |
625 | 4607c978 | Iustin Pop | @_Repeat
|
626 | 42a999d1 | Guido Trotter | def testSharerBlocksExclusive(self): |
627 | 42a999d1 | Guido Trotter | _decoratorlock.acquire(shared=1)
|
628 | 4607c978 | Iustin Pop | self._addThread(target=self._doItExclusive) |
629 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
630 | 42a999d1 | Guido Trotter | _decoratorlock.release() |
631 | 4607c978 | Iustin Pop | self._waitThreads()
|
632 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'EXC') |
633 | 42a999d1 | Guido Trotter | |
634 | 42a999d1 | Guido Trotter | |
635 | 4607c978 | Iustin Pop | class TestLockSet(_ThreadedTestCase): |
636 | aaae9bc0 | Guido Trotter | """LockSet tests"""
|
637 | aaae9bc0 | Guido Trotter | |
638 | aaae9bc0 | Guido Trotter | def setUp(self): |
639 | 4607c978 | Iustin Pop | _ThreadedTestCase.setUp(self)
|
640 | 4607c978 | Iustin Pop | self._setUpLS()
|
641 | aaae9bc0 | Guido Trotter | # helper threads use the 'done' queue to tell the master they finished.
|
642 | aaae9bc0 | Guido Trotter | self.done = Queue.Queue(0) |
643 | aaae9bc0 | Guido Trotter | |
644 | 4607c978 | Iustin Pop | def _setUpLS(self): |
645 | 4607c978 | Iustin Pop | """Helper to (re)initialize the lock set"""
|
646 | 4607c978 | Iustin Pop | self.resources = ['one', 'two', 'three'] |
647 | 4607c978 | Iustin Pop | self.ls = locking.LockSet(members=self.resources) |
648 | 4607c978 | Iustin Pop | |
649 | aaae9bc0 | Guido Trotter | def testResources(self): |
650 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._names(), set(self.resources)) |
651 | aaae9bc0 | Guido Trotter | newls = locking.LockSet() |
652 | aaae9bc0 | Guido Trotter | self.assertEquals(newls._names(), set()) |
653 | aaae9bc0 | Guido Trotter | |
654 | aaae9bc0 | Guido Trotter | def testAcquireRelease(self): |
655 | 0cc00929 | Guido Trotter | self.assert_(self.ls.acquire('one')) |
656 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set(['one'])) |
657 | aaae9bc0 | Guido Trotter | self.ls.release()
|
658 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set()) |
659 | 0cc00929 | Guido Trotter | self.assertEquals(self.ls.acquire(['one']), set(['one'])) |
660 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set(['one'])) |
661 | aaae9bc0 | Guido Trotter | self.ls.release()
|
662 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set()) |
663 | aaae9bc0 | Guido Trotter | self.ls.acquire(['one', 'two', 'three']) |
664 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set(['one', 'two', 'three'])) |
665 | aaae9bc0 | Guido Trotter | self.ls.release('one') |
666 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set(['two', 'three'])) |
667 | aaae9bc0 | Guido Trotter | self.ls.release(['three']) |
668 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set(['two'])) |
669 | aaae9bc0 | Guido Trotter | self.ls.release()
|
670 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set()) |
671 | 0cc00929 | Guido Trotter | self.assertEquals(self.ls.acquire(['one', 'three']), set(['one', 'three'])) |
672 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set(['one', 'three'])) |
673 | aaae9bc0 | Guido Trotter | self.ls.release()
|
674 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set()) |
675 | aaae9bc0 | Guido Trotter | |
676 | aaae9bc0 | Guido Trotter | def testNoDoubleAcquire(self): |
677 | aaae9bc0 | Guido Trotter | self.ls.acquire('one') |
678 | aaae9bc0 | Guido Trotter | self.assertRaises(AssertionError, self.ls.acquire, 'one') |
679 | aaae9bc0 | Guido Trotter | self.assertRaises(AssertionError, self.ls.acquire, ['two']) |
680 | aaae9bc0 | Guido Trotter | self.assertRaises(AssertionError, self.ls.acquire, ['two', 'three']) |
681 | aaae9bc0 | Guido Trotter | self.ls.release()
|
682 | aaae9bc0 | Guido Trotter | self.ls.acquire(['one', 'three']) |
683 | aaae9bc0 | Guido Trotter | self.ls.release('one') |
684 | aaae9bc0 | Guido Trotter | self.assertRaises(AssertionError, self.ls.acquire, ['two']) |
685 | aaae9bc0 | Guido Trotter | self.ls.release('three') |
686 | aaae9bc0 | Guido Trotter | |
687 | aaae9bc0 | Guido Trotter | def testNoWrongRelease(self): |
688 | aaae9bc0 | Guido Trotter | self.assertRaises(AssertionError, self.ls.release) |
689 | aaae9bc0 | Guido Trotter | self.ls.acquire('one') |
690 | aaae9bc0 | Guido Trotter | self.assertRaises(AssertionError, self.ls.release, 'two') |
691 | aaae9bc0 | Guido Trotter | |
692 | aaae9bc0 | Guido Trotter | def testAddRemove(self): |
693 | aaae9bc0 | Guido Trotter | self.ls.add('four') |
694 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set()) |
695 | aaae9bc0 | Guido Trotter | self.assert_('four' in self.ls._names()) |
696 | aaae9bc0 | Guido Trotter | self.ls.add(['five', 'six', 'seven'], acquired=1) |
697 | aaae9bc0 | Guido Trotter | self.assert_('five' in self.ls._names()) |
698 | aaae9bc0 | Guido Trotter | self.assert_('six' in self.ls._names()) |
699 | aaae9bc0 | Guido Trotter | self.assert_('seven' in self.ls._names()) |
700 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set(['five', 'six', 'seven'])) |
701 | 3f404fc5 | Guido Trotter | self.assertEquals(self.ls.remove(['five', 'six']), ['five', 'six']) |
702 | aaae9bc0 | Guido Trotter | self.assert_('five' not in self.ls._names()) |
703 | aaae9bc0 | Guido Trotter | self.assert_('six' not in self.ls._names()) |
704 | aaae9bc0 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set(['seven'])) |
705 | d2aff862 | Guido Trotter | self.assertRaises(AssertionError, self.ls.add, 'eight', acquired=1) |
706 | aaae9bc0 | Guido Trotter | self.ls.remove('seven') |
707 | aaae9bc0 | Guido Trotter | self.assert_('seven' not in self.ls._names()) |
708 | d2aff862 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set([])) |
709 | d2aff862 | Guido Trotter | self.ls.acquire(None, shared=1) |
710 | d2aff862 | Guido Trotter | self.assertRaises(AssertionError, self.ls.add, 'eight') |
711 | d2aff862 | Guido Trotter | self.ls.release()
|
712 | d2aff862 | Guido Trotter | self.ls.acquire(None) |
713 | d2aff862 | Guido Trotter | self.ls.add('eight', acquired=1) |
714 | d2aff862 | Guido Trotter | self.assert_('eight' in self.ls._names()) |
715 | d2aff862 | Guido Trotter | self.assert_('eight' in self.ls._list_owned()) |
716 | d2aff862 | Guido Trotter | self.ls.add('nine') |
717 | d2aff862 | Guido Trotter | self.assert_('nine' in self.ls._names()) |
718 | d2aff862 | Guido Trotter | self.assert_('nine' not in self.ls._list_owned()) |
719 | aaae9bc0 | Guido Trotter | self.ls.release()
|
720 | aaae9bc0 | Guido Trotter | self.ls.remove(['two']) |
721 | aaae9bc0 | Guido Trotter | self.assert_('two' not in self.ls._names()) |
722 | aaae9bc0 | Guido Trotter | self.ls.acquire('three') |
723 | 3f404fc5 | Guido Trotter | self.assertEquals(self.ls.remove(['three']), ['three']) |
724 | aaae9bc0 | Guido Trotter | self.assert_('three' not in self.ls._names()) |
725 | 3f404fc5 | Guido Trotter | self.assertEquals(self.ls.remove('three'), []) |
726 | 3f404fc5 | Guido Trotter | self.assertEquals(self.ls.remove(['one', 'three', 'six']), ['one']) |
727 | aaae9bc0 | Guido Trotter | self.assert_('one' not in self.ls._names()) |
728 | aaae9bc0 | Guido Trotter | |
729 | aaae9bc0 | Guido Trotter | def testRemoveNonBlocking(self): |
730 | aaae9bc0 | Guido Trotter | self.assertRaises(NotImplementedError, self.ls.remove, 'one', blocking=0) |
731 | aaae9bc0 | Guido Trotter | self.ls.acquire('one') |
732 | 3f404fc5 | Guido Trotter | self.assertEquals(self.ls.remove('one', blocking=0), ['one']) |
733 | aaae9bc0 | Guido Trotter | self.ls.acquire(['two', 'three']) |
734 | 3f404fc5 | Guido Trotter | self.assertEquals(self.ls.remove(['two', 'three'], blocking=0), |
735 | 3f404fc5 | Guido Trotter | ['two', 'three']) |
736 | aaae9bc0 | Guido Trotter | |
737 | aaae9bc0 | Guido Trotter | def testNoDoubleAdd(self): |
738 | aaae9bc0 | Guido Trotter | self.assertRaises(errors.LockError, self.ls.add, 'two') |
739 | aaae9bc0 | Guido Trotter | self.ls.add('four') |
740 | aaae9bc0 | Guido Trotter | self.assertRaises(errors.LockError, self.ls.add, 'four') |
741 | aaae9bc0 | Guido Trotter | |
742 | aaae9bc0 | Guido Trotter | def testNoWrongRemoves(self): |
743 | aaae9bc0 | Guido Trotter | self.ls.acquire(['one', 'three'], shared=1) |
744 | aaae9bc0 | Guido Trotter | # Cannot remove 'two' while holding something which is not a superset
|
745 | aaae9bc0 | Guido Trotter | self.assertRaises(AssertionError, self.ls.remove, 'two') |
746 | aaae9bc0 | Guido Trotter | # Cannot remove 'three' as we are sharing it
|
747 | aaae9bc0 | Guido Trotter | self.assertRaises(AssertionError, self.ls.remove, 'three') |
748 | aaae9bc0 | Guido Trotter | |
749 | 3b7ed473 | Guido Trotter | def testAcquireSetLock(self): |
750 | 3b7ed473 | Guido Trotter | # acquire the set-lock exclusively
|
751 | 3b7ed473 | Guido Trotter | self.assertEquals(self.ls.acquire(None), set(['one', 'two', 'three'])) |
752 | d4803c24 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set(['one', 'two', 'three'])) |
753 | d4803c24 | Guido Trotter | self.assertEquals(self.ls._is_owned(), True) |
754 | d4803c24 | Guido Trotter | self.assertEquals(self.ls._names(), set(['one', 'two', 'three'])) |
755 | 3b7ed473 | Guido Trotter | # I can still add/remove elements...
|
756 | 3b7ed473 | Guido Trotter | self.assertEquals(self.ls.remove(['two', 'three']), ['two', 'three']) |
757 | 3b7ed473 | Guido Trotter | self.assert_(self.ls.add('six')) |
758 | 3b7ed473 | Guido Trotter | self.ls.release()
|
759 | 3b7ed473 | Guido Trotter | # share the set-lock
|
760 | 3b7ed473 | Guido Trotter | self.assertEquals(self.ls.acquire(None, shared=1), set(['one', 'six'])) |
761 | 3b7ed473 | Guido Trotter | # adding new elements is not possible
|
762 | 3b7ed473 | Guido Trotter | self.assertRaises(AssertionError, self.ls.add, 'five') |
763 | 3b7ed473 | Guido Trotter | self.ls.release()
|
764 | 3b7ed473 | Guido Trotter | |
765 | d4f6a91c | Guido Trotter | def testAcquireWithRepetitions(self): |
766 | d4f6a91c | Guido Trotter | self.assertEquals(self.ls.acquire(['two', 'two', 'three'], shared=1), |
767 | d4f6a91c | Guido Trotter | set(['two', 'two', 'three'])) |
768 | d4f6a91c | Guido Trotter | self.ls.release(['two', 'two']) |
769 | d4f6a91c | Guido Trotter | self.assertEquals(self.ls._list_owned(), set(['three'])) |
770 | d4f6a91c | Guido Trotter | |
771 | 2e1d6d96 | Guido Trotter | def testEmptyAcquire(self): |
772 | 2e1d6d96 | Guido Trotter | # Acquire an empty list of locks...
|
773 | 2e1d6d96 | Guido Trotter | self.assertEquals(self.ls.acquire([]), set()) |
774 | 2e1d6d96 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set()) |
775 | 2e1d6d96 | Guido Trotter | # New locks can still be addded
|
776 | 2e1d6d96 | Guido Trotter | self.assert_(self.ls.add('six')) |
777 | 2e1d6d96 | Guido Trotter | # "re-acquiring" is not an issue, since we had really acquired nothing
|
778 | 2e1d6d96 | Guido Trotter | self.assertEquals(self.ls.acquire([], shared=1), set()) |
779 | 2e1d6d96 | Guido Trotter | self.assertEquals(self.ls._list_owned(), set()) |
780 | 2e1d6d96 | Guido Trotter | # We haven't really acquired anything, so we cannot release
|
781 | 2e1d6d96 | Guido Trotter | self.assertRaises(AssertionError, self.ls.release) |
782 | 2e1d6d96 | Guido Trotter | |
783 | 84e344d4 | Michael Hanselmann | def _doLockSet(self, names, shared): |
784 | aaae9bc0 | Guido Trotter | try:
|
785 | 84e344d4 | Michael Hanselmann | self.ls.acquire(names, shared=shared)
|
786 | aaae9bc0 | Guido Trotter | self.done.put('DONE') |
787 | aaae9bc0 | Guido Trotter | self.ls.release()
|
788 | aaae9bc0 | Guido Trotter | except errors.LockError:
|
789 | aaae9bc0 | Guido Trotter | self.done.put('ERR') |
790 | aaae9bc0 | Guido Trotter | |
791 | 84e344d4 | Michael Hanselmann | def _doAddSet(self, names): |
792 | 3b7ed473 | Guido Trotter | try:
|
793 | 84e344d4 | Michael Hanselmann | self.ls.add(names, acquired=1) |
794 | 3b7ed473 | Guido Trotter | self.done.put('DONE') |
795 | 3b7ed473 | Guido Trotter | self.ls.release()
|
796 | 3b7ed473 | Guido Trotter | except errors.LockError:
|
797 | 3b7ed473 | Guido Trotter | self.done.put('ERR') |
798 | 3b7ed473 | Guido Trotter | |
799 | 84e344d4 | Michael Hanselmann | def _doRemoveSet(self, names): |
800 | 84e344d4 | Michael Hanselmann | self.done.put(self.ls.remove(names)) |
801 | aaae9bc0 | Guido Trotter | |
802 | 4607c978 | Iustin Pop | @_Repeat
|
803 | aaae9bc0 | Guido Trotter | def testConcurrentSharedAcquire(self): |
804 | aaae9bc0 | Guido Trotter | self.ls.acquire(['one', 'two'], shared=1) |
805 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['one', 'two'], 1)) |
806 | 4607c978 | Iustin Pop | self._waitThreads()
|
807 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
808 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['one', 'two', 'three'], 1)) |
809 | 4607c978 | Iustin Pop | self._waitThreads()
|
810 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
811 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=('three', 1)) |
812 | 4607c978 | Iustin Pop | self._waitThreads()
|
813 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
814 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['one', 'two'], 0)) |
815 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['two', 'three'], 0)) |
816 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
817 | aaae9bc0 | Guido Trotter | self.ls.release()
|
818 | 4607c978 | Iustin Pop | self._waitThreads()
|
819 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
820 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
821 | aaae9bc0 | Guido Trotter | |
822 | 4607c978 | Iustin Pop | @_Repeat
|
823 | aaae9bc0 | Guido Trotter | def testConcurrentExclusiveAcquire(self): |
824 | aaae9bc0 | Guido Trotter | self.ls.acquire(['one', 'two']) |
825 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=('three', 1)) |
826 | 4607c978 | Iustin Pop | self._waitThreads()
|
827 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
828 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=('three', 0)) |
829 | 4607c978 | Iustin Pop | self._waitThreads()
|
830 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
831 | 84e344d4 | Michael Hanselmann | self.assertRaises(Queue.Empty, self.done.get_nowait) |
832 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['one', 'two'], 0)) |
833 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['one', 'two'], 1)) |
834 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=('one', 0)) |
835 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=('one', 1)) |
836 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['two', 'three'], 0)) |
837 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['two', 'three'], 1)) |
838 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
839 | aaae9bc0 | Guido Trotter | self.ls.release()
|
840 | 4607c978 | Iustin Pop | self._waitThreads()
|
841 | 4607c978 | Iustin Pop | for _ in range(6): |
842 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'DONE') |
843 | aaae9bc0 | Guido Trotter | |
844 | 4607c978 | Iustin Pop | @_Repeat
|
845 | aaae9bc0 | Guido Trotter | def testConcurrentRemove(self): |
846 | aaae9bc0 | Guido Trotter | self.ls.add('four') |
847 | aaae9bc0 | Guido Trotter | self.ls.acquire(['one', 'two', 'four']) |
848 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['one', 'four'], 0)) |
849 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['one', 'four'], 1)) |
850 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['one', 'two'], 0)) |
851 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['one', 'two'], 1)) |
852 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
853 | aaae9bc0 | Guido Trotter | self.ls.remove('one') |
854 | aaae9bc0 | Guido Trotter | self.ls.release()
|
855 | 4607c978 | Iustin Pop | self._waitThreads()
|
856 | 4607c978 | Iustin Pop | for i in range(4): |
857 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'ERR') |
858 | aaae9bc0 | Guido Trotter | self.ls.add(['five', 'six'], acquired=1) |
859 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['three', 'six'], 1)) |
860 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['three', 'six'], 0)) |
861 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['four', 'six'], 1)) |
862 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['four', 'six'], 0)) |
863 | aaae9bc0 | Guido Trotter | self.ls.remove('five') |
864 | aaae9bc0 | Guido Trotter | self.ls.release()
|
865 | 4607c978 | Iustin Pop | self._waitThreads()
|
866 | 4607c978 | Iustin Pop | for i in range(4): |
867 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), 'DONE') |
868 | aaae9bc0 | Guido Trotter | self.ls.acquire(['three', 'four']) |
869 | 4607c978 | Iustin Pop | self._addThread(target=self._doRemoveSet, args=(['four', 'six'], )) |
870 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
871 | aaae9bc0 | Guido Trotter | self.ls.remove('four') |
872 | 4607c978 | Iustin Pop | self._waitThreads()
|
873 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), ['six']) |
874 | 4607c978 | Iustin Pop | self._addThread(target=self._doRemoveSet, args=(['two'])) |
875 | 4607c978 | Iustin Pop | self._waitThreads()
|
876 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), ['two']) |
877 | aaae9bc0 | Guido Trotter | self.ls.release()
|
878 | 4607c978 | Iustin Pop | # reset lockset
|
879 | 4607c978 | Iustin Pop | self._setUpLS()
|
880 | aaae9bc0 | Guido Trotter | |
881 | 4607c978 | Iustin Pop | @_Repeat
|
882 | 3b7ed473 | Guido Trotter | def testConcurrentSharedSetLock(self): |
883 | 3b7ed473 | Guido Trotter | # share the set-lock...
|
884 | 3b7ed473 | Guido Trotter | self.ls.acquire(None, shared=1) |
885 | 3b7ed473 | Guido Trotter | # ...another thread can share it too
|
886 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(None, 1)) |
887 | 4607c978 | Iustin Pop | self._waitThreads()
|
888 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
889 | 3b7ed473 | Guido Trotter | # ...or just share some elements
|
890 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['one', 'three'], 1)) |
891 | 4607c978 | Iustin Pop | self._waitThreads()
|
892 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
893 | 3b7ed473 | Guido Trotter | # ...but not add new ones or remove any
|
894 | 4607c978 | Iustin Pop | t = self._addThread(target=self._doAddSet, args=(['nine'])) |
895 | 4607c978 | Iustin Pop | self._addThread(target=self._doRemoveSet, args=(['two'], )) |
896 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
897 | 3b7ed473 | Guido Trotter | # this just releases the set-lock
|
898 | 3b7ed473 | Guido Trotter | self.ls.release([])
|
899 | 4607c978 | Iustin Pop | t.join(60)
|
900 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
901 | 3b7ed473 | Guido Trotter | # release the lock on the actual elements so remove() can proceed too
|
902 | 3b7ed473 | Guido Trotter | self.ls.release()
|
903 | 4607c978 | Iustin Pop | self._waitThreads()
|
904 | 4607c978 | Iustin Pop | self.failUnlessEqual(self.done.get_nowait(), ['two']) |
905 | 4607c978 | Iustin Pop | # reset lockset
|
906 | 4607c978 | Iustin Pop | self._setUpLS()
|
907 | 3b7ed473 | Guido Trotter | |
908 | 4607c978 | Iustin Pop | @_Repeat
|
909 | 3b7ed473 | Guido Trotter | def testConcurrentExclusiveSetLock(self): |
910 | 3b7ed473 | Guido Trotter | # acquire the set-lock...
|
911 | 3b7ed473 | Guido Trotter | self.ls.acquire(None, shared=0) |
912 | 3b7ed473 | Guido Trotter | # ...no one can do anything else
|
913 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(None, 1)) |
914 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(None, 0)) |
915 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['three'], 0)) |
916 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(['two'], 1)) |
917 | 4607c978 | Iustin Pop | self._addThread(target=self._doAddSet, args=(['nine'])) |
918 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
919 | 3b7ed473 | Guido Trotter | self.ls.release()
|
920 | 4607c978 | Iustin Pop | self._waitThreads()
|
921 | 4607c978 | Iustin Pop | for _ in range(5): |
922 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get(True, 1), 'DONE') |
923 | 4607c978 | Iustin Pop | # cleanup
|
924 | 4607c978 | Iustin Pop | self._setUpLS()
|
925 | 3b7ed473 | Guido Trotter | |
926 | 4607c978 | Iustin Pop | @_Repeat
|
927 | d2aff862 | Guido Trotter | def testConcurrentSetLockAdd(self): |
928 | d2aff862 | Guido Trotter | self.ls.acquire('one') |
929 | d2aff862 | Guido Trotter | # Another thread wants the whole SetLock
|
930 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(None, 0)) |
931 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(None, 1)) |
932 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
933 | d2aff862 | Guido Trotter | self.assertRaises(AssertionError, self.ls.add, 'four') |
934 | d2aff862 | Guido Trotter | self.ls.release()
|
935 | 4607c978 | Iustin Pop | self._waitThreads()
|
936 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
937 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
938 | d2aff862 | Guido Trotter | self.ls.acquire(None) |
939 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(None, 0)) |
940 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(None, 1)) |
941 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
942 | d2aff862 | Guido Trotter | self.ls.add('four') |
943 | d2aff862 | Guido Trotter | self.ls.add('five', acquired=1) |
944 | d2aff862 | Guido Trotter | self.ls.add('six', acquired=1, shared=1) |
945 | d2aff862 | Guido Trotter | self.assertEquals(self.ls._list_owned(), |
946 | d2aff862 | Guido Trotter | set(['one', 'two', 'three', 'five', 'six'])) |
947 | d2aff862 | Guido Trotter | self.assertEquals(self.ls._is_owned(), True) |
948 | d2aff862 | Guido Trotter | self.assertEquals(self.ls._names(), |
949 | d2aff862 | Guido Trotter | set(['one', 'two', 'three', 'four', 'five', 'six'])) |
950 | d2aff862 | Guido Trotter | self.ls.release()
|
951 | 4607c978 | Iustin Pop | self._waitThreads()
|
952 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
953 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
954 | 4607c978 | Iustin Pop | self._setUpLS()
|
955 | d2aff862 | Guido Trotter | |
956 | 4607c978 | Iustin Pop | @_Repeat
|
957 | b2dabfd6 | Guido Trotter | def testEmptyLockSet(self): |
958 | b2dabfd6 | Guido Trotter | # get the set-lock
|
959 | b2dabfd6 | Guido Trotter | self.assertEqual(self.ls.acquire(None), set(['one', 'two', 'three'])) |
960 | b2dabfd6 | Guido Trotter | # now empty it...
|
961 | b2dabfd6 | Guido Trotter | self.ls.remove(['one', 'two', 'three']) |
962 | b2dabfd6 | Guido Trotter | # and adds/locks by another thread still wait
|
963 | 4607c978 | Iustin Pop | self._addThread(target=self._doAddSet, args=(['nine'])) |
964 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(None, 1)) |
965 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(None, 0)) |
966 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
967 | b2dabfd6 | Guido Trotter | self.ls.release()
|
968 | 4607c978 | Iustin Pop | self._waitThreads()
|
969 | 4607c978 | Iustin Pop | for _ in range(3): |
970 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
971 | b2dabfd6 | Guido Trotter | # empty it again...
|
972 | b2dabfd6 | Guido Trotter | self.assertEqual(self.ls.remove(['nine']), ['nine']) |
973 | b2dabfd6 | Guido Trotter | # now share it...
|
974 | b2dabfd6 | Guido Trotter | self.assertEqual(self.ls.acquire(None, shared=1), set()) |
975 | b2dabfd6 | Guido Trotter | # other sharers can go, adds still wait
|
976 | 4607c978 | Iustin Pop | self._addThread(target=self._doLockSet, args=(None, 1)) |
977 | 4607c978 | Iustin Pop | self._waitThreads()
|
978 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
979 | 4607c978 | Iustin Pop | self._addThread(target=self._doAddSet, args=(['nine'])) |
980 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
981 | b2dabfd6 | Guido Trotter | self.ls.release()
|
982 | 4607c978 | Iustin Pop | self._waitThreads()
|
983 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
984 | 4607c978 | Iustin Pop | self._setUpLS()
|
985 | b2dabfd6 | Guido Trotter | |
986 | aaae9bc0 | Guido Trotter | |
987 | 4607c978 | Iustin Pop | class TestGanetiLockManager(_ThreadedTestCase): |
988 | 7ee7c0c7 | Guido Trotter | |
989 | 7ee7c0c7 | Guido Trotter | def setUp(self): |
990 | 4607c978 | Iustin Pop | _ThreadedTestCase.setUp(self)
|
991 | 7ee7c0c7 | Guido Trotter | self.nodes=['n1', 'n2'] |
992 | 7ee7c0c7 | Guido Trotter | self.instances=['i1', 'i2', 'i3'] |
993 | 7ee7c0c7 | Guido Trotter | self.GL = locking.GanetiLockManager(nodes=self.nodes, |
994 | 7ee7c0c7 | Guido Trotter | instances=self.instances)
|
995 | 7ee7c0c7 | Guido Trotter | self.done = Queue.Queue(0) |
996 | 7ee7c0c7 | Guido Trotter | |
997 | 7ee7c0c7 | Guido Trotter | def tearDown(self): |
998 | 7ee7c0c7 | Guido Trotter | # Don't try this at home...
|
999 | 7ee7c0c7 | Guido Trotter | locking.GanetiLockManager._instance = None
|
1000 | 7ee7c0c7 | Guido Trotter | |
1001 | 7ee7c0c7 | Guido Trotter | def testLockingConstants(self): |
1002 | 7ee7c0c7 | Guido Trotter | # The locking library internally cheats by assuming its constants have some
|
1003 | 7ee7c0c7 | Guido Trotter | # relationships with each other. Check those hold true.
|
1004 | b10b9d74 | Guido Trotter | # This relationship is also used in the Processor to recursively acquire
|
1005 | b10b9d74 | Guido Trotter | # the right locks. Again, please don't break it.
|
1006 | 7ee7c0c7 | Guido Trotter | for i in range(len(locking.LEVELS)): |
1007 | 7ee7c0c7 | Guido Trotter | self.assertEqual(i, locking.LEVELS[i])
|
1008 | 7ee7c0c7 | Guido Trotter | |
1009 | 7ee7c0c7 | Guido Trotter | def testDoubleGLFails(self): |
1010 | 7ee7c0c7 | Guido Trotter | self.assertRaises(AssertionError, locking.GanetiLockManager) |
1011 | 7ee7c0c7 | Guido Trotter | |
1012 | 7ee7c0c7 | Guido Trotter | def testLockNames(self): |
1013 | 7ee7c0c7 | Guido Trotter | self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL'])) |
1014 | 7ee7c0c7 | Guido Trotter | self.assertEqual(self.GL._names(locking.LEVEL_NODE), set(self.nodes)) |
1015 | cdb08f44 | Michael Hanselmann | self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), |
1016 | cdb08f44 | Michael Hanselmann | set(self.instances)) |
1017 | 7ee7c0c7 | Guido Trotter | |
1018 | 7ee7c0c7 | Guido Trotter | def testInitAndResources(self): |
1019 | 7ee7c0c7 | Guido Trotter | locking.GanetiLockManager._instance = None
|
1020 | 7ee7c0c7 | Guido Trotter | self.GL = locking.GanetiLockManager()
|
1021 | 7ee7c0c7 | Guido Trotter | self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL'])) |
1022 | 7ee7c0c7 | Guido Trotter | self.assertEqual(self.GL._names(locking.LEVEL_NODE), set()) |
1023 | 7ee7c0c7 | Guido Trotter | self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), set()) |
1024 | 7ee7c0c7 | Guido Trotter | |
1025 | 7ee7c0c7 | Guido Trotter | locking.GanetiLockManager._instance = None
|
1026 | 7ee7c0c7 | Guido Trotter | self.GL = locking.GanetiLockManager(nodes=self.nodes) |
1027 | 7ee7c0c7 | Guido Trotter | self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL'])) |
1028 | 7ee7c0c7 | Guido Trotter | self.assertEqual(self.GL._names(locking.LEVEL_NODE), set(self.nodes)) |
1029 | 7ee7c0c7 | Guido Trotter | self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), set()) |
1030 | 7ee7c0c7 | Guido Trotter | |
1031 | 7ee7c0c7 | Guido Trotter | locking.GanetiLockManager._instance = None
|
1032 | 7ee7c0c7 | Guido Trotter | self.GL = locking.GanetiLockManager(instances=self.instances) |
1033 | 7ee7c0c7 | Guido Trotter | self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL'])) |
1034 | 7ee7c0c7 | Guido Trotter | self.assertEqual(self.GL._names(locking.LEVEL_NODE), set()) |
1035 | cdb08f44 | Michael Hanselmann | self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), |
1036 | cdb08f44 | Michael Hanselmann | set(self.instances)) |
1037 | 7ee7c0c7 | Guido Trotter | |
1038 | 7ee7c0c7 | Guido Trotter | def testAcquireRelease(self): |
1039 | 7ee7c0c7 | Guido Trotter | self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1) |
1040 | 7ee7c0c7 | Guido Trotter | self.assertEquals(self.GL._list_owned(locking.LEVEL_CLUSTER), set(['BGL'])) |
1041 | 04e1bfaf | Guido Trotter | self.GL.acquire(locking.LEVEL_INSTANCE, ['i1']) |
1042 | 7ee7c0c7 | Guido Trotter | self.GL.acquire(locking.LEVEL_NODE, ['n1', 'n2'], shared=1) |
1043 | 04e1bfaf | Guido Trotter | self.GL.release(locking.LEVEL_NODE, ['n2']) |
1044 | 7ee7c0c7 | Guido Trotter | self.assertEquals(self.GL._list_owned(locking.LEVEL_NODE), set(['n1'])) |
1045 | 7ee7c0c7 | Guido Trotter | self.assertEquals(self.GL._list_owned(locking.LEVEL_INSTANCE), set(['i1'])) |
1046 | 7ee7c0c7 | Guido Trotter | self.GL.release(locking.LEVEL_NODE)
|
1047 | 04e1bfaf | Guido Trotter | self.assertEquals(self.GL._list_owned(locking.LEVEL_NODE), set()) |
1048 | 04e1bfaf | Guido Trotter | self.assertEquals(self.GL._list_owned(locking.LEVEL_INSTANCE), set(['i1'])) |
1049 | 7ee7c0c7 | Guido Trotter | self.GL.release(locking.LEVEL_INSTANCE)
|
1050 | 7ee7c0c7 | Guido Trotter | self.assertRaises(errors.LockError, self.GL.acquire, |
1051 | 7ee7c0c7 | Guido Trotter | locking.LEVEL_INSTANCE, ['i5'])
|
1052 | 7ee7c0c7 | Guido Trotter | self.GL.acquire(locking.LEVEL_INSTANCE, ['i3'], shared=1) |
1053 | 7ee7c0c7 | Guido Trotter | self.assertEquals(self.GL._list_owned(locking.LEVEL_INSTANCE), set(['i3'])) |
1054 | 7ee7c0c7 | Guido Trotter | |
1055 | 90c942d1 | Guido Trotter | def testAcquireWholeSets(self): |
1056 | 90c942d1 | Guido Trotter | self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1) |
1057 | 90c942d1 | Guido Trotter | self.assertEquals(self.GL.acquire(locking.LEVEL_INSTANCE, None), |
1058 | 90c942d1 | Guido Trotter | set(self.instances)) |
1059 | 90c942d1 | Guido Trotter | self.assertEquals(self.GL._list_owned(locking.LEVEL_INSTANCE), |
1060 | 90c942d1 | Guido Trotter | set(self.instances)) |
1061 | 90c942d1 | Guido Trotter | self.assertEquals(self.GL.acquire(locking.LEVEL_NODE, None, shared=1), |
1062 | 90c942d1 | Guido Trotter | set(self.nodes)) |
1063 | 90c942d1 | Guido Trotter | self.assertEquals(self.GL._list_owned(locking.LEVEL_NODE), |
1064 | 90c942d1 | Guido Trotter | set(self.nodes)) |
1065 | 90c942d1 | Guido Trotter | self.GL.release(locking.LEVEL_NODE)
|
1066 | d4f6a91c | Guido Trotter | self.GL.release(locking.LEVEL_INSTANCE)
|
1067 | d4f6a91c | Guido Trotter | self.GL.release(locking.LEVEL_CLUSTER)
|
1068 | d4f6a91c | Guido Trotter | |
1069 | d4f6a91c | Guido Trotter | def testAcquireWholeAndPartial(self): |
1070 | d4f6a91c | Guido Trotter | self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1) |
1071 | d4f6a91c | Guido Trotter | self.assertEquals(self.GL.acquire(locking.LEVEL_INSTANCE, None), |
1072 | d4f6a91c | Guido Trotter | set(self.instances)) |
1073 | d4f6a91c | Guido Trotter | self.assertEquals(self.GL._list_owned(locking.LEVEL_INSTANCE), |
1074 | d4f6a91c | Guido Trotter | set(self.instances)) |
1075 | d4f6a91c | Guido Trotter | self.assertEquals(self.GL.acquire(locking.LEVEL_NODE, ['n2'], shared=1), |
1076 | d4f6a91c | Guido Trotter | set(['n2'])) |
1077 | d4f6a91c | Guido Trotter | self.assertEquals(self.GL._list_owned(locking.LEVEL_NODE), |
1078 | d4f6a91c | Guido Trotter | set(['n2'])) |
1079 | d4f6a91c | Guido Trotter | self.GL.release(locking.LEVEL_NODE)
|
1080 | 90c942d1 | Guido Trotter | self.GL.release(locking.LEVEL_INSTANCE)
|
1081 | 90c942d1 | Guido Trotter | self.GL.release(locking.LEVEL_CLUSTER)
|
1082 | 90c942d1 | Guido Trotter | |
1083 | 7ee7c0c7 | Guido Trotter | def testBGLDependency(self): |
1084 | 7ee7c0c7 | Guido Trotter | self.assertRaises(AssertionError, self.GL.acquire, |
1085 | 7ee7c0c7 | Guido Trotter | locking.LEVEL_NODE, ['n1', 'n2']) |
1086 | 7ee7c0c7 | Guido Trotter | self.assertRaises(AssertionError, self.GL.acquire, |
1087 | 7ee7c0c7 | Guido Trotter | locking.LEVEL_INSTANCE, ['i3'])
|
1088 | 7ee7c0c7 | Guido Trotter | self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1) |
1089 | 7ee7c0c7 | Guido Trotter | self.GL.acquire(locking.LEVEL_NODE, ['n1']) |
1090 | 7ee7c0c7 | Guido Trotter | self.assertRaises(AssertionError, self.GL.release, |
1091 | 7ee7c0c7 | Guido Trotter | locking.LEVEL_CLUSTER, ['BGL'])
|
1092 | 7ee7c0c7 | Guido Trotter | self.assertRaises(AssertionError, self.GL.release, |
1093 | 7ee7c0c7 | Guido Trotter | locking.LEVEL_CLUSTER) |
1094 | 7ee7c0c7 | Guido Trotter | self.GL.release(locking.LEVEL_NODE)
|
1095 | 7ee7c0c7 | Guido Trotter | self.GL.acquire(locking.LEVEL_INSTANCE, ['i1', 'i2']) |
1096 | 7ee7c0c7 | Guido Trotter | self.assertRaises(AssertionError, self.GL.release, |
1097 | 7ee7c0c7 | Guido Trotter | locking.LEVEL_CLUSTER, ['BGL'])
|
1098 | 7ee7c0c7 | Guido Trotter | self.assertRaises(AssertionError, self.GL.release, |
1099 | 7ee7c0c7 | Guido Trotter | locking.LEVEL_CLUSTER) |
1100 | 7ee7c0c7 | Guido Trotter | self.GL.release(locking.LEVEL_INSTANCE)
|
1101 | 7ee7c0c7 | Guido Trotter | |
1102 | 7ee7c0c7 | Guido Trotter | def testWrongOrder(self): |
1103 | 7ee7c0c7 | Guido Trotter | self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1) |
1104 | 04e1bfaf | Guido Trotter | self.GL.acquire(locking.LEVEL_NODE, ['n2']) |
1105 | 7ee7c0c7 | Guido Trotter | self.assertRaises(AssertionError, self.GL.acquire, |
1106 | 7ee7c0c7 | Guido Trotter | locking.LEVEL_NODE, ['n1'])
|
1107 | 7ee7c0c7 | Guido Trotter | self.assertRaises(AssertionError, self.GL.acquire, |
1108 | 7ee7c0c7 | Guido Trotter | locking.LEVEL_INSTANCE, ['i2'])
|
1109 | 7ee7c0c7 | Guido Trotter | |
1110 | 7ee7c0c7 | Guido Trotter | # Helper function to run as a thread that shared the BGL and then acquires
|
1111 | 7ee7c0c7 | Guido Trotter | # some locks at another level.
|
1112 | 7ee7c0c7 | Guido Trotter | def _doLock(self, level, names, shared): |
1113 | 7ee7c0c7 | Guido Trotter | try:
|
1114 | 7ee7c0c7 | Guido Trotter | self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1) |
1115 | 7ee7c0c7 | Guido Trotter | self.GL.acquire(level, names, shared=shared)
|
1116 | 7ee7c0c7 | Guido Trotter | self.done.put('DONE') |
1117 | 7ee7c0c7 | Guido Trotter | self.GL.release(level)
|
1118 | 7ee7c0c7 | Guido Trotter | self.GL.release(locking.LEVEL_CLUSTER)
|
1119 | 7ee7c0c7 | Guido Trotter | except errors.LockError:
|
1120 | 7ee7c0c7 | Guido Trotter | self.done.put('ERR') |
1121 | 7ee7c0c7 | Guido Trotter | |
1122 | 4607c978 | Iustin Pop | @_Repeat
|
1123 | 7ee7c0c7 | Guido Trotter | def testConcurrency(self): |
1124 | 7ee7c0c7 | Guido Trotter | self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1) |
1125 | 4607c978 | Iustin Pop | self._addThread(target=self._doLock, |
1126 | 4607c978 | Iustin Pop | args=(locking.LEVEL_INSTANCE, 'i1', 1)) |
1127 | 4607c978 | Iustin Pop | self._waitThreads()
|
1128 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
1129 | 7ee7c0c7 | Guido Trotter | self.GL.acquire(locking.LEVEL_INSTANCE, ['i3']) |
1130 | 4607c978 | Iustin Pop | self._addThread(target=self._doLock, |
1131 | 4607c978 | Iustin Pop | args=(locking.LEVEL_INSTANCE, 'i1', 1)) |
1132 | 4607c978 | Iustin Pop | self._waitThreads()
|
1133 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
1134 | 4607c978 | Iustin Pop | self._addThread(target=self._doLock, |
1135 | 4607c978 | Iustin Pop | args=(locking.LEVEL_INSTANCE, 'i3', 1)) |
1136 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
1137 | 7ee7c0c7 | Guido Trotter | self.GL.release(locking.LEVEL_INSTANCE)
|
1138 | 4607c978 | Iustin Pop | self._waitThreads()
|
1139 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
1140 | 7ee7c0c7 | Guido Trotter | self.GL.acquire(locking.LEVEL_INSTANCE, ['i2'], shared=1) |
1141 | 4607c978 | Iustin Pop | self._addThread(target=self._doLock, |
1142 | 4607c978 | Iustin Pop | args=(locking.LEVEL_INSTANCE, 'i2', 1)) |
1143 | 4607c978 | Iustin Pop | self._waitThreads()
|
1144 | 4607c978 | Iustin Pop | self.assertEqual(self.done.get_nowait(), 'DONE') |
1145 | 4607c978 | Iustin Pop | self._addThread(target=self._doLock, |
1146 | 4607c978 | Iustin Pop | args=(locking.LEVEL_INSTANCE, 'i2', 0)) |
1147 | 4607c978 | Iustin Pop | self.assertRaises(Queue.Empty, self.done.get_nowait) |
1148 | 7ee7c0c7 | Guido Trotter | self.GL.release(locking.LEVEL_INSTANCE)
|
1149 | 4607c978 | Iustin Pop | self._waitThreads()
|
1150 | 7ee7c0c7 | Guido Trotter | self.assertEqual(self.done.get(True, 1), 'DONE') |
1151 | 4607c978 | Iustin Pop | self.GL.release(locking.LEVEL_CLUSTER, ['BGL']) |
1152 | 7ee7c0c7 | Guido Trotter | |
1153 | 7ee7c0c7 | Guido Trotter | |
1154 | 162c1c1f | Guido Trotter | if __name__ == '__main__': |
1155 | 162c1c1f | Guido Trotter | unittest.main() |
1156 | 162c1c1f | Guido Trotter | #suite = unittest.TestLoader().loadTestsFromTestCase(TestSharedLock)
|
1157 | 162c1c1f | Guido Trotter | #unittest.TextTestRunner(verbosity=2).run(suite) |