summaryrefslogtreecommitdiffstats
path: root/third_party/rust/minidump-writer/tests/common/mod.rs
blob: bbb6abf35c54d850e834ff0c445d4319c4289450 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::error;
use std::io::{BufRead, BufReader, Write};
use std::process::{Child, Command, Stdio};
use std::result;

#[allow(unused)]
type Error = Box<dyn error::Error + std::marker::Send + std::marker::Sync>;
#[allow(unused)]
pub type Result<T> = result::Result<T, Error>;

#[allow(unused)]
pub fn spawn_child(command: &str, args: &[&str]) {
    let mut cmd_object = Command::new("cargo");
    let mut cmd_ref = cmd_object
        .env("RUST_BACKTRACE", "1")
        .arg("run")
        .arg("-q")
        .arg("--bin")
        .arg("test")
        .arg("--")
        .arg(command);
    for arg in args {
        cmd_ref = cmd_ref.arg(arg);
    }
    let child = cmd_ref.output().expect("failed to execute child");

    println!("Child output:");
    std::io::stdout().write_all(&child.stdout).unwrap();
    std::io::stdout().write_all(&child.stderr).unwrap();
    assert_eq!(child.status.code().expect("No return value"), 0);
}

fn start_child_and_wait_for_threads_helper(cmd: &str, num: usize) -> Child {
    let mut child = Command::new("cargo")
        .env("RUST_BACKTRACE", "1")
        .arg("run")
        .arg("-q")
        .arg("--bin")
        .arg("test")
        .arg("--")
        .arg(cmd)
        .arg(format!("{}", num))
        .stdout(Stdio::piped())
        .spawn()
        .expect("failed to execute child");

    wait_for_threads(&mut child, num);
    child
}

#[allow(unused)]
pub fn start_child_and_wait_for_threads(num: usize) -> Child {
    start_child_and_wait_for_threads_helper("spawn_and_wait", num)
}

#[allow(unused)]
pub fn start_child_and_wait_for_named_threads(num: usize) -> Child {
    start_child_and_wait_for_threads_helper("spawn_name_wait", num)
}

#[allow(unused)]
pub fn start_child_and_wait_for_create_files(num: usize) -> Child {
    start_child_and_wait_for_threads_helper("create_files_wait", num)
}

#[allow(unused)]
pub fn wait_for_threads(child: &mut Child, num: usize) {
    let mut f = BufReader::new(child.stdout.as_mut().expect("Can't open stdout"));
    let mut lines = 0;
    while lines < num {
        let mut buf = String::new();
        match f.read_line(&mut buf) {
            Ok(_) => {
                if buf == "1\n" {
                    lines += 1;
                }
            }
            Err(e) => {
                std::panic::panic_any(e);
            }
        }
    }
}

#[allow(unused)]
pub fn start_child_and_return(args: &[&str]) -> Child {
    let mut child = Command::new("cargo")
        .env("RUST_BACKTRACE", "1")
        .arg("run")
        .arg("-q")
        .arg("--bin")
        .arg("test")
        .arg("--")
        .args(args)
        .stdout(Stdio::piped())
        .spawn()
        .expect("failed to execute child");

    child
}