Skip to content

Commit 12e4584

Browse files
committed
Move truncation next to other thread tests for tidy
1 parent 7280f3d commit 12e4584

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

library/std/src/sys/unix/thread.rs

-25
Original file line numberDiff line numberDiff line change
@@ -920,28 +920,3 @@ fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
920920
fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
921921
2048 // just a guess
922922
}
923-
924-
#[test]
925-
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "ios", target_os = "watchos"))]
926-
fn test_named_thread_truncation() {
927-
use crate::thread::{self, Builder};
928-
929-
let long_name = crate::iter::once("test_named_thread_truncation")
930-
.chain(crate::iter::repeat(" yada").take(100))
931-
.collect::<String>();
932-
933-
let result = Builder::new().name(long_name.clone()).spawn(move || {
934-
// Rust remembers the full thread name itself.
935-
assert_eq!(thread::current().name(), Some(long_name.as_str()));
936-
937-
// But the kernel is limited -- make sure we successfully set a truncation.
938-
let mut buf = vec![0u8; long_name.len() + 1];
939-
unsafe {
940-
libc::pthread_getname_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len());
941-
}
942-
let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
943-
assert!(cstr.to_bytes().len() > 0);
944-
assert!(long_name.as_bytes().starts_with(cstr.to_bytes()));
945-
});
946-
result.unwrap().join().unwrap();
947-
}

library/std/src/thread/tests.rs

+25
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,31 @@ fn test_named_thread() {
3737
.unwrap();
3838
}
3939

40+
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "ios", target_os = "watchos"))]
41+
#[test]
42+
fn test_named_thread_truncation() {
43+
use crate::ffi::CStr;
44+
45+
let long_name = crate::iter::once("test_named_thread_truncation")
46+
.chain(crate::iter::repeat(" yada").take(100))
47+
.collect::<String>();
48+
49+
let result = Builder::new().name(long_name.clone()).spawn(move || {
50+
// Rust remembers the full thread name itself.
51+
assert_eq!(thread::current().name(), Some(long_name.as_str()));
52+
53+
// But the system is limited -- make sure we successfully set a truncation.
54+
let mut buf = vec![0u8; long_name.len() + 1];
55+
unsafe {
56+
libc::pthread_getname_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len());
57+
}
58+
let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
59+
assert!(cstr.to_bytes().len() > 0);
60+
assert!(long_name.as_bytes().starts_with(cstr.to_bytes()));
61+
});
62+
result.unwrap().join().unwrap();
63+
}
64+
4065
#[test]
4166
#[should_panic]
4267
fn test_invalid_named_thread() {

0 commit comments

Comments
 (0)