Skip to content

Commit

Permalink
Added example tokio test
Browse files Browse the repository at this point in the history
  • Loading branch information
mvniekerk committed Dec 30, 2022
1 parent 02f4ec3 commit 7274e40
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
37 changes: 37 additions & 0 deletions examples/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 7274e40

Please sign in to comment.