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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
"""
mostly functional tests of gateways.
"""
from __future__ import annotations
import time
import pytest
from execnet.gateway import Gateway
from execnet.gateway_base import Channel
needs_early_gc = pytest.mark.skipif("not hasattr(sys, 'getrefcount')")
needs_osdup = pytest.mark.skipif("not hasattr(os, 'dup')")
TESTTIMEOUT = 10.0 # seconds
class TestChannelBasicBehaviour:
def test_serialize_error(self, gw: Gateway) -> None:
ch = gw.remote_exec("channel.send(ValueError(42))")
excinfo = pytest.raises(ch.RemoteError, ch.receive)
assert "can't serialize" in str(excinfo.value)
def test_channel_close_and_then_receive_error(self, gw: Gateway) -> None:
channel = gw.remote_exec("raise ValueError")
pytest.raises(channel.RemoteError, channel.receive)
def test_channel_finish_and_then_EOFError(self, gw: Gateway) -> None:
channel = gw.remote_exec("channel.send(42)")
x = channel.receive()
assert x == 42
pytest.raises(EOFError, channel.receive)
pytest.raises(EOFError, channel.receive)
pytest.raises(EOFError, channel.receive)
def test_waitclose_timeouterror(self, gw: Gateway) -> None:
channel = gw.remote_exec("channel.receive()")
pytest.raises(channel.TimeoutError, channel.waitclose, 0.02)
channel.send(1)
channel.waitclose(timeout=TESTTIMEOUT)
def test_channel_receive_timeout(self, gw: Gateway) -> None:
channel = gw.remote_exec("channel.send(channel.receive())")
with pytest.raises(channel.TimeoutError):
channel.receive(timeout=0.2)
channel.send(1)
channel.receive(timeout=TESTTIMEOUT)
def test_channel_receive_internal_timeout(
self, gw: Gateway, monkeypatch: pytest.MonkeyPatch
) -> None:
channel = gw.remote_exec(
"""
import time
time.sleep(0.5)
channel.send(1)
"""
)
monkeypatch.setattr(channel.__class__, "_INTERNALWAKEUP", 0.2)
channel.receive()
def test_channel_close_and_then_receive_error_multiple(self, gw: Gateway) -> None:
channel = gw.remote_exec("channel.send(42) ; raise ValueError")
x = channel.receive()
assert x == 42
pytest.raises(channel.RemoteError, channel.receive)
def test_channel__local_close(self, gw: Gateway) -> None:
channel = gw._channelfactory.new()
gw._channelfactory._local_close(channel.id)
channel.waitclose(0.1)
def test_channel__local_close_error(self, gw: Gateway) -> None:
channel = gw._channelfactory.new()
gw._channelfactory._local_close(channel.id, channel.RemoteError("error"))
pytest.raises(channel.RemoteError, channel.waitclose, 0.01)
def test_channel_error_reporting(self, gw: Gateway) -> None:
channel = gw.remote_exec("def foo():\n return foobar()\nfoo()\n")
excinfo = pytest.raises(channel.RemoteError, channel.receive)
msg = str(excinfo.value)
assert msg.startswith("Traceback (most recent call last):")
assert "NameError" in msg
assert "foobar" in msg
def test_channel_syntax_error(self, gw: Gateway) -> None:
# missing colon
channel = gw.remote_exec("def foo()\n return 1\nfoo()\n")
excinfo = pytest.raises(channel.RemoteError, channel.receive)
msg = str(excinfo.value)
assert msg.startswith("Traceback (most recent call last):")
assert "SyntaxError" in msg
def test_channel_iter(self, gw: Gateway) -> None:
channel = gw.remote_exec(
"""
for x in range(3):
channel.send(x)
"""
)
l = list(channel)
assert l == [0, 1, 2]
def test_channel_pass_in_structure(self, gw: Gateway) -> None:
channel = gw.remote_exec(
"""
ch1, ch2 = channel.receive()
data = ch1.receive()
ch2.send(data+1)
"""
)
newchan1 = gw.newchannel()
newchan2 = gw.newchannel()
channel.send((newchan1, newchan2))
newchan1.send(1)
data = newchan2.receive()
assert data == 2
def test_channel_multipass(self, gw: Gateway) -> None:
channel = gw.remote_exec(
"""
channel.send(channel)
xchan = channel.receive()
assert xchan == channel
"""
)
newchan = channel.receive()
assert newchan == channel
channel.send(newchan)
channel.waitclose()
def test_channel_passing_over_channel(self, gw: Gateway) -> None:
channel = gw.remote_exec(
"""
c = channel.gateway.newchannel()
channel.send(c)
c.send(42)
"""
)
c = channel.receive()
assert isinstance(c, Channel)
x = c.receive()
assert x == 42
# check that the both sides previous channels are really gone
channel.waitclose(TESTTIMEOUT)
# assert c.id not in gw._channelfactory
newchan = gw.remote_exec(
"""
assert %d not in channel.gateway._channelfactory._channels
"""
% channel.id
)
newchan.waitclose(TESTTIMEOUT)
assert channel.id not in gw._channelfactory._channels
def test_channel_receiver_callback(self, gw: Gateway) -> None:
l: list[int] = []
# channel = gw.newchannel(receiver=l.append)
channel = gw.remote_exec(
source="""
channel.send(42)
channel.send(13)
channel.send(channel.gateway.newchannel())
"""
)
channel.setcallback(callback=l.append)
pytest.raises(IOError, channel.receive)
channel.waitclose(TESTTIMEOUT)
assert len(l) == 3
assert l[:2] == [42, 13]
assert isinstance(l[2], channel.__class__)
def test_channel_callback_after_receive(self, gw: Gateway) -> None:
l: list[int] = []
channel = gw.remote_exec(
source="""
channel.send(42)
channel.send(13)
channel.send(channel.gateway.newchannel())
"""
)
x = channel.receive()
assert x == 42
channel.setcallback(callback=l.append)
pytest.raises(IOError, channel.receive)
channel.waitclose(TESTTIMEOUT)
assert len(l) == 2
assert l[0] == 13
assert isinstance(l[1], channel.__class__)
def test_waiting_for_callbacks(self, gw: Gateway) -> None:
l = []
def callback(msg) -> None:
import time
time.sleep(0.2)
l.append(msg)
channel = gw.remote_exec(
source="""
channel.send(42)
"""
)
channel.setcallback(callback)
channel.waitclose(TESTTIMEOUT)
assert l == [42]
def test_channel_callback_stays_active(self, gw: Gateway) -> None:
self.check_channel_callback_stays_active(gw, earlyfree=True)
def check_channel_callback_stays_active(
self, gw: Gateway, earlyfree: bool = True
) -> Channel | None:
if gw.spec.execmodel == "gevent":
pytest.xfail("investigate gevent failure")
# with 'earlyfree==True', this tests the "sendonly" channel state.
l: list[int] = []
channel = gw.remote_exec(
source="""
import _thread
import time
def producer(subchannel):
for i in range(5):
time.sleep(0.15)
subchannel.send(i*100)
channel2 = channel.receive()
_thread.start_new_thread(producer, (channel2,))
del channel2
"""
)
subchannel = gw.newchannel()
subchannel.setcallback(l.append)
channel.send(subchannel)
subchan = None if earlyfree else subchannel
counter = 100
while len(l) < 5:
if subchan and subchan.isclosed():
break
counter -= 1
print(counter)
if not counter:
pytest.fail("timed out waiting for the answer[%d]" % len(l))
time.sleep(0.04) # busy-wait
assert l == [0, 100, 200, 300, 400]
return subchan
@needs_early_gc
def test_channel_callback_remote_freed(self, gw: Gateway) -> None:
channel = self.check_channel_callback_stays_active(gw, earlyfree=False)
assert channel is not None
# freed automatically at the end of producer()
channel.waitclose(TESTTIMEOUT)
def test_channel_endmarker_callback(self, gw: Gateway) -> None:
l: list[int | Channel] = []
channel = gw.remote_exec(
source="""
channel.send(42)
channel.send(13)
channel.send(channel.gateway.newchannel())
"""
)
channel.setcallback(l.append, 999)
pytest.raises(IOError, channel.receive)
channel.waitclose(TESTTIMEOUT)
assert len(l) == 4
assert l[:2] == [42, 13]
assert isinstance(l[2], channel.__class__)
assert l[3] == 999
def test_channel_endmarker_callback_error(self, gw: Gateway) -> None:
q = gw.execmodel.queue.Queue()
channel = gw.remote_exec(
source="""
raise ValueError()
"""
)
channel.setcallback(q.put, endmarker=999)
val = q.get(TESTTIMEOUT)
assert val == 999
err = channel._getremoteerror()
assert err
assert str(err).find("ValueError") != -1
def test_channel_callback_error(self, gw: Gateway) -> None:
channel = gw.remote_exec(
"""
def f(item):
raise ValueError(42)
ch = channel.gateway.newchannel()
ch.setcallback(f)
channel.send(ch)
channel.receive()
assert ch.isclosed()
"""
)
subchan = channel.receive()
assert isinstance(subchan, Channel)
subchan.send(1)
with pytest.raises(subchan.RemoteError) as excinfo:
subchan.waitclose(TESTTIMEOUT)
assert "42" in excinfo.value.formatted
channel.send(1)
channel.waitclose()
class TestChannelFile:
def test_channel_file_write(self, gw: Gateway) -> None:
channel = gw.remote_exec(
"""
f = channel.makefile()
f.write("hello world\\n")
f.close()
channel.send(42)
"""
)
first = channel.receive()
assert isinstance(first, str)
assert first.strip() == "hello world"
second = channel.receive()
assert second == 42
def test_channel_file_write_error(self, gw: Gateway) -> None:
channel = gw.remote_exec("pass")
f = channel.makefile()
assert not f.isatty()
channel.waitclose(TESTTIMEOUT)
with pytest.raises(IOError):
f.write(b"hello")
def test_channel_file_proxyclose(self, gw: Gateway) -> None:
channel = gw.remote_exec(
"""
f = channel.makefile(proxyclose=True)
f.write("hello world")
f.close()
channel.send(42)
"""
)
first = channel.receive()
assert isinstance(first, str)
assert first.strip() == "hello world"
pytest.raises(channel.RemoteError, channel.receive)
def test_channel_file_read(self, gw: Gateway) -> None:
channel = gw.remote_exec(
"""
f = channel.makefile(mode='r')
s = f.read(2)
channel.send(s)
s = f.read(5)
channel.send(s)
"""
)
channel.send("xyabcde")
s1 = channel.receive()
s2 = channel.receive()
assert s1 == "xy"
assert s2 == "abcde"
def test_channel_file_read_empty(self, gw: Gateway) -> None:
channel = gw.remote_exec("pass")
f = channel.makefile(mode="r")
s = f.read(3)
assert s == ""
s = f.read(5)
assert s == ""
def test_channel_file_readline_remote(self, gw: Gateway) -> None:
channel = gw.remote_exec(
"""
channel.send('123\\n45')
"""
)
channel.waitclose(TESTTIMEOUT)
f = channel.makefile(mode="r")
s = f.readline()
assert s == "123\n"
s = f.readline()
assert s == "45"
def test_channel_makefile_incompatmode(self, gw: Gateway) -> None:
channel = gw.newchannel()
with pytest.raises(ValueError):
channel.makefile("rw") # type: ignore[call-overload]
|