| 1 |
import os |
| 2 |
from pathlib import Path |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from execnet.gateway_base import ExecModel |
| 7 |
from execnet.gateway_base import WorkerPool |
| 8 |
|
| 9 |
|
| 10 |
def test_execmodel(execmodel: ExecModel, tmp_path: Path) -> None: |
| 11 |
assert execmodel.backend |
| 12 |
p = tmp_path / "somefile" |
| 13 |
p.write_text("content") |
| 14 |
fd = os.open(p, os.O_RDONLY) |
| 15 |
f = execmodel.fdopen(fd, "r") |
| 16 |
assert f.read() == "content" |
| 17 |
f.close() |
| 18 |
|
| 19 |
|
| 20 |
def test_execmodel_basic_attrs(execmodel: ExecModel) -> None: |
| 21 |
m = execmodel |
| 22 |
assert callable(m.start) |
| 23 |
assert m.get_ident() |
| 24 |
|
| 25 |
|
| 26 |
def test_simple(pool: WorkerPool) -> None: |
| 27 |
reply = pool.spawn(lambda: 42) |
| 28 |
assert reply.get() == 42 |
| 29 |
|
| 30 |
|
| 31 |
def test_some(pool: WorkerPool, execmodel: ExecModel) -> None: |
| 32 |
q = execmodel.queue.Queue() |
| 33 |
num = 4 |
| 34 |
|
| 35 |
def f(i: int) -> None: |
| 36 |
q.put(i) |
| 37 |
while q.qsize(): |
| 38 |
execmodel.sleep(0.01) |
| 39 |
|
| 40 |
for i in range(num): |
| 41 |
pool.spawn(f, i) |
| 42 |
for i in range(num): |
| 43 |
q.get() |
| 44 |
|
| 45 |
assert pool.waitall(timeout=1.0) |
| 46 |
|
| 47 |
assert len(pool._running) == 0 |
| 48 |
|
| 49 |
|
| 50 |
def test_running_semnatics(pool: WorkerPool, execmodel: ExecModel) -> None: |
| 51 |
q = execmodel.queue.Queue() |
| 52 |
|
| 53 |
def first() -> None: |
| 54 |
q.get() |
| 55 |
|
| 56 |
reply = pool.spawn(first) |
| 57 |
assert reply.running |
| 58 |
assert pool.active_count() == 1 |
| 59 |
q.put(1) |
| 60 |
assert pool.waitall() |
| 61 |
assert pool.active_count() == 0 |
| 62 |
assert not reply.running |
| 63 |
|
| 64 |
|
| 65 |
def test_waitfinish_on_reply(pool: WorkerPool) -> None: |
| 66 |
l = [] |
| 67 |
reply = pool.spawn(lambda: l.append(1)) |
| 68 |
reply.waitfinish() |
| 69 |
assert l == [1] |
| 70 |
reply = pool.spawn(lambda: 0 / 0) |
| 71 |
reply.waitfinish() |
| 72 |
pytest.raises(ZeroDivisionError, reply.get) |
| 73 |
|
| 74 |
|
| 75 |
@pytest.mark.xfail(reason="WorkerPool does not implement limited size") |
| 76 |
def test_limited_size(execmodel: ExecModel) -> None: |
| 77 |
pool = WorkerPool(execmodel, size=1) |
| 78 |
q = execmodel.queue.Queue() |
| 79 |
q2 = execmodel.queue.Queue() |
| 80 |
q3 = execmodel.queue.Queue() |
| 81 |
|
| 82 |
def first() -> None: |
| 83 |
q.put(1) |
| 84 |
q2.get() |
| 85 |
|
| 86 |
pool.spawn(first) |
| 87 |
assert q.get() == 1 |
| 88 |
|
| 89 |
def second() -> None: |
| 90 |
q3.put(3) |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
pool2 = WorkerPool(execmodel) |
| 95 |
pool2.spawn(pool.spawn, second) |
| 96 |
assert not pool2.waitall(1.0) |
| 97 |
assert q3.qsize() == 0 |
| 98 |
q2.put(2) |
| 99 |
assert pool2.waitall() |
| 100 |
assert pool.waitall() |
| 101 |
|
| 102 |
|
| 103 |
def test_get(pool: WorkerPool) -> None: |
| 104 |
def f() -> int: |
| 105 |
return 42 |
| 106 |
|
| 107 |
reply = pool.spawn(f) |
| 108 |
result = reply.get() |
| 109 |
assert result == 42 |
| 110 |
|
| 111 |
|
| 112 |
def test_get_timeout(execmodel: ExecModel, pool: WorkerPool) -> None: |
| 113 |
def f() -> int: |
| 114 |
execmodel.sleep(0.2) |
| 115 |
return 42 |
| 116 |
|
| 117 |
reply = pool.spawn(f) |
| 118 |
with pytest.raises(IOError): |
| 119 |
reply.get(timeout=0.01) |
| 120 |
|
| 121 |
|
| 122 |
def test_get_excinfo(pool: WorkerPool) -> None: |
| 123 |
def f() -> None: |
| 124 |
raise ValueError("42") |
| 125 |
|
| 126 |
reply = pool.spawn(f) |
| 127 |
with pytest.raises(ValueError): |
| 128 |
reply.get(1.0) |
| 129 |
with pytest.raises(ValueError): |
| 130 |
reply.get(1.0) |
| 131 |
|
| 132 |
|
| 133 |
def test_waitall_timeout(pool: WorkerPool, execmodel: ExecModel) -> None: |
| 134 |
q = execmodel.queue.Queue() |
| 135 |
|
| 136 |
def f() -> None: |
| 137 |
q.get() |
| 138 |
|
| 139 |
reply = pool.spawn(f) |
| 140 |
assert not pool.waitall(0.01) |
| 141 |
q.put(None) |
| 142 |
reply.get(timeout=1.0) |
| 143 |
assert pool.waitall(timeout=0.1) |
| 144 |
|
| 145 |
|
| 146 |
@pytest.mark.skipif(not hasattr(os, "dup"), reason="no os.dup") |
| 147 |
def test_pool_clean_shutdown( |
| 148 |
pool: WorkerPool, capfd: pytest.CaptureFixture[str] |
| 149 |
) -> None: |
| 150 |
q = pool.execmodel.queue.Queue() |
| 151 |
|
| 152 |
def f() -> None: |
| 153 |
q.get() |
| 154 |
|
| 155 |
pool.spawn(f) |
| 156 |
assert not pool.waitall(timeout=1.0) |
| 157 |
pool.trigger_shutdown() |
| 158 |
with pytest.raises(ValueError): |
| 159 |
pool.spawn(f) |
| 160 |
|
| 161 |
def wait_then_put() -> None: |
| 162 |
pool.execmodel.sleep(0.1) |
| 163 |
q.put(1) |
| 164 |
|
| 165 |
pool.execmodel.start(wait_then_put) |
| 166 |
assert pool.waitall() |
| 167 |
_out, err = capfd.readouterr() |
| 168 |
assert err == "" |
| 169 |
|
| 170 |
|
| 171 |
def test_primary_thread_integration(execmodel: ExecModel) -> None: |
| 172 |
if execmodel.backend not in ("thread", "main_thread_only"): |
| 173 |
with pytest.raises(ValueError): |
| 174 |
WorkerPool(execmodel=execmodel, hasprimary=True) |
| 175 |
return |
| 176 |
pool = WorkerPool(execmodel=execmodel, hasprimary=True) |
| 177 |
queue = execmodel.queue.Queue() |
| 178 |
|
| 179 |
def do_integrate() -> None: |
| 180 |
queue.put(execmodel.get_ident()) |
| 181 |
pool.integrate_as_primary_thread() |
| 182 |
|
| 183 |
execmodel.start(do_integrate) |
| 184 |
|
| 185 |
def func() -> None: |
| 186 |
queue.put(execmodel.get_ident()) |
| 187 |
|
| 188 |
pool.spawn(func) |
| 189 |
ident1 = queue.get() |
| 190 |
ident2 = queue.get() |
| 191 |
assert ident1 == ident2 |
| 192 |
pool.terminate() |
| 193 |
|
| 194 |
|
| 195 |
def test_primary_thread_integration_shutdown(execmodel: ExecModel) -> None: |
| 196 |
if execmodel.backend not in ("thread", "main_thread_only"): |
| 197 |
pytest.skip("can only run with threading") |
| 198 |
pool = WorkerPool(execmodel=execmodel, hasprimary=True) |
| 199 |
queue = execmodel.queue.Queue() |
| 200 |
|
| 201 |
def do_integrate() -> None: |
| 202 |
queue.put(execmodel.get_ident()) |
| 203 |
pool.integrate_as_primary_thread() |
| 204 |
|
| 205 |
execmodel.start(do_integrate) |
| 206 |
queue.get() |
| 207 |
|
| 208 |
queue2 = execmodel.queue.Queue() |
| 209 |
|
| 210 |
def get_two() -> None: |
| 211 |
queue.put(execmodel.get_ident()) |
| 212 |
queue2.get() |
| 213 |
|
| 214 |
reply = pool.spawn(get_two) |
| 215 |
|
| 216 |
queue.get() |
| 217 |
|
| 218 |
pool.trigger_shutdown() |
| 219 |
|
| 220 |
queue2.put(1) |
| 221 |
reply.get() |
| 222 |
assert pool.waitall(5.0) |