jhpratt / num_threads

Obtain the number of threads in the current process
https://docs.rs/num_threads
Apache License 2.0
12 stars 7 forks source link

cargo test fails due to other threads from cargo #11

Closed ecnelises closed 2 years ago

ecnelises commented 2 years ago

Run cargo test on both x86_64-linux and aarch64-darwin:

running 2 tests
test test::is_single_threaded ... FAILED
test test::num_threads ... FAILED

failures:

---- test::is_single_threaded stdout ----
thread 'test::is_single_threaded' panicked at 'assertion failed: `(left == right)`
  left: `Some(false)`,
 right: `Some(true)`', src/lib.rs:56:13

---- test::num_threads stdout ----
thread 'test::num_threads' panicked at 'assertion failed: `(left == right)`
  left: `Some(3)`,
 right: `Some(1)`', src/lib.rs:44:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

failures:
    test::is_single_threaded
    test::num_threads

Commenting out is_single_threaded, I'll see Some(2) from num_threads. So I guess that's because cargo or rustc has a main thread and the cases are executed in respective threads.

Can we specify RUST_TEST_THREADS=1 by some way in tests?

jhpratt commented 2 years ago

cargo test -- --test-threads=1 is what you need. I just added a test that gives a helpful message on failure.