Revision 48dabc6a test/ganeti.locking_unittest.py
b/test/ganeti.locking_unittest.py | ||
---|---|---|
69 | 69 |
self.threads = [] |
70 | 70 |
|
71 | 71 |
|
72 |
class TestPipeCondition(_ThreadedTestCase): |
|
73 |
"""_PipeCondition tests""" |
|
74 |
|
|
75 |
def setUp(self): |
|
76 |
_ThreadedTestCase.setUp(self) |
|
77 |
self.lock = threading.Lock() |
|
78 |
self.cond = locking._PipeCondition(self.lock) |
|
79 |
self.done = Queue.Queue(0) |
|
80 |
|
|
81 |
def testAcquireRelease(self): |
|
82 |
self.assert_(not self.cond._is_owned()) |
|
83 |
self.assertRaises(RuntimeError, self.cond.wait) |
|
84 |
self.assertRaises(RuntimeError, self.cond.notifyAll) |
|
85 |
|
|
86 |
self.cond.acquire() |
|
87 |
self.assert_(self.cond._is_owned()) |
|
88 |
self.cond.notifyAll() |
|
89 |
self.assert_(self.cond._is_owned()) |
|
90 |
self.cond.release() |
|
91 |
|
|
92 |
self.assert_(not self.cond._is_owned()) |
|
93 |
self.assertRaises(RuntimeError, self.cond.wait) |
|
94 |
self.assertRaises(RuntimeError, self.cond.notifyAll) |
|
95 |
|
|
96 |
def testNotification(self): |
|
97 |
def _NotifyAll(): |
|
98 |
self.cond.acquire() |
|
99 |
self.cond.notifyAll() |
|
100 |
self.cond.release() |
|
101 |
|
|
102 |
self.cond.acquire() |
|
103 |
self._addThread(target=_NotifyAll) |
|
104 |
self.cond.wait() |
|
105 |
self.assert_(self.cond._is_owned()) |
|
106 |
self.cond.release() |
|
107 |
self.assert_(not self.cond._is_owned()) |
|
108 |
|
|
109 |
def _TestWait(self, fn): |
|
110 |
self._addThread(target=fn) |
|
111 |
self._addThread(target=fn) |
|
112 |
self._addThread(target=fn) |
|
113 |
|
|
114 |
# Wait for threads to be waiting |
|
115 |
self.assertEqual(self.done.get(True, 1), "A") |
|
116 |
self.assertEqual(self.done.get(True, 1), "A") |
|
117 |
self.assertEqual(self.done.get(True, 1), "A") |
|
118 |
|
|
119 |
self.assertRaises(Queue.Empty, self.done.get_nowait) |
|
120 |
|
|
121 |
self.cond.acquire() |
|
122 |
self.assertEqual(self.cond._nwaiters, 3) |
|
123 |
# This new thread can"t acquire the lock, and thus call wait, before we |
|
124 |
# release it |
|
125 |
self._addThread(target=fn) |
|
126 |
self.cond.notifyAll() |
|
127 |
self.assertRaises(Queue.Empty, self.done.get_nowait) |
|
128 |
self.cond.release() |
|
129 |
|
|
130 |
# We should now get 3 W and 1 A (for the new thread) in whatever order |
|
131 |
w = 0 |
|
132 |
a = 0 |
|
133 |
for i in range(4): |
|
134 |
got = self.done.get(True, 1) |
|
135 |
if got == "W": |
|
136 |
w += 1 |
|
137 |
elif got == "A": |
|
138 |
a += 1 |
|
139 |
else: |
|
140 |
self.fail("Got %s on the done queue" % got) |
|
141 |
|
|
142 |
self.assertEqual(w, 3) |
|
143 |
self.assertEqual(a, 1) |
|
144 |
|
|
145 |
self.cond.acquire() |
|
146 |
self.cond.notifyAll() |
|
147 |
self.cond.release() |
|
148 |
self._waitThreads() |
|
149 |
self.assertEqual(self.done.get_nowait(), "W") |
|
150 |
self.assertRaises(Queue.Empty, self.done.get_nowait) |
|
151 |
|
|
152 |
def testBlockingWait(self): |
|
153 |
def _BlockingWait(): |
|
154 |
self.cond.acquire() |
|
155 |
self.done.put("A") |
|
156 |
self.cond.wait() |
|
157 |
self.cond.release() |
|
158 |
self.done.put("W") |
|
159 |
|
|
160 |
self._TestWait(_BlockingWait) |
|
161 |
|
|
162 |
def testLongTimeoutWait(self): |
|
163 |
def _Helper(): |
|
164 |
self.cond.acquire() |
|
165 |
self.done.put("A") |
|
166 |
self.cond.wait(15.0) |
|
167 |
self.cond.release() |
|
168 |
self.done.put("W") |
|
169 |
|
|
170 |
self._TestWait(_Helper) |
|
171 |
|
|
172 |
def _TimeoutWait(self, timeout, check): |
|
173 |
self.cond.acquire() |
|
174 |
self.cond.wait(timeout) |
|
175 |
self.cond.release() |
|
176 |
self.done.put(check) |
|
177 |
|
|
178 |
def testShortTimeoutWait(self): |
|
179 |
self._addThread(target=self._TimeoutWait, args=(0.1, "T1")) |
|
180 |
self._addThread(target=self._TimeoutWait, args=(0.1, "T1")) |
|
181 |
self._waitThreads() |
|
182 |
self.assertEqual(self.done.get_nowait(), "T1") |
|
183 |
self.assertEqual(self.done.get_nowait(), "T1") |
|
184 |
self.assertRaises(Queue.Empty, self.done.get_nowait) |
|
185 |
|
|
186 |
def testZeroTimeoutWait(self): |
|
187 |
self._addThread(target=self._TimeoutWait, args=(0, "T0")) |
|
188 |
self._addThread(target=self._TimeoutWait, args=(0, "T0")) |
|
189 |
self._addThread(target=self._TimeoutWait, args=(0, "T0")) |
|
190 |
self._waitThreads() |
|
191 |
self.assertEqual(self.done.get_nowait(), "T0") |
|
192 |
self.assertEqual(self.done.get_nowait(), "T0") |
|
193 |
self.assertEqual(self.done.get_nowait(), "T0") |
|
194 |
self.assertRaises(Queue.Empty, self.done.get_nowait) |
|
195 |
|
|
196 |
|
|
72 | 197 |
class TestSingleActionPipeCondition(unittest.TestCase): |
73 | 198 |
"""_SingleActionPipeCondition tests""" |
74 | 199 |
|
Also available in: Unified diff