Skip to content

Commit

Permalink
fix unstable the channel tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
utam0k committed Sep 5, 2021
1 parent 3fac283 commit a8e0e03
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/process/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,48 +230,55 @@ fn new_pipe() -> Result<(Sender, Receiver)> {
}

#[cfg(test)]
// Tests become unstable if not serial. The cause is not known.
mod tests {
use super::*;
use anyhow::Context;
use nix::sys::wait;
use nix::unistd;
use serial_test::serial;

#[test]
#[serial]
fn test_channel_intermadiate_ready() -> Result<()> {
let (sender, receiver) = &mut intermediate_to_main()?;
match unsafe { unistd::fork()? } {
unistd::ForkResult::Parent { child } => {
wait::waitpid(child, None)?;
let pid = receiver
.wait_for_intermediate_ready()
.with_context(|| "Failed to wait for intermadiate ready")?;
receiver.close()?;
assert_eq!(pid, child);
wait::waitpid(child, None)?;
}
unistd::ForkResult::Child => {
let pid = unistd::getpid();
sender
.intermediate_ready(pid)
.with_context(|| "Failed to send intermadiate ready")?;
sender.intermediate_ready(pid)?;
// .with_context(|| "Failed to send intermadiate ready")?;
sender.close()?;
std::process::exit(0);
}
};

Ok(())
}

#[test]
#[serial]
fn test_channel_id_mapping_request() -> Result<()> {
let (sender, receiver) = &mut intermediate_to_main()?;
match unsafe { unistd::fork()? } {
unistd::ForkResult::Parent { child } => {
receiver
.wait_for_mapping_request()
.with_context(|| "Failed to wait for mapping ack")?;
wait::waitpid(child, None)?;
receiver.wait_for_mapping_request()?;
// .with_context(|| "Failed to wait for mapping ack")?;
receiver.close()?;
}
unistd::ForkResult::Child => {
sender
.identifier_mapping_request()
.with_context(|| "Failed to send mapping written")?;
sender.close()?;
std::process::exit(0);
}
};
Expand All @@ -280,14 +287,13 @@ mod tests {
}

#[test]
#[serial]
fn test_channel_id_mapping_ack() -> Result<()> {
let (sender, receiver) = &mut main_to_intermediate()?;
match unsafe { unistd::fork()? } {
unistd::ForkResult::Parent { child } => {
receiver
.wait_for_mapping_ack()
.with_context(|| "Failed to wait for mapping ack")?;
wait::waitpid(child, None)?;
receiver.wait_for_mapping_ack()?;
}
unistd::ForkResult::Child => {
sender
Expand All @@ -301,26 +307,29 @@ mod tests {
}

#[test]
#[serial]
fn test_channel_init_ready() -> Result<()> {
let (sender, receiver) = &mut init_to_intermediate()?;
match unsafe { unistd::fork()? } {
unistd::ForkResult::Parent { child } => {
receiver
.wait_for_init_ready()
.with_context(|| "Failed to wait for init ready")?;
wait::waitpid(child, None)?;
receiver.wait_for_init_ready()?;
receiver.close()?;
}
unistd::ForkResult::Child => {
sender
.init_ready()
.with_context(|| "Failed to send init ready")?;
sender.close()?;
std::process::exit(0);
}
};

Ok(())
}

#[test]
#[serial]
fn test_channel_intermedaite_graceful_exit() -> Result<()> {
let (sender, receiver) = &mut intermediate_to_main()?;
match unsafe { unistd::fork()? } {
Expand All @@ -343,6 +352,7 @@ mod tests {
}

#[test]
#[serial]
fn test_channel_init_graceful_exit() -> Result<()> {
let (sender, receiver) = &mut init_to_intermediate()?;
match unsafe { unistd::fork()? } {
Expand Down

0 comments on commit a8e0e03

Please sign in to comment.