| 1 |
@not.with_python2=true |
| 2 |
Feature: Async-Test Support (async-step, ...) |
| 3 |
|
| 4 |
As a test writer and step provider |
| 5 |
I want to test async frameworks or protocols (that use asyncio) |
| 6 |
|
| 7 |
. USE CASES: |
| 8 |
. * async-step with run-to-complete semantics (like synchronous step) |
| 9 |
. * async-dispatch-and-collect-results-later: |
| 10 |
. one or more steps dispatch async-calls (tasks) |
| 11 |
. and final step(s) that waits until tasks have been completed |
| 12 |
. (collects the results of the async-calls and verifies them) |
| 13 |
. |
| 14 |
. TERMINOLOGY: async-step |
| 15 |
. An async-step is either |
| 16 |
. * an async-function as coroutine using async/await keywords (Python 3.5) |
| 17 |
. * an async-function tagged with @asyncio.coroutine and using "yield from" |
| 18 |
. |
| 19 |
. # -- EXAMPLE CASE 1 (since Python 3.5): |
| 20 |
. async def coroutine1(duration): |
| 21 |
. await asyncio.sleep(duration) |
| 22 |
. |
| 23 |
. # -- EXAMPLE CASE 2 (since Python 3.4): |
| 24 |
. @asyncio.coroutine |
| 25 |
. def coroutine2(duration): |
| 26 |
. yield from asyncio.sleep(duration) |
| 27 |
. |
| 28 |
. RATIONALE: |
| 29 |
. By using async-steps, an additional layer of indirection is avoided. |
| 30 |
. The async-step can directly interact with other async-functions. |
| 31 |
|
| 32 |
|
| 33 |
@use.with_python.version=3.5 |
| 34 |
@use.with_python.version=3.6 |
| 35 |
Scenario: Use async-step with @async_run_until_complete (async) |
| 36 |
Given a new working directory |
| 37 |
And a file named "features/steps/async_steps35.py" with: |
| 38 |
""" |
| 39 |
from behave import step |
| 40 |
from behave.api.async_step import async_run_until_complete |
| 41 |
import asyncio |
| 42 |
|
| 43 |
@step('an async-step waits {duration:f} seconds') |
| 44 |
@async_run_until_complete |
| 45 |
async def step_async_step_waits_seconds(context, duration): |
| 46 |
await asyncio.sleep(duration) |
| 47 |
""" |
| 48 |
And a file named "features/async_run.feature" with: |
| 49 |
""" |
| 50 |
Feature: |
| 51 |
Scenario: |
| 52 |
Given an async-step waits 0.2 seconds |
| 53 |
""" |
| 54 |
When I run "behave -f plain --show-timings features/async_run.feature" |
| 55 |
Then it should pass with: |
| 56 |
""" |
| 57 |
Feature: |
| 58 |
Scenario: |
| 59 |
Given an async-step waits 0.2 seconds ... passed in 0.2 |
| 60 |
""" |
| 61 |
|
| 62 |
|
| 63 |
@use.with_python.version=3.4 |
| 64 |
@use.with_python.version=3.5 |
| 65 |
@use.with_python.version=3.6 |
| 66 |
Scenario: Use async-step with @async_run_until_complete (@coroutine) |
| 67 |
Given a new working directory |
| 68 |
And a file named "features/steps/async_steps34.py" with: |
| 69 |
""" |
| 70 |
from behave import step |
| 71 |
from behave.api.async_step import async_run_until_complete |
| 72 |
import asyncio |
| 73 |
|
| 74 |
@step('an async-step waits {duration:f} seconds') |
| 75 |
@async_run_until_complete |
| 76 |
@asyncio.coroutine |
| 77 |
def step_async_step_waits_seconds2(context, duration): |
| 78 |
yield from asyncio.sleep(duration) |
| 79 |
""" |
| 80 |
And a file named "features/async_run.feature" with: |
| 81 |
""" |
| 82 |
Feature: |
| 83 |
Scenario: |
| 84 |
Given an async-step waits 0.3 seconds |
| 85 |
""" |
| 86 |
When I run "behave -f plain --show-timings features/async_run.feature" |
| 87 |
Then it should pass with: |
| 88 |
""" |
| 89 |
Feature: |
| 90 |
Scenario: |
| 91 |
Given an async-step waits 0.3 seconds ... passed in 0.3 |
| 92 |
""" |
| 93 |
|
| 94 |
@use.with_python.version=3.5 |
| 95 |
@use.with_python.version=3.6 |
| 96 |
Scenario: Use @async_run_until_complete(timeout=...) and TIMEOUT occurs (async) |
| 97 |
Given a new working directory |
| 98 |
And a file named "features/steps/async_steps_timeout35.py" with: |
| 99 |
""" |
| 100 |
from behave import step |
| 101 |
from behave.api.async_step import async_run_until_complete |
| 102 |
import asyncio |
| 103 |
|
| 104 |
@step('an async-step waits {duration:f} seconds with timeout') |
| 105 |
@async_run_until_complete(timeout=0.1) # BAD-TIMEOUT-BY-DESIGN |
| 106 |
async def step_async_step_waits_seconds_with_timeout35(context, duration): |
| 107 |
await asyncio.sleep(duration) |
| 108 |
""" |
| 109 |
And a file named "features/async_timeout35.feature" with: |
| 110 |
""" |
| 111 |
Feature: |
| 112 |
Scenario: |
| 113 |
Given an async-step waits 1.0 seconds with timeout |
| 114 |
""" |
| 115 |
When I run "behave -f plain --show-timings features/async_timeout35.feature" |
| 116 |
Then it should fail with: |
| 117 |
""" |
| 118 |
0 steps passed, 1 failed, 0 skipped, 0 undefined |
| 119 |
""" |
| 120 |
And the command output should contain: |
| 121 |
""" |
| 122 |
Given an async-step waits 1.0 seconds with timeout ... failed in 0.1 |
| 123 |
""" |
| 124 |
And the command output should contain: |
| 125 |
""" |
| 126 |
Assertion Failed: TIMEOUT-OCCURED: timeout=0.1 |
| 127 |
""" |
| 128 |
|
| 129 |
@use.with_python.version=3.4 |
| 130 |
@use.with_python.version=3.5 |
| 131 |
@use.with_python.version=3.6 |
| 132 |
Scenario: Use @async_run_until_complete(timeout=...) and TIMEOUT occurs (@coroutine) |
| 133 |
Given a new working directory |
| 134 |
And a file named "features/steps/async_steps_timeout34.py" with: |
| 135 |
""" |
| 136 |
from behave import step |
| 137 |
from behave.api.async_step import async_run_until_complete |
| 138 |
import asyncio |
| 139 |
|
| 140 |
@step('an async-step waits {duration:f} seconds with timeout') |
| 141 |
@async_run_until_complete(timeout=0.2) # BAD-TIMEOUT-BY-DESIGN |
| 142 |
@asyncio.coroutine |
| 143 |
def step_async_step_waits_seconds_with_timeout34(context, duration): |
| 144 |
yield from asyncio.sleep(duration) |
| 145 |
""" |
| 146 |
And a file named "features/async_timeout34.feature" with: |
| 147 |
""" |
| 148 |
Feature: |
| 149 |
Scenario: |
| 150 |
Given an async-step waits 1.0 seconds with timeout |
| 151 |
""" |
| 152 |
When I run "behave -f plain --show-timings features/async_timeout34.feature" |
| 153 |
Then it should fail with: |
| 154 |
""" |
| 155 |
0 steps passed, 1 failed, 0 skipped, 0 undefined |
| 156 |
""" |
| 157 |
And the command output should contain: |
| 158 |
""" |
| 159 |
Given an async-step waits 1.0 seconds with timeout ... failed in 0.2 |
| 160 |
""" |
| 161 |
And the command output should contain: |
| 162 |
""" |
| 163 |
Assertion Failed: TIMEOUT-OCCURED: timeout=0.2 |
| 164 |
""" |
| 165 |
|
| 166 |
@use.with_python.version=3.4 |
| 167 |
@use.with_python.version=3.5 |
| 168 |
@use.with_python.version=3.6 |
| 169 |
Scenario: Use async-dispatch and async-collect concepts |
| 170 |
Given a new working directory |
| 171 |
And a file named "features/steps/async_dispatch_steps.py" with: |
| 172 |
""" |
| 173 |
from behave import given, then, step |
| 174 |
from behave.api.async_step import use_or_create_async_context, AsyncContext |
| 175 |
from hamcrest import assert_that, equal_to, empty |
| 176 |
import asyncio |
| 177 |
|
| 178 |
@asyncio.coroutine |
| 179 |
def async_func(param): |
| 180 |
yield from asyncio.sleep(0.2) |
| 181 |
return str(param).upper() |
| 182 |
|
| 183 |
@given('I dispatch an async-call with param "{param}"') |
| 184 |
def step_dispatch_async_call(context, param): |
| 185 |
async_context = use_or_create_async_context(context, "async_context1") |
| 186 |
task = async_context.loop.create_task(async_func(param)) |
| 187 |
async_context.tasks.append(task) |
| 188 |
|
| 189 |
@then('the collected result of the async-calls is "{expected}"') |
| 190 |
def step_collected_async_call_result_is(context, expected): |
| 191 |
async_context = context.async_context1 |
| 192 |
done, pending = async_context.loop.run_until_complete( |
| 193 |
asyncio.wait(async_context.tasks, loop=async_context.loop)) |
| 194 |
|
| 195 |
parts = [task.result() for task in done] |
| 196 |
joined_result = ", ".join(sorted(parts)) |
| 197 |
assert_that(joined_result, equal_to(expected)) |
| 198 |
assert_that(pending, empty()) |
| 199 |
""" |
| 200 |
And a file named "features/async_dispatch.feature" with: |
| 201 |
""" |
| 202 |
Feature: |
| 203 |
Scenario: |
| 204 |
Given I dispatch an async-call with param "Alice" |
| 205 |
And I dispatch an async-call with param "Bob" |
| 206 |
Then the collected result of the async-calls is "ALICE, BOB" |
| 207 |
""" |
| 208 |
When I run "behave -f plain --show-timings features/async_dispatch.feature" |
| 209 |
Then it should pass with: |
| 210 |
""" |
| 211 |
3 steps passed, 0 failed, 0 skipped, 0 undefined |
| 212 |
""" |
| 213 |
And the command output should contain: |
| 214 |
""" |
| 215 |
Given I dispatch an async-call with param "Alice" ... passed in 0.00 |
| 216 |
""" |
| 217 |
And the command output should contain: |
| 218 |
""" |
| 219 |
And I dispatch an async-call with param "Bob" ... passed in 0.00 |
| 220 |
""" |
| 221 |
And the command output should contain: |
| 222 |
""" |
| 223 |
Then the collected result of the async-calls is "ALICE, BOB" ... passed in 0.2 |
| 224 |
""" |
| 225 |
But note that "the async-collect step waits 0.2 seconds for both async-tasks to finish" |
| 226 |
And note that "the async-dispatch steps do not wait at all" |