root / test / ganeti.locking_unittest.py @ 9b603bff
History | View | Annotate | Download (5.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 | 162c1c1f | Guido Trotter | |
30 | 162c1c1f | Guido Trotter | from ganeti import locking |
31 | 162c1c1f | Guido Trotter | from threading import Thread |
32 | 162c1c1f | Guido Trotter | |
33 | 162c1c1f | Guido Trotter | |
34 | 162c1c1f | Guido Trotter | class TestSharedLock(unittest.TestCase): |
35 | 162c1c1f | Guido Trotter | """Shared lock tests"""
|
36 | 162c1c1f | Guido Trotter | |
37 | 162c1c1f | Guido Trotter | def setUp(self): |
38 | 162c1c1f | Guido Trotter | self.sl = locking.SharedLock()
|
39 | 162c1c1f | Guido Trotter | # helper threads use the 'done' queue to tell the master they finished.
|
40 | 162c1c1f | Guido Trotter | self.done = Queue.Queue(0) |
41 | 162c1c1f | Guido Trotter | |
42 | 162c1c1f | Guido Trotter | def testSequenceAndOwnership(self): |
43 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned()) |
44 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
45 | 162c1c1f | Guido Trotter | self.assert_(self.sl._is_owned()) |
46 | 162c1c1f | Guido Trotter | self.assert_(self.sl._is_owned(shared=1)) |
47 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned(shared=0)) |
48 | 162c1c1f | Guido Trotter | self.sl.release()
|
49 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned()) |
50 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
51 | 162c1c1f | Guido Trotter | self.assert_(self.sl._is_owned()) |
52 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned(shared=1)) |
53 | 162c1c1f | Guido Trotter | self.assert_(self.sl._is_owned(shared=0)) |
54 | 162c1c1f | Guido Trotter | self.sl.release()
|
55 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned()) |
56 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
57 | 162c1c1f | Guido Trotter | self.assert_(self.sl._is_owned()) |
58 | 162c1c1f | Guido Trotter | self.assert_(self.sl._is_owned(shared=1)) |
59 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned(shared=0)) |
60 | 162c1c1f | Guido Trotter | self.sl.release()
|
61 | 162c1c1f | Guido Trotter | self.assert_(not self.sl._is_owned()) |
62 | 162c1c1f | Guido Trotter | |
63 | 162c1c1f | Guido Trotter | def testBooleanValue(self): |
64 | 162c1c1f | Guido Trotter | # semaphores are supposed to return a true value on a successful acquire
|
65 | 162c1c1f | Guido Trotter | self.assert_(self.sl.acquire(shared=1)) |
66 | 162c1c1f | Guido Trotter | self.sl.release()
|
67 | 162c1c1f | Guido Trotter | self.assert_(self.sl.acquire()) |
68 | 162c1c1f | Guido Trotter | self.sl.release()
|
69 | 162c1c1f | Guido Trotter | |
70 | 162c1c1f | Guido Trotter | def testDoubleLockingStoE(self): |
71 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
72 | 162c1c1f | Guido Trotter | self.assertRaises(AssertionError, self.sl.acquire) |
73 | 162c1c1f | Guido Trotter | |
74 | 162c1c1f | Guido Trotter | def testDoubleLockingEtoS(self): |
75 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
76 | 162c1c1f | Guido Trotter | self.assertRaises(AssertionError, self.sl.acquire, shared=1) |
77 | 162c1c1f | Guido Trotter | |
78 | 162c1c1f | Guido Trotter | def testDoubleLockingStoS(self): |
79 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
80 | 162c1c1f | Guido Trotter | self.assertRaises(AssertionError, self.sl.acquire, shared=1) |
81 | 162c1c1f | Guido Trotter | |
82 | 162c1c1f | Guido Trotter | def testDoubleLockingEtoE(self): |
83 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
84 | 162c1c1f | Guido Trotter | self.assertRaises(AssertionError, self.sl.acquire) |
85 | 162c1c1f | Guido Trotter | |
86 | 162c1c1f | Guido Trotter | # helper functions: called in a separate thread they acquire the lock, send
|
87 | 162c1c1f | Guido Trotter | # their identifier on the done queue, then release it.
|
88 | 162c1c1f | Guido Trotter | def _doItSharer(self): |
89 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
90 | 162c1c1f | Guido Trotter | self.done.put('SHR') |
91 | 162c1c1f | Guido Trotter | self.sl.release()
|
92 | 162c1c1f | Guido Trotter | |
93 | 162c1c1f | Guido Trotter | def _doItExclusive(self): |
94 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
95 | 162c1c1f | Guido Trotter | self.done.put('EXC') |
96 | 162c1c1f | Guido Trotter | self.sl.release()
|
97 | 162c1c1f | Guido Trotter | |
98 | 162c1c1f | Guido Trotter | def testSharersCanCoexist(self): |
99 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
100 | 162c1c1f | Guido Trotter | Thread(target=self._doItSharer).start()
|
101 | 162c1c1f | Guido Trotter | self.assert_(self.done.get(True, 1)) |
102 | 162c1c1f | Guido Trotter | self.sl.release()
|
103 | 162c1c1f | Guido Trotter | |
104 | 162c1c1f | Guido Trotter | def testExclusiveBlocksExclusive(self): |
105 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
106 | 162c1c1f | Guido Trotter | Thread(target=self._doItExclusive).start()
|
107 | 162c1c1f | Guido Trotter | # give it a bit of time to check that it's not actually doing anything
|
108 | 162c1c1f | Guido Trotter | self.assertRaises(Queue.Empty, self.done.get, True, 0.2) |
109 | 162c1c1f | Guido Trotter | self.sl.release()
|
110 | 162c1c1f | Guido Trotter | self.assert_(self.done.get(True, 1)) |
111 | 162c1c1f | Guido Trotter | |
112 | 162c1c1f | Guido Trotter | def testExclusiveBlocksSharer(self): |
113 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
114 | 162c1c1f | Guido Trotter | Thread(target=self._doItSharer).start()
|
115 | 162c1c1f | Guido Trotter | time.sleep(0.05)
|
116 | 162c1c1f | Guido Trotter | self.assertRaises(Queue.Empty, self.done.get, True, 0.2) |
117 | 162c1c1f | Guido Trotter | self.sl.release()
|
118 | 162c1c1f | Guido Trotter | self.assert_(self.done.get(True, 1)) |
119 | 162c1c1f | Guido Trotter | |
120 | 162c1c1f | Guido Trotter | def testSharerBlocksExclusive(self): |
121 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
122 | 162c1c1f | Guido Trotter | Thread(target=self._doItExclusive).start()
|
123 | 162c1c1f | Guido Trotter | time.sleep(0.05)
|
124 | 162c1c1f | Guido Trotter | self.assertRaises(Queue.Empty, self.done.get, True, 0.2) |
125 | 162c1c1f | Guido Trotter | self.sl.release()
|
126 | 162c1c1f | Guido Trotter | self.assert_(self.done.get(True, 1)) |
127 | 162c1c1f | Guido Trotter | |
128 | 162c1c1f | Guido Trotter | def testWaitingExclusiveBlocksSharer(self): |
129 | 162c1c1f | Guido Trotter | self.sl.acquire(shared=1) |
130 | 162c1c1f | Guido Trotter | # the lock is acquired in shared mode...
|
131 | 162c1c1f | Guido Trotter | Thread(target=self._doItExclusive).start()
|
132 | 162c1c1f | Guido Trotter | # ...but now an exclusive is waiting...
|
133 | 162c1c1f | Guido Trotter | time.sleep(0.05)
|
134 | 162c1c1f | Guido Trotter | Thread(target=self._doItSharer).start()
|
135 | 162c1c1f | Guido Trotter | # ...so the sharer should be blocked as well
|
136 | 162c1c1f | Guido Trotter | self.assertRaises(Queue.Empty, self.done.get, True, 0.2) |
137 | 162c1c1f | Guido Trotter | self.sl.release()
|
138 | 162c1c1f | Guido Trotter | # The exclusive passed before
|
139 | 162c1c1f | Guido Trotter | self.assertEqual(self.done.get(True, 1), 'EXC') |
140 | 162c1c1f | Guido Trotter | self.assertEqual(self.done.get(True, 1), 'SHR') |
141 | 162c1c1f | Guido Trotter | |
142 | 162c1c1f | Guido Trotter | def testWaitingSharerBlocksExclusive(self): |
143 | 162c1c1f | Guido Trotter | self.sl.acquire()
|
144 | 162c1c1f | Guido Trotter | # the lock is acquired in exclusive mode...
|
145 | 162c1c1f | Guido Trotter | Thread(target=self._doItSharer).start()
|
146 | 162c1c1f | Guido Trotter | # ...but now a sharer is waiting...
|
147 | 162c1c1f | Guido Trotter | time.sleep(0.05)
|
148 | 162c1c1f | Guido Trotter | Thread(target=self._doItExclusive).start()
|
149 | 162c1c1f | Guido Trotter | # ...the exclusive is waiting too...
|
150 | 162c1c1f | Guido Trotter | self.assertRaises(Queue.Empty, self.done.get, True, 0.2) |
151 | 162c1c1f | Guido Trotter | self.sl.release()
|
152 | 162c1c1f | Guido Trotter | # The sharer passed before
|
153 | 162c1c1f | Guido Trotter | self.assertEqual(self.done.get(True, 1), 'SHR') |
154 | 162c1c1f | Guido Trotter | self.assertEqual(self.done.get(True, 1), 'EXC') |
155 | 162c1c1f | Guido Trotter | |
156 | 162c1c1f | Guido Trotter | |
157 | 162c1c1f | Guido Trotter | if __name__ == '__main__': |
158 | 162c1c1f | Guido Trotter | unittest.main() |
159 | 162c1c1f | Guido Trotter | #suite = unittest.TestLoader().loadTestsFromTestCase(TestSharedLock)
|
160 | 162c1c1f | Guido Trotter | #unittest.TextTestRunner(verbosity=2).run(suite) |