Skip to content

Commit 9a9f55d

Browse files
Add tests for 'run_in_thread'
1 parent 138622d commit 9a9f55d

5 files changed

Lines changed: 257 additions & 82 deletions

File tree

test/__init__.py

Whitespace-only changes.
Lines changed: 56 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# MIT License
22
#
3-
# Copyright (c) 2019 Sam McCormack
3+
# Copyright (c) 2020 Sam McCormack
44
#
55
# Permission is hereby granted, free of charge, to any person obtaining a copy
66
# of this software and associated documentation files (the "Software"), to deal
@@ -19,56 +19,37 @@
1919
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
22+
2223
import asyncio
2324
from multiprocessing import Process, Queue
2425
from typing import List, Tuple
2526

2627
import multiprocess
2728

28-
from scheduler.Scheduler import Scheduler
29-
from scheduler.Task import Task
30-
31-
try:
32-
from test.utils import (
33-
_long_task,
34-
_get_input_output,
35-
_get_input_output_numpy,
36-
_get_input_output_single_result,
37-
_get_input_output_two_results,
38-
assert_results,
39-
assert_results_numpy,
40-
_func,
41-
_funcq,
42-
_func_no_params,
43-
_func_numpy,
44-
_func_no_return,
45-
_func_returns_single_value,
46-
_func_returns_two_values,
47-
)
48-
except:
49-
from utils import (
50-
_long_task,
51-
_get_input_output,
52-
_get_input_output_numpy,
53-
_get_input_output_single_result,
54-
_get_input_output_two_results,
55-
assert_results,
56-
assert_results_numpy,
57-
_func,
58-
_funcq,
59-
_func_no_params,
60-
_func_numpy,
61-
_func_no_return,
62-
_func_returns_single_value,
63-
_func_returns_two_values,
64-
)
65-
66-
67-
def test_shared_memory_numpy():
29+
from scheduler.ProcessTask import ProcessTask
30+
from test.utils import (
31+
_get_input_output_numpy,
32+
_func_numpy,
33+
assert_results_numpy,
34+
_get_input_output,
35+
_funcq,
36+
assert_results,
37+
_func,
38+
_func_no_params,
39+
_get_input_output_single_result,
40+
_func_returns_single_value,
41+
_get_input_output_two_results,
42+
_func_returns_two_values,
43+
_func_no_return,
44+
_long_task,
45+
)
46+
47+
48+
def test_shared_memory_numpy(scheduler):
6849
"""
6950
Tests whether shared memory works correctly.
7051
"""
71-
scheduler = Scheduler(shared_memory=True, shared_memory_threshold=0)
52+
# scheduler = Scheduler(shared_memory=True, shared_memory_threshold=0)
7253

7354
args, expected = _get_input_output_numpy()
7455
for a in args:
@@ -81,9 +62,9 @@ def test_shared_memory_numpy():
8162
assert scheduler.finished
8263

8364

84-
def test_add_process():
65+
def test_add_process(scheduler):
8566
"""Tests whether tasks are added to the scheduler correctly with `add_process()`."""
86-
scheduler = Scheduler()
67+
# scheduler = Scheduler()
8768

8869
args, expected = _get_input_output()
8970
for a in args:
@@ -98,9 +79,9 @@ def test_add_process():
9879
assert scheduler.finished
9980

10081

101-
def test_add():
82+
def test_add(scheduler):
10283
"""Tests whether `add()` works correctly."""
103-
scheduler = Scheduler()
84+
# scheduler = Scheduler()
10485

10586
args, expected = _get_input_output()
10687
for a in args:
@@ -112,9 +93,9 @@ def test_add():
11293
assert scheduler.finished
11394

11495

115-
def test_run_no_params():
96+
def test_run_no_params(scheduler):
11697
"""Tests whether `run()` works correctly on a function with no parameters."""
117-
scheduler = Scheduler()
98+
# scheduler = Scheduler()
11899

119100
expected = _func_no_params()
120101
for a in range(50):
@@ -129,9 +110,9 @@ def test_run_no_params():
129110
assert scheduler.finished
130111

131112

132-
def test_multiprocess():
113+
def test_multiprocess(scheduler):
133114
"""Tests whether `add()` and `run_blocking()` work correctly with `multiprocess` instead of `multiprocessing`."""
134-
scheduler = Scheduler()
115+
# scheduler = Scheduler()
135116

136117
args, expected = _get_input_output()
137118
for a in args:
@@ -148,9 +129,9 @@ def test_multiprocess():
148129
assert scheduler.finished
149130

150131

151-
def test_run_blocking():
132+
def test_run_blocking(scheduler):
152133
"""Tests whether `run_blocking()` works correctly."""
153-
scheduler = Scheduler()
134+
# scheduler = Scheduler()
154135

155136
args, expected = _get_input_output()
156137
for a in args:
@@ -164,9 +145,9 @@ def test_run_blocking():
164145
assert scheduler.finished
165146

166147

167-
def test_run():
148+
def test_run(scheduler):
168149
"""Tests whether `run()` works correctly."""
169-
scheduler = Scheduler()
150+
# scheduler = Scheduler()
170151

171152
args, expected = _get_input_output()
172153
for a in args:
@@ -180,9 +161,9 @@ def test_run():
180161
assert scheduler.finished
181162

182163

183-
def test_map():
164+
def test_map(scheduler):
184165
"""Tests whether `map()` works correctly."""
185-
scheduler = Scheduler()
166+
# scheduler = Scheduler()
186167

187168
args, expected = _get_input_output()
188169

@@ -195,9 +176,9 @@ def test_map():
195176
assert scheduler.finished
196177

197178

198-
def test_map_one_return_value():
179+
def test_map_one_return_value(scheduler):
199180
"""Tests whether `map()` works correctly when the target function only returns a single result."""
200-
scheduler = Scheduler()
181+
# scheduler = Scheduler()
201182

202183
args, expected = _get_input_output_single_result()
203184

@@ -212,9 +193,9 @@ def test_map_one_return_value():
212193
assert scheduler.finished
213194

214195

215-
def test_map_two_return_values():
196+
def test_map_two_return_values(scheduler):
216197
"""Tests whether `map()` works correctly when the target function returns two results."""
217-
scheduler = Scheduler()
198+
# scheduler = Scheduler()
218199

219200
args, expected = _get_input_output_two_results()
220201

@@ -228,11 +209,11 @@ def test_map_two_return_values():
228209
assert scheduler.finished
229210

230211

231-
def test_map_no_args():
212+
def test_map_no_args(scheduler):
232213
"""
233214
Tests whether `map()` works correctly with no arguments.
234215
"""
235-
scheduler = Scheduler()
216+
# scheduler = Scheduler()
236217

237218
loop = asyncio.get_event_loop()
238219
results: List = loop.run_until_complete(scheduler.map(target=_func_no_params))
@@ -243,12 +224,12 @@ def test_map_no_args():
243224
assert scheduler.finished
244225

245226

246-
def test_map_no_return():
227+
def test_map_no_return(scheduler):
247228
"""
248-
Tests whether 'map()' works correctly when the target function
229+
Tests whether 'map()' works correctly when the target function
249230
does not return any values.
250231
"""
251-
scheduler = Scheduler()
232+
# scheduler = Scheduler()
252233

253234
loop = asyncio.get_event_loop()
254235
results = loop.run_until_complete(scheduler.map(target=_func_no_return))
@@ -257,13 +238,12 @@ def test_map_no_return():
257238
assert scheduler.finished
258239

259240

260-
def test_map_blocking():
241+
def test_map_blocking(scheduler):
261242
"""Tests whether `map_blocking()` works correctly."""
262-
scheduler = Scheduler()
243+
# scheduler = Scheduler()
263244

264245
args, expected = _get_input_output()
265246

