Skip to content

Commit

Permalink
Add 2 itests
Browse files Browse the repository at this point in the history
  • Loading branch information
amh4r committed Oct 25, 2023
1 parent 474dc2e commit 3ff321e
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 2 deletions.
3 changes: 3 additions & 0 deletions inngest/_internal/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
class Call(BaseModel):
ctx: CallContext
event: Event
events: list[Event]
steps: dict[str, object]


class CallContext(BaseModel):
attempt: int
run_id: str
stack: CallStack


Expand Down
16 changes: 14 additions & 2 deletions inngest/_internal/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import threading
import traceback
from datetime import datetime
from typing import Callable, Protocol
from typing import Callable, Protocol, runtime_checkable

from pydantic import ValidationError

Expand Down Expand Up @@ -70,7 +70,10 @@ def call(
) -> list[CallResponse] | str | CallError:
try:
res = self._handler(
attempt=call.ctx.attempt,
event=call.event,
events=call.events,
run_id=call.ctx.run_id,
step=_Step(client, call.steps, _StepIDCounter()),
)
return json.dumps(res)
Expand Down Expand Up @@ -272,10 +275,19 @@ def wait_for_event(


class _FunctionHandler(Protocol):
def __call__(self, *, event: Event, step: Step) -> object:
def __call__(
self,
*,
attempt: int,
event: Event,
events: list[Event],
run_id: str,
step: Step,
) -> object:
...


@runtime_checkable
class Step(Protocol):
def run(
self,
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from . import (
event_payload,
function_args,
no_steps,
sleep_until,
two_steps,
unserializable_step_output,
wait_for_event_fulfill,
Expand All @@ -16,7 +18,9 @@ def create_cases(client: inngest.Inngest, framework: str) -> list[Case]:
case.create(client, framework)
for case in (
event_payload,
function_args,
no_steps,
sleep_until,
two_steps,
unserializable_step_output,
wait_for_event_fulfill,
Expand Down
60 changes: 60 additions & 0 deletions tests/cases/function_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import inngest

from .base import BaseState, Case, wait_for

_TEST_NAME = "function_args"


class _State(BaseState):
attempt: int | None = None
event: inngest.Event | None = None
events: list[inngest.Event] | None = None
run_id: str | None = None
step: inngest.Step | None = None

def is_done(self) -> bool:
return self.attempt is not None


def create(client: inngest.Inngest, framework: str) -> Case:
event_name = f"{framework}/{_TEST_NAME}"
state = _State()

@inngest.create_function(
inngest.FunctionOpts(id=_TEST_NAME),
inngest.TriggerEvent(event=event_name),
)
def fn(
*,
attempt: int,
event: inngest.Event,
events: list[inngest.Event],
run_id: str,
step: inngest.Step,
) -> None:
state.attempt = attempt
state.event = event
state.events = events
state.run_id = run_id
state.step = step

def run_test(_self: object) -> None:
client.send(inngest.Event(name=event_name))

def assertion() -> None:
assert state.is_done()
assert state.attempt == 0
assert isinstance(state.event, inngest.Event)
assert isinstance(state.events, list) and len(state.events) == 1
assert state.run_id != ""
assert isinstance(state.step, inngest.Step)

wait_for(assertion)

return Case(
event_name=event_name,
fn=fn,
run_test=run_test,
state=state,
name=_TEST_NAME,
)
55 changes: 55 additions & 0 deletions tests/cases/sleep_until.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from datetime import datetime, timedelta

import inngest

from .base import BaseState, Case, wait_for

_TEST_NAME = "sleep_until"


class _State(BaseState):
after_sleep: datetime | None = None
before_sleep: datetime | None = None

def is_done(self) -> bool:
return self.after_sleep is not None and self.before_sleep is not None


def create(client: inngest.Inngest, framework: str) -> Case:
event_name = f"{framework}/{_TEST_NAME}"
state = _State()

@inngest.create_function(
inngest.FunctionOpts(id=_TEST_NAME),
inngest.TriggerEvent(event=event_name),
)
def fn(*, step: inngest.Step, **_kwargs: object) -> None:
if state.before_sleep is None:
state.before_sleep = datetime.now()

step.sleep_until("zzz", datetime.now() + timedelta(seconds=2))

if state.after_sleep is None:
state.after_sleep = datetime.now()

def run_test(_self: object) -> None:
client.send(inngest.Event(name=event_name))

def assertion() -> None:
assert state.is_done()
assert (
state.after_sleep is not None and state.before_sleep is not None
)
assert state.after_sleep - state.before_sleep >= timedelta(
seconds=2
)

wait_for(assertion)

return Case(
event_name=event_name,
fn=fn,
run_test=run_test,
state=state,
name=_TEST_NAME,
)

0 comments on commit 3ff321e

Please sign in to comment.