From af23bce54cc4fea8055c226e777a61426089ab0c Mon Sep 17 00:00:00 2001 From: Joseph Perez Date: Mon, 20 Nov 2023 01:32:25 +0100 Subject: [PATCH] fix: fix coroutine tests on windows --- tests/test_coroutine.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tests/test_coroutine.rs b/tests/test_coroutine.rs index ddfd24bc142..84326101b57 100644 --- a/tests/test_coroutine.rs +++ b/tests/test_coroutine.rs @@ -1,11 +1,8 @@ #![cfg(feature = "macros")] -use futures::channel::oneshot; -use futures::future::poll_fn; -use pyo3::prelude::*; -use pyo3::py_run; -use std::task::Poll; -use std::thread; -use std::time::Duration; +use std::{task::Poll, thread, time::Duration}; + +use futures::{channel::oneshot, future::poll_fn}; +use pyo3::{prelude::*, py_run}; #[path = "../src/tests/common.rs"] mod common; @@ -59,7 +56,12 @@ async fn sleep(seconds: f64) -> usize { fn sleep_coroutine() { Python::with_gil(|gil| { let sleep = wrap_pyfunction!(sleep, gil).unwrap(); - let test = "import asyncio; assert asyncio.run(sleep(0.1)) == 42"; + let test = r#" + import asyncio, sys + if sys.platform == "win32": + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) + assert asyncio.run(sleep(0.1)) == 42 + "#; py_run!(gil, sleep, test); }) } @@ -69,7 +71,9 @@ fn cancelled_coroutine() { Python::with_gil(|gil| { let sleep = wrap_pyfunction!(sleep, gil).unwrap(); let test = r#" - import asyncio; + import asyncio, sys + if sys.platform == "win32": + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) async def main(): task = asyncio.create_task(sleep(1)) await asyncio.sleep(0)