Skip to content

Commit

Permalink
migrate tests to integ tests to run as separate processes
Browse files Browse the repository at this point in the history
  • Loading branch information
jlizen committed Dec 19, 2024
1 parent 710609f commit 07da544
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 180 deletions.
181 changes: 1 addition & 180 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,183 +363,4 @@ where
}
}

// Use nextest to run these, otherwise there will be race conditions against the strategy oncecell
#[cfg(test)]
mod tests {
use std::any::Any;

use super::*;

#[cfg(not(feature = "tokio"))]
#[tokio::test]
async fn default_to_current_context_non_tokio() {
let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

assert!(matches!(COMPUTE_HEAVY_FUTURE_EXECUTOR_STRATEGY.get(), Some(&ExecutorStrategy::CurrentContext(..))));
}

#[cfg(feature = "tokio")]
#[tokio::test]
async fn default_to_current_context_tokio_single_threaded() {
// this is a tokio test but we haven't enabled the tokio config flag

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

assert!(matches!(COMPUTE_HEAVY_FUTURE_EXECUTOR_STRATEGY.get(), Some(&ExecutorStrategy::SpawnBlocking(..))));
}

#[cfg(feature = "tokio")]
#[tokio::test(flavor = "multi_thread")]
async fn default_to_current_context_tokio_multi_threaded() {
let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

assert!(matches!(COMPUTE_HEAVY_FUTURE_EXECUTOR_STRATEGY.get(), Some(&ExecutorStrategy::BlockInPlace(..))));
}

#[tokio::test]
async fn current_context_strategy() {
initialize_current_context_strategy();

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

assert!(matches!(COMPUTE_HEAVY_FUTURE_EXECUTOR_STRATEGY.get(), Some(&ExecutorStrategy::CurrentContext(..))));
}

#[tokio::test]
async fn custom_strategy_legal_closure() {
let closure: CustomExecutorClosure = Box::new(|fut| {
Box::new(async move {
let res = fut.await;
Ok(res)
})
});

initialize_custom_executor_strategy(closure).await;

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

assert!(matches!(COMPUTE_HEAVY_FUTURE_EXECUTOR_STRATEGY.get(), Some(&ExecutorStrategy::CustomExecutor(..))));
}

#[cfg(feature = "tokio")]
#[tokio::test]
async fn custom_strategy_legal_closure_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())
}
)
});
initialize_custom_executor_strategy(closure).await;

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

assert!(matches!(COMPUTE_HEAVY_FUTURE_EXECUTOR_STRATEGY.get(), Some(&ExecutorStrategy::CustomExecutor(..))));
}

#[tokio::test]
#[should_panic]
async fn custom_strategy_illegal_closure() {
// this closure overwrites the input type, causing a mismatch
let closure: CustomExecutorClosure = Box::new(|_| {
Box::new(async move {
Ok(Box::new("foo") as Box<dyn Any + Send>)
})
});
// this should panic due to bad closure
initialize_custom_executor_strategy(closure).await;
}

#[cfg(feature = "tokio")]
#[tokio::test]
async fn spawn_blocking_strategy() {
initialize_spawn_blocking_strategy();

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

assert!(matches!(COMPUTE_HEAVY_FUTURE_EXECUTOR_STRATEGY.get(), Some(&ExecutorStrategy::SpawnBlocking(..))));
}

#[cfg(feature = "tokio")]
#[tokio::test(flavor = "multi_thread")]
async fn block_in_place_strategy() {
initialize_block_in_place_strategy();

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

assert!(matches!(COMPUTE_HEAVY_FUTURE_EXECUTOR_STRATEGY.get(), Some(&ExecutorStrategy::BlockInPlace(..))));
}

#[cfg(feature = "tokio")]
#[tokio::test]
async fn secondary_tokio_runtime_strategy() {
initialize_secondary_tokio_runtime_strategy();

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

assert!(matches!(COMPUTE_HEAVY_FUTURE_EXECUTOR_STRATEGY.get(), Some(&ExecutorStrategy::SecondaryTokioRuntime(..))));
}

#[cfg(feature = "tokio")]
#[tokio::test]
async fn secondary_tokio_runtime_strategy_allowed_config() {
initialize_secondary_tokio_runtime_strategy_and_config(Some(5), Some(50));

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

assert!(matches!(COMPUTE_HEAVY_FUTURE_EXECUTOR_STRATEGY.get(), Some(&ExecutorStrategy::SecondaryTokioRuntime(..))));
}

#[cfg(feature = "tokio")]
#[tokio::test]
#[should_panic]
async fn secondary_tokio_runtime_strategy_disallowed_config() {
initialize_secondary_tokio_runtime_strategy_and_config(Some(30), Some(50));

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

assert!(matches!(COMPUTE_HEAVY_FUTURE_EXECUTOR_STRATEGY.get(), Some(&ExecutorStrategy::SecondaryTokioRuntime(..))));
}

