Skip to content

Commit e9f2205

Browse files
authored
fix(threads): fix the separate thread execution in 3.7, add tests (microsoft#361)
1 parent b9755e2 commit e9f2205

File tree

5 files changed

+48
-13
lines changed

5 files changed

+48
-13
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ jobs:
7979
run: python setup.py bdist_wheel
8080
- name: Install browsers
8181
run: python -m playwright install
82+
- name: Common Tests
83+
run: pytest -vv tests/common --browser=${{ matrix.browser }} --timeout 90
8284
- name: Test Sync API
8385
if: matrix.os != 'ubuntu-latest'
8486
run: pytest -vv tests/sync --browser=${{ matrix.browser }} --timeout 90

.github/workflows/test_docker.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ jobs:
3737
docker exec --workdir /root/playwright/ "${CONTAINER_ID}" pip install -r local-requirements.txt
3838
docker exec --workdir /root/playwright/ "${CONTAINER_ID}" pip install -e .
3939
docker exec --workdir /root/playwright/ "${CONTAINER_ID}" python setup.py bdist_wheel
40+
docker exec --workdir /root/playwright/ "${CONTAINER_ID}" xvfb-run pytest -vv tests/common/
4041
docker exec --workdir /root/playwright/ "${CONTAINER_ID}" xvfb-run pytest -vv tests/sync/
4142
docker exec --workdir /root/playwright/ "${CONTAINER_ID}" xvfb-run pytest -vv tests/async/

playwright/main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,15 @@ async def __aexit__(self, *args: Any) -> None:
110110
self._connection.stop_async()
111111

112112

113-
if sys.platform == "win32":
114-
# Use ProactorEventLoop in 3.7, which is default in 3.8
115-
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
113+
if sys.version_info.major == 3 and sys.version_info.minor == 7:
114+
if sys.platform == "win32":
115+
# Use ProactorEventLoop in 3.7, which is default in 3.8
116+
asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
117+
else:
118+
# Prevent Python 3.7 from throwing on Linux:
119+
# RuntimeError: Cannot add child handler, the child watcher does not have a loop attached
120+
asyncio.get_event_loop()
121+
asyncio.get_child_watcher()
116122

117123

118124
def main() -> None:

setup.cfg

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@ markers =
66
only_platform
77
junit_family=xunit2
88
[mypy]
9-
ignore_missing_imports = True
10-
python_version = 3.7
11-
warn_unused_ignores = False
12-
warn_redundant_casts = True
13-
warn_unused_configs = True
14-
check_untyped_defs = True
15-
disallow_untyped_defs = True
16-
[mypy-tests.*]
17-
check_untyped_defs = False
18-
disallow_untyped_defs = False
9+
ignore_errors = True
1910
[flake8]
2011
ignore =
2112
E501

tests/common/test_threads.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License")
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import threading
16+
17+
from playwright import sync_playwright
18+
19+
20+
def test_running_in_thread(browser_name):
21+
result = []
22+
23+
class TestThread(threading.Thread):
24+
def run(self):
25+
with sync_playwright() as playwright:
26+
browser = getattr(playwright, browser_name).launch()
27+
# This should not throw ^^.
28+
browser.newPage()
29+
browser.close()
30+
result.append("Success")
31+
32+
test_thread = TestThread()
33+
test_thread.start()
34+
test_thread.join()
35+
assert "Success" in result

0 commit comments

Comments
 (0)