Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.locking_unittest.py @ 3f404fc5

History | View | Annotate | Download (22.5 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2006, 2007 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 0.0510-1301, USA.
20

    
21

    
22
"""Script for unittesting the locking module"""
23

    
24

    
25
import os
26
import unittest
27
import time
28
import Queue
29

    
30
from ganeti import locking
31
from ganeti import errors
32
from threading import Thread
33

    
34

    
35
class TestSharedLock(unittest.TestCase):
36
  """SharedLock tests"""
37

    
38
  def setUp(self):
39
    self.sl = locking.SharedLock()
40
    # helper threads use the 'done' queue to tell the master they finished.
41
    self.done = Queue.Queue(0)
42

    
43
  def testSequenceAndOwnership(self):
44
    self.assert_(not self.sl._is_owned())
45
    self.sl.acquire(shared=1)
46
    self.assert_(self.sl._is_owned())
47
    self.assert_(self.sl._is_owned(shared=1))
48
    self.assert_(not self.sl._is_owned(shared=0))
49
    self.sl.release()
50
    self.assert_(not self.sl._is_owned())
51
    self.sl.acquire()
52
    self.assert_(self.sl._is_owned())
53
    self.assert_(not self.sl._is_owned(shared=1))
54
    self.assert_(self.sl._is_owned(shared=0))
55
    self.sl.release()
56
    self.assert_(not self.sl._is_owned())
57
    self.sl.acquire(shared=1)
58
    self.assert_(self.sl._is_owned())
59
    self.assert_(self.sl._is_owned(shared=1))
60
    self.assert_(not self.sl._is_owned(shared=0))
61
    self.sl.release()
62
    self.assert_(not self.sl._is_owned())
63

    
64
  def testBooleanValue(self):
65
    # semaphores are supposed to return a true value on a successful acquire
66
    self.assert_(self.sl.acquire(shared=1))
67
    self.sl.release()
68
    self.assert_(self.sl.acquire())
69
    self.sl.release()
70

    
71
  def testDoubleLockingStoE(self):
72
    self.sl.acquire(shared=1)
73
    self.assertRaises(AssertionError, self.sl.acquire)
74

    
75
  def testDoubleLockingEtoS(self):
76
    self.sl.acquire()
77
    self.assertRaises(AssertionError, self.sl.acquire, shared=1)
78

    
79
  def testDoubleLockingStoS(self):
80
    self.sl.acquire(shared=1)
81
    self.assertRaises(AssertionError, self.sl.acquire, shared=1)
82

    
83
  def testDoubleLockingEtoE(self):
84
    self.sl.acquire()
85
    self.assertRaises(AssertionError, self.sl.acquire)
86

    
87
  # helper functions: called in a separate thread they acquire the lock, send
88
  # their identifier on the done queue, then release it.
89
  def _doItSharer(self):
90
    try:
91
      self.sl.acquire(shared=1)
92
      self.done.put('SHR')
93
      self.sl.release()
94
    except errors.LockError:
95
      self.done.put('ERR')
96

    
97
  def _doItExclusive(self):
98
    try:
99
      self.sl.acquire()
100
      self.done.put('EXC')
101
      self.sl.release()
102
    except errors.LockError:
103
      self.done.put('ERR')
104

    
105
  def _doItDelete(self):
106
    try:
107
      self.sl.delete()
108
      self.done.put('DEL')
109
    except errors.LockError:
110
      self.done.put('ERR')
111

    
112
  def testSharersCanCoexist(self):
113
    self.sl.acquire(shared=1)
114
    Thread(target=self._doItSharer).start()
115
    self.assert_(self.done.get(True, 1))
116
    self.sl.release()
117

    
118
  def testExclusiveBlocksExclusive(self):
119
    self.sl.acquire()
120
    Thread(target=self._doItExclusive).start()
121
    # give it a bit of time to check that it's not actually doing anything
122
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
123
    self.sl.release()
124
    self.assert_(self.done.get(True, 1))
125

    
126
  def testExclusiveBlocksDelete(self):
127
    self.sl.acquire()
128
    Thread(target=self._doItDelete).start()
129
    # give it a bit of time to check that it's not actually doing anything
130
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
131
    self.sl.release()
132
    self.assert_(self.done.get(True, 1))
133

    
134
  def testExclusiveBlocksSharer(self):
135
    self.sl.acquire()
136
    Thread(target=self._doItSharer).start()
137
    time.sleep(0.05)
138
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
139
    self.sl.release()
140
    self.assert_(self.done.get(True, 1))
141

    
142
  def testSharerBlocksExclusive(self):
143
    self.sl.acquire(shared=1)
144
    Thread(target=self._doItExclusive).start()
145
    time.sleep(0.05)
146
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
147
    self.sl.release()
148
    self.assert_(self.done.get(True, 1))
149

    
150
  def testSharerBlocksDelete(self):
151
    self.sl.acquire(shared=1)
152
    Thread(target=self._doItDelete).start()
153
    time.sleep(0.05)
154
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
155
    self.sl.release()
156
    self.assert_(self.done.get(True, 1))
157

    
158
  def testWaitingExclusiveBlocksSharer(self):
159
    self.sl.acquire(shared=1)
160
    # the lock is acquired in shared mode...
161
    Thread(target=self._doItExclusive).start()
162
    # ...but now an exclusive is waiting...
163
    time.sleep(0.05)
164
    Thread(target=self._doItSharer).start()
165
    # ...so the sharer should be blocked as well
166
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
167
    self.sl.release()
168
    # The exclusive passed before
169
    self.assertEqual(self.done.get(True, 1), 'EXC')
170
    self.assertEqual(self.done.get(True, 1), 'SHR')
171

    
172
  def testWaitingSharerBlocksExclusive(self):
173
    self.sl.acquire()
174
    # the lock is acquired in exclusive mode...
175
    Thread(target=self._doItSharer).start()
176
    # ...but now a sharer is waiting...
177
    time.sleep(0.05)
178
    Thread(target=self._doItExclusive).start()
179
    # ...the exclusive is waiting too...
180
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
181
    self.sl.release()
182
    # The sharer passed before
183
    self.assertEqual(self.done.get(True, 1), 'SHR')
184
    self.assertEqual(self.done.get(True, 1), 'EXC')
185

    
186
  def testNoNonBlocking(self):
187
    self.assertRaises(NotImplementedError, self.sl.acquire, blocking=0)
188
    self.assertRaises(NotImplementedError, self.sl.delete, blocking=0)
189
    self.sl.acquire()
190
    self.sl.delete(blocking=0) # Fine, because the lock is already acquired
191

    
192
  def testDelete(self):
193
    self.sl.delete()
194
    self.assertRaises(errors.LockError, self.sl.acquire)
195
    self.assertRaises(errors.LockError, self.sl.acquire, shared=1)
196
    self.assertRaises(errors.LockError, self.sl.delete)
197

    
198
  def testNoDeleteIfSharer(self):
199
    self.sl.acquire(shared=1)
200
    self.assertRaises(AssertionError, self.sl.delete)
201

    
202
  def testDeletePendingSharersExclusiveDelete(self):
203
    self.sl.acquire()
204
    Thread(target=self._doItSharer).start()
205
    Thread(target=self._doItSharer).start()
206
    time.sleep(0.05)
207
    Thread(target=self._doItExclusive).start()
208
    Thread(target=self._doItDelete).start()
209
    time.sleep(0.05)
210
    self.sl.delete()
211
    # The two threads who were pending return both ERR
212
    self.assertEqual(self.done.get(True, 1), 'ERR')
213
    self.assertEqual(self.done.get(True, 1), 'ERR')
214
    self.assertEqual(self.done.get(True, 1), 'ERR')
215
    self.assertEqual(self.done.get(True, 1), 'ERR')
216

    
217
  def testDeletePendingDeleteExclusiveSharers(self):
218
    self.sl.acquire()
219
    Thread(target=self._doItDelete).start()
220
    Thread(target=self._doItExclusive).start()
221
    time.sleep(0.05)
222
    Thread(target=self._doItSharer).start()
223
    Thread(target=self._doItSharer).start()
224
    time.sleep(0.05)
225
    self.sl.delete()
226
    # The two threads who were pending return both ERR
227
    self.assertEqual(self.done.get(True, 1), 'ERR')
228
    self.assertEqual(self.done.get(True, 1), 'ERR')
229
    self.assertEqual(self.done.get(True, 1), 'ERR')
230
    self.assertEqual(self.done.get(True, 1), 'ERR')
231

    
232

    
233
class TestLockSet(unittest.TestCase):
234
  """LockSet tests"""
235

    
236
  def setUp(self):
237
    self.resources = ['one', 'two', 'three']
238
    self.ls = locking.LockSet(self.resources)
239
    # helper threads use the 'done' queue to tell the master they finished.
240
    self.done = Queue.Queue(0)
241

    
242
  def testResources(self):
243
    self.assertEquals(self.ls._names(), set(self.resources))
244
    newls = locking.LockSet()
245
    self.assertEquals(newls._names(), set())
246

    
247
  def testAcquireRelease(self):
248
    self.assert_(self.ls.acquire('one'))
249
    self.assertEquals(self.ls._list_owned(), set(['one']))
250
    self.ls.release()
251
    self.assertEquals(self.ls._list_owned(), set())
252
    self.assertEquals(self.ls.acquire(['one']), set(['one']))
253
    self.assertEquals(self.ls._list_owned(), set(['one']))
254
    self.ls.release()
255
    self.assertEquals(self.ls._list_owned(), set())
256
    self.ls.acquire(['one', 'two', 'three'])
257
    self.assertEquals(self.ls._list_owned(), set(['one', 'two', 'three']))
258
    self.ls.release('one')
259
    self.assertEquals(self.ls._list_owned(), set(['two', 'three']))
260
    self.ls.release(['three'])
261
    self.assertEquals(self.ls._list_owned(), set(['two']))
262
    self.ls.release()
263
    self.assertEquals(self.ls._list_owned(), set())
264
    self.assertEquals(self.ls.acquire(['one', 'three']), set(['one', 'three']))
265
    self.assertEquals(self.ls._list_owned(), set(['one', 'three']))
266
    self.ls.release()
267
    self.assertEquals(self.ls._list_owned(), set())
268

    
269
  def testNoDoubleAcquire(self):
270
    self.ls.acquire('one')
271
    self.assertRaises(AssertionError, self.ls.acquire, 'one')
272
    self.assertRaises(AssertionError, self.ls.acquire, ['two'])
273
    self.assertRaises(AssertionError, self.ls.acquire, ['two', 'three'])
274
    self.ls.release()
275
    self.ls.acquire(['one', 'three'])
276
    self.ls.release('one')
277
    self.assertRaises(AssertionError, self.ls.acquire, ['two'])
278
    self.ls.release('three')
279

    
280
  def testNoWrongRelease(self):
281
    self.assertRaises(AssertionError, self.ls.release)
282
    self.ls.acquire('one')
283
    self.assertRaises(AssertionError, self.ls.release, 'two')
284

    
285
  def testAddRemove(self):
286
    self.ls.add('four')
287
    self.assertEquals(self.ls._list_owned(), set())
288
    self.assert_('four' in self.ls._names())
289
    self.ls.add(['five', 'six', 'seven'], acquired=1)
290
    self.assert_('five' in self.ls._names())
291
    self.assert_('six' in self.ls._names())
292
    self.assert_('seven' in self.ls._names())
293
    self.assertEquals(self.ls._list_owned(), set(['five', 'six', 'seven']))
294
    self.assertEquals(self.ls.remove(['five', 'six']), ['five', 'six'])
295
    self.assert_('five' not in self.ls._names())
296
    self.assert_('six' not in self.ls._names())
297
    self.assertEquals(self.ls._list_owned(), set(['seven']))
298
    self.ls.add('eight', acquired=1, shared=1)
299
    self.assert_('eight' in self.ls._names())
300
    self.assertEquals(self.ls._list_owned(), set(['seven', 'eight']))
301
    self.ls.remove('seven')
302
    self.assert_('seven' not in self.ls._names())
303
    self.assertEquals(self.ls._list_owned(), set(['eight']))
304
    self.ls.release()
305
    self.ls.remove(['two'])
306
    self.assert_('two' not in self.ls._names())
307
    self.ls.acquire('three')
308
    self.assertEquals(self.ls.remove(['three']), ['three'])
309
    self.assert_('three' not in self.ls._names())
310
    self.assertEquals(self.ls.remove('three'), [])
311
    self.assertEquals(self.ls.remove(['one', 'three', 'six']), ['one'])
312
    self.assert_('one' not in self.ls._names())
313

    
314
  def testRemoveNonBlocking(self):
315
    self.assertRaises(NotImplementedError, self.ls.remove, 'one', blocking=0)
316
    self.ls.acquire('one')
317
    self.assertEquals(self.ls.remove('one', blocking=0), ['one'])
318
    self.ls.acquire(['two', 'three'])
319
    self.assertEquals(self.ls.remove(['two', 'three'], blocking=0),
320
                      ['two', 'three'])
321

    
322
  def testNoDoubleAdd(self):
323
    self.assertRaises(errors.LockError, self.ls.add, 'two')
324
    self.ls.add('four')
325
    self.assertRaises(errors.LockError, self.ls.add, 'four')
326

    
327
  def testNoWrongRemoves(self):
328
    self.ls.acquire(['one', 'three'], shared=1)
329
    # Cannot remove 'two' while holding something which is not a superset
330
    self.assertRaises(AssertionError, self.ls.remove, 'two')
331
    # Cannot remove 'three' as we are sharing it
332
    self.assertRaises(AssertionError, self.ls.remove, 'three')
333

    
334
  def _doLockSet(self, set, shared):
335
    try:
336
      self.ls.acquire(set, shared=shared)
337
      self.done.put('DONE')
338
      self.ls.release()
339
    except errors.LockError:
340
      self.done.put('ERR')
341

    
342
  def _doRemoveSet(self, set):
343
    self.done.put(self.ls.remove(set))
344

    
345
  def testConcurrentSharedAcquire(self):
346
    self.ls.acquire(['one', 'two'], shared=1)
347
    Thread(target=self._doLockSet, args=(['one', 'two'], 1)).start()
348
    self.assertEqual(self.done.get(True, 1), 'DONE')
349
    Thread(target=self._doLockSet, args=(['one', 'two', 'three'], 1)).start()
350
    self.assertEqual(self.done.get(True, 1), 'DONE')
351
    Thread(target=self._doLockSet, args=('three', 1)).start()
352
    self.assertEqual(self.done.get(True, 1), 'DONE')
353
    Thread(target=self._doLockSet, args=(['one', 'two'], 0)).start()
354
    Thread(target=self._doLockSet, args=(['two', 'three'], 0)).start()
355
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
356
    self.ls.release()
357
    self.assertEqual(self.done.get(True, 1), 'DONE')
358
    self.assertEqual(self.done.get(True, 1), 'DONE')
359

    
360
  def testConcurrentExclusiveAcquire(self):
361
    self.ls.acquire(['one', 'two'])
362
    Thread(target=self._doLockSet, args=('three', 1)).start()
363
    self.assertEqual(self.done.get(True, 1), 'DONE')
364
    Thread(target=self._doLockSet, args=('three', 0)).start()
365
    self.assertEqual(self.done.get(True, 1), 'DONE')
366
    Thread(target=self._doLockSet, args=(['one', 'two'], 0)).start()
367
    Thread(target=self._doLockSet, args=(['one', 'two'], 1)).start()
368
    Thread(target=self._doLockSet, args=('one', 0)).start()
369
    Thread(target=self._doLockSet, args=('one', 1)).start()
370
    Thread(target=self._doLockSet, args=(['two', 'three'], 0)).start()
371
    Thread(target=self._doLockSet, args=(['two', 'three'], 1)).start()
372
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
373
    self.ls.release()
374
    self.assertEqual(self.done.get(True, 1), 'DONE')
375
    self.assertEqual(self.done.get(True, 1), 'DONE')
376
    self.assertEqual(self.done.get(True, 1), 'DONE')
377
    self.assertEqual(self.done.get(True, 1), 'DONE')
378
    self.assertEqual(self.done.get(True, 1), 'DONE')
379
    self.assertEqual(self.done.get(True, 1), 'DONE')
380

    
381
  def testConcurrentRemove(self):
382
    self.ls.add('four')
383
    self.ls.acquire(['one', 'two', 'four'])
384
    Thread(target=self._doLockSet, args=(['one', 'four'], 0)).start()
385
    Thread(target=self._doLockSet, args=(['one', 'four'], 1)).start()
386
    Thread(target=self._doLockSet, args=(['one', 'two'], 0)).start()
387
    Thread(target=self._doLockSet, args=(['one', 'two'], 1)).start()
388
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
389
    self.ls.remove('one')
390
    self.ls.release()
391
    self.assertEqual(self.done.get(True, 1), 'ERR')
392
    self.assertEqual(self.done.get(True, 1), 'ERR')
393
    self.assertEqual(self.done.get(True, 1), 'ERR')
394
    self.assertEqual(self.done.get(True, 1), 'ERR')
395
    self.ls.add(['five', 'six'], acquired=1)
396
    Thread(target=self._doLockSet, args=(['three', 'six'], 1)).start()
397
    Thread(target=self._doLockSet, args=(['three', 'six'], 0)).start()
398
    Thread(target=self._doLockSet, args=(['four', 'six'], 1)).start()
399
    Thread(target=self._doLockSet, args=(['four', 'six'], 0)).start()
400
    self.ls.remove('five')
401
    self.ls.release()
402
    self.assertEqual(self.done.get(True, 1), 'DONE')
403
    self.assertEqual(self.done.get(True, 1), 'DONE')
404
    self.assertEqual(self.done.get(True, 1), 'DONE')
405
    self.assertEqual(self.done.get(True, 1), 'DONE')
406
    self.ls.acquire(['three', 'four'])
407
    Thread(target=self._doRemoveSet, args=(['four', 'six'], )).start()
408
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
409
    self.ls.remove('four')
410
    self.assertEqual(self.done.get(True, 1), ['six'])
411
    Thread(target=self._doRemoveSet, args=(['two'])).start()
412
    self.assertEqual(self.done.get(True, 1), ['two'])
413
    self.ls.release()
414

    
415

    
416
class TestGanetiLockManager(unittest.TestCase):
417

    
418
  def setUp(self):
419
    self.nodes=['n1', 'n2']
420
    self.instances=['i1', 'i2', 'i3']
421
    self.GL = locking.GanetiLockManager(nodes=self.nodes,
422
                                        instances=self.instances)
423
    self.done = Queue.Queue(0)
424

    
425
  def tearDown(self):
426
    # Don't try this at home...
427
    locking.GanetiLockManager._instance = None
428

    
429
  def testLockingConstants(self):
430
    # The locking library internally cheats by assuming its constants have some
431
    # relationships with each other. Check those hold true.
432
    for i in range(len(locking.LEVELS)):
433
      self.assertEqual(i, locking.LEVELS[i])
434

    
435
  def testDoubleGLFails(self):
436
    # We are not passing test=True, so instantiating a new one should fail
437
    self.assertRaises(AssertionError, locking.GanetiLockManager)
438

    
439
  def testLockNames(self):
440
    self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL']))
441
    self.assertEqual(self.GL._names(locking.LEVEL_NODE), set(self.nodes))
442
    self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), set(self.instances))
