Debian Codegraph @

examples/async_step/features/steps/async_dispatch_steps.py

raw
1 # -*- coding: UTF-8 -*-
2 # REQUIRES: Python >= 3.5
3 from behave import given, then, step
4 from behave.api.async_step import use_or_create_async_context, AsyncContext
5 from hamcrest import assert_that, equal_to, empty
6 import asyncio
7
8 @asyncio.coroutine
9 def async_func(param):
10 yield from asyncio.sleep(0.2)
11 return str(param).upper()
12
13 @given('I dispatch an async-call with param "{param}"')
14 def step_dispatch_async_call(context, param):
15 async_context = use_or_create_async_context(context, "async_context1")
16 task = async_context.loop.create_task(async_func(param))
17 async_context.tasks.append(task)
18
19 @then('the collected result of the async-calls is "{expected}"')
20 def step_collected_async_call_result_is(context, expected):
21 async_context = context.async_context1
22 done, pending = async_context.loop.run_until_complete(
23 asyncio.wait(async_context.tasks, loop=async_context.loop))
24
25 parts = [task.result() for task in done]
26 joined_result = ", ".join(sorted(parts))
27 assert_that(joined_result, equal_to(expected))
28 assert_that(pending, empty())