|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 | import asyncio
|
15 |
| -import contextlib |
| 15 | +import gc |
| 16 | +from typing import Dict |
| 17 | + |
| 18 | +import pytest |
16 | 19 |
|
17 | 20 | from playwright.async_api import async_playwright
|
18 | 21 |
|
19 | 22 |
|
20 |
| -def test_should_cancel_underlying_calls(browser_name: str): |
| 23 | +async def test_should_cancel_underlying_protocol_calls( |
| 24 | + browser_name: str, launch_arguments: Dict |
| 25 | +): |
21 | 26 | handler_exception = None
|
22 | 27 |
|
23 |
| - async def main(): |
24 |
| - loop = asyncio.get_running_loop() |
| 28 | + def exception_handlerdler(loop, context) -> None: |
| 29 | + nonlocal handler_exception |
| 30 | + handler_exception = context["exception"] |
25 | 31 |
|
26 |
| - def handler(loop, context): |
27 |
| - nonlocal handler_exception |
28 |
| - handler_exception = context["exception"] |
| 32 | + asyncio.get_running_loop().set_exception_handler(exception_handlerdler) |
29 | 33 |
|
30 |
| - async with async_playwright() as p: |
31 |
| - loop.set_exception_handler(handler) |
32 |
| - browser = await p[browser_name].launch() |
33 |
| - page = await browser.new_page() |
34 |
| - task = asyncio.create_task(page.wait_for_selector("will-never-find")) |
35 |
| - # make sure that the wait_for_selector message was sent to the server (driver) |
36 |
| - await asyncio.sleep(1) |
37 |
| - task.cancel() |
38 |
| - with contextlib.suppress(asyncio.CancelledError): |
39 |
| - await task |
40 |
| - await browser.close() |
| 34 | + async with async_playwright() as p: |
| 35 | + browser = await p[browser_name].launch(**launch_arguments) |
| 36 | + page = await browser.new_page() |
| 37 | + task = asyncio.create_task(page.wait_for_selector("will-never-find")) |
| 38 | + # make sure that the wait_for_selector message was sent to the server (driver) |
| 39 | + await asyncio.sleep(0.1) |
| 40 | + task.cancel() |
| 41 | + with pytest.raises(asyncio.CancelledError): |
| 42 | + await task |
| 43 | + await browser.close() |
41 | 44 |
|
42 |
| - asyncio.run(main()) |
| 45 | + # The actual 'Future exception was never retrieved' is logged inside the Future destructor (__del__). |
| 46 | + gc.collect() |
43 | 47 |
|
44 | 48 | assert handler_exception is None
|
| 49 | + |
| 50 | + asyncio.get_running_loop().set_exception_handler(None) |
0 commit comments