forked from QuantCrimAtLeeds/PredictCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_test.py
More file actions
180 lines (148 loc) · 6 KB
/
Copy pathpool_test.py
File metadata and controls
180 lines (148 loc) · 6 KB
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
import pytest
import unittest.mock
import tests.helpers
import pickle
import concurrent.futures
import datetime, time
import open_cp.pool as pool
class OurTask(pool.Task):
def __init__(self, key, wait=0, ex=None):
super().__init__(key)
self._wait = wait
self._ex = ex
def __call__(self):
if self._wait > 0:
time.sleep(self._wait)
if self._ex is not None:
raise self._ex
return "ahgsga"
def test_runs():
with pool.PoolExecutor() as executor:
futures = [executor.submit(OurTask("absvs"))]
for key, result in pool.yield_task_results(futures):
assert key == "absvs"
assert result == "ahgsga"
def test_runs_timeout():
with pool.PoolExecutor() as executor:
now = datetime.datetime.now()
futures = [executor.submit(OurTask("ab", wait=0.5))]
for key, result in pool.yield_task_results(futures):
assert key == "ab"
assert result == "ahgsga"
assert datetime.datetime.now() - now >= datetime.timedelta(seconds=0.5)
def test_runs_exception():
with pool.PoolExecutor() as executor:
futures = [executor.submit(OurTask("ab", ex=RuntimeError()))]
with pytest.raises(RuntimeError):
for key, result in pool.yield_task_results(futures):
pass
def test_runs_exception_get_exceptions():
with pool.PoolExecutor() as executor:
future = executor.submit(OurTask("ab", ex=RuntimeWarning()))
assert isinstance(future.exception(), RuntimeWarning)
def test_catches_internal_error():
class LambdaTask(pool.Task):
def __init__(self, task):
super().__init__("key")
self._task = task
def __call__(self):
return self._task()
with pool.PoolExecutor() as executor:
task = LambdaTask(lambda : 5)
future = executor.submit(task)
with pytest.raises(AttributeError):
future.result(timeout=1)
@pytest.fixture
def mockPPE():
with unittest.mock.patch("open_cp.pool._ProcessPoolExecutor") as mockPPEClass:
mockPPEClass.return_value = unittest.mock.MagicMock()
fut = concurrent.futures.Future()
fut.set_result(result=("test key", "test result"))
mockPPEClass.return_value.submit.return_value = fut
yield mockPPEClass.return_value
def test_PoolExecutor(mockPPE):
with pool.PoolExecutor() as executor:
fut = executor.submit(OurTask("absvs"))
func = mockPPE.submit.call_args[0][0]
assert(func() == ("absvs", "ahgsga"))
mockPPE.shutdown.assert_called_once_with(False)
def test_PoolExecutor_cantSubmitBeforeEntered(mockPPE):
executor = pool.PoolExecutor()
with pytest.raises(RuntimeError):
executor.submit(OurTask(1))
def test_yield_task_results():
fut = concurrent.futures.Future()
fut.set_result(1)
fut1 = concurrent.futures.Future()
fut1.set_result(10)
assert list(pool.yield_task_results([fut, fut1])) == [1, 10]
def test_check_finished():
fut = concurrent.futures.Future()
fut.set_running_or_notify_cancel()
results, futures = pool.check_finished([fut])
assert len(results) == 0
assert futures == [fut]
def test_yield_task_results_timeout():
fut = concurrent.futures.Future()
fut.set_running_or_notify_cancel()
with pytest.raises(TimeoutError):
for x in pool.yield_task_results([fut], 0.1):
raise AssertionError()
@unittest.mock.patch("concurrent.futures.as_completed")
def test_RestorableExecutor(mock, mockPPE):
executor = pool.RestorableExecutor("")
with executor:
fut = executor.submit(OurTask("absvs"))
mockPPE.shutdown.assert_called_once_with(False)
func = mockPPE.submit.call_args[0][0]
assert(func() == ("absvs", "ahgsga"))
class ResultsTest():
def __iter__(self):
yield ("ytreqe", "adgas")
yield ("111", "22")
@unittest.mock.patch("open_cp.pool.yield_task_results")
def test_RestorableExecutor_getResults(mock, mockPPE):
mock.return_value = ResultsTest()
executor = pool.RestorableExecutor("")
with executor:
fut = executor.submit(OurTask("absvs"))
assert(executor.results == {"111": "22", "ytreqe": "adgas"})
@unittest.mock.patch("open_cp.pool.yield_task_results")
def test_RestorableExecutor_cannotGetResultInContext(mock, mockPPE):
executor = pool.RestorableExecutor("")
with executor:
fut = executor.submit(OurTask("absvs"))
with pytest.raises(RuntimeError):
executor.results
def test_RestorableExecutor_cannotSubmitBeforeEntering(mockPPE):
executor = pool.RestorableExecutor("")
with pytest.raises(RuntimeError):
executor.submit(OurTask(1))
@unittest.mock.patch("open_cp.pool.yield_task_results")
def test_RestorableExecutor_dontRecompute(mock, mockPPE):
existing = {"absvs": 5}
with unittest.mock.patch("builtins.open", tests.helpers.MockOpen(pickle.dumps(existing))):
executor = pool.RestorableExecutor("")
with executor:
fut = executor.submit(OurTask("absvs"))
fut = executor.submit(OurTask("abstt"))
func = mockPPE.submit.call_args[0][0]
assert(func() == ("abstt", "ahgsga"))
assert( len(mockPPE.submit.call_args_list) == 1 )
assert( executor.results == existing )
@unittest.mock.patch("open_cp.pool.yield_task_results")
def test_RestorableExecutor_savePartialResult(mock, mockPPE):
file = tests.helpers.BytesIOWrapper()
class YieldOnce():
def __iter__(self):
yield ("absa", 123)
raise KeyboardInterrupt()
with pytest.raises(KeyboardInterrupt):
with unittest.mock.patch("builtins.open", tests.helpers.MockOpen(file)) as open_mock:
open_mock.filter = tests.helpers.ExactlyTheseFilter([2])
mock.return_value = YieldOnce()
executor = pool.RestorableExecutor("")
with executor:
fut = executor.submit(OurTask("absvs"))
got = pickle.loads(file.data)
assert(got == {"absa": 123})