Skip to content

Commit

Permalink
test_unix_msg: Remove thread synchronisation
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
psychon committed May 6, 2023
1 parent bff1d53 commit b5e9770
Showing 1 changed file with 22 additions and 55 deletions.
77 changes: 22 additions & 55 deletions tests/net/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)] = &[
Expand Down Expand Up @@ -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))];
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit b5e9770

Please sign in to comment.