Skip to content

Commit 2cd4420

Browse files
committed
Don't fork in test_mq_send_receive
It isn't necessary, and can cause deadlocks in Rust's test harness
1 parent 64f7984 commit 2cd4420

File tree

1 file changed

+19
-37
lines changed

1 file changed

+19
-37
lines changed

test/test_mq.rs

+19-37
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,27 @@ use nix::Error::Sys;
1616

1717
#[test]
1818
fn test_mq_send_and_receive() {
19-
#[allow(unused_variables)]
20-
let m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");
21-
2219
const MSG_SIZE: c_long = 32;
2320
let attr = MqAttr::new(0, 10, MSG_SIZE, 0);
24-
let mq_name_in_parent = &CString::new(b"/a_nix_test_queue".as_ref()).unwrap();
25-
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();
26-
let msg_to_send = "msg_1".as_bytes();
27-
28-
mq_send(mqd_in_parent, msg_to_send, 1).unwrap();
29-
30-
let (reader, writer) = pipe().unwrap();
31-
32-
let pid = fork();
33-
match pid {
34-
Ok(Child) => {
35-
let mq_name_in_child = &CString::new(b"/a_nix_test_queue".as_ref()).unwrap();
36-
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();
37-
let mut buf = [0u8; 32];
38-
let mut prio = 0u32;
39-
mq_receive(mqd_in_child, &mut buf, &mut prio).unwrap();
40-
assert!(prio == 1);
41-
write(writer, &buf).unwrap(); // pipe result to parent process. Otherwise cargo does not report test failures correctly
42-
mq_close(mqd_in_child).unwrap();
43-
}
44-
Ok(Parent { child }) => {
45-
mq_close(mqd_in_parent).unwrap();
46-
47-
// Wait for the child to exit.
48-
waitpid(child, None).unwrap();
49-
// Read 1024 bytes.
50-
let mut read_buf = [0u8; 32];
51-
read(reader, &mut read_buf).unwrap();
52-
let message_str = str::from_utf8(&read_buf).unwrap();
53-
assert_eq!(&message_str[.. message_str.char_indices().nth(5).unwrap().0], "msg_1");
54-
},
55-
// panic, fork should never fail unless there is a serious problem with the OS
56-
Err(_) => panic!("Error: Fork Failed")
57-
}
21+
let mq_name= &CString::new(b"/a_nix_test_queue".as_ref()).unwrap();
22+
23+
let mqd0 = mq_open(mq_name, O_CREAT | O_WRONLY,
24+
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
25+
Some(&attr)).unwrap();
26+
let msg_to_send = "msg_1";
27+
mq_send(mqd0, msg_to_send.as_bytes(), 1).unwrap();
28+
29+
let mqd1 = mq_open(mq_name, O_CREAT | O_RDONLY,
30+
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
31+
Some(&attr)).unwrap();
32+
let mut buf = [0u8; 32];
33+
let mut prio = 0u32;
34+
let len = mq_receive(mqd1, &mut buf, &mut prio).unwrap();
35+
assert!(prio == 1);
36+
37+
mq_close(mqd1).unwrap();
38+
mq_close(mqd0).unwrap();
39+
assert_eq!(msg_to_send, str::from_utf8(&buf[0..len]).unwrap());
5840
}
5941

6042

0 commit comments

Comments
 (0)