2023-01-27 21:40:28 +00:00
|
|
|
'''
|
|
|
|
Sketchy network blackoutz, ugly byzantine gens, puedes eschuchar la
|
|
|
|
cancelacion?..
|
|
|
|
|
|
|
|
'''
|
2023-01-28 03:59:15 +00:00
|
|
|
from functools import partial
|
|
|
|
|
2023-01-27 21:40:28 +00:00
|
|
|
import pytest
|
|
|
|
from _pytest.pathlib import import_path
|
|
|
|
import trio
|
|
|
|
|
|
|
|
from conftest import (
|
|
|
|
examples_dir,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'debug_mode',
|
|
|
|
[False, True],
|
2023-01-28 03:59:15 +00:00
|
|
|
ids=['no_debug_mode', 'debug_mode'],
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'ipc_break',
|
|
|
|
[
|
2023-01-28 21:44:35 +00:00
|
|
|
# no breaks
|
2023-01-28 03:59:15 +00:00
|
|
|
{
|
2023-01-28 21:44:35 +00:00
|
|
|
'break_parent_ipc_after': False,
|
|
|
|
'break_child_ipc_after': False,
|
2023-01-28 03:59:15 +00:00
|
|
|
},
|
2023-01-28 21:44:35 +00:00
|
|
|
|
|
|
|
# only parent breaks
|
|
|
|
{
|
|
|
|
'break_parent_ipc_after': 500,
|
|
|
|
'break_child_ipc_after': False,
|
|
|
|
},
|
|
|
|
|
|
|
|
# only child breaks
|
|
|
|
{
|
|
|
|
'break_parent_ipc_after': False,
|
|
|
|
'break_child_ipc_after': 500,
|
|
|
|
},
|
|
|
|
|
|
|
|
# both: break parent first
|
|
|
|
{
|
|
|
|
'break_parent_ipc_after': 500,
|
|
|
|
'break_child_ipc_after': 800,
|
|
|
|
},
|
|
|
|
# both: break child first
|
|
|
|
{
|
|
|
|
'break_parent_ipc_after': 800,
|
|
|
|
'break_child_ipc_after': 500,
|
|
|
|
},
|
|
|
|
|
2023-01-28 03:59:15 +00:00
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
'no_break',
|
|
|
|
'break_parent',
|
|
|
|
'break_child',
|
2023-01-28 21:44:35 +00:00
|
|
|
'break_both_parent_first',
|
|
|
|
'break_both_child_first',
|
2023-01-28 03:59:15 +00:00
|
|
|
],
|
2023-01-27 21:40:28 +00:00
|
|
|
)
|
2023-01-28 21:44:35 +00:00
|
|
|
def test_ipc_channel_break_during_stream(
|
2023-01-27 21:40:28 +00:00
|
|
|
debug_mode: bool,
|
2023-01-27 22:02:36 +00:00
|
|
|
spawn_backend: str,
|
2023-01-28 03:59:15 +00:00
|
|
|
ipc_break: dict | None,
|
2023-01-27 21:40:28 +00:00
|
|
|
):
|
|
|
|
'''
|
2023-01-28 21:44:35 +00:00
|
|
|
Ensure we can have an IPC channel break its connection during
|
|
|
|
streaming and it's still possible for the (simulated) user to kill
|
|
|
|
the actor tree using SIGINT.
|
|
|
|
|
|
|
|
We also verify the type of connection error expected in the parent
|
|
|
|
depending on which side if the IPC breaks first.
|
2023-01-27 21:40:28 +00:00
|
|
|
|
|
|
|
'''
|
2023-01-27 22:02:36 +00:00
|
|
|
if spawn_backend != 'trio':
|
|
|
|
if debug_mode:
|
|
|
|
pytest.skip('`debug_mode` only supported on `trio` spawner')
|
|
|
|
|
2023-01-28 03:59:15 +00:00
|
|
|
# non-`trio` spawners should never hit the hang condition that
|
|
|
|
# requires the user to do ctl-c to cancel the actor tree.
|
2023-01-27 22:02:36 +00:00
|
|
|
expect_final_exc = trio.ClosedResourceError
|
|
|
|
|
2023-01-27 21:40:28 +00:00
|
|
|
mod = import_path(
|
|
|
|
examples_dir() / 'advanced_faults' / 'ipc_failure_during_stream.py',
|
|
|
|
root=examples_dir(),
|
|
|
|
)
|
|
|
|
|
2023-01-28 03:59:15 +00:00
|
|
|
expect_final_exc = KeyboardInterrupt
|
|
|
|
|
|
|
|
# when ONLY the child breaks we expect the parent to get a closed
|
|
|
|
# resource error on the next `MsgStream.receive()` and then fail out
|
|
|
|
# and cancel the child from there.
|
2023-01-28 21:44:35 +00:00
|
|
|
if (
|
|
|
|
|
|
|
|
# only child breaks
|
|
|
|
(
|
|
|
|
ipc_break['break_child_ipc_after']
|
|
|
|
and ipc_break['break_parent_ipc_after'] is False
|
|
|
|
)
|
|
|
|
|
|
|
|
# both break but, parent breaks first
|
|
|
|
or (
|
|
|
|
ipc_break['break_child_ipc_after'] is not False
|
|
|
|
and (
|
|
|
|
ipc_break['break_parent_ipc_after']
|
|
|
|
> ipc_break['break_child_ipc_after']
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
):
|
2023-01-28 03:59:15 +00:00
|
|
|
expect_final_exc = trio.ClosedResourceError
|
|
|
|
|
2023-01-28 21:44:35 +00:00
|
|
|
# when the parent IPC side dies (even if the child's does as well
|
|
|
|
# but the child fails BEFORE the parent) we expect the channel to be
|
|
|
|
# sent a stop msg from the child at some point which will signal the
|
|
|
|
# parent that the stream has been terminated.
|
|
|
|
# NOTE: when the parent breaks "after" the child you get this same
|
|
|
|
# case as well, the child breaks the IPC channel with a stop msg
|
|
|
|
# before any closure takes place.
|
|
|
|
elif (
|
|
|
|
# only parent breaks
|
|
|
|
(
|
|
|
|
ipc_break['break_parent_ipc_after']
|
|
|
|
and ipc_break['break_child_ipc_after'] is False
|
|
|
|
)
|
|
|
|
|
|
|
|
# both break but, child breaks first
|
|
|
|
or (
|
|
|
|
ipc_break['break_parent_ipc_after'] is not False
|
|
|
|
and (
|
|
|
|
ipc_break['break_child_ipc_after']
|
|
|
|
> ipc_break['break_parent_ipc_after']
|
|
|
|
)
|
|
|
|
)
|
|
|
|
):
|
2023-01-28 03:59:15 +00:00
|
|
|
expect_final_exc = trio.EndOfChannel
|
|
|
|
|
2023-01-27 22:02:36 +00:00
|
|
|
with pytest.raises(expect_final_exc):
|
2023-01-27 21:40:28 +00:00
|
|
|
trio.run(
|
2023-01-28 03:59:15 +00:00
|
|
|
partial(
|
|
|
|
mod.main,
|
|
|
|
debug_mode=debug_mode,
|
|
|
|
start_method=spawn_backend,
|
|
|
|
**ipc_break,
|
|
|
|
)
|
2023-01-27 21:40:28 +00:00
|
|
|
)
|