Skip to content

Commit 3e44464

Browse files
authored
test: move SyncBase._gather into tests (#2316)
1 parent c4ffd45 commit 3e44464

File tree

3 files changed

+44
-38
lines changed

3 files changed

+44
-38
lines changed

playwright/_impl/_sync_base.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
Any,
2121
Callable,
2222
Coroutine,
23-
Dict,
2423
Generator,
2524
Generic,
26-
List,
2725
Type,
2826
TypeVar,
2927
Union,
@@ -133,38 +131,6 @@ def remove_listener(self, event: Any, f: Any) -> None:
133131
"""Removes the function ``f`` from ``event``."""
134132
self._impl_obj.remove_listener(event, self._wrap_handler(f))
135133

136-
def _gather(self, *actions: Callable) -> List[Any]:
137-
g_self = greenlet.getcurrent()
138-
results: Dict[Callable, Any] = {}
139-
exceptions: List[Exception] = []
140-
141-
def action_wrapper(action: Callable) -> Callable:
142-
def body() -> Any:
143-
try:
144-
results[action] = action()
145-
except Exception as e:
146-
results[action] = e
147-
exceptions.append(e)
148-
g_self.switch()
149-
150-
return body
151-
152-
async def task() -> None:
153-
for action in actions:
154-
g = greenlet.greenlet(action_wrapper(action))
155-
g.switch()
156-
157-
self._loop.create_task(task())
158-
159-
while len(results) < len(actions):
160-
self._dispatcher_fiber.switch()
161-
162-
asyncio._set_running_loop(self._loop)
163-
if exceptions:
164-
raise exceptions[0]
165-
166-
return list(map(lambda action: results[action], actions))
167-
168134

169135
class SyncContextManager(SyncBase):
170136
def __enter__(self: Self) -> Self:

tests/sync/conftest.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
# limitations under the License.
1414

1515

16-
from typing import Dict, Generator
16+
import asyncio
17+
from typing import Any, Callable, Dict, Generator, List
1718

1819
import pytest
20+
from greenlet import greenlet
1921

2022
from playwright.sync_api import (
2123
Browser,
@@ -83,3 +85,39 @@ def page(context: BrowserContext) -> Generator[Page, None, None]:
8385
@pytest.fixture(scope="session")
8486
def selectors(playwright: Playwright) -> Selectors:
8587
return playwright.selectors
88+
89+
90+
@pytest.fixture(scope="session")
91+
def sync_gather(playwright: Playwright) -> Generator[Callable, None, None]:
92+
def _sync_gather_impl(*actions: Callable) -> List[Any]:
93+
g_self = greenlet.getcurrent()
94+
results: Dict[Callable, Any] = {}
95+
exceptions: List[Exception] = []
96+
97+
def action_wrapper(action: Callable) -> Callable:
98+
def body() -> Any:
99+
try:
100+
results[action] = action()
101+
except Exception as e:
102+
results[action] = e
103+
exceptions.append(e)
104+
g_self.switch()
105+
106+
return body
107+
108+
async def task() -> None:
109+
for action in actions:
110+
g = greenlet(action_wrapper(action))
111+
g.switch()
112+
113+
asyncio.create_task(task())
114+
115+
while len(results) < len(actions):
116+
playwright._dispatcher_fiber.switch()
117+
118+
if exceptions:
119+
raise exceptions[0]
120+
121+
return list(map(lambda action: results[action], actions))
122+
123+
yield _sync_gather_impl

tests/sync/test_sync.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import multiprocessing
1616
import os
17-
from typing import Any, Dict
17+
from typing import Any, Callable, Dict
1818

1919
import pytest
2020

@@ -266,10 +266,12 @@ def test_sync_set_default_timeout(page: Page) -> None:
266266
assert "Timeout 1ms exceeded." in exc.value.message
267267

268268

269-
def test_close_should_reject_all_promises(context: BrowserContext) -> None:
269+
def test_close_should_reject_all_promises(
270+
context: BrowserContext, sync_gather: Callable
271+
) -> None:
270272
new_page = context.new_page()
271273
with pytest.raises(Error) as exc_info:
272-
new_page._gather(
274+
sync_gather(
273275
lambda: new_page.evaluate("() => new Promise(r => {})"),
274276
lambda: new_page.close(),
275277
)

0 commit comments

Comments
 (0)