Skip to content

Commit

Permalink
Add test example to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
mvniekerk committed Dec 30, 2022
1 parent 7274e40 commit c0eb657
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c0eb657

Please sign in to comment.