Skip to content

Commit b895d63

Browse files
committed
Added a test for a short thread name
1 parent 8b4e661 commit b895d63

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

tests/pass-dep/libc/threadname.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ffi::CStr;
33
use std::thread;
44

55
fn main() {
6+
let short_name = "test_named".to_owned();
67
let long_name = std::iter::once("test_named_thread_truncation")
78
.chain(std::iter::repeat(" yada").take(100))
89
.collect::<String>();
@@ -54,24 +55,37 @@ fn main() {
5455
}
5556
}
5657

57-
let result = thread::Builder::new().name(long_name.clone()).spawn(move || {
58-
// Rust remembers the full thread name itself.
59-
assert_eq!(thread::current().name(), Some(long_name.as_str()));
58+
fn test_using(name: String) {
59+
let result = thread::Builder::new().name(name.clone()).spawn(move || {
60+
assert_eq!(thread::current().name(), Some(name.as_str()));
6061

61-
// But the system is limited -- make sure we successfully set a truncation.
62-
let mut buf = vec![0u8; long_name.len() + 1];
63-
assert_eq!(get_thread_name(&mut buf), 0);
64-
let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
65-
assert!(cstr.to_bytes().len() >= 15, "name is too short: len={}", cstr.to_bytes().len()); // POSIX seems to promise at least 15 chars
66-
assert!(long_name.as_bytes().starts_with(cstr.to_bytes()));
62+
let mut buf = vec![0u8; name.len() + 1];
63+
assert_eq!(get_thread_name(&mut buf), 0);
64+
let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
65+
if name.len() >= 15 {
66+
assert!(
67+
cstr.to_bytes().len() >= 15,
68+
"name is too short: len={}",
69+
cstr.to_bytes().len()
70+
); // POSIX seems to promise at least 15 chars
71+
assert!(name.as_bytes().starts_with(cstr.to_bytes()));
72+
} else {
73+
assert_eq!(name.as_bytes(), cstr.to_bytes());
74+
}
75+
76+
// Also test directly calling pthread_setname to check its return value.
77+
assert_eq!(set_thread_name(&cstr), 0);
78+
// But with a too long name it should fail except:
79+
// * on FreeBSD where the function has no return, hence cannot indicate failure,
80+
// * on Android where prctl silently truncates the string.
81+
#[cfg(not(any(target_os = "freebsd", target_os = "android")))]
82+
assert_ne!(set_thread_name(&std::ffi::CString::new(name).unwrap()), 0);
83+
});
84+
result.unwrap().join().unwrap();
85+
}
6786

68-
// Also test directly calling pthread_setname to check its return value.
69-
assert_eq!(set_thread_name(&cstr), 0);
70-
// But with a too long name it should fail except:
71-
// * on FreeBSD where the function has no return, hence cannot indicate failure,
72-
// * on Android where prctl silently truncates the string.
73-
#[cfg(not(any(target_os = "freebsd", target_os = "android")))]
74-
assert_ne!(set_thread_name(&std::ffi::CString::new(long_name).unwrap()), 0);
75-
});
76-
result.unwrap().join().unwrap();
87+
test_using(short_name);
88+
// Rust remembers the full thread name itself.
89+
// But the system is limited -- make sure we successfully set a truncation.
90+
test_using(long_name);
7791
}

0 commit comments

Comments
 (0)