Skip to content

Commit

Permalink
Test coroutines can receive errors from poll
Browse files Browse the repository at this point in the history
  • Loading branch information
pelletier committed Feb 1, 2024
1 parent 2aaec3f commit 66b8802
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,43 @@ def coroutine_main(input: Input) -> Output:
out = response_output(resp)
self.assertEqual("length=10 text='cool stuff'", out)

def test_coroutine_poll_error(self):
@self.app.dispatch_coroutine()
def coro_compute_len(input: Input) -> Output:
return Output.error(Error(Status.PERMANENT_ERROR, "type", "Dead"))

@self.app.dispatch_coroutine()
def coroutine_main(input: Input) -> Output:
if input.is_first_call:
text: str = input.input
return Output.callback(
state=text, calls=[coro_compute_len.call_with(text)]
)
msg = input.calls[0].error.message
type = input.calls[0].error.type
return Output.value(f"msg={msg} type='{type}'")

resp = self.execute(coroutine_main, input="cool stuff")

# main saved some state
state = resp.poll.state
self.assertTrue(len(state) > 0)
# main asks for 1 call to compute_len
self.assertEqual(len(resp.poll.calls), 1)
call = resp.poll.calls[0]
self.assertEqual(call.coroutine_uri, coro_compute_len.uri)
self.assertEqual(dispatch.coroutine._any_unpickle(call.input), "cool stuff")

# make the requested compute_len
resp2 = self.call(call)

# resume main with the result
resp = self.execute(coroutine_main, state=state, calls=[resp2])
# validate the final result
self.assertTrue(len(resp.poll.state) == 0)
out = response_output(resp)
self.assertEqual(out, "msg=Dead type='type'")

def test_coroutine_error(self):
@self.app.dispatch_coroutine()
def mycoro(input: Input) -> Output:
Expand Down

0 comments on commit 66b8802

Please sign in to comment.