Skip to content

Commit f425e7b

Browse files
author
Joel Collins
committed
Coverage for core tasks pool
1 parent f53de6d commit f425e7b

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

tests/test_core_tasks_pool.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
from labthings.core import tasks
2+
from flask import Flask, Response
3+
import pytest
4+
5+
import gevent
6+
7+
8+
@pytest.fixture()
9+
def app(request):
10+
11+
app = Flask(__name__)
12+
13+
# pushes an application context manually
14+
ctx = app.app_context()
15+
ctx.push()
16+
17+
# bind the test life with the context through the
18+
request.addfinalizer(ctx.pop)
19+
return app
20+
21+
22+
@pytest.fixture()
23+
def app_context(app):
24+
with app.app_context():
25+
yield app
26+
27+
28+
def test_taskify_without_context():
29+
def task_func():
30+
pass
31+
32+
task_obj = tasks.taskify(task_func)()
33+
assert isinstance(task_obj, gevent.Greenlet)
34+
35+
36+
def test_taskify_with_context(app_context):
37+
def task_func():
38+
pass
39+
40+
with app_context.test_request_context():
41+
task_obj = tasks.taskify(task_func)()
42+
assert isinstance(task_obj, gevent.Greenlet)
43+
44+
45+
def test_update_task_data():
46+
def task_func():
47+
tasks.update_task_data({"key": "value"})
48+
49+
task_obj = tasks.taskify(task_func)()
50+
task_obj.join()
51+
assert task_obj.state.get("data") == {"key": "value"}
52+
53+
54+
def test_update_task_data_main_thread():
55+
# Should do nothing
56+
tasks.update_task_data({"key": "value"})
57+
58+
59+
def test_update_task_progress():
60+
def task_func():
61+
tasks.update_task_progress(100)
62+
63+
task_obj = tasks.taskify(task_func)()
64+
task_obj.join()
65+
assert task_obj.state.get("progress") == 100
66+
67+
68+
def test_update_task_progress_main_thread():
69+
# Should do nothing
70+
tasks.update_task_progress(100)
71+
72+
73+
def test_tasks_list():
74+
assert all([isinstance(task_obj, gevent.Greenlet) for task_obj in tasks.tasks()])
75+
76+
77+
def test_tasks_dict():
78+
assert all(
79+
[
80+
isinstance(task_obj, gevent.Greenlet)
81+
for task_obj in tasks.dictionary().values()
82+
]
83+
)
84+
85+
assert all([k == str(t.id) for k, t in tasks.dictionary().items()])
86+
87+
88+
def test_task_states():
89+
state_keys = {
90+
"function",
91+
"id",
92+
"status",
93+
"progress",
94+
"data",
95+
"return",
96+
"start_time",
97+
"end_time",
98+
}
99+
100+
for state in tasks.states().values():
101+
assert all(k in state for k in state_keys)
102+
103+
104+
def test_remove_task():
105+
def task_func():
106+
pass
107+
108+
task_obj = tasks.taskify(task_func)()
109+
assert str(task_obj.id) in tasks.dictionary()
110+
task_obj.join()
111+
112+
tasks.remove_task(task_obj.id)
113+
assert not str(task_obj.id) in tasks.dictionary()
114+
115+
116+
def test_cleanup_task():
117+
import time
118+
119+
def task_func():
120+
pass
121+
122+
# Make sure at least 1 tasks is around
123+
tasks.taskify(task_func)()
124+
125+
# Wait for all tasks to finish
126+
gevent.joinall(tasks.tasks())
127+
128+
assert len(tasks.tasks()) > 0
129+
tasks.cleanup_tasks()
130+
assert len(tasks.tasks()) == 0

0 commit comments

Comments
 (0)