Skip to content

Commit 36dc34f

Browse files
+ test cases
1 parent 5f1905c commit 36dc34f

File tree

4 files changed

+197
-6
lines changed

4 files changed

+197
-6
lines changed

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Need this file, else it can't import src.thread

tests/test_parallelprocessing.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import time
2+
import numpy
3+
import pytest
4+
from src.thread import ParallelProcessing, exceptions
5+
6+
7+
# >>>>>>>>>> Dummy Functions <<<<<<<<<< #
8+
def _dummy_dataProcessor(data_in: int, delay: float = 0) -> int:
9+
time.sleep(delay)
10+
return data_in
11+
12+
def _dummy_raiseException(x: Exception, delay: float = 0):
13+
time.sleep(delay)
14+
raise x
15+
16+
17+
18+
19+
# >>>>>>>>>> General Use <<<<<<<<<< #
20+
def test_threadsScaleDown():
21+
"""This test is for testing if threads scale down `max_threads` when the dataset is lesser than the thread count"""
22+
dataset = numpy.arange(0, 2).tolist()
23+
new = ParallelProcessing(
24+
function = _dummy_dataProcessor,
25+
dataset = dataset,
26+
max_threads = 4,
27+
kwargs = { 'delay': 2 }
28+
)
29+
new.start()
30+
assert len(new._threads) == 2
31+
32+
def test_threadsProcessing():
33+
"""This test is for testing if threads correctly order data in the `dataset` arrangement"""
34+
dataset = numpy.arange(0, 500).tolist()
35+
new = ParallelProcessing(
36+
function = _dummy_dataProcessor,
37+
dataset = dataset,
38+
args = [0.001]
39+
)
40+
new.start()
41+
assert new.get_return_values() == dataset
42+
43+
44+
45+
46+
# >>>>>>>>>> Raising Exceptions <<<<<<<<<< #
47+
def test_raises_notInitializedError():
48+
"""This test should raise ThreadNotInitializedError"""
49+
dataset = numpy.arange(0, 8).tolist()
50+
new = ParallelProcessing(
51+
function = _dummy_dataProcessor,
52+
dataset = dataset
53+
)
54+
55+
with pytest.raises(exceptions.ThreadNotInitializedError):
56+
new.results
57+
58+
def test_raises_StillRunningError():
59+
"""This test should raise ThreadStillRunningError"""
60+
dataset = numpy.arange(0, 8).tolist()
61+
new = ParallelProcessing(
62+
function = _dummy_dataProcessor,
63+
dataset = dataset,
64+
args = [1]
65+
)
66+
new.start()
67+
with pytest.raises(exceptions.ThreadStillRunningError):
68+
new.results
69+
70+
def test_raises_RunTimeError():
71+
"""This test should raise a RunTimeError"""
72+
dataset = [RuntimeError()] * 8
73+
new = ParallelProcessing(
74+
function = _dummy_raiseException,
75+
dataset = dataset,
76+
args = [0.01]
77+
)
78+
with pytest.raises(RuntimeError):
79+
new.start()
80+
new.join()

tests/test_placeholder.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/test_thread.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import time
2+
import pytest
3+
from src.thread import Thread, exceptions
4+
5+
6+
# >>>>>>>>>> Dummy Functions <<<<<<<<<< #
7+
def _dummy_target_raiseToPower(x: float, power: float, delay: float = 0):
8+
time.sleep(delay)
9+
return x**power
10+
11+
def _dummy_raiseException(x: Exception, delay: float = 0):
12+
time.sleep(delay)
13+
raise x
14+
15+
16+
17+
18+
# >>>>>>>>>> General Use <<<<<<<<<< #
19+
def test_threadCreation():
20+
"""This test is for testing parsing of args and kwargs and `.join()` method"""
21+
new = Thread(
22+
target = _dummy_target_raiseToPower,
23+
args = [4],
24+
kwargs = { 'power': 2 }
25+
)
26+
new.start()
27+
s = new.join()
28+
assert s.status == 'Thread terminated'
29+
assert new.result == 16
30+
31+
def test_threadingThreadParsing():
32+
"""This test is for testing parsing arguments to `threading.Thead`"""
33+
new = Thread(
34+
target = _dummy_target_raiseToPower,
35+
args = [4, 2, 5],
36+
name = 'testingThread',
37+
daemon = True
38+
)
39+
new.start()
40+
assert new._thread and new._thread.name == 'testingThread'
41+
42+
def test_suppressAll():
43+
"""This test is for testing that errors are suppressed properly"""
44+
new = Thread(
45+
target = _dummy_raiseException,
46+
args = [ValueError()],
47+
suppress_errors = True,
48+
daemon = True
49+
)
50+
new.start()
51+
new.join()
52+
assert len(new.errors) == 1
53+
assert isinstance(new.errors[0], ValueError)
54+
55+
def test_ignoreSpecificError():
56+
"""This test is for testing that specific errors are ignored properly"""
57+
new = Thread(
58+
target = _dummy_raiseException,
59+
args = [ValueError()],
60+
ignore_errors = [ValueError],
61+
)
62+
new.start()
63+
new.join()
64+
assert len(new.errors) == 0
65+
66+
67+
68+
69+
# >>>>>>>>>> Raising Exceptions <<<<<<<<<< #
70+
def test_raises_notInitializedError():
71+
"""This test should raise ThreadNotInitializedError"""
72+
new = Thread(
73+
target = _dummy_target_raiseToPower,
74+
args = [4, 2]
75+
)
76+
77+
with pytest.raises(exceptions.ThreadNotInitializedError):
78+
new.result
79+
80+
def test_raises_stillRunningError():
81+
"""This test should raise ThreadStillRunningError"""
82+
new = Thread(
83+
target = _dummy_target_raiseToPower,
84+
args = [4, 2, 5]
85+
)
86+
new.start()
87+
88+
with pytest.raises(exceptions.ThreadStillRunningError):
89+
new.result
90+
91+
def test_raises_ignoreSpecificError():
92+
"""This test is for testing that non-specified errors are not ignored"""
93+
new = Thread(
94+
target = _dummy_raiseException,
95+
args = [FileExistsError()],
96+
ignore_errors = [ValueError],
97+
suppress_errors = False
98+
)
99+
with pytest.raises(FileExistsError):
100+
new.start()
101+
new.join()
102+
103+
def test_raises_HookError():
104+
"""This test should raise """
105+
new = Thread(
106+
target = _dummy_target_raiseToPower,
107+
args = [4, 2]
108+
)
109+
110+
def newhook(x: int):
111+
raise RuntimeError()
112+
new.add_hook(newhook)
113+
114+
with pytest.raises(exceptions.HookRuntimeError):
115+
new.start()
116+
new.join()

0 commit comments

Comments
 (0)