Skip to content

Commit

Permalink
Make tokio unstable opt in (#73)
Browse files Browse the repository at this point in the history
* Make tokio unstable opt in

We use `unhandled_panic` to forward host software panics as test
failures.

Forcing this is a bit invasive for consumers of the crate, so
instead we make it enabled conditionally.

* conditional compilation
  • Loading branch information
mcches authored Jan 10, 2023
1 parent b412c06 commit fcf338e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ sim.client("client", async move {
sim.run();
```

`RUSTFLAG="--cfg tokio_unstable"` is necessary to compile the project.

See `ping_pong` in [udp.rs](tests/udp.rs) for a networking example. For more
examples, check out the [tests](tests) directory.

### tokio_unstable

Turmoil uses [unhandled_panic] to forward host panics as test failures. See
[unstable features] to opt in.

[unhandled_panic]: https://docs.rs/tokio/latest/tokio/runtime/struct.Builder.html#method.unhandled_panic
[unstable features]: https://docs.rs/tokio/latest/tokio/#unstable-features

## License

This project is licensed under the [MIT license](LICENSE).
Expand Down
15 changes: 9 additions & 6 deletions src/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ impl Rt {
}

fn init() -> (Runtime, LocalSet) {
let tokio = tokio::runtime::Builder::new_current_thread()
.enable_time()
.start_paused(true)
.unhandled_panic(tokio::runtime::UnhandledPanic::ShutdownRuntime)
.build()
.unwrap();
let mut builder = tokio::runtime::Builder::new_current_thread();

#[cfg(tokio_unstable)]
builder.unhandled_panic(tokio::runtime::UnhandledPanic::ShutdownRuntime);

let tokio = builder.enable_time().start_paused(true).build().unwrap();

tokio.block_on(async {
// Sleep to "round" `Instant::now()` to the closest `ms`
Expand All @@ -100,6 +100,9 @@ fn init() -> (Runtime, LocalSet) {

fn new_local() -> LocalSet {
let mut local = LocalSet::new();

#[cfg(tokio_unstable)]
local.unhandled_panic(tokio::runtime::UnhandledPanic::ShutdownRuntime);

local
}

0 comments on commit fcf338e

Please sign in to comment.