Skip to content

Commit

Permalink
Don't fork in test_mq_send_receive
Browse files Browse the repository at this point in the history
It isn't necessary, and can cause deadlocks in Rust's test harness
  • Loading branch information
asomers committed Jul 16, 2017
1 parent 64f7984 commit 2cd4420
Showing 1 changed file with 19 additions and 37 deletions.
56 changes: 19 additions & 37 deletions test/test_mq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,27 @@ use nix::Error::Sys;

#[test]
fn test_mq_send_and_receive() {
#[allow(unused_variables)]
let m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");

const MSG_SIZE: c_long = 32;
let attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name_in_parent = &CString::new(b"/a_nix_test_queue".as_ref()).unwrap();
let mqd_in_parent = mq_open(mq_name_in_parent, O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH, Some(&attr)).unwrap();
let msg_to_send = "msg_1".as_bytes();

mq_send(mqd_in_parent, msg_to_send, 1).unwrap();

let (reader, writer) = pipe().unwrap();

let pid = fork();
match pid {
Ok(Child) => {
let mq_name_in_child = &CString::new(b"/a_nix_test_queue".as_ref()).unwrap();
let mqd_in_child = mq_open(mq_name_in_child, O_CREAT | O_RDONLY, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH, Some(&attr)).unwrap();
let mut buf = [0u8; 32];
let mut prio = 0u32;
mq_receive(mqd_in_child, &mut buf, &mut prio).unwrap();
assert!(prio == 1);
write(writer, &buf).unwrap(); // pipe result to parent process. Otherwise cargo does not report test failures correctly
mq_close(mqd_in_child).unwrap();
}
Ok(Parent { child }) => {
mq_close(mqd_in_parent).unwrap();

// Wait for the child to exit.
waitpid(child, None).unwrap();
// Read 1024 bytes.
let mut read_buf = [0u8; 32];
read(reader, &mut read_buf).unwrap();
let message_str = str::from_utf8(&read_buf).unwrap();
assert_eq!(&message_str[.. message_str.char_indices().nth(5).unwrap().0], "msg_1");
},
// panic, fork should never fail unless there is a serious problem with the OS
Err(_) => panic!("Error: Fork Failed")
}
let mq_name= &CString::new(b"/a_nix_test_queue".as_ref()).unwrap();

let mqd0 = mq_open(mq_name, O_CREAT | O_WRONLY,
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
Some(&attr)).unwrap();
let msg_to_send = "msg_1";
mq_send(mqd0, msg_to_send.as_bytes(), 1).unwrap();

let mqd1 = mq_open(mq_name, O_CREAT | O_RDONLY,
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
Some(&attr)).unwrap();
let mut buf = [0u8; 32];
let mut prio = 0u32;
let len = mq_receive(mqd1, &mut buf, &mut prio).unwrap();
assert!(prio == 1);

mq_close(mqd1).unwrap();
mq_close(mqd0).unwrap();
assert_eq!(msg_to_send, str::from_utf8(&buf[0..len]).unwrap());
}


Expand Down

0 comments on commit 2cd4420

Please sign in to comment.