Skip to content

Commit

Permalink
Fix shutdown of child processes in tests/cli.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
fkettelhoit committed Apr 16, 2024
1 parent b7998b7 commit 8d0b22d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/postgres-integration/.add.garble.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub fn main(x: u32, y: u32) -> u32 {
x + y
pub fn main(x: u32, y: u32, z: u32) -> u32 {
x + y + z
}
5 changes: 3 additions & 2 deletions examples/postgres-integration/policies0.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"participants": [
"http://localhost:8000",
"http://localhost:8001",
"http://localhost:8002"
"http://localhost:8002",
"http://localhost:8003"
],
"program": ".add.garble.rs",
"leader": 0,
"party": 0,
"input": "2u32"
"input": "0u32"
}
]
}
5 changes: 3 additions & 2 deletions examples/postgres-integration/policies1.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"participants": [
"http://localhost:8000",
"http://localhost:8001",
"http://localhost:8002"
"http://localhost:8002",
"http://localhost:8003"
],
"program": ".add.garble.rs",
"leader": 0,
"party": 1,
"input": "3u32"
"input": "1u32"
}
]
}
16 changes: 16 additions & 0 deletions examples/postgres-integration/policies2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"accepted": [
{
"participants": [
"http://localhost:8000",
"http://localhost:8001",
"http://localhost:8002",
"http://localhost:8003"
],
"program": ".add.garble.rs",
"leader": 0,
"party": 2,
"input": "2u32"
}
]
}
3 changes: 2 additions & 1 deletion examples/postgres-integration/preprocessor.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"participants": [
"http://localhost:8000",
"http://localhost:8001",
"http://localhost:8002"
"http://localhost:8002",
"http://localhost:8003"
],
"program": "",
"leader": 0,
Expand Down
21 changes: 14 additions & 7 deletions examples/postgres-integration/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
io::{BufRead, BufReader},
process::{exit, Command, Stdio},
sync::mpsc::channel,
thread::{self, sleep},
time::Duration,
};
Expand All @@ -11,7 +12,7 @@ fn simulate() {
println!("\n\n--- {millis}ms ---\n\n");
let mut children = vec![];
let mut cmd = Command::new("cargo")
.args(["run", "--", "--port=8002", "--config=preprocessor.json"])
.args(["run", "--", "--port=8003", "--config=preprocessor.json"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
Expand All @@ -31,7 +32,7 @@ fn simulate() {
}
});
children.push(cmd);
for p in [1] {
for p in [1, 2] {
let port = format!("--port=800{p}");
let config = format!("--config=policies{p}.json");
let args = vec!["run", "--", &port, &config];
Expand Down Expand Up @@ -66,11 +67,12 @@ fn simulate() {
let mut stdout = BufReader::new(cmd.stdout.take().unwrap()).lines();
let mut stderr = BufReader::new(cmd.stderr.take().unwrap()).lines();
children.push(cmd);
let (s, r) = channel::<()>();
thread::spawn(move || {
while let Some(Ok(line)) = stdout.next() {
println!("party0> {line}");
if line == "Output is 5u32" {
exit(0);
if line == "Output is 3u32" {
return s.send(()).unwrap();
}
}
});
Expand All @@ -79,10 +81,15 @@ fn simulate() {
eprintln!("party0> {line}");
}
});
sleep(Duration::from_secs(10));
eprintln!("Test did not complete!");
let result = r.recv_timeout(Duration::from_secs(10));
for mut child in children {
child.kill().unwrap();
}
exit(-1);
match result {
Ok(_) => exit(0),
Err(_) => {
eprintln!("Test did not complete!");
exit(-1);
}
}
}

0 comments on commit 8d0b22d

Please sign in to comment.