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