diff --git a/README.md b/README.md index 5c916ed..5f8f44d 100644 --- a/README.md +++ b/README.md @@ -249,7 +249,7 @@ POSTGRES_INIT_METADATA=true POSTGRES_INIT_NOTIFICATIONS=true cargo run --example ### nats -Needs a running Nats instance first with Jetream enabled: +Needs a running Nats instance first with Jetstream enabled: ```shell docker run --rm -it -p 4222:4222 -p 6222:6222 -p 7222:7222 -p 8222:8222 nats -js -DV ``` diff --git a/examples/lib.rs b/examples/lib.rs index 29e0e2e..db81375 100644 --- a/examples/lib.rs +++ b/examples/lib.rs @@ -161,3 +161,40 @@ pub async fn run_example(mut sched: JobScheduler) -> Result<()> { fn main() { eprintln!("Should not be run on its own."); } + +#[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; + } +}