Skip to content

Commit 790757d

Browse files
authored
test: add test for Command + C does not terminate driver connection (microsoft#1908)
1 parent a45cb4c commit 790757d

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

tests/common/test_signals.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import asyncio
2+
import multiprocessing
3+
import os
4+
import signal
5+
import sys
6+
from typing import Any, Dict
7+
8+
import pytest
9+
10+
from playwright.async_api import async_playwright
11+
from playwright.sync_api import sync_playwright
12+
13+
14+
def _test_signals_async(
15+
browser_name: str, launch_arguments: Dict, wait_queue: "multiprocessing.Queue[str]"
16+
) -> None:
17+
os.setpgrp()
18+
sigint_received = False
19+
20+
def my_sig_handler(signum: int, frame: Any) -> None:
21+
nonlocal sigint_received
22+
sigint_received = True
23+
24+
signal.signal(signal.SIGINT, my_sig_handler)
25+
26+
async def main() -> None:
27+
playwright = await async_playwright().start()
28+
browser = await playwright[browser_name].launch(
29+
**launch_arguments,
30+
handle_sigint=False,
31+
)
32+
context = await browser.new_context()
33+
page = await context.new_page()
34+
notified = False
35+
try:
36+
nonlocal sigint_received
37+
while not sigint_received:
38+
if not notified:
39+
wait_queue.put("ready")
40+
notified = True
41+
await page.wait_for_timeout(100)
42+
finally:
43+
wait_queue.put("close context")
44+
await context.close()
45+
wait_queue.put("close browser")
46+
await browser.close()
47+
wait_queue.put("close playwright")
48+
await playwright.stop()
49+
wait_queue.put("all done")
50+
51+
asyncio.run(main())
52+
53+
54+
def _test_signals_sync(
55+
browser_name: str, launch_arguments: Dict, wait_queue: "multiprocessing.Queue[str]"
56+
) -> None:
57+
os.setpgrp()
58+
sigint_received = False
59+
60+
def my_sig_handler(signum: int, frame: Any) -> None:
61+
nonlocal sigint_received
62+
sigint_received = True
63+
64+
signal.signal(signal.SIGINT, my_sig_handler)
65+
66+
playwright = sync_playwright().start()
67+
browser = playwright[browser_name].launch(
68+
**launch_arguments,
69+
handle_sigint=False,
70+
)
71+
context = browser.new_context()
72+
page = context.new_page()
73+
notified = False
74+
try:
75+
while not sigint_received:
76+
if not notified:
77+
wait_queue.put("ready")
78+
notified = True
79+
page.wait_for_timeout(100)
80+
finally:
81+
wait_queue.put("close context")
82+
context.close()
83+
wait_queue.put("close browser")
84+
browser.close()
85+
wait_queue.put("close playwright")
86+
playwright.stop()
87+
wait_queue.put("all done")
88+
89+
90+
def _create_signals_test(
91+
target: Any, browser_name: str, launch_arguments: Dict
92+
) -> None:
93+
wait_queue: "multiprocessing.Queue[str]" = multiprocessing.Queue()
94+
process = multiprocessing.Process(
95+
target=target, args=[browser_name, launch_arguments, wait_queue]
96+
)
97+
process.start()
98+
assert process.pid is not None
99+
logs = [wait_queue.get()]
100+
os.killpg(os.getpgid(process.pid), signal.SIGINT)
101+
process.join()
102+
while not wait_queue.empty():
103+
logs.append(wait_queue.get())
104+
assert logs == [
105+
"ready",
106+
"close context",
107+
"close browser",
108+
"close playwright",
109+
"all done",
110+
]
111+
assert process.exitcode == 0
112+
113+
114+
@pytest.mark.skipif(sys.platform == "win32", reason="there is no SIGINT on Windows")
115+
def test_signals_sync(browser_name: str, launch_arguments: Dict) -> None:
116+
_create_signals_test(_test_signals_sync, browser_name, launch_arguments)
117+
118+
119+
@pytest.mark.skipif(sys.platform == "win32", reason="there is no SIGINT on Windows")
120+
def test_signals_async(browser_name: str, launch_arguments: Dict) -> None:
121+
_create_signals_test(_test_signals_async, browser_name, launch_arguments)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy