redis-allocator 0.0.1__py3-none-any.whl → 0.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- redis_allocator/__init__.py +5 -1
- redis_allocator/_version.py +1 -0
- redis_allocator/allocator.py +816 -280
- redis_allocator/lock.py +66 -17
- redis_allocator/task_queue.py +81 -57
- redis_allocator-0.3.1.dist-info/METADATA +529 -0
- redis_allocator-0.3.1.dist-info/RECORD +15 -0
- {redis_allocator-0.0.1.dist-info → redis_allocator-0.3.1.dist-info}/licenses/LICENSE +21 -21
- tests/conftest.py +160 -46
- tests/test_allocator.py +461 -488
- tests/test_lock.py +675 -338
- tests/test_task_queue.py +136 -136
- redis_allocator-0.0.1.dist-info/METADATA +0 -229
- redis_allocator-0.0.1.dist-info/RECORD +0 -14
- {redis_allocator-0.0.1.dist-info → redis_allocator-0.3.1.dist-info}/WHEEL +0 -0
- {redis_allocator-0.0.1.dist-info → redis_allocator-0.3.1.dist-info}/top_level.txt +0 -0
tests/conftest.py
CHANGED
@@ -1,46 +1,160 @@
|
|
1
|
-
"""Fixtures for tests."""
|
2
|
-
|
3
|
-
import pytest
|
4
|
-
import fakeredis
|
5
|
-
from redis.client import Redis
|
6
|
-
from redis_allocator.lock import RedisLock, RedisLockPool, ThreadLock, ThreadLockPool
|
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
|
-
|
1
|
+
"""Fixtures for tests."""
|
2
|
+
|
3
|
+
import pytest
|
4
|
+
import fakeredis
|
5
|
+
from redis.client import Redis
|
6
|
+
from redis_allocator.lock import RedisLock, RedisLockPool, ThreadLock, ThreadLockPool
|
7
|
+
from redis_allocator.allocator import (
|
8
|
+
RedisAllocator, RedisThreadHealthCheckPool, RedisAllocatableClass,
|
9
|
+
RedisAllocatorUpdater, DefaultRedisAllocatorPolicy
|
10
|
+
)
|
11
|
+
|
12
|
+
|
13
|
+
@pytest.fixture
|
14
|
+
def redis_client():
|
15
|
+
"""Create a fakeredis client for testing."""
|
16
|
+
return fakeredis.FakeRedis(decode_responses=True)
|
17
|
+
|
18
|
+
|
19
|
+
@pytest.fixture
|
20
|
+
def redis_client_raw():
|
21
|
+
"""Create a fakeredis client with decode_responses=False for testing."""
|
22
|
+
return fakeredis.FakeRedis(decode_responses=False)
|
23
|
+
|
24
|
+
|
25
|
+
@pytest.fixture
|
26
|
+
def redis_lock(redis_client: Redis):
|
27
|
+
"""Create a RedisLock for testing."""
|
28
|
+
return RedisLock(redis_client, 'test-lock')
|
29
|
+
|
30
|
+
|
31
|
+
@pytest.fixture
|
32
|
+
def redis_lock_pool(redis_client: Redis):
|
33
|
+
"""Create a RedisLockPool for testing."""
|
34
|
+
pool = RedisLockPool(redis_client, 'test-pool')
|
35
|
+
yield pool
|
36
|
+
pool.clear()
|
37
|
+
|
38
|
+
|
39
|
+
@pytest.fixture
|
40
|
+
def thread_lock():
|
41
|
+
"""Create a ThreadLock for testing."""
|
42
|
+
return ThreadLock()
|
43
|
+
|
44
|
+
|
45
|
+
@pytest.fixture
|
46
|
+
def thread_lock_pool():
|
47
|
+
"""Create a ThreadLockPool for testing."""
|
48
|
+
return ThreadLockPool()
|
49
|
+
|
50
|
+
|
51
|
+
# Test helper classes
|
52
|
+
class _TestObject(RedisAllocatableClass):
|
53
|
+
"""Test implementation of RedisAllocatableClass for testing."""
|
54
|
+
|
55
|
+
def __init__(self, name=None):
|
56
|
+
self.config_key = None
|
57
|
+
self.config_params = None
|
58
|
+
self.closed = False
|
59
|
+
self._name = name
|
60
|
+
|
61
|
+
def set_config(self, key, params):
|
62
|
+
"""Set configuration parameters."""
|
63
|
+
self.config_key = key
|
64
|
+
self.config_params = params
|
65
|
+
|
66
|
+
def open(self):
|
67
|
+
"""Open the object."""
|
68
|
+
self.closed = False
|
69
|
+
|
70
|
+
def close(self):
|
71
|
+
"""Mark the object as closed."""
|
72
|
+
self.closed = True
|
73
|
+
|
74
|
+
@property
|
75
|
+
def name(self):
|
76
|
+
"""Return a name for soft binding."""
|
77
|
+
return self._name
|
78
|
+
|
79
|
+
|
80
|
+
class _TestUpdater(RedisAllocatorUpdater):
|
81
|
+
"""Test implementation of RedisAllocatorUpdater."""
|
82
|
+
|
83
|
+
def __init__(self, updates):
|
84
|
+
super().__init__(updates)
|
85
|
+
self.call_count = 0
|
86
|
+
|
87
|
+
def fetch(self, param):
|
88
|
+
"""Fetch keys based on the param."""
|
89
|
+
self.call_count += 1
|
90
|
+
return param
|
91
|
+
|
92
|
+
|
93
|
+
# Additional fixtures
|
94
|
+
@pytest.fixture(params=[None, "test_object"])
|
95
|
+
def test_object(request: pytest.FixtureRequest) -> _TestObject:
|
96
|
+
"""Create a test object implementing RedisAllocatableClass."""
|
97
|
+
return _TestObject(request.param)
|
98
|
+
|
99
|
+
|
100
|
+
@pytest.fixture(params=[False, True])
|
101
|
+
def redis_allocator(redis_client: Redis, request: pytest.FixtureRequest) -> RedisAllocator:
|
102
|
+
"""Create a RedisAllocator instance for testing."""
|
103
|
+
alloc = RedisAllocator(
|
104
|
+
redis_client,
|
105
|
+
'test',
|
106
|
+
'alloc-lock',
|
107
|
+
shared=request.param
|
108
|
+
)
|
109
|
+
# Set up initial keys
|
110
|
+
alloc.extend(['key1', 'key2', 'key3'])
|
111
|
+
return alloc
|
112
|
+
|
113
|
+
|
114
|
+
@pytest.fixture
|
115
|
+
def health_checker(redis_client: Redis) -> RedisThreadHealthCheckPool:
|
116
|
+
"""Create a RedisThreadHealthCheckPool instance for testing."""
|
117
|
+
return RedisThreadHealthCheckPool(
|
118
|
+
redis_client,
|
119
|
+
'test',
|
120
|
+
timeout=60
|
121
|
+
)
|
122
|
+
|
123
|
+
|
124
|
+
@pytest.fixture
|
125
|
+
def test_updater() -> _TestUpdater:
|
126
|
+
"""Create a test updater."""
|
127
|
+
return _TestUpdater([["key1", "key2"], ["key4", "key5", "key6"], ["key7", "key8", "key9"]])
|
128
|
+
|
129
|
+
|
130
|
+
class _TestRedisAllocator(RedisAllocator):
|
131
|
+
|
132
|
+
@property
|
133
|
+
def _lua_required_string(self):
|
134
|
+
return f'''
|
135
|
+
os.time = function() return tonumber(redis.call("TIME")[1]) end
|
136
|
+
{super()._lua_required_string}
|
137
|
+
'''
|
138
|
+
|
139
|
+
|
140
|
+
@pytest.fixture(params=[False, True])
|
141
|
+
def allocator_with_policy(redis_client: Redis, test_updater: _TestUpdater, request: pytest.FixtureRequest) -> RedisAllocator:
|
142
|
+
"""Create a RedisAllocator with a default policy."""
|
143
|
+
policy = DefaultRedisAllocatorPolicy(
|
144
|
+
gc_count=2,
|
145
|
+
update_interval=60,
|
146
|
+
expiry_duration=300,
|
147
|
+
updater=test_updater
|
148
|
+
)
|
149
|
+
|
150
|
+
alloc = _TestRedisAllocator(
|
151
|
+
redis_client,
|
152
|
+
'test-policy',
|
153
|
+
'alloc-lock',
|
154
|
+
shared=request.param,
|
155
|
+
policy=policy
|
156
|
+
)
|
157
|
+
|
158
|
+
# Set up initial keys
|
159
|
+
alloc.extend(['key1', 'key2', 'key3'])
|
160
|
+
return alloc
|