443
    self.assertEqual(self.GL._names(locking.LEVEL_CONFIG), set(['config']))
444

    
445
  def testInitAndResources(self):
446
    locking.GanetiLockManager._instance = None
447
    self.GL = locking.GanetiLockManager()
448
    self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL']))
449
    self.assertEqual(self.GL._names(locking.LEVEL_NODE), set())
450
    self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), set())
451
    self.assertEqual(self.GL._names(locking.LEVEL_CONFIG), set(['config']))
452

    
453
    locking.GanetiLockManager._instance = None
454
    self.GL = locking.GanetiLockManager(nodes=self.nodes)
455
    self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL']))
456
    self.assertEqual(self.GL._names(locking.LEVEL_NODE), set(self.nodes))
457
    self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), set())
458
    self.assertEqual(self.GL._names(locking.LEVEL_CONFIG), set(['config']))
459

    
460
    locking.GanetiLockManager._instance = None
461
    self.GL = locking.GanetiLockManager(instances=self.instances)
462
    self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL']))
463
    self.assertEqual(self.GL._names(locking.LEVEL_NODE), set())
464
    self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), set(self.instances))
465
    self.assertEqual(self.GL._names(locking.LEVEL_CONFIG), set(['config']))
466

    
467
  def testAcquireRelease(self):