#[tokio::test]
#[should_panic]
async fn multiple_initializes_panic() {
initialize_current_context_strategy();
initialize_current_context_strategy();
}
}
// tests are in /tests/ to allow seperate intialization of oncelock when using default cargo test runner
12 changes: 12 additions & 0 deletions tests/block_in_place_strategy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[cfg(feature = "tokio")]
#[tokio::test(flavor = "multi_thread")]
async fn block_in_place_strategy() {
use compute_heavy_future_executor::{initialize_block_in_place_strategy, spawn_compute_heavy_future};

initialize_block_in_place_strategy();

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);
}
11 changes: 11 additions & 0 deletions tests/current_context_strategy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use compute_heavy_future_executor::{initialize_current_context_strategy, spawn_compute_heavy_future};

#[tokio::test]
async fn current_context_strategy() {
initialize_current_context_strategy();

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);
}
16 changes: 16 additions & 0 deletions tests/custom_closure_strategy_illegal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::any::Any;

use compute_heavy_future_executor::{initialize_custom_executor_strategy, CustomExecutorClosure};

#[tokio::test]
#[should_panic]
async fn custom_strategy_illegal_closure() {
// this closure overwrites the input type, causing a mismatch
let closure: CustomExecutorClosure = Box::new(|_| {
Box::new(async move {
Ok(Box::new("foo") as Box<dyn Any + Send>)
})
});
// this should panic due to bad closure
initialize_custom_executor_strategy(closure).await;
}
18 changes: 18 additions & 0 deletions tests/custom_closure_strategy_legal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use compute_heavy_future_executor::{initialize_custom_executor_strategy, spawn_compute_heavy_future, CustomExecutorClosure};

#[tokio::test]
async fn custom_strategy_legal_closure() {
let closure: CustomExecutorClosure = Box::new(|fut| {
Box::new(async move {
let res = fut.await;
Ok(res)
})
});

initialize_custom_executor_strategy(closure).await;

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);
}
20 changes: 20 additions & 0 deletions tests/custom_closure_strategy_legal_tokio_spawn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[cfg(feature = "tokio")]
#[tokio::test]
async fn custom_strategy_legal_closure_tokio_spawn() {
use compute_heavy_future_executor::{initialize_custom_executor_strategy, spawn_compute_heavy_future, CustomExecutorClosure};

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())
}
)
});
initialize_custom_executor_strategy(closure).await;

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);
}
13 changes: 13 additions & 0 deletions tests/default_current_thread_tokio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[cfg(feature = "tokio")]
#[tokio::test]
async fn default_to_current_context_tokio_single_threaded() {
use compute_heavy_future_executor::spawn_compute_heavy_future;

// this is a tokio test but we haven't enabled the tokio config flag

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

}
10 changes: 10 additions & 0 deletions tests/default_multi_threaded_tokio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[cfg(feature = "tokio")]
#[tokio::test(flavor = "multi_thread")]
async fn default_to_current_context_tokio_multi_threaded() {
use compute_heavy_future_executor::spawn_compute_heavy_future;

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);
}
10 changes: 10 additions & 0 deletions tests/default_no_tokio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[cfg(not(feature = "tokio"))]
#[tokio::test]
async fn default_to_current_context_non_tokio() {
use compute_heavy_future_executor::{initialize_current_context_strategy, spawn_compute_heavy_future};

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);
}
14 changes: 14 additions & 0 deletions tests/multiple_initializes_panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use compute_heavy_future_executor::{initialize_spawn_blocking_strategy, spawn_compute_heavy_future};


#[cfg(feature = "tokio")]
#[tokio::test]
async fn spawn_blocking_strategy() {
initialize_spawn_blocking_strategy();

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

}
12 changes: 12 additions & 0 deletions tests/secondary_tokio_runtime_strategy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[cfg(feature = "tokio")]
#[tokio::test]
async fn secondary_tokio_runtime_strategy() {
use compute_heavy_future_executor::{initialize_secondary_tokio_runtime_strategy, spawn_compute_heavy_future};

initialize_secondary_tokio_runtime_strategy();

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);
}
8 changes: 8 additions & 0 deletions tests/secondary_tokio_runtime_strategy_allowed_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use compute_heavy_future_executor::initialize_current_context_strategy;

#[tokio::test]
#[should_panic]
async fn multiple_initializes_panic() {
initialize_current_context_strategy();
initialize_current_context_strategy();
}
13 changes: 13 additions & 0 deletions tests/secondary_tokio_runtime_strategy_disallowed_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[cfg(feature = "tokio")]
#[tokio::test]
#[should_panic]
async fn secondary_tokio_runtime_strategy_disallowed_config() {
use compute_heavy_future_executor::{initialize_secondary_tokio_runtime_strategy_and_config, spawn_compute_heavy_future};

initialize_secondary_tokio_runtime_strategy_and_config(Some(30), Some(50));

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);
}
14 changes: 14 additions & 0 deletions tests/spawn_blocking_strategy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use compute_heavy_future_executor::{initialize_spawn_blocking_strategy, spawn_compute_heavy_future};


#[cfg(feature = "tokio")]
#[tokio::test]
async fn spawn_blocking_strategy() {
initialize_spawn_blocking_strategy();

let future = async { 5 };

let res = spawn_compute_heavy_future(future).await.unwrap();
assert_eq!(res, 5);

}

0 comments on commit 07da544

Please sign in to comment.