Skip to content

Commit

Permalink
tweak tests a bit for resilience
Browse files Browse the repository at this point in the history
  • Loading branch information
jlizen committed Dec 20, 2024
1 parent 0e1440c commit 6cca3c3
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 21 deletions.
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,7 @@ impl GlobalStrategyBuilder {
/// Intended for injecting arbitrary runtimes/strategies or customizing existing ones.
///
/// # Cancellation
/// Yes, the future is dropped if the caller drops the returned future from spawn_compute_heavy_future(),
/// unless you spawn an uncancellable task within it.
///
/// Yes, the closure's returned future is dropped if the caller drops the returned future from [`spawn_compute_heavy_future()`].
/// Note that it will only be dropped across yield points in the case of long-blocking futures.
///
/// ## Error
Expand Down Expand Up @@ -550,7 +548,7 @@ where
}
}

pub(crate) fn make_future_cancellable<F, O>(fut: F) -> (impl Future<Output = ()>, Receiver<O>)
pub fn make_future_cancellable<F, O>(fut: F) -> (impl Future<Output = ()>, Receiver<O>)
where
F: std::future::Future<Output = O> + Send + 'static,
O: Send + 'static,
Expand Down
2 changes: 1 addition & 1 deletion tests/block_in_place_concurrency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn block_in_place_concurrency() {
join_all(futures).await;

let elapsed_millis = start.elapsed().as_millis();
assert!(elapsed_millis < 40, "futures did not run concurrently");
assert!(elapsed_millis < 50, "futures did not run concurrently");

assert!(elapsed_millis > 20, "futures exceeded max concurrency");
}
4 changes: 2 additions & 2 deletions tests/current_context_cancellable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ async fn current_context_strategy() {
};

select! {
_ = tokio::time::sleep(Duration::from_millis(10)) => { },
_ = tokio::time::sleep(Duration::from_millis(4)) => { },
_ = spawn_compute_heavy_future(future) => {}
}

tokio::time::sleep(Duration::from_millis(10)).await;
tokio::time::sleep(Duration::from_millis(8)).await;

// future should have been cancelled when spawn compute heavy future was dropped
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion tests/current_context_concurrency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn current_context_concurrency() {
join_all(futures).await;

let elapsed_millis = start.elapsed().as_millis();
assert!(elapsed_millis < 40, "futures did not run concurrently");
assert!(elapsed_millis < 50, "futures did not run concurrently");

assert!(elapsed_millis > 20, "futures exceeded max concurrency");
}
9 changes: 5 additions & 4 deletions tests/custom_executor_cancellable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ async fn custom_strategy_cancellable() {

let closure: CustomExecutorClosure = Box::new(|fut| {
Box::new(async move {
let handle = tokio::task::spawn(async move { fut.await });
handle.await.map_err(|err| err.into())
tokio::task::spawn(async move { fut.await })
.await
.map_err(|err| err.into())
})
});
global_strategy_builder()
Expand All @@ -28,11 +29,11 @@ async fn custom_strategy_cancellable() {
};

select! {
_ = tokio::time::sleep(Duration::from_millis(10)) => { },
_ = tokio::time::sleep(Duration::from_millis(4)) => { },
_ = spawn_compute_heavy_future(future) => {}
}

tokio::time::sleep(Duration::from_millis(10)).await;
tokio::time::sleep(Duration::from_millis(8)).await;

// future should have been cancelled when spawn compute heavy future was dropped
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion tests/custom_executor_concurrency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async fn custom_strategy_concurrency() {
join_all(futures).await;

let elapsed_millis = start.elapsed().as_millis();
assert!(elapsed_millis < 40, "futures did not run concurrently");
assert!(elapsed_millis < 50, "futures did not run concurrently");

assert!(elapsed_millis > 20, "futures exceeded max concurrency");
}
5 changes: 3 additions & 2 deletions tests/custom_executor_tokio_spawn_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ async fn custom_strategy_tokio_spawn() {

let closure: CustomExecutorClosure = Box::new(|fut| {
Box::new(async move {
let handle = tokio::task::spawn(async move { fut.await });
handle.await.map_err(|err| err.into())
tokio::task::spawn(async move { fut.await })
.await
.map_err(|err| err.into())
})
});
global_strategy_builder()
Expand Down
4 changes: 2 additions & 2 deletions tests/secondary_tokio_cancellable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ async fn secondary_tokio_runtime_strategy_cancel_safe() {
};

select! {
_ = tokio::time::sleep(Duration::from_millis(10)) => { },
_ = tokio::time::sleep(Duration::from_millis(4)) => { },
_ = spawn_compute_heavy_future(future) => {}
}

tokio::time::sleep(Duration::from_millis(10)).await;
tokio::time::sleep(Duration::from_millis(8)).await;

// future should have been cancelled when spawn compute heavy future was dropped
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion tests/secondary_tokio_concurrencyrs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn secondary_tokio_runtime_concurrency() {
join_all(futures).await;

let elapsed_millis = start.elapsed().as_millis();
assert!(elapsed_millis < 40, "futures did not run concurrently");
assert!(elapsed_millis < 50, "futures did not run concurrently");

assert!(elapsed_millis > 20, "futures exceeded max concurrency");
}
4 changes: 2 additions & 2 deletions tests/spawn_blocking_cancellable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ async fn spawn_blocking_strategy_cancellable() {
};

select! {
_ = tokio::time::sleep(Duration::from_millis(10)) => { },
_ = tokio::time::sleep(Duration::from_millis(4)) => { },
_ = spawn_compute_heavy_future(future) => {}
}

tokio::time::sleep(Duration::from_millis(10)).await;
tokio::time::sleep(Duration::from_millis(8)).await;

// future should have been cancelled when spawn compute heavy future was dropped
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion tests/spawn_blocking_concurrency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn spawn_blocking_concurrency() {
join_all(futures).await;

let elapsed_millis = start.elapsed().as_millis();
assert!(elapsed_millis < 40, "futures did not run concurrently");
assert!(elapsed_millis < 60, "futures did not run concurrently");

assert!(elapsed_millis > 20, "futures exceeded max concurrency");
}

0 comments on commit 6cca3c3

Please sign in to comment.