468
    self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
469
    self.assertEquals(self.GL._list_owned(locking.LEVEL_CLUSTER), set(['BGL']))
470
    self.GL.acquire(locking.LEVEL_NODE, ['n1', 'n2'], shared=1)
471
    self.GL.release(locking.LEVEL_NODE)
472
    self.GL.acquire(locking.LEVEL_NODE, ['n1'])
473
    self.assertEquals(self.GL._list_owned(locking.LEVEL_NODE), set(['n1']))
474
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i1', 'i2'])
475
    self.GL.acquire(locking.LEVEL_CONFIG, ['config'])
476
    self.GL.release(locking.LEVEL_INSTANCE, ['i2'])
477
    self.assertEquals(self.GL._list_owned(locking.LEVEL_INSTANCE), set(['i1']))
478
    self.GL.release(locking.LEVEL_NODE)
479
    self.GL.release(locking.LEVEL_INSTANCE)
480
    self.GL.release(locking.LEVEL_CONFIG)
481
    self.assertRaises(errors.LockError, self.GL.acquire,
482
                      locking.LEVEL_INSTANCE, ['i5'])
483
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i3'], shared=1)
484
    self.assertEquals(self.GL._list_owned(locking.LEVEL_INSTANCE), set(['i3']))
485

    
486
  def testBGLDependency(self):
