Skip to content

Commit d6ad47e

Browse files
author
Joel Collins
committed
Improved thread coverage
1 parent 0656200 commit d6ad47e

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

tests/test_tasks_thread.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import threading
33

44
import time
5+
import pytest
56

67

78
def test_task_with_args():
@@ -29,6 +30,43 @@ def task_func():
2930
assert task_obj._kwargs == {}
3031

3132

33+
def test_task_properties():
34+
def task_func(arg, kwarg=False):
35+
pass
36+
37+
task_obj = thread.ActionThread(
38+
target=task_func, args=("String arg",), kwargs={"kwarg": True}
39+
)
40+
assert task_obj.status == task_obj._status
41+
assert task_obj.id == task_obj._ID
42+
assert task_obj.status == task_obj._status
43+
assert task_obj.output == task_obj._return_value
44+
45+
46+
def test_task_update_progress():
47+
def task_func():
48+
pool.current_action().update_progress(100)
49+
return
50+
51+
task_obj = thread.ActionThread(target=task_func)
52+
53+
task_obj.start()
54+
task_obj.join()
55+
assert task_obj.progress == 100
56+
57+
58+
def test_task_update_data():
59+
def task_func():
60+
pool.current_action().update_data({"key": "value"})
61+
return
62+
63+
task_obj = thread.ActionThread(target=task_func)
64+
65+
task_obj.start()
66+
task_obj.join()
67+
assert task_obj.data == {"key": "value"}
68+
69+
3270
def test_task_start():
3371
def task_func():
3472
return "Return value"
@@ -44,6 +82,38 @@ def task_func():
4482
assert task_obj._status == "success"
4583

4684

85+
def test_task_get():
86+
def task_func():
87+
time.sleep(0.1)
88+
return "Return value"
89+
90+
task_obj = thread.ActionThread(target=task_func)
91+
task_obj.start()
92+
assert task_obj.get() == "Return value"
93+
94+
95+
def test_task_get_noblock():
96+
def task_func():
97+
time.sleep(0.1)
98+
return "Return value"
99+
100+
task_obj = thread.ActionThread(target=task_func)
101+
task_obj.start()
102+
task_obj.join()
103+
assert task_obj.get(block=False, timeout=0) == "Return value"
104+
105+
106+
def test_task_get_noblock_timeout():
107+
def task_func():
108+
time.sleep(0.1)
109+
return "Return value"
110+
111+
task_obj = thread.ActionThread(target=task_func)
112+
task_obj.start()
113+
with pytest.raises(TimeoutError):
114+
assert task_obj.get(block=False, timeout=0)
115+
116+
47117
def test_task_exception():
48118
exc_to_raise = Exception("Exception message")
49119

@@ -73,6 +143,21 @@ def task_func():
73143
assert task_obj._return_value is None
74144

75145

146+
def test_task_stop_timeout():
147+
def task_func():
148+
while True:
149+
time.sleep(0)
150+
151+
task_obj = thread.ActionThread(target=task_func)
152+
task_obj.start()
153+
task_obj.started.wait()
154+
assert task_obj._status == "running"
155+
task_obj.stop(timeout=0)
156+
task_obj.join()
157+
assert task_obj._status == "terminated"
158+
assert task_obj._return_value is None
159+
160+
76161
def test_task_terminate():
77162
def task_func():
78163
while True:
@@ -88,6 +173,16 @@ def task_func():
88173
assert task_obj._return_value is None
89174

90175

176+
def test_task_terminate_not_running():
177+
def task_func():
178+
return
179+
180+
task_obj = thread.ActionThread(target=task_func)
181+
task_obj.start()
182+
task_obj.join()
183+
assert task_obj.terminate() is False
184+
185+
91186
def test_task_log_list():
92187
import logging
93188
import os

0 commit comments

Comments
 (0)