-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_locker_room.py
77 lines (61 loc) · 2.89 KB
/
test_locker_room.py
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
31
32
33
34
35
36
37
38
39
40
41
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
# -*- coding: utf-8 -*-
"""
LockerRoom - Distributed lock manager using MongoDB
Copyright (C) 2014 Gustav Arngården
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 3 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, see <http://www.gnu.org/licenses/>.
"""
import unittest
import time
import locker_room
from locker_room import LockerException
class TestLock(unittest.TestCase):
def setUp(self):
self.locker = locker_room.LockerRoom(db='locks_test')
def tearDown(self):
self.locker.lock_collection.drop()
def _assert_lock(self, name, locked=True, owner=None, expire=None):
status = self.locker.status(name)
self.assertTrue(status['locked'] == locked)
self.assertTrue(status['owner'] == owner)
self.assertTrue(status['expire'] == expire)
def test_lock(self):
self.locker.lock('test_lock', owner='unittest', timeout=1)
self._assert_lock('test_lock', locked=True, owner='unittest')
self.locker.release('test_lock')
self._assert_lock('test_lock', locked=False, owner=None)
self.locker.lock('test_lock2', owner='unittest')
with self.assertRaises(LockerException):
self.locker.lock('test_lock2', owner='unittest', timeout=1)
self.locker.release('test_lock2')
with self.assertRaises(LockerException):
self.locker.release('test_lock2')
with self.assertRaises(LockerException):
self.locker.release('test_lock3')
with self.locker.lock_and_release('test_lock'):
self._assert_lock('test_lock', locked=True)
self._assert_lock('test_lock', locked=False)
# test expiration
self.locker.lock('test_lock4', expire=1)
time.sleep(2)
self._assert_lock('test_lock4', locked=True, expire=1)
with self.locker.lock_and_release('test_lock4', timeout=1, expire=2):
self._assert_lock('test_lock4', locked=True, expire=2)
self._assert_lock('test_lock4', locked=False)
# test touching lock to prevent expiration
self.locker.lock('test_lock4', expire=1)
time.sleep(2)
self._assert_lock('test_lock4', locked=True, expire=1)
self.locker.touch('test_lock4')
with self.assertRaises(LockerException):
self.locker.lock('test_lock4', timeout=0.1)
if __name__ == '__main__':
unittest.main()