487
    self.assertRaises(AssertionError, self.GL.acquire,
488
                      locking.LEVEL_NODE, ['n1', 'n2'])
489
    self.assertRaises(AssertionError, self.GL.acquire,
490
                      locking.LEVEL_INSTANCE, ['i3'])
491
    self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
492
    self.GL.acquire(locking.LEVEL_NODE, ['n1'])
493
    self.assertRaises(AssertionError, self.GL.release,
494
                      locking.LEVEL_CLUSTER, ['BGL'])
495
    self.assertRaises(AssertionError, self.GL.release,
496
                      locking.LEVEL_CLUSTER)
497
    self.GL.release(locking.LEVEL_NODE)
498
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i1', 'i2'])
499
    self.assertRaises(AssertionError, self.GL.release,
500
                      locking.LEVEL_CLUSTER, ['BGL'])
501
    self.assertRaises(AssertionError, self.GL.release,
502
                      locking.LEVEL_CLUSTER)
503
    self.GL.release(locking.LEVEL_INSTANCE)
504
    self.GL.acquire(locking.LEVEL_CONFIG, ['config'])
505
    self.assertRaises(AssertionError, self.GL.release,
506
                      locking.LEVEL_CLUSTER)
507

    
508
  def testWrongOrder(self):
509
    self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
510
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i3'])
511
    self.assertRaises(AssertionError, self.GL.acquire,
512
                      locking.LEVEL_NODE, ['n1'])
513
    self.assertRaises(AssertionError, self.GL.acquire,
514
                      locking.LEVEL_INSTANCE, ['i2'])
515
    self.GL.acquire(locking.LEVEL_CONFIG, ['config'])
516
    self.assertRaises(AssertionError, self.GL.acquire,
517
                      locking.LEVEL_CONFIG, ['config'])
518
    self.GL.release(locking.LEVEL_INSTANCE)
519
    self.assertRaises(AssertionError, self.GL.acquire,
520
                      locking.LEVEL_NODE, ['n1'])
521
    self.assertRaises(AssertionError, self.GL.acquire,
522
                      locking.LEVEL_INSTANCE, ['i2'])
523
    self.assertRaises(AssertionError, self.GL.acquire,
524
                      locking.LEVEL_CONFIG, ['config'])
525

    
526
  # Helper function to run as a thread that shared the BGL and then acquires
527
  # some locks at another level.
528
  def _doLock(self, level, names, shared):
529
    try:
530
      self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
531
      self.GL.acquire(level, names, shared=shared)
532
      self.done.put('DONE')
533
      self.GL.release(level)
534
      self.GL.release(locking.LEVEL_CLUSTER)
535
    except errors.LockError:
536
      self.done.put('ERR')
537

    
538
  def testConcurrency(self):
539
    self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
540
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i1', 1)).start()
541
    self.assertEqual(self.done.get(True, 1), 'DONE')
542
    self.GL.acquire(locking.LEVEL_NODE, ['n1'])
543
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i3'])
544
    self.GL.acquire(locking.LEVEL_CONFIG, ['config'])
545
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i1', 1)).start()
546
    self.assertEqual(self.done.get(True, 1), 'DONE')
547
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i3', 1)).start()
548
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
549
    self.GL.release(locking.LEVEL_CONFIG)
550
    self.GL.release(locking.LEVEL_INSTANCE)
551
    self.assertEqual(self.done.get(True, 1), 'DONE')
552
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i2'], shared=1)
553
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i2', 1)).start()
554
    self.assertEqual(self.done.get(True, 1), 'DONE')
555
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i2', 0)).start()
556
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
557
    self.GL.release(locking.LEVEL_INSTANCE)
558
    self.assertEqual(self.done.get(True, 1), 'DONE')
559

    
560

    
561
if __name__ == '__main__':
562
  unittest.main()
563
  #suite = unittest.TestLoader().loadTestsFromTestCase(TestSharedLock)
564
  #unittest.TextTestRunner(verbosity=2).run(suite)