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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
"""
tests for multi channels and gateway Groups
"""
from __future__ import annotations
import gc
from time import sleep
from typing import Callable
import execnet
import pytest
from execnet import XSpec
from execnet.gateway import Gateway
from execnet.gateway_base import Channel
from execnet.gateway_base import ExecModel
from execnet.multi import Group
from execnet.multi import safe_terminate
class TestMultiChannelAndGateway:
def test_multichannel_container_basics(
self, gw: Gateway, execmodel: ExecModel
) -> None:
mch = execnet.MultiChannel([Channel(gw, i) for i in range(3)])
assert len(mch) == 3
channels = list(mch)
assert len(channels) == 3
# ordering
for i in range(3):
assert channels[i].id == i
assert channels[i] == mch[i]
assert channels[0] in mch
assert channels[1] in mch
assert channels[2] in mch
def test_multichannel_receive_each(self) -> None:
class pseudochannel:
def receive(self) -> object:
return 12
pc1 = pseudochannel()
pc2 = pseudochannel()
multichannel = execnet.MultiChannel([pc1, pc2]) # type: ignore[list-item]
l = multichannel.receive_each(withchannel=True)
assert len(l) == 2
assert l == [(pc1, 12), (pc2, 12)] # type: ignore[comparison-overlap]
l2 = multichannel.receive_each(withchannel=False)
assert l2 == [12, 12]
def test_multichannel_send_each(self) -> None:
gm = execnet.Group(["popen"] * 2)
mc = gm.remote_exec(
"""
import os
channel.send(channel.receive() + 1)
"""
)
mc.send_each(41)
l = mc.receive_each()
assert l == [42, 42]
def test_Group_execmodel_setting(self) -> None:
gm = execnet.Group()
gm.set_execmodel("thread")
assert gm.execmodel.backend == "thread"
assert gm.remote_execmodel.backend == "thread"
gm._gateways.append(1) # type: ignore[arg-type]
try:
with pytest.raises(ValueError):
gm.set_execmodel("eventlet")
assert gm.execmodel.backend == "thread"
finally:
gm._gateways.pop()
def test_multichannel_receive_queue_for_two_subprocesses(self) -> None:
gm = execnet.Group(["popen"] * 2)
mc = gm.remote_exec(
"""
import os
channel.send(os.getpid())
"""
)
queue = mc.make_receive_queue()
ch, item = queue.get(timeout=10)
ch2, item2 = queue.get(timeout=10)
assert ch != ch2
assert ch.gateway != ch2.gateway
assert item != item2
mc.waitclose()
def test_multichannel_waitclose(self) -> None:
l = []
class pseudochannel:
def waitclose(self) -> None:
l.append(0)
multichannel = execnet.MultiChannel([pseudochannel(), pseudochannel()]) # type: ignore[list-item]
multichannel.waitclose()
assert len(l) == 2
class TestGroup:
def test_basic_group(self, monkeypatch: pytest.MonkeyPatch) -> None:
import atexit
atexitlist: list[Callable[[], object]] = []
monkeypatch.setattr(atexit, "register", atexitlist.append)
group = Group()
assert atexitlist == [group._cleanup_atexit]
exitlist = []
joinlist = []
class PseudoIO:
def wait(self) -> None:
pass
class PseudoSpec:
via = None
class PseudoGW:
id = "9999"
_io = PseudoIO()
spec = PseudoSpec()
def exit(self) -> None:
exitlist.append(self)
group._unregister(self) # type: ignore[arg-type]
def join(self) -> None:
joinlist.append(self)
gw = PseudoGW()
group._register(gw) # type: ignore[arg-type]
assert len(exitlist) == 0
assert len(joinlist) == 0
group._cleanup_atexit()
assert len(exitlist) == 1
assert exitlist == [gw]
assert len(joinlist) == 1
assert joinlist == [gw]
group._cleanup_atexit()
assert len(exitlist) == 1
assert len(joinlist) == 1
def test_group_default_spec(self) -> None:
group = Group()
group.defaultspec = "not-existing-type"
pytest.raises(ValueError, group.makegateway)
def test_group_PopenGateway(self) -> None:
group = Group()
gw = group.makegateway("popen")
assert list(group) == [gw]
assert group[0] == gw
assert len(group) == 1
group._cleanup_atexit()
assert not group._gateways
def test_group_ordering_and_termination(self) -> None:
group = Group()
group.makegateway("popen//id=3")
group.makegateway("popen//id=2")
group.makegateway("popen//id=5")
gwlist = list(group)
assert len(gwlist) == 3
idlist = [x.id for x in gwlist]
assert idlist == list("325")
print(group)
group.terminate()
print(group)
assert not group
assert repr(group) == "<Group []>"
def test_group_id_allocation(self) -> None:
group = Group()
specs = [XSpec("popen"), XSpec("popen//id=hello")]
group.allocate_id(specs[0])
group.allocate_id(specs[1])
gw = group.makegateway(specs[1])
assert gw.id == "hello"
gw = group.makegateway(specs[0])
assert gw.id == "gw0"
# pytest.raises(ValueError,
# group.allocate_id, XSpec("popen//id=hello"))
group.terminate()
def test_gateway_and_id(self) -> None:
group = Group()
gw = group.makegateway("popen//id=hello")
assert group["hello"] == gw
with pytest.raises((TypeError, AttributeError)):
del group["hello"] # type: ignore[attr-defined]
with pytest.raises((TypeError, AttributeError)):
group["hello"] = 5 # type: ignore[index]
assert "hello" in group
assert gw in group
assert len(group) == 1
gw.exit()
assert "hello" not in group
with pytest.raises(KeyError):
_ = group["hello"]
def test_default_group(self) -> None:
oldlist = list(execnet.default_group)
gw = execnet.makegateway("popen")
try:
newlist = list(execnet.default_group)
assert len(newlist) == len(oldlist) + 1
assert gw in newlist
assert gw not in oldlist
finally:
gw.exit()
def test_remote_exec_args(self) -> None:
group = Group()
group.makegateway("popen")
def fun(channel, arg) -> None:
channel.send(arg)
mch = group.remote_exec(fun, arg=1)
result = mch.receive_each()
assert result == [1]
def test_terminate_with_proxying(self) -> None:
group = Group()
group.makegateway("popen//id=master")
group.makegateway("popen//via=master//id=worker")
group.terminate(1.0)
@pytest.mark.xfail(reason="active_count() has been broken for some time")
def test_safe_terminate(execmodel: ExecModel) -> None:
if execmodel.backend not in ("thread", "main_thread_only"):
pytest.xfail(
"execution model %r does not support task count" % execmodel.backend
)
import threading
active = threading.active_count()
l = []
def term() -> None:
sleep(3)
def kill() -> None:
l.append(1)
safe_terminate(execmodel, 1, [(term, kill)] * 10)
assert len(l) == 10
sleep(0.1)
gc.collect()
assert execmodel.active_count() == active # type: ignore[attr-defined]
@pytest.mark.xfail(reason="active_count() has been broken for some time")
def test_safe_terminate2(execmodel: ExecModel) -> None:
if execmodel.backend not in ("thread", "main_thread_only"):
pytest.xfail(
"execution model %r does not support task count" % execmodel.backend
)
import threading
active = threading.active_count()
l = []
def term() -> None:
return
def kill() -> None:
l.append(1)
safe_terminate(execmodel, 3, [(term, kill)] * 10)
assert len(l) == 0
sleep(0.1)
gc.collect()
assert threading.active_count() == active
|