Skip to content

Commit 72c3d0b

Browse files
author
Andrew J Westlake
committed
Modified test after checking against invalid-state-fix PR, added it to tokio as well
1 parent 41ed201 commit 72c3d0b

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

pytests/test_async_std_asyncio.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod common;
33
use std::{rc::Rc, time::Duration};
44

55
use async_std::task;
6-
use pyo3::{prelude::*, wrap_pyfunction};
6+
use pyo3::{prelude::*, types::PyType, wrap_pyfunction};
77

88
#[pyfunction]
99
fn sleep_for(py: Python, secs: &PyAny) -> PyResult<PyObject> {
@@ -54,24 +54,6 @@ async fn test_async_sleep() -> PyResult<()> {
5454
Ok(())
5555
}
5656

57-
#[pyo3_asyncio::async_std::test]
58-
async fn test_cancel() -> PyResult<()> {
59-
let py_future = Python::with_gil(|py| {
60-
pyo3_asyncio::async_std::into_coroutine(py, async {
61-
async_std::task::sleep(Duration::from_secs(1)).await;
62-
Ok(Python::with_gil(|py| py.None()))
63-
})
64-
})?;
65-
66-
Python::with_gil(|py| -> PyResult<_> {
67-
py_future.as_ref(py).call_method0("cancel")?;
68-
pyo3_asyncio::into_future(py_future.as_ref(py))
69-
})?
70-
.await?;
71-
72-
Ok(())
73-
}
74-
7557
#[pyo3_asyncio::async_std::test]
7658
fn test_blocking_sleep() -> PyResult<()> {
7759
common::test_blocking_sleep()
@@ -113,3 +95,34 @@ async fn test_local_coroutine() -> PyResult<()> {
11395

11496
Ok(())
11597
}
98+
99+
#[pyo3_asyncio::async_std::test]
100+
async fn test_cancel() -> PyResult<()> {
101+
let py_future = Python::with_gil(|py| {
102+
pyo3_asyncio::async_std::into_coroutine(py, async {
103+
async_std::task::sleep(Duration::from_secs(1)).await;
104+
Ok(Python::with_gil(|py| py.None()))
105+
})
106+
})?;
107+
108+
if let Err(e) = Python::with_gil(|py| -> PyResult<_> {
109+
py_future.as_ref(py).call_method0("cancel")?;
110+
pyo3_asyncio::into_future(py_future.as_ref(py))
111+
})?
112+
.await
113+
{
114+
Python::with_gil(|py| -> PyResult<()> {
115+
assert!(py
116+
.import("asyncio.exceptions")?
117+
.getattr("CancelledError")?
118+
.downcast::<PyType>()
119+
.unwrap()
120+
.is_instance(e.pvalue(py))?);
121+
Ok(())
122+
})?;
123+
} else {
124+
panic!("expected CancelledError");
125+
}
126+
127+
Ok(())
128+
}

pytests/tokio_asyncio/mod.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{rc::Rc, time::Duration};
22

3-
use pyo3::{prelude::*, wrap_pyfunction};
3+
use pyo3::{prelude::*, types::PyType, wrap_pyfunction};
44

55
use crate::common;
66

@@ -101,3 +101,34 @@ fn test_local_set_coroutine() -> PyResult<()> {
101101
Ok(())
102102
})
103103
}
104+
105+
#[pyo3_asyncio::tokio::test]
106+
async fn test_cancel() -> PyResult<()> {
107+
let py_future = Python::with_gil(|py| {
108+
pyo3_asyncio::tokio::into_coroutine(py, async {
109+
tokio::time::sleep(Duration::from_secs(1)).await;
110+
Ok(Python::with_gil(|py| py.None()))
111+
})
112+
})?;
113+
114+
if let Err(e) = Python::with_gil(|py| -> PyResult<_> {
115+
py_future.as_ref(py).call_method0("cancel")?;
116+
pyo3_asyncio::into_future(py_future.as_ref(py))
117+
})?
118+
.await
119+
{
120+
Python::with_gil(|py| -> PyResult<()> {
121+
assert!(py
122+
.import("asyncio.exceptions")?
123+
.getattr("CancelledError")?
124+
.downcast::<PyType>()
125+
.unwrap()
126+
.is_instance(e.pvalue(py))?);
127+
Ok(())
128+
})?;
129+
} else {
130+
panic!("expected CancelledError");
131+
}
132+
133+
Ok(())
134+
}

0 commit comments

Comments
 (0)