Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gather): introduce sync gather for easier async tests migration #131

Merged
merged 1 commit into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion playwright/sync_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@
# limitations under the License.

import asyncio
from typing import Any, Callable, Coroutine, Generic, Optional, TypeVar, cast
from typing import (
Any,
Callable,
Coroutine,
Dict,
Generic,
List,
Optional,
TypeVar,
cast,
)

import greenlet

Expand Down Expand Up @@ -109,3 +119,35 @@ def once(self, event_name: str, handler: Any) -> None:

def remove_listener(self, event_name: str, handler: Any) -> None:
self._impl_obj.remove_listener(event_name, handler)

def _gather(self, *actions: Callable) -> List[Any]:
g_self = greenlet.getcurrent()
results: Dict[Callable, Any] = {}
exceptions: List[Exception] = []

def action_wrapper(action: Callable) -> Callable:
def body() -> Any:
try:
results[action] = action()
except Exception as e:
results[action] = e
exceptions.append(e)
g_self.switch()

return body

async def task() -> None:
for action in actions:
g = greenlet.greenlet(action_wrapper(action))
g.switch()

self._loop.create_task(task())

while len(results) < len(actions):
dispatcher_fiber_.switch()

asyncio._set_running_loop(self._loop)
if exceptions:
raise exceptions[0]

return list(map(lambda action: results[action], actions))
2 changes: 1 addition & 1 deletion tests/async/test_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ async def test_expect_navigation_should_work_for_cross_process_navigations(
await goto_task


@pytest.mark.skip_browser("webkit")
@pytest.mark.skip("flaky, investigate")
async def test_wait_for_load_state_should_pick_up_ongoing_navigation(page, server):
requests = []

Expand Down
10 changes: 10 additions & 0 deletions tests/sync/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,13 @@ def test_sync_set_default_timeout(page):
with pytest.raises(TimeoutError) as exc:
page.waitForFunction("false")
assert "Timeout 1ms exceeded." in exc.value.message


def test_close_should_reject_all_promises(context):
new_page = context.newPage()
with pytest.raises(Error) as exc_info:
new_page._gather(
lambda: new_page.evaluate("() => new Promise(r => {})"),
lambda: new_page.close(),
)
assert "Protocol error" in exc_info.value.message