-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_lock.py
118 lines (85 loc) · 3.27 KB
/
test_lock.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from doajtest.helpers import DoajTestCase
from portality import models, lock
from portality.lib import dates
from doajtest.fixtures import JournalFixtureFactory
import time
from datetime import timedelta
from copy import deepcopy
class TestLock(DoajTestCase):
def test_01_lock_success_fail(self):
source = JournalFixtureFactory.make_journal_source()
j = models.Journal(**source)
j.save(blocking=True)
# first set a lock
l = lock.lock("journal", j.id, "testuser")
assert l.about == j.id
assert l.type == "journal"
assert l.username == "testuser"
assert not l.is_expired()
time.sleep(1)
# now try and set the lock again for the same thing by the same user
l2 = lock.lock("journal", j.id, "testuser")
assert l2 is not None
assert l2.id == l.id
# now try and set the lock as another user
with self.assertRaises(lock.Locked):
l3 = lock.lock("journal", j.id, "otheruser")
def test_02_unlock(self):
unlocked = lock.unlock("journal", "qojoiwjreiqwefoijqwiefjw", "testuser")
assert unlocked is True
source = JournalFixtureFactory.make_journal_source()
j = models.Journal(**source)
j.save(blocking=True)
# first set a lock
l = lock.lock("journal", j.id, "testuser")
time.sleep(1)
# try and unlock as a different user
unlocked = lock.unlock("journal", j.id, "otheruser")
assert unlocked is False
# now unlock for real
unlocked = lock.unlock("journal", j.id, "testuser")
assert unlocked is True
def test_03_batch_lock_unlock(self):
source = JournalFixtureFactory.make_journal_source()
ids = []
# create a bunch of journals that we can play with
j = models.Journal(**deepcopy(source))
j.save()
ids.append(j.id)
j = models.Journal(**deepcopy(source))
j.save()
ids.append(j.id)
j = models.Journal(**deepcopy(source))
j.save()
ids.append(j.id)
j = models.Journal(**deepcopy(source))
j.save()
ids.append(j.id)
j = models.Journal(**deepcopy(source))
j.save()
ids.append(j.id)
time.sleep(2)
ls = lock.batch_lock("journal", ids, "testuser")
assert len(ls) == 5
time.sleep(2)
report = lock.batch_unlock("journal", ids, "testuser")
assert len(report["success"]) == 5
assert len(report["fail"]) == 0
time.sleep(2)
# now lock an individual record by a different user and check that no locks are set
# in batch
l = lock.lock("journal", ids[3], "otheruser")
time.sleep(2)
with self.assertRaises(lock.Locked):
ls = lock.batch_lock("journal", ids, "testuser")
for id in ids:
assert lock.has_lock("journal", id, "testuser") is False
def test_04_timeout(self):
source = JournalFixtureFactory.make_journal_source()
j = models.Journal(**source)
j.save()
time.sleep(2)
after = dates.now() + timedelta(seconds=2300)
# set a lock with a longer timout
l = lock.lock("journal", j.id, "testuser", 2400)
assert dates.parse(l.expires) > after