about summary refs log tree commit diff
path: root/library/std/src/thread
diff options
context:
space:
mode:
authorJosh Stone <jistone@redhat.com>2022-10-21 18:13:22 -0700
committerJosh Stone <jistone@redhat.com>2022-10-21 18:13:22 -0700
commit12e45846ebbc32bd6a56f2de2658d3d9ad459032 (patch)
tree20e8f242d482e0bd272783f6bfdbf0b713fd7fe2 /library/std/src/thread
parent7280f3d28aa139cec0c75072a3e66294b7f99b59 (diff)
downloadrust-12e45846ebbc32bd6a56f2de2658d3d9ad459032.tar.gz
rust-12e45846ebbc32bd6a56f2de2658d3d9ad459032.zip
Move truncation next to other thread tests for tidy
Diffstat (limited to 'library/std/src/thread')
-rw-r--r--library/std/src/thread/tests.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/library/std/src/thread/tests.rs b/library/std/src/thread/tests.rs
index dfb8765ab4e..71eb41cd564 100644
--- a/library/std/src/thread/tests.rs
+++ b/library/std/src/thread/tests.rs
@@ -37,6 +37,31 @@ fn test_named_thread() {
         .unwrap();
 }
 
+#[cfg(any(target_os = "linux", target_os = "macos", target_os = "ios", target_os = "watchos"))]
+#[test]
+fn test_named_thread_truncation() {
+    use crate::ffi::CStr;
+
+    let long_name = crate::iter::once("test_named_thread_truncation")
+        .chain(crate::iter::repeat(" yada").take(100))
+        .collect::<String>();
+
+    let result = Builder::new().name(long_name.clone()).spawn(move || {
+        // Rust remembers the full thread name itself.
+        assert_eq!(thread::current().name(), Some(long_name.as_str()));
+
+        // But the system is limited -- make sure we successfully set a truncation.
+        let mut buf = vec![0u8; long_name.len() + 1];
+        unsafe {
+            libc::pthread_getname_np(libc::pthread_self(), buf.as_mut_ptr().cast(), buf.len());
+        }
+        let cstr = CStr::from_bytes_until_nul(&buf).unwrap();
+        assert!(cstr.to_bytes().len() > 0);
+        assert!(long_name.as_bytes().starts_with(cstr.to_bytes()));
+    });
+    result.unwrap().join().unwrap();
+}
+
 #[test]
 #[should_panic]
 fn test_invalid_named_thread() {