diff --git a/README.md b/README.md index 5f8f44d..22b1bec 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,53 @@ Adds `shutdown_on_signal` and `shutdown_on_ctrl_c` to the scheduler. Both shuts the system down (stops the scheduler, removes all the tasks) when a signal was received. +## Writing tests + +When doing a tokio::test, remember to have it run in a multi-threaded context otherwise the test +will hang on `scheduler.add()`. + +For example: + +```rust + +#[cfg(test)] +mod test { + use tokio_cron_scheduler::{Job, JobScheduler}; + use tracing::{info, Level}; + use tracing_subscriber::FmtSubscriber; + + // Needs multi_thread to test, otherwise it hangs on scheduler.add() + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + // #[tokio::test] + async fn test_schedule() { + let subscriber = FmtSubscriber::builder() + .with_max_level(Level::TRACE) + .finish(); + tracing::subscriber::set_global_default(subscriber) + .expect("Setting default subscriber failed"); + + info!("Create scheduler"); + let scheduler = JobScheduler::new().await.unwrap(); + info!("Add job"); + scheduler + .add( + Job::new_async("*/1 * * * * *", |_, _| { + Box::pin(async { + info!("Run every seconds"); + }) + }) + .unwrap(), + ) + .await + .expect("Should be able to add a job"); + + scheduler.start().await.unwrap(); + + tokio::time::sleep(core::time::Duration::from_secs(20)).await; + } +} +``` + ## Examples ### simple