-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_lock.py
More file actions
59 lines (54 loc) · 2.69 KB
/
Copy pathtest_lock.py
File metadata and controls
59 lines (54 loc) · 2.69 KB
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
import time
from api.tests import DryccTestCase
from api.utils import CacheLock, DeployLock
class TestLock(DryccTestCase):
def test_cache_lock(self):
key = f"test_key_1_{int(time.time())}"
lock1 = CacheLock(key)
lock2 = CacheLock(key)
timeout = 5
self.assertEqual(lock1.acquire(True, timeout), True)
self.assertEqual(lock2.acquire(False, timeout), False)
time.sleep(5)
start_time = time.time()
self.assertEqual(lock2.acquire(False, timeout), True)
self.assertEqual(lock1.acquire(True, timeout + 1), True)
self.assertTrue(time.time()-start_time > 5)
self.assertEqual(lock2.acquire(False, timeout), False)
lock1.release()
self.assertEqual(lock2.acquire(False, timeout), True)
def test_deploy_lock(self):
app_id = f"test_key_1_{int(time.time())}"
lock1 = DeployLock(app_id)
lock2 = DeployLock(app_id)
self.assertEqual(lock2.acquire(["web", "task"]), True)
self.assertEqual(lock1.acquire(["web"]), False)
self.assertEqual(lock1.acquire(["bing"]), True)
lock2.release(["web", "task"])
self.assertEqual(lock1.acquire(["web"]), True)
self.assertEqual(lock1.acquire(["task"]), True)
self.assertEqual(lock2.acquire(["web", "task"]), False)
lock2.release(["web", "task", "bing"])
self.assertEqual(lock1.acquire(["web", "task"]), True)
self.assertEqual(lock2.acquire(["web", "task"]), False)
self.assertEqual(lock2.acquire(["web", "bing"], True), True)
self.assertEqual(lock1.acquire(["web", "bing", "task"]), False)
self.assertEqual(lock1.acquire(["web", "bing", "task"], True), True)
lock2.release(["web", "bing", "task"])
self.assertEqual(lock1.acquire(["web"], True), True)
lock1.release(["web", "bing", "task"])
def test_deploy_lock_release_clears_ptypes_when_inner_lock_busy(self):
"""DeployLock.release must clear ptypes even when the inner CacheLock
acquisition fails, to prevent a permanent deploy deadlock."""
from unittest.mock import patch
from django.core.cache import cache
app_id = f"test_release_busy_{int(time.time())}"
lock = DeployLock(app_id)
# Seed some ptypes as if they were recorded by acquire().
cache.set(lock.ptypes_key, ["web", "worker"], timeout=3600)
# Simulate the inner CacheLock being held by another owner.
with patch.object(CacheLock, "acquire", return_value=False):
lock.release(["web", "worker"])
# ptypes must be cleared regardless of inner lock failure.
remaining = cache.get(lock.ptypes_key, [])
self.assertEqual(remaining, [])