Skip to content

Commit 637adf6

Browse files
author
Andrew J Westlake
committed
Reviewed docs and made some corrections
1 parent 053c785 commit 637adf6

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

src/async_std.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub mod testing {
149149
//! pyo3-asyncio = { version = "0.13", features = ["testing", "async-std-runtime"] }
150150
//! ```
151151
//!
152-
//! Now, in your test's main file, call [`async_std::testing::test_main`]:
152+
//! Now, in your test's main file, call [`crate::async_std::testing::test_main`]:
153153
//!
154154
//! ```no_run
155155
//! fn main() {
@@ -200,11 +200,10 @@ pub mod testing {
200200
Test::new_async(name, async move { task::spawn_blocking(func).await })
201201
}
202202

203-
/// Default main function for the test harness.
203+
/// Default main function for the async-std test harness.
204204
///
205205
/// This is meant to perform the necessary initialization for most test cases. If you want
206-
/// additional control over the initialization (i.e. env_logger initialization), you can use this
207-
/// function as a template.
206+
/// additional control over the initialization, you can use this function as a template.
208207
pub fn test_main(suite_name: &str, tests: Vec<Test>) {
209208
generic::testing::test_main::<AsyncStdRuntime>(suite_name, tests)
210209
}

src/generic.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,19 @@ pub trait JoinError {
1313

1414
/// Generic Rust async/await runtime
1515
pub trait Runtime {
16-
/// The type of errors that a JoinHandle can return after awaited
16+
/// The error returned by a JoinHandle after being awaited
1717
type JoinError: JoinError + Send;
1818
/// A future that completes with the result of the spawned task
1919
type JoinHandle: Future<Output = Result<(), Self::JoinError>> + Send;
2020

21-
/// Spawn a function onto this runtime's event loop
21+
/// Spawn a future onto this runtime's event loop
2222
fn spawn<F>(fut: F) -> Self::JoinHandle
2323
where
2424
F: Future<Output = ()> + Send + 'static;
2525
}
2626

2727
/// Run the event loop until the given Future completes
2828
///
29-
/// The event loop runs until the given future is complete.
30-
///
3129
/// After this function returns, the event loop can be resumed with either [`run_until_complete`] or
3230
/// [`crate::run_forever`]
3331
///
@@ -264,7 +262,7 @@ where
264262
Ok(future_rx)
265263
}
266264

267-
/// <span class="module-item stab portability" style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"><code>testing</code></span> Testing Utilities for the Tokio runtime.
265+
/// <span class="module-item stab portability" style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"><code>testing</code></span> Testing utilities for generic runtimes.
268266
#[cfg(feature = "testing")]
269267
pub mod testing {
270268
use pyo3::prelude::*;
@@ -276,10 +274,10 @@ pub mod testing {
276274
with_runtime,
277275
};
278276

279-
/// Default main function for the test harness.
277+
/// Default main function for the generic test harness.
280278
///
281279
/// This is meant to perform the necessary initialization for most test cases. If you want
282-
/// additional control over the initialization (i.e. env_logger initialization), you can use this
280+
/// additional control over the initialization, you can use this
283281
/// function as a template.
284282
pub fn test_main<R>(suite_name: &str, tests: Vec<Test>)
285283
where

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! Python's coroutines cannot be directly spawned on a Rust event loop. The two coroutine models
1616
//! require some additional assistance from their event loops, so in all likelihood they will need
1717
//! a new _unique_ event loop that addresses the needs of both languages if the coroutines are to
18-
//! be run on the same event loop.
18+
//! be run on the same loop.
1919
//!
2020
//! It's not immediately clear that this would provide worthwhile performance wins either, so in the
2121
//! interest of keeping things simple, this crate creates and manages the Python event loop and
@@ -218,8 +218,8 @@ pub fn get_event_loop(py: Python) -> &PyAny {
218218
/// # Python::with_gil(|py| {
219219
/// # pyo3_asyncio::with_runtime(py, || {
220220
/// // Wait 1 second, then stop the event loop
221-
/// tokio::spawn(async move {
222-
/// tokio::time::sleep(Duration::from_secs(1)).await;
221+
/// async_std::task::spawn(async move {
222+
/// async_std::task::sleep(Duration::from_secs(1)).await;
223223
/// Python::with_gil(|py| {
224224
/// let event_loop = pyo3_asyncio::get_event_loop(py);
225225
///

src/testing.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
//! This module provides some utilities for parsing test arguments as well as running and filtering
44
//! a sequence of tests.
55
//!
6+
//! > These utilities are completely independent of the runtime being used. To
7+
//! > get a more complete picture of how to run tests for PyO3 Asyncio applications, see
8+
//! > [`crate::async_std::testing`] or [`crate::tokio::testing`] for more details.
9+
//!
610
//! As mentioned [here](crate#pythons-event-loop), PyO3 Asyncio tests cannot use the default test
711
//! harness since it doesn't allow Python to gain control over the main thread. Instead, we have to
812
//! provide our own test harness in order to create integration tests.

src/tokio.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub fn init_multi_thread() {
100100
/// #
101101
/// # Python::with_gil(|py| {
102102
/// # pyo3_asyncio::with_runtime(py, || {
103-
/// pyo3_asyncio::tokio::init_current_thread();
103+
/// # pyo3_asyncio::tokio::init_current_thread();
104104
/// pyo3_asyncio::tokio::run_until_complete(py, async move {
105105
/// tokio::time::sleep(Duration::from_secs(1)).await;
106106
/// Ok(())
@@ -199,10 +199,13 @@ where
199199
/// pyo3-asyncio = { version = "0.13", features = ["testing", "tokio-runtime"] }
200200
/// ```
201201
///
202-
/// Now, in your test's main file, call [`crate::tokio::testing::test_main`]:
202+
/// Now, in your test's main file, initialize the tokio runtime and call
203+
/// [`crate::tokio::testing::test_main`]:
203204
///
204205
/// ```no_run
205206
/// fn main() {
207+
/// pyo3_asyncio::tokio::init_current_thread();
208+
///
206209
/// pyo3_asyncio::tokio::testing::test_main("Example Test Suite", vec![]);
207210
/// }
208211
/// ```
@@ -215,6 +218,8 @@ where
215218
/// use pyo3_asyncio::testing::Test;
216219
///
217220
/// fn main() {
221+
/// pyo3_asyncio::tokio::init_current_thread();
222+
///
218223
/// pyo3_asyncio::tokio::testing::test_main(
219224
/// "Example Test Suite",
220225
/// vec![
@@ -259,7 +264,7 @@ pub mod testing {
259264
/// additional control over the initialization (i.e. env_logger initialization), you can use this
260265
/// function as a template.
261266
///
262-
/// Note: The tokio runtime must be initialized before calling this function!
267+
/// > _The tokio runtime must be initialized before calling this function!_
263268
pub fn test_main(suite_name: &str, tests: Vec<Test>) {
264269
Python::with_gil(|py| {
265270
with_runtime(py, || {

0 commit comments

Comments
 (0)