Skip to content

Commit 3c3afa0

Browse files
author
Andrew J Westlake
committed
Changed uvloop test to no_run in README, added integration tests for uvloop
1 parent a7f0def commit 3c3afa0

9 files changed

+135
-3
lines changed

Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ path = "pytests/test_tokio_multi_thread_run_forever.rs"
8383
harness = false
8484
required-features = ["tokio-runtime", "testing"]
8585

86+
[[test]]
87+
name = "test_async_std_uvloop"
88+
path = "pytests/test_async_std_uvloop.rs"
89+
harness = false
90+
required-features = ["async-std-runtime", "testing"]
91+
92+
[[test]]
93+
name = "test_tokio_current_thread_uvloop"
94+
path = "pytests/test_tokio_current_thread_uvloop.rs"
95+
harness = false
96+
required-features = ["tokio-runtime", "testing"]
97+
98+
[[test]]
99+
name = "test_tokio_multi_thread_uvloop"
100+
path = "pytests/test_tokio_multi_thread_uvloop.rs"
101+
harness = false
102+
required-features = ["tokio-runtime", "testing"]
103+
86104
[dependencies]
87105
clap = { version = "2.33", optional = true }
88106
futures = "0.3"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ pyo3 = "0.14"
482482
pyo3-asyncio = { version = "0.14", features = ["async-std-runtime"] }
483483
```
484484

485-
```rust
485+
```rust no_run
486486
//! main.rs
487487

488488
use pyo3::{prelude::*, types::PyType};

pytests/test_async_std_run_forever.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn main() {
4040

4141
event_loop.call_method0("run_forever")?;
4242

43-
println!("test test_run_forever ... ok");
43+
println!("test test_async_std_run_forever ... ok");
4444
Ok(())
4545
})
4646
.map_err(|e| Python::with_gil(|py| dump_err(py, e)))

pytests/test_async_std_uvloop.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use pyo3::{prelude::*, types::PyType};
2+
3+
#[cfg(not(target_os = "windows"))]
4+
fn main() -> PyResult<()> {
5+
pyo3::prepare_freethreaded_python();
6+
7+
Python::with_gil(|py| {
8+
let uvloop = py.import("uvloop")?;
9+
uvloop.call_method0("install")?;
10+
11+
// store a reference for the assertion
12+
let uvloop = PyObject::from(uvloop);
13+
14+
pyo3_asyncio::async_std::run(py, async move {
15+
// verify that we are on a uvloop.Loop
16+
Python::with_gil(|py| -> PyResult<()> {
17+
assert!(uvloop
18+
.as_ref(py)
19+
.getattr("Loop")?
20+
.downcast::<PyType>()
21+
.unwrap()
22+
.is_instance(pyo3_asyncio::async_std::get_current_loop(py)?)?);
23+
Ok(())
24+
})?;
25+
26+
async_std::task::sleep(std::time::Duration::from_secs(1)).await;
27+
28+
println!("test test_async_std_uvloop ... ok");
29+
Ok(())
30+
})
31+
})
32+
}
33+
34+
#[cfg(target_os = "windows")]
35+
fn main() {}

pytests/test_tokio_current_thread_run_forever.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ fn main() {
1212
});
1313

1414
tokio_run_forever::test_main();
15+
println!("test test_tokio_current_thread_run_forever ... ok");
1516
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use pyo3::{prelude::*, types::PyType};
2+
3+
#[cfg(not(target_os = "windows"))]
4+
fn main() -> PyResult<()> {
5+
pyo3::prepare_freethreaded_python();
6+
7+
let mut builder = tokio::runtime::Builder::new_current_thread();
8+
builder.enable_all();
9+
10+
pyo3_asyncio::tokio::init(builder);
11+
std::thread::spawn(move || {
12+
pyo3_asyncio::tokio::get_runtime().block_on(futures::future::pending::<()>());
13+
});
14+
15+
Python::with_gil(|py| {
16+
let uvloop = py.import("uvloop")?;
17+
uvloop.call_method0("install")?;
18+
19+
// store a reference for the assertion
20+
let uvloop = PyObject::from(uvloop);
21+
22+
pyo3_asyncio::tokio::run(py, async move {
23+
// verify that we are on a uvloop.Loop
24+
Python::with_gil(|py| -> PyResult<()> {
25+
assert!(uvloop
26+
.as_ref(py)
27+
.getattr("Loop")?
28+
.downcast::<PyType>()
29+
.unwrap()
30+
.is_instance(pyo3_asyncio::tokio::get_current_loop(py)?)?);
31+
Ok(())
32+
})?;
33+
34+
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
35+
36+
println!("test test_tokio_current_thread_uvloop ... ok");
37+
Ok(())
38+
})
39+
})
40+
}
41+
42+
#[cfg(target_os = "windows")]
43+
fn main() {}

pytests/test_tokio_multi_thread_run_forever.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ mod tokio_run_forever;
33
fn main() {
44
pyo3::prepare_freethreaded_python();
55
tokio_run_forever::test_main();
6+
println!("test test_tokio_multi_thread_run_forever ... ok");
67
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use pyo3::{prelude::*, types::PyType};
2+
3+
#[cfg(not(target_os = "windows"))]
4+
fn main() -> PyResult<()> {
5+
pyo3::prepare_freethreaded_python();
6+
7+
Python::with_gil(|py| {
8+
let uvloop = py.import("uvloop")?;
9+
uvloop.call_method0("install")?;
10+
11+
// store a reference for the assertion
12+
let uvloop = PyObject::from(uvloop);
13+
14+
pyo3_asyncio::tokio::run(py, async move {
15+
// verify that we are on a uvloop.Loop
16+
Python::with_gil(|py| -> PyResult<()> {
17+
assert!(uvloop
18+
.as_ref(py)
19+
.getattr("Loop")?
20+
.downcast::<PyType>()
21+
.unwrap()
22+
.is_instance(pyo3_asyncio::tokio::get_current_loop(py)?)?);
23+
Ok(())
24+
})?;
25+
26+
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
27+
28+
println!("test test_tokio_multi_thread_uvloop ... ok");
29+
Ok(())
30+
})
31+
})
32+
}
33+
34+
#[cfg(target_os = "windows")]
35+
fn main() {}

pytests/tokio_run_forever/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ pub(super) fn test_main() {
3838

3939
event_loop.call_method0("run_forever")?;
4040

41-
println!("test test_run_forever ... ok");
4241
Ok(())
4342
})
4443
.map_err(|e| Python::with_gil(|py| dump_err(py, e)))

0 commit comments

Comments
 (0)