-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.py
201 lines (138 loc) · 4.34 KB
/
test.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
191
192
193
194
195
196
197
198
199
200
201
import sqlite3
import time
import pytest
from litequeue import LiteQueue
from litequeue import MessageStatus
print(sqlite3.sqlite_version)
# https://docs.pytest.org/en/7.1.x/how-to/fixtures.html#parametrizing-fixtures
@pytest.fixture(scope="function", params=[None, "CustomQueue"], ids=["table_name=<Default>", "table_name=CustomQueue"])
def queue_name(request) -> str:
return request.param
@pytest.fixture(scope="function", params=["_pop_transaction", "_pop_returning"])
def single_queue(request, queue_name) -> LiteQueue:
kwargs = {'filename_or_conn': ':memory:'}
if queue_name is not None:
kwargs['queue_name'] = queue_name
_q = LiteQueue(**kwargs)
if _q.get_sqlite_version() > 35:
_q.pop = getattr(_q, request.param)
return _q
@pytest.fixture(scope="function")
def queue_with_data(single_queue) -> LiteQueue:
q = single_queue
q.put("hello")
q.put("world")
q.put("foo")
q.put("bar")
return q
@pytest.mark.parametrize(
"kwargs",
(
{"filename_or_conn": sqlite3.connect(":memory:")},
{"filename_or_conn": ":memory:"},
{"memory": True},
),
)
def test_isolation_level(kwargs):
q = LiteQueue(**kwargs)
assert (
q.conn.isolation_level is None
), f"Isolation level not set properly for connection '{kwargs}'"
def test_insert_pop(single_queue):
q = single_queue
first = q.put("hello")
q.put("world")
q.put("foo")
q.put("bar")
task = q.pop()
assert task.data == "hello"
assert str(task.message_id) == str(first.message_id)
assert task.status == MessageStatus.LOCKED
assert task.done_time is None
def test_get_unknow(single_queue):
q = single_queue
assert q.get("nothing") is None
def test_pop_all_locked(queue_with_data):
q = queue_with_data
# Lock every message
for _ in range(4):
q.pop()
assert q.pop() is None
def test_basic_actions(queue_with_data):
q = queue_with_data
task = q.pop()
assert task.data == "hello"
# Peek next READY message
assert q.peek().data == "world"
assert q.peek().status == MessageStatus.READY
q.done(task.message_id)
already_done = q.get(task.message_id)
assert already_done.status == MessageStatus.DONE
in_time = already_done.in_time
lock_time = already_done.lock_time
done_time = already_done.done_time
assert done_time >= lock_time >= in_time
print(
f"Task {already_done.message_id} took {done_time - lock_time} seconds to get done and was in the queue for {done_time - in_time} seconds"
)
def test_queue_size(queue_with_data):
q = queue_with_data
assert q.qsize() == 4
task = q.pop()
q.put("x")
q.put("y")
assert q.qsize() == 6
q.done(task.message_id)
assert q.qsize() == 5
def test_prune(queue_with_data):
q = queue_with_data
while not q.empty():
t = q.pop()
q.done(t.message_id)
q.prune()
assert (
q.conn.execute(
f"SELECT * FROM {q.table} WHERE status = {MessageStatus.DONE.value}"
).fetchall()
== []
)
def test_max_size():
q = LiteQueue(":memory:", maxsize=50)
for i in range(50):
q.put(f"data_{i}")
assert q.qsize() == 50
with pytest.raises(sqlite3.IntegrityError):
q.put("new")
assert q.full()
q.pop()
assert not q.full()
def test_empty(queue_with_data):
q = queue_with_data
assert q.empty() is False
q2 = LiteQueue(":memory:")
assert q2.empty() is True
def test_list_locked(single_queue):
q = single_queue
q.put("foo")
task = q.pop()
time.sleep(0.2)
assert len(list(q.list_locked(threshold_seconds=0.1))) == 1
assert len(list(q.list_locked(threshold_seconds=20))) == 0
q.done(task.message_id)
assert len(list(q.list_locked(threshold_seconds=0.1))) == 0
def test_retry_failed(single_queue):
q = single_queue
q.put("foo")
task = q.pop()
q.mark_failed(task.message_id)
assert q.get(task.message_id).status == MessageStatus.FAILED
q.retry(task.message_id)
assert q.get(task.message_id).status == MessageStatus.READY
assert q.get(task.message_id).done_time is None
assert q.qsize() == 1
def test_count_failed(single_queue):
q = single_queue
q.put("foot")
task = q.pop()
q.mark_failed(task.message_id)
assert len(list(q.list_failed())) == 1