From b5e97708f9f6f0ae9a57965ea3cb4779827fcd22 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 6 May 2023 15:58:54 +0200 Subject: [PATCH] test_unix_msg: Remove thread synchronisation This refactors the test_unix_msg() test a bit. No change in behaviour is intended. Previously, two threads were started in parallel. The server thread used a mutex and a condition variable to indicate that it set up its listening socket. The client thread waited for this signal before it attempted to connect. This commit makes the code instead bind the server socket before starting the worker threads. That way, no synchronisation is necessary. Signed-off-by: Uli Schlachter --- tests/net/unix.rs | 77 ++++++++++++++--------------------------------- 1 file changed, 22 insertions(+), 55 deletions(-) diff --git a/tests/net/unix.rs b/tests/net/unix.rs index b69fc8453..510b5f5c7 100644 --- a/tests/net/unix.rs +++ b/tests/net/unix.rs @@ -150,31 +150,22 @@ fn test_unix_msg() { let tmpdir = tempfile::tempdir().unwrap(); let path = tmpdir.path().join("scp_4804"); - let ready = Arc::new((Mutex::new(false), Condvar::new())); + + let connection_socket = socket( + AddressFamily::UNIX, + SocketType::SEQPACKET, + Protocol::default(), + ) + .unwrap(); + + let name = SocketAddrUnix::new(&path).unwrap(); + bind_unix(&connection_socket, &name).unwrap(); + listen(&connection_socket, 1).unwrap(); let server = { - let ready = ready.clone(); let path = path.clone(); move || { - let connection_socket = socket( - AddressFamily::UNIX, - SocketType::SEQPACKET, - Protocol::default(), - ) - .unwrap(); - - let name = SocketAddrUnix::new(&path).unwrap(); - bind_unix(&connection_socket, &name).unwrap(); - listen(&connection_socket, 1).unwrap(); - - { - let (lock, cvar) = &*ready; - let mut started = lock.lock().unwrap(); - *started = true; - cvar.notify_all(); - } - let mut buffer = vec![0; BUFFER_SIZE]; 'exit: loop { let data_socket = accept(&connection_socket).unwrap(); @@ -214,14 +205,6 @@ fn test_unix_msg() { }; let client = move || { - { - let (lock, cvar) = &*ready; - let mut started = lock.lock().unwrap(); - while !*started { - started = cvar.wait(started).unwrap(); - } - } - let addr = SocketAddrUnix::new(path).unwrap(); let mut buffer = vec![0; BUFFER_SIZE]; let runs: &[(&[&str], i32)] = &[ @@ -318,31 +301,23 @@ fn test_unix_msg_with_scm_rights() { let tmpdir = tempfile::tempdir().unwrap(); let path = tmpdir.path().join("scp_4804"); - let ready = Arc::new((Mutex::new(false), Condvar::new())); let server = { - let ready = ready.clone(); let path = path.clone(); - move || { - let connection_socket = socket( - AddressFamily::UNIX, - SocketType::SEQPACKET, - Protocol::default(), - ) - .unwrap(); - let mut pipe_end = None; + let connection_socket = socket( + AddressFamily::UNIX, + SocketType::SEQPACKET, + Protocol::default(), + ) + .unwrap(); - let name = SocketAddrUnix::new(&path).unwrap(); - bind_unix(&connection_socket, &name).unwrap(); - listen(&connection_socket, 1).unwrap(); + let name = SocketAddrUnix::new(&path).unwrap(); + bind_unix(&connection_socket, &name).unwrap(); + listen(&connection_socket, 1).unwrap(); - { - let (lock, cvar) = &*ready; - let mut started = lock.lock().unwrap(); - *started = true; - cvar.notify_all(); - } + move || { + let mut pipe_end = None; let mut buffer = vec![0; BUFFER_SIZE]; let mut cmsg_space = vec![0; rustix::cmsg_space!(ScmRights(1))]; @@ -403,14 +378,6 @@ fn test_unix_msg_with_scm_rights() { }; let client = move || { - { - let (lock, cvar) = &*ready; - let mut started = lock.lock().unwrap(); - while !*started { - started = cvar.wait(started).unwrap(); - } - } - let addr = SocketAddrUnix::new(path).unwrap(); let (read_end, write_end) = pipe().unwrap(); let mut buffer = vec![0; BUFFER_SIZE];