Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.locking_unittest.py @ 5b69bc7c

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