Skip to content

Instantly share code, notes, and snippets.

@oconnor663
Last active October 9, 2019 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oconnor663/7a82e5130715731890ec7f959903e552 to your computer and use it in GitHub Desktop.
Save oconnor663/7a82e5130715731890ec7f959903e552 to your computer and use it in GitHub Desktop.
Windows race test
[package]
name = "race"
version = "0.1.0"
authors = ["Jack O'Connor <oconnor663@gmail.com>"]
edition = "2018"
[dependencies]
os_pipe = "0.9.0"
[[bin]]
name = "race"
path = "main.rs"
use std::io::prelude::*;
use std::process::Command;
use std::sync::{Arc, Barrier};
fn main() {
loop {
let barrier = Arc::new(Barrier::new(2));
let barrier_clone = barrier.clone();
let thread1 = std::thread::spawn(move || {
barrier_clone.wait();
let (r, w) = os_pipe::pipe().unwrap();
let child1 = Command::new("python3")
.arg("-c")
.arg("import time; time.sleep(1000000)")
.stdout(w)
.spawn()
.expect("thread1 spawn failure");
(child1, r)
});
let barrier_clone = barrier.clone();
let thread2 = std::thread::spawn(move || {
barrier_clone.wait();
let (r, w) = os_pipe::pipe().unwrap();
let child2 = Command::new("python3")
.arg("-c")
.arg("import sys; print('hello, world'); sys.stdout.flush()")
.stdout(w)
.spawn()
.expect("thread2 spawn failure");
(child2, r)
});
let (mut child1, _stdout1) = thread1.join().expect("thread1 join failure");
let (mut child2, mut stdout2) = thread2.join().expect("thread2 join failure");
let mut buf = [0; 12];
stdout2.read_exact(&mut buf).expect("read_exact");
assert_eq!(&buf, b"hello, world");
child2.wait().expect("child2 wait");
child1.kill().expect("child1 kill");
child1.wait().expect("child1 wait");
print!(".");
std::io::stdout().flush().unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment