move thread-panic tests to their own file; test getting the thread name

This commit is contained in:
Ralf Jung 2024-03-03 09:00:38 +01:00
parent be5da3a4aa
commit 8deb651603
5 changed files with 51 additions and 24 deletions

View file

@ -45,23 +45,6 @@ fn create_move_out() {
assert_eq!(result.len(), 6);
}
fn panic() {
let result = thread::spawn(|| panic!("Hello!")).join().unwrap_err();
let msg = result.downcast_ref::<&'static str>().unwrap();
assert_eq!(*msg, "Hello!");
}
fn panic_named() {
thread::Builder::new()
.name("childthread".to_string())
.spawn(move || {
panic!("Hello, world!");
})
.unwrap()
.join()
.unwrap_err();
}
// This is not a data race!
fn shared_readonly() {
use std::sync::Arc;
@ -89,6 +72,4 @@ fn main() {
create_move_in();
create_move_out();
shared_readonly();
panic();
panic_named();
}

View file

@ -1,5 +0,0 @@
thread '<unnamed>' panicked at $DIR/simple.rs:LL:CC:
Hello!
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'childthread' panicked at $DIR/simple.rs:LL:CC:
Hello, world!

View file

@ -0,0 +1,21 @@
use std::thread;
fn main() {
// When we have not set the name...
thread::spawn(|| {
assert!(thread::current().name().is_none());
});
// ... and when we have set it.
thread::Builder::new()
.name("childthread".to_string())
.spawn(move || {
assert_eq!(thread::current().name().unwrap(), "childthread");
})
.unwrap()
.join()
.unwrap();
// Also check main thread name.
assert_eq!(thread::current().name().unwrap(), "main");
}

View file

@ -0,0 +1,25 @@
//! Panicking in other threads.
use std::thread;
fn panic() {
let result = thread::spawn(|| panic!("Hello!")).join().unwrap_err();
let msg = result.downcast_ref::<&'static str>().unwrap();
assert_eq!(*msg, "Hello!");
}
fn panic_named() {
thread::Builder::new()
.name("childthread".to_string())
.spawn(move || {
panic!("Hello, world!");
})
.unwrap()
.join()
.unwrap_err();
}
fn main() {
panic();
panic_named();
}

View file

@ -0,0 +1,5 @@
thread '<unnamed>' panicked at $DIR/thread_panic.rs:LL:CC:
Hello!
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'childthread' panicked at $DIR/thread_panic.rs:LL:CC:
Hello, world!