Skip to content

Commit 23e6fa1

Browse files
committed
Add tokio test
1 parent ed84849 commit 23e6fa1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

pytests/tokio_asyncio/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,39 @@ async fn test_async_gen_v2() -> PyResult<()> {
362362
Ok(())
363363
}
364364

365+
#[cfg(feature = "unstable-streams")]
366+
const TOKIO_TEST_MOD_FASTGEN: &str = r#"
367+
import asyncio
368+
369+
async def gen():
370+
for i in range(1000):
371+
yield i
372+
"#;
373+
374+
#[cfg(feature = "unstable-streams")]
375+
#[pyo3_async_runtimes::tokio::test]
376+
async fn test_async_gen_full_buffer() -> PyResult<()> {
377+
let stream = Python::attach(|py| {
378+
let test_mod = PyModule::from_code(
379+
py,
380+
&CString::new(TOKIO_TEST_MOD_FASTGEN).unwrap(),
381+
&CString::new("test_rust_coroutine/tokio_test_mod.py").unwrap(),
382+
&CString::new("tokio_test_mod").unwrap(),
383+
)?;
384+
385+
pyo3_async_runtimes::tokio::into_stream_v2(test_mod.call_method0("gen")?)
386+
})?;
387+
388+
let vals = stream
389+
.map(|item| Python::attach(|py| -> PyResult<i32> { item.bind(py).extract() }))
390+
.try_collect::<Vec<i32>>()
391+
.await?;
392+
393+
assert_eq!((0..1000).collect::<Vec<i32>>(), vals);
394+
395+
Ok(())
396+
}
397+
365398
const CONTEXTVARS_CODE: &str = r#"
366399
cx = contextvars.ContextVar("cx")
367400

0 commit comments

Comments
 (0)