266-
loop = asyncio.get_event_loop()
267247
results: List[Tuple[int, int, int]] = scheduler.map_blocking(
268248
target=_func, args=args
269249
)
@@ -272,16 +252,16 @@ def test_map_blocking():
272252
assert scheduler.finished
273253

274254

275-
def test_add_task():
255+
def test_add_task(scheduler):
276256
"""Tests whether tasks are added to the scheduler correctly with `add_task()`."""
277-
scheduler = Scheduler()
257+
# scheduler = Scheduler()
278258

279259
args, expected = _get_input_output()
280260
for a in args:
281261
q = Queue()
282262
p = Process(target=_funcq, args=(q,) + a)
283263

284-
task = Task(p, q)
264+
task = ProcessTask(p, q)
285265
scheduler.add_task(task)
286266

287267
results: List[Tuple[int, int, int]] = scheduler.run_blocking()
@@ -290,17 +270,17 @@ def test_add_task():
290270
assert scheduler.finished
291271

292272

293-
def test_add_tasks():
273+
def test_add_tasks(scheduler):
294274
"""Tests whether tasks are added to the scheduler correctly with `add_tasks()`."""
295-
scheduler = Scheduler()
275+
# scheduler = Scheduler()
296276
tasks = []
297277

298278
args, expected = _get_input_output()
299279
for a in args:
300280
q = Queue()
301281
p = Process(target=_funcq, args=(q,) + a)
302282

303-
task = Task(p, q)
283+
task = ProcessTask(p, q)
304284
tasks.append(task)
305285

306286
scheduler.add_tasks(*tasks)
@@ -311,9 +291,9 @@ def test_add_tasks():
311291
assert scheduler.finished
312292

313293

314-
def test_terminate():
294+
def test_terminate(scheduler):
315295
"""Tests whether the scheduler returns the correct result - an empty list - when terminated."""
316-
scheduler = Scheduler()
296+
# scheduler = Scheduler()
317297
count = 10
318298

319299
async def run_scheduler():
@@ -344,9 +324,3 @@ async def terminate():
344324

345325
assert success
346326
assert not scheduler.finished
347-
348-
349-
if __name__ == "__main__":
350-
test_shared_memory_numpy()
351-
test_map_two_return_values()
352-
print("Finished.")

test/test_defaults.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2019 Sam McCormack
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
from scheduler.Scheduler import Scheduler
23+
from test import impl
24+
25+
26+
def scheduler():
27+
return Scheduler()
28+
29+
30+
def test_add_process():
31+
impl.test_add_process(scheduler())
32+
33+
34+
def test_add():
35+
impl.test_add(scheduler())
36+
37+
38+
def test_run_no_params():
39+
impl.test_run_no_params(scheduler())
40+
41+
42+
def test_multiprocess():
43+
impl.test_multiprocess(scheduler())
44+
45+
46+
def test_run_blocking():
47+
impl.test_run_blocking(scheduler())
48+
49+
50+
def test_run():
51+
impl.test_run(scheduler())
52+
53+
54+
def test_map():
55+
impl.test_map(scheduler())
56+
57+
58+
def test_map_one_return_value():
59+
impl.test_map_one_return_value(scheduler())
60+
61+
62+
def test_map_two_return_values():
63+
impl.test_map_two_return_values(scheduler())
64+
65+
66+
def test_map_no_args():
67+
impl.test_map_no_args(scheduler())
68+
69+
70+
def test_map_no_return():
71+
impl.test_map_no_return(scheduler())
72+
73+
74+
def test_map_blocking():
75+
impl.test_map_blocking(scheduler())
76+
77+
78+
def test_add_task():
79+
impl.test_add_task(scheduler())
80+
81+
82+
def test_add_tasks():
83+
impl.test_add_tasks(scheduler())
84+
85+
86+
def test_terminate():
87+
impl.test_terminate(scheduler())

0 commit comments

Comments
 (0)