ganeti.locking_unittest.py 27.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/python
#

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


"""Script for unittesting the locking module"""


import os
import unittest
import time
import Queue

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


35
36
37
38
39
# This is used to test the ssynchronize decorator.
# Since it's passed as input to a decorator it must be declared as a global.
_decoratorlock = locking.SharedLock()


40
class TestSharedLock(unittest.TestCase):
41
  """SharedLock tests"""
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

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

  def testSequenceAndOwnership(self):
    self.assert_(not self.sl._is_owned())
    self.sl.acquire(shared=1)
    self.assert_(self.sl._is_owned())
    self.assert_(self.sl._is_owned(shared=1))
    self.assert_(not self.sl._is_owned(shared=0))
    self.sl.release()
    self.assert_(not self.sl._is_owned())
    self.sl.acquire()
    self.assert_(self.sl._is_owned())
    self.assert_(not self.sl._is_owned(shared=1))
    self.assert_(self.sl._is_owned(shared=0))
    self.sl.release()
    self.assert_(not self.sl._is_owned())
    self.sl.acquire(shared=1)
    self.assert_(self.sl._is_owned())
    self.assert_(self.sl._is_owned(shared=1))
    self.assert_(not self.sl._is_owned(shared=0))
    self.sl.release()
    self.assert_(not self.sl._is_owned())

  def testBooleanValue(self):
    # semaphores are supposed to return a true value on a successful acquire
    self.assert_(self.sl.acquire(shared=1))
    self.sl.release()
    self.assert_(self.sl.acquire())
    self.sl.release()

  def testDoubleLockingStoE(self):
    self.sl.acquire(shared=1)
    self.assertRaises(AssertionError, self.sl.acquire)

  def testDoubleLockingEtoS(self):
    self.sl.acquire()
    self.assertRaises(AssertionError, self.sl.acquire, shared=1)

  def testDoubleLockingStoS(self):
    self.sl.acquire(shared=1)
    self.assertRaises(AssertionError, self.sl.acquire, shared=1)

  def testDoubleLockingEtoE(self):
    self.sl.acquire()
    self.assertRaises(AssertionError, self.sl.acquire)

  # helper functions: called in a separate thread they acquire the lock, send
  # their identifier on the done queue, then release it.
  def _doItSharer(self):
95
96
97
98
99
100
    try:
      self.sl.acquire(shared=1)
      self.done.put('SHR')
      self.sl.release()
    except errors.LockError:
      self.done.put('ERR')
101
102

  def _doItExclusive(self):
103
104
105
106
107
108
109
110
111
    try:
      self.sl.acquire()
      self.done.put('EXC')
      self.sl.release()
    except errors.LockError:
      self.done.put('ERR')

  def _doItDelete(self):
    try:
112
      self.sl.delete()
113
114
115
      self.done.put('DEL')
    except errors.LockError:
      self.done.put('ERR')
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

  def testSharersCanCoexist(self):
    self.sl.acquire(shared=1)
    Thread(target=self._doItSharer).start()
    self.assert_(self.done.get(True, 1))
    self.sl.release()

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

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

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  def testExclusiveBlocksSharer(self):
    self.sl.acquire()
    Thread(target=self._doItSharer).start()
    time.sleep(0.05)
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.sl.release()
    self.assert_(self.done.get(True, 1))

  def testSharerBlocksExclusive(self):
    self.sl.acquire(shared=1)
    Thread(target=self._doItExclusive).start()
    time.sleep(0.05)
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.sl.release()
    self.assert_(self.done.get(True, 1))

155
156
157
158
159
160
161
162
  def testSharerBlocksDelete(self):
    self.sl.acquire(shared=1)
    Thread(target=self._doItDelete).start()
    time.sleep(0.05)
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.sl.release()
    self.assert_(self.done.get(True, 1))

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  def testWaitingExclusiveBlocksSharer(self):
    self.sl.acquire(shared=1)
    # the lock is acquired in shared mode...
    Thread(target=self._doItExclusive).start()
    # ...but now an exclusive is waiting...
    time.sleep(0.05)
    Thread(target=self._doItSharer).start()
    # ...so the sharer should be blocked as well
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.sl.release()
    # The exclusive passed before
    self.assertEqual(self.done.get(True, 1), 'EXC')
    self.assertEqual(self.done.get(True, 1), 'SHR')

  def testWaitingSharerBlocksExclusive(self):
    self.sl.acquire()
    # the lock is acquired in exclusive mode...
    Thread(target=self._doItSharer).start()
    # ...but now a sharer is waiting...
    time.sleep(0.05)
    Thread(target=self._doItExclusive).start()
    # ...the exclusive is waiting too...
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.sl.release()
    # The sharer passed before
    self.assertEqual(self.done.get(True, 1), 'SHR')
    self.assertEqual(self.done.get(True, 1), 'EXC')

191
192
193
194
195
196
197
198
199
  def testNoNonBlocking(self):
    self.assertRaises(NotImplementedError, self.sl.acquire, blocking=0)
    self.assertRaises(NotImplementedError, self.sl.delete, blocking=0)
    self.sl.acquire()
    self.sl.delete(blocking=0) # Fine, because the lock is already acquired

  def testDelete(self):
    self.sl.delete()
    self.assertRaises(errors.LockError, self.sl.acquire)
200
    self.assertRaises(errors.LockError, self.sl.acquire, shared=1)
201
202
    self.assertRaises(errors.LockError, self.sl.delete)

203
204
205
206
  def testNoDeleteIfSharer(self):
    self.sl.acquire(shared=1)
    self.assertRaises(AssertionError, self.sl.delete)

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  def testDeletePendingSharersExclusiveDelete(self):
    self.sl.acquire()
    Thread(target=self._doItSharer).start()
    Thread(target=self._doItSharer).start()
    time.sleep(0.05)
    Thread(target=self._doItExclusive).start()
    Thread(target=self._doItDelete).start()
    time.sleep(0.05)
    self.sl.delete()
    # The two threads who were pending return both ERR
    self.assertEqual(self.done.get(True, 1), 'ERR')
    self.assertEqual(self.done.get(True, 1), 'ERR')
    self.assertEqual(self.done.get(True, 1), 'ERR')
    self.assertEqual(self.done.get(True, 1), 'ERR')

  def testDeletePendingDeleteExclusiveSharers(self):
    self.sl.acquire()
    Thread(target=self._doItDelete).start()
    Thread(target=self._doItExclusive).start()
    time.sleep(0.05)
    Thread(target=self._doItSharer).start()
    Thread(target=self._doItSharer).start()
    time.sleep(0.05)
    self.sl.delete()
    # The two threads who were pending return both ERR
    self.assertEqual(self.done.get(True, 1), 'ERR')
    self.assertEqual(self.done.get(True, 1), 'ERR')
    self.assertEqual(self.done.get(True, 1), 'ERR')
    self.assertEqual(self.done.get(True, 1), 'ERR')

237

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
class TestSSynchronizedDecorator(unittest.TestCase):
  """Shared Lock Synchronized decorator test"""

  def setUp(self):
    # helper threads use the 'done' queue to tell the master they finished.
    self.done = Queue.Queue(0)

  @locking.ssynchronized(_decoratorlock)
  def _doItExclusive(self):
    self.assert_(_decoratorlock._is_owned())
    self.done.put('EXC')

  @locking.ssynchronized(_decoratorlock, shared=1)
  def _doItSharer(self):
    self.assert_(_decoratorlock._is_owned(shared=1))
    self.done.put('SHR')

  def testDecoratedFunctions(self):
    self._doItExclusive()
    self.assert_(not _decoratorlock._is_owned())
    self._doItSharer()
    self.assert_(not _decoratorlock._is_owned())

  def testSharersCanCoexist(self):
    _decoratorlock.acquire(shared=1)
    Thread(target=self._doItSharer).start()
    self.assert_(self.done.get(True, 1))
    _decoratorlock.release()

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

  def testExclusiveBlocksSharer(self):
    _decoratorlock.acquire()
    Thread(target=self._doItSharer).start()
    time.sleep(0.05)
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    _decoratorlock.release()
    self.assert_(self.done.get(True, 1))

  def testSharerBlocksExclusive(self):
    _decoratorlock.acquire(shared=1)
    Thread(target=self._doItExclusive).start()
    time.sleep(0.05)
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    _decoratorlock.release()
    self.assert_(self.done.get(True, 1))


292
293
294
295
296
class TestLockSet(unittest.TestCase):
  """LockSet tests"""

  def setUp(self):
    self.resources = ['one', 'two', 'three']
297
    self.ls = locking.LockSet(members=self.resources)
298
299
300
301
302
303
304
305
306
    # helper threads use the 'done' queue to tell the master they finished.
    self.done = Queue.Queue(0)

  def testResources(self):
    self.assertEquals(self.ls._names(), set(self.resources))
    newls = locking.LockSet()
    self.assertEquals(newls._names(), set())

  def testAcquireRelease(self):
307
    self.assert_(self.ls.acquire('one'))
308
309
310
    self.assertEquals(self.ls._list_owned(), set(['one']))
    self.ls.release()
    self.assertEquals(self.ls._list_owned(), set())
311
    self.assertEquals(self.ls.acquire(['one']), set(['one']))
312
313
314
315
316
317
318
319
320
321
322
    self.assertEquals(self.ls._list_owned(), set(['one']))
    self.ls.release()
    self.assertEquals(self.ls._list_owned(), set())
    self.ls.acquire(['one', 'two', 'three'])
    self.assertEquals(self.ls._list_owned(), set(['one', 'two', 'three']))
    self.ls.release('one')
    self.assertEquals(self.ls._list_owned(), set(['two', 'three']))
    self.ls.release(['three'])
    self.assertEquals(self.ls._list_owned(), set(['two']))
    self.ls.release()
    self.assertEquals(self.ls._list_owned(), set())
323
    self.assertEquals(self.ls.acquire(['one', 'three']), set(['one', 'three']))
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
    self.assertEquals(self.ls._list_owned(), set(['one', 'three']))
    self.ls.release()
    self.assertEquals(self.ls._list_owned(), set())

  def testNoDoubleAcquire(self):
    self.ls.acquire('one')
    self.assertRaises(AssertionError, self.ls.acquire, 'one')
    self.assertRaises(AssertionError, self.ls.acquire, ['two'])
    self.assertRaises(AssertionError, self.ls.acquire, ['two', 'three'])
    self.ls.release()
    self.ls.acquire(['one', 'three'])
    self.ls.release('one')
    self.assertRaises(AssertionError, self.ls.acquire, ['two'])
    self.ls.release('three')

  def testNoWrongRelease(self):
    self.assertRaises(AssertionError, self.ls.release)
    self.ls.acquire('one')
    self.assertRaises(AssertionError, self.ls.release, 'two')

  def testAddRemove(self):
    self.ls.add('four')
    self.assertEquals(self.ls._list_owned(), set())
    self.assert_('four' in self.ls._names())
    self.ls.add(['five', 'six', 'seven'], acquired=1)
    self.assert_('five' in self.ls._names())
    self.assert_('six' in self.ls._names())
    self.assert_('seven' in self.ls._names())
    self.assertEquals(self.ls._list_owned(), set(['five', 'six', 'seven']))
Guido Trotter's avatar
Guido Trotter committed
353
    self.assertEquals(self.ls.remove(['five', 'six']), ['five', 'six'])
354
355
356
357
358
359
360
361
362
363
364
365
366
    self.assert_('five' not in self.ls._names())
    self.assert_('six' not in self.ls._names())
    self.assertEquals(self.ls._list_owned(), set(['seven']))
    self.ls.add('eight', acquired=1, shared=1)
    self.assert_('eight' in self.ls._names())
    self.assertEquals(self.ls._list_owned(), set(['seven', 'eight']))
    self.ls.remove('seven')
    self.assert_('seven' not in self.ls._names())
    self.assertEquals(self.ls._list_owned(), set(['eight']))
    self.ls.release()
    self.ls.remove(['two'])
    self.assert_('two' not in self.ls._names())
    self.ls.acquire('three')
Guido Trotter's avatar
Guido Trotter committed
367
    self.assertEquals(self.ls.remove(['three']), ['three'])
368
    self.assert_('three' not in self.ls._names())
Guido Trotter's avatar
Guido Trotter committed
369
370
    self.assertEquals(self.ls.remove('three'), [])
    self.assertEquals(self.ls.remove(['one', 'three', 'six']), ['one'])
371
372
373
374
375
    self.assert_('one' not in self.ls._names())

  def testRemoveNonBlocking(self):
    self.assertRaises(NotImplementedError, self.ls.remove, 'one', blocking=0)
    self.ls.acquire('one')
Guido Trotter's avatar
Guido Trotter committed
376
    self.assertEquals(self.ls.remove('one', blocking=0), ['one'])
377
    self.ls.acquire(['two', 'three'])
Guido Trotter's avatar
Guido Trotter committed
378
379
    self.assertEquals(self.ls.remove(['two', 'three'], blocking=0),
                      ['two', 'three'])
380
381
382
383
384
385
386
387
388
389
390
391
392

  def testNoDoubleAdd(self):
    self.assertRaises(errors.LockError, self.ls.add, 'two')
    self.ls.add('four')
    self.assertRaises(errors.LockError, self.ls.add, 'four')

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

393
394
395
396
397
398
399
400
401
402
403
404
405
  def testAcquireSetLock(self):
    # acquire the set-lock exclusively
    self.assertEquals(self.ls.acquire(None), set(['one', 'two', 'three']))
    # I can still add/remove elements...
    self.assertEquals(self.ls.remove(['two', 'three']), ['two', 'three'])
    self.assert_(self.ls.add('six'))
    self.ls.release()
    # share the set-lock
    self.assertEquals(self.ls.acquire(None, shared=1), set(['one', 'six']))
    # adding new elements is not possible
    self.assertRaises(AssertionError, self.ls.add, 'five')
    self.ls.release()

Guido Trotter's avatar
Guido Trotter committed
406
407
408
409
410
411
412
413
414
415
416
417
  def testEmptyAcquire(self):
    # Acquire an empty list of locks...
    self.assertEquals(self.ls.acquire([]), set())
    self.assertEquals(self.ls._list_owned(), set())
    # New locks can still be addded
    self.assert_(self.ls.add('six'))
    # "re-acquiring" is not an issue, since we had really acquired nothing
    self.assertEquals(self.ls.acquire([], shared=1), set())
    self.assertEquals(self.ls._list_owned(), set())
    # We haven't really acquired anything, so we cannot release
    self.assertRaises(AssertionError, self.ls.release)

418
419
420
421
422
423
424
425
  def _doLockSet(self, set, shared):
    try:
      self.ls.acquire(set, shared=shared)
      self.done.put('DONE')
      self.ls.release()
    except errors.LockError:
      self.done.put('ERR')

426
427
428
429
430
431
432
433
  def _doAddSet(self, set):
    try:
      self.ls.add(set, acquired=1)
      self.done.put('DONE')
      self.ls.release()
    except errors.LockError:
      self.done.put('ERR')

434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
  def _doRemoveSet(self, set):
    self.done.put(self.ls.remove(set))

  def testConcurrentSharedAcquire(self):
    self.ls.acquire(['one', 'two'], shared=1)
    Thread(target=self._doLockSet, args=(['one', 'two'], 1)).start()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    Thread(target=self._doLockSet, args=(['one', 'two', 'three'], 1)).start()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    Thread(target=self._doLockSet, args=('three', 1)).start()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    Thread(target=self._doLockSet, args=(['one', 'two'], 0)).start()
    Thread(target=self._doLockSet, args=(['two', 'three'], 0)).start()
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.ls.release()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')

  def testConcurrentExclusiveAcquire(self):
    self.ls.acquire(['one', 'two'])
    Thread(target=self._doLockSet, args=('three', 1)).start()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    Thread(target=self._doLockSet, args=('three', 0)).start()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    Thread(target=self._doLockSet, args=(['one', 'two'], 0)).start()
    Thread(target=self._doLockSet, args=(['one', 'two'], 1)).start()
    Thread(target=self._doLockSet, args=('one', 0)).start()
    Thread(target=self._doLockSet, args=('one', 1)).start()
    Thread(target=self._doLockSet, args=(['two', 'three'], 0)).start()
    Thread(target=self._doLockSet, args=(['two', 'three'], 1)).start()
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.ls.release()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')

  def testConcurrentRemove(self):
    self.ls.add('four')
    self.ls.acquire(['one', 'two', 'four'])
    Thread(target=self._doLockSet, args=(['one', 'four'], 0)).start()
    Thread(target=self._doLockSet, args=(['one', 'four'], 1)).start()
    Thread(target=self._doLockSet, args=(['one', 'two'], 0)).start()
    Thread(target=self._doLockSet, args=(['one', 'two'], 1)).start()
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.ls.remove('one')
    self.ls.release()
    self.assertEqual(self.done.get(True, 1), 'ERR')
    self.assertEqual(self.done.get(True, 1), 'ERR')
    self.assertEqual(self.done.get(True, 1), 'ERR')
    self.assertEqual(self.done.get(True, 1), 'ERR')
    self.ls.add(['five', 'six'], acquired=1)
    Thread(target=self._doLockSet, args=(['three', 'six'], 1)).start()
    Thread(target=self._doLockSet, args=(['three', 'six'], 0)).start()
    Thread(target=self._doLockSet, args=(['four', 'six'], 1)).start()
    Thread(target=self._doLockSet, args=(['four', 'six'], 0)).start()
    self.ls.remove('five')
    self.ls.release()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.ls.acquire(['three', 'four'])
    Thread(target=self._doRemoveSet, args=(['four', 'six'], )).start()
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.ls.remove('four')
Guido Trotter's avatar
Guido Trotter committed
502
    self.assertEqual(self.done.get(True, 1), ['six'])
503
    Thread(target=self._doRemoveSet, args=(['two'])).start()
Guido Trotter's avatar
Guido Trotter committed
504
    self.assertEqual(self.done.get(True, 1), ['two'])
505
506
    self.ls.release()

507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
  def testConcurrentSharedSetLock(self):
    # share the set-lock...
    self.ls.acquire(None, shared=1)
    # ...another thread can share it too
    Thread(target=self._doLockSet, args=(None, 1)).start()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    # ...or just share some elements
    Thread(target=self._doLockSet, args=(['one', 'three'], 1)).start()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    # ...but not add new ones or remove any
    Thread(target=self._doAddSet, args=(['nine'])).start()
    Thread(target=self._doRemoveSet, args=(['two'], )).start()
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    # this just releases the set-lock
    self.ls.release([])
    self.assertEqual(self.done.get(True, 1), 'DONE')
    # release the lock on the actual elements so remove() can proceed too
    self.ls.release()
    self.assertEqual(self.done.get(True, 1), ['two'])

  def testConcurrentExclusiveSetLock(self):
    # acquire the set-lock...
    self.ls.acquire(None, shared=0)
    # ...no one can do anything else
    Thread(target=self._doLockSet, args=(None, 1)).start()
    Thread(target=self._doLockSet, args=(None, 0)).start()
    Thread(target=self._doLockSet, args=(['three'], 0)).start()
    Thread(target=self._doLockSet, args=(['two'], 1)).start()
    Thread(target=self._doAddSet, args=(['nine'])).start()
536
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
537
538
539
540
541
542
543
    self.ls.release()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')

Guido Trotter's avatar
Guido Trotter committed
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
  def testEmptyLockSet(self):
    # get the set-lock
    self.assertEqual(self.ls.acquire(None), set(['one', 'two', 'three']))
    # now empty it...
    self.ls.remove(['one', 'two', 'three'])
    # and adds/locks by another thread still wait
    Thread(target=self._doAddSet, args=(['nine'])).start()
    Thread(target=self._doLockSet, args=(None, 1)).start()
    Thread(target=self._doLockSet, args=(None, 0)).start()
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.ls.release()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.assertEqual(self.done.get(True, 1), 'DONE')
    # empty it again...
    self.assertEqual(self.ls.remove(['nine']), ['nine'])
    # now share it...
    self.assertEqual(self.ls.acquire(None, shared=1), set())
    # other sharers can go, adds still wait
    Thread(target=self._doLockSet, args=(None, 1)).start()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    Thread(target=self._doAddSet, args=(['nine'])).start()
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.ls.release()
    self.assertEqual(self.done.get(True, 1), 'DONE')

570

571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
class TestGanetiLockManager(unittest.TestCase):

  def setUp(self):
    self.nodes=['n1', 'n2']
    self.instances=['i1', 'i2', 'i3']
    self.GL = locking.GanetiLockManager(nodes=self.nodes,
                                        instances=self.instances)
    self.done = Queue.Queue(0)

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

  def testLockingConstants(self):
    # The locking library internally cheats by assuming its constants have some
    # relationships with each other. Check those hold true.
587
588
    # This relationship is also used in the Processor to recursively acquire
    # the right locks. Again, please don't break it.
589
590
591
592
593
594
595
596
597
    for i in range(len(locking.LEVELS)):
      self.assertEqual(i, locking.LEVELS[i])

  def testDoubleGLFails(self):
    self.assertRaises(AssertionError, locking.GanetiLockManager)

  def testLockNames(self):
    self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL']))
    self.assertEqual(self.GL._names(locking.LEVEL_NODE), set(self.nodes))
598
599
    self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE),
                     set(self.instances))
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617

  def testInitAndResources(self):
    locking.GanetiLockManager._instance = None
    self.GL = locking.GanetiLockManager()
    self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL']))
    self.assertEqual(self.GL._names(locking.LEVEL_NODE), set())
    self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), set())

    locking.GanetiLockManager._instance = None
    self.GL = locking.GanetiLockManager(nodes=self.nodes)
    self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL']))
    self.assertEqual(self.GL._names(locking.LEVEL_NODE), set(self.nodes))
    self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE), set())

    locking.GanetiLockManager._instance = None
    self.GL = locking.GanetiLockManager(instances=self.instances)
    self.assertEqual(self.GL._names(locking.LEVEL_CLUSTER), set(['BGL']))
    self.assertEqual(self.GL._names(locking.LEVEL_NODE), set())
618
619
    self.assertEqual(self.GL._names(locking.LEVEL_INSTANCE),
                     set(self.instances))
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697

  def testAcquireRelease(self):
    self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
    self.assertEquals(self.GL._list_owned(locking.LEVEL_CLUSTER), set(['BGL']))
    self.GL.acquire(locking.LEVEL_NODE, ['n1', 'n2'], shared=1)
    self.GL.release(locking.LEVEL_NODE)
    self.GL.acquire(locking.LEVEL_NODE, ['n1'])
    self.assertEquals(self.GL._list_owned(locking.LEVEL_NODE), set(['n1']))
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i1', 'i2'])
    self.GL.release(locking.LEVEL_INSTANCE, ['i2'])
    self.assertEquals(self.GL._list_owned(locking.LEVEL_INSTANCE), set(['i1']))
    self.GL.release(locking.LEVEL_NODE)
    self.GL.release(locking.LEVEL_INSTANCE)
    self.assertRaises(errors.LockError, self.GL.acquire,
                      locking.LEVEL_INSTANCE, ['i5'])
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i3'], shared=1)
    self.assertEquals(self.GL._list_owned(locking.LEVEL_INSTANCE), set(['i3']))

  def testBGLDependency(self):
    self.assertRaises(AssertionError, self.GL.acquire,
                      locking.LEVEL_NODE, ['n1', 'n2'])
    self.assertRaises(AssertionError, self.GL.acquire,
                      locking.LEVEL_INSTANCE, ['i3'])
    self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
    self.GL.acquire(locking.LEVEL_NODE, ['n1'])
    self.assertRaises(AssertionError, self.GL.release,
                      locking.LEVEL_CLUSTER, ['BGL'])
    self.assertRaises(AssertionError, self.GL.release,
                      locking.LEVEL_CLUSTER)
    self.GL.release(locking.LEVEL_NODE)
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i1', 'i2'])
    self.assertRaises(AssertionError, self.GL.release,
                      locking.LEVEL_CLUSTER, ['BGL'])
    self.assertRaises(AssertionError, self.GL.release,
                      locking.LEVEL_CLUSTER)
    self.GL.release(locking.LEVEL_INSTANCE)

  def testWrongOrder(self):
    self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i3'])
    self.assertRaises(AssertionError, self.GL.acquire,
                      locking.LEVEL_NODE, ['n1'])
    self.assertRaises(AssertionError, self.GL.acquire,
                      locking.LEVEL_INSTANCE, ['i2'])

  # Helper function to run as a thread that shared the BGL and then acquires
  # some locks at another level.
  def _doLock(self, level, names, shared):
    try:
      self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
      self.GL.acquire(level, names, shared=shared)
      self.done.put('DONE')
      self.GL.release(level)
      self.GL.release(locking.LEVEL_CLUSTER)
    except errors.LockError:
      self.done.put('ERR')

  def testConcurrency(self):
    self.GL.acquire(locking.LEVEL_CLUSTER, ['BGL'], shared=1)
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i1', 1)).start()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.GL.acquire(locking.LEVEL_NODE, ['n1'])
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i3'])
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i1', 1)).start()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i3', 1)).start()
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.GL.release(locking.LEVEL_INSTANCE)
    self.assertEqual(self.done.get(True, 1), 'DONE')
    self.GL.acquire(locking.LEVEL_INSTANCE, ['i2'], shared=1)
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i2', 1)).start()
    self.assertEqual(self.done.get(True, 1), 'DONE')
    Thread(target=self._doLock, args=(locking.LEVEL_INSTANCE, 'i2', 0)).start()
    self.assertRaises(Queue.Empty, self.done.get, True, 0.2)
    self.GL.release(locking.LEVEL_INSTANCE)
    self.assertEqual(self.done.get(True, 1), 'DONE')


698
699
700
701
if __name__ == '__main__':
  unittest.main()
  #suite = unittest.TestLoader().loadTestsFromTestCase(TestSharedLock)
  #unittest.TextTestRunner(verbosity=2).run(suite)