forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_signals.py
More file actions
145 lines (123 loc) · 4.47 KB
/
test_signals.py
File metadata and controls
145 lines (123 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import multiprocessing
import os
import signal
import sys
from typing import Any, Dict
import pytest
from playwright.async_api import async_playwright
from playwright.sync_api import sync_playwright
def _test_signals_async(
browser_name: str, launch_arguments: Dict, wait_queue: "multiprocessing.Queue[str]"
) -> None:
# On Windows, hint to mypy and pyright that they shouldn't check this function
if sys.platform == "win32":
return
os.setpgrp()
sigint_received = False
def my_sig_handler(signum: int, frame: Any) -> None:
nonlocal sigint_received
sigint_received = True
signal.signal(signal.SIGINT, my_sig_handler)
async def main() -> None:
playwright = await async_playwright().start()
browser = await playwright[browser_name].launch(
**launch_arguments,
handle_sigint=False,
)
context = await browser.new_context()
page = await context.new_page()
notified = False
try:
while not sigint_received:
if not notified:
wait_queue.put("ready")
notified = True
await page.wait_for_timeout(100)
finally:
wait_queue.put("close context")
await context.close()
wait_queue.put("close browser")
await browser.close()
wait_queue.put("close playwright")
await playwright.stop()
wait_queue.put("all done")
asyncio.run(main())
def _test_signals_sync(
browser_name: str, launch_arguments: Dict, wait_queue: "multiprocessing.Queue[str]"
) -> None:
# On Windows, hint to mypy and pyright that they shouldn't check this function
if sys.platform == "win32":
return
os.setpgrp()
sigint_received = False
def my_sig_handler(signum: int, frame: Any) -> None:
nonlocal sigint_received
sigint_received = True
signal.signal(signal.SIGINT, my_sig_handler)
playwright = sync_playwright().start()
browser = playwright[browser_name].launch(
**launch_arguments,
handle_sigint=False,
)
context = browser.new_context()
page = context.new_page()
notified = False
try:
while not sigint_received:
if not notified:
wait_queue.put("ready")
notified = True
page.wait_for_timeout(100)
finally:
wait_queue.put("close context")
context.close()
wait_queue.put("close browser")
browser.close()
wait_queue.put("close playwright")
playwright.stop()
wait_queue.put("all done")
def _create_signals_test(
target: Any, browser_name: str, launch_arguments: Dict
) -> None:
# On Windows, hint to mypy and pyright that they shouldn't check this function
if sys.platform == "win32":
return
wait_queue: "multiprocessing.Queue[str]" = multiprocessing.Queue()
process = multiprocessing.Process(
target=target, args=[browser_name, launch_arguments, wait_queue]
)
process.start()
assert process.pid is not None
logs = [wait_queue.get()]
os.killpg(os.getpgid(process.pid), signal.SIGINT)
process.join()
while not wait_queue.empty():
logs.append(wait_queue.get())
assert logs == [
"ready",
"close context",
"close browser",
"close playwright",
"all done",
]
assert process.exitcode == 0
@pytest.mark.skipif(sys.platform == "win32", reason="there is no SIGINT on Windows")
def test_signals_sync(browser_name: str, launch_arguments: Dict) -> None:
_create_signals_test(_test_signals_sync, browser_name, launch_arguments)
@pytest.mark.skipif(sys.platform == "win32", reason="there is no SIGINT on Windows")
def test_signals_async(browser_name: str, launch_arguments: Dict) -> None:
_create_signals_test(_test_signals_async, browser_name, launch_arguments)