forked from goodboy/tractor
1
0
Fork 0

Update pld-rx limiting test(s) to use deco input

The tests only use one input spec (conveniently) so there's not much to
change in the logic,
- only pass the `maybe_msg_spec` to the child-side decorator and obvi
  drop the surrounding `msgops.limit_plds()` block in the child.
- tweak a few `MsgDec` asserts, mostly dropping the
  `msg._ops._def_any_spec` state checks since the child-side won't have
  any pre pld-spec state given the runtime now applies the `pld_spec`
  before running the task's func body.
  - also allowed dropping the `finally:` which did a similar check
    outside the `.limit_plds()` block.
runtime_to_msgspec
Tyler Goodlet 2024-06-17 09:24:03 -04:00
parent 04bd111037
commit affc210033
1 changed files with 99 additions and 104 deletions

View File

@ -7,9 +7,6 @@ related settings around IPC contexts.
from contextlib import ( from contextlib import (
asynccontextmanager as acm, asynccontextmanager as acm,
) )
from contextvars import (
Context,
)
from msgspec import ( from msgspec import (
Struct, Struct,
@ -19,6 +16,7 @@ import trio
import tractor import tractor
from tractor import ( from tractor import (
Context,
MsgTypeError, MsgTypeError,
current_ipc_ctx, current_ipc_ctx,
Portal, Portal,
@ -35,7 +33,17 @@ from tractor.msg.types import (
) )
class PldMsg(Struct): class PldMsg(
Struct,
# TODO: with multiple structs in-spec we need to tag them!
# -[ ] offer a built-in `PldMsg` type to inherit from which takes
# case of these details?
#
# https://jcristharif.com/msgspec/structs.html#tagged-unions
# tag=True,
# tag_field='msg_type',
):
field: str field: str
@ -96,7 +104,9 @@ async def maybe_expect_raises(
) )
@tractor.context @tractor.context(
pld_spec=maybe_msg_spec,
)
async def child( async def child(
ctx: Context, ctx: Context,
started_value: int|PldMsg|None, started_value: int|PldMsg|None,
@ -116,21 +126,19 @@ async def child(
assert ctx is curr_ctx assert ctx is curr_ctx
rx: msgops.PldRx = ctx._pld_rx rx: msgops.PldRx = ctx._pld_rx
orig_pldec: _codec.MsgDec = rx.pld_dec curr_pldec: _codec.MsgDec = rx.pld_dec
# senity that default pld-spec should be set
assert (
rx.pld_dec
is
msgops._def_any_pldec
)
try: ctx_meta: dict = getattr(
with msgops.limit_plds( child,
spec=maybe_msg_spec, '_tractor_context_meta',
) as pldec: None,
# sanity on `MsgDec` state )
assert rx.pld_dec is pldec if ctx_meta:
assert pldec.spec is maybe_msg_spec assert (
ctx_meta['pld_spec']
is curr_pldec.spec
is curr_pldec.pld_spec
)
# 2 cases: hdndle send-side and recv-only validation # 2 cases: hdndle send-side and recv-only validation
# - when `raise_on_started_mte == True`, send validate # - when `raise_on_started_mte == True`, send validate
@ -211,18 +219,6 @@ async def child(
# msg-type-error from this RPC task ;) # msg-type-error from this RPC task ;)
return return_value return return_value
finally:
# sanity on `limit_plds()` reversion
assert (
rx.pld_dec
is
msgops._def_any_pldec
)
log.runtime(
'Reverted to previous pld-spec\n\n'
f'{orig_pldec}\n'
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
'return_value', 'return_value',
@ -321,7 +317,6 @@ def test_basic_payload_spec(
child, child,
return_value=return_value, return_value=return_value,
started_value=started_value, started_value=started_value,
pld_spec=maybe_msg_spec,
validate_pld_spec=pld_check_started_value, validate_pld_spec=pld_check_started_value,
) as (ctx, first), ) as (ctx, first),
): ):