Debian Codegraph @

.cargo/vendor/jobserver/tests/client.rs

raw
1 use std::env;
2 use std::fs::File;
3 use std::io::Write;
4 use std::process::Command;
5 use std::sync::atomic::{AtomicBool, Ordering};
6 use std::sync::mpsc;
7 use std::sync::Arc;
8 use std::thread;
9
10 use jobserver::Client;
11
12 macro_rules! t {
13 ($e:expr) => {
14 match $e {
15 Ok(e) => e,
16 Err(e) => panic!("{} failed with {}", stringify!($e), e),
17 }
18 };
19 }
20
21 struct Test {
22 name: &'static str,
23 f: &'static (dyn Fn() + Send + Sync),
24 make_args: &'static [&'static str],
25 rule: &'static (dyn Fn(&str) -> String + Send + Sync),
26 }
27
28 const TESTS: &[Test] = &[
29 Test {
30 name: "no j args",
31 make_args: &[],
32 rule: &|me| me.to_string(),
33 f: &|| {
34 assert!(unsafe { Client::from_env().is_none() });
35 },
36 },
37 Test {
38 name: "no j args with plus",
39 make_args: &[],
40 rule: &|me| format!("+{}", me),
41 f: &|| {
42 assert!(unsafe { Client::from_env().is_none() });
43 },
44 },
45 Test {
46 name: "j args with plus",
47 make_args: &["-j2"],
48 rule: &|me| format!("+{}", me),
49 f: &|| {
50 assert!(unsafe { Client::from_env().is_some() });
51 },
52 },
53 Test {
54 name: "acquire",
55 make_args: &["-j2"],
56 rule: &|me| format!("+{}", me),
57 f: &|| {
58 let c = unsafe { Client::from_env().unwrap() };
59 drop(c.acquire().unwrap());
60 drop(c.acquire().unwrap());
61 },
62 },
63 Test {
64 name: "acquire3",
65 make_args: &["-j3"],
66 rule: &|me| format!("+{}", me),
67 f: &|| {
68 let c = unsafe { Client::from_env().unwrap() };
69 let a = c.acquire().unwrap();
70 let b = c.acquire().unwrap();
71 drop((a, b));
72 },
73 },
74 Test {
75 name: "acquire blocks",
76 make_args: &["-j2"],
77 rule: &|me| format!("+{}", me),
78 f: &|| {
79 let c = unsafe { Client::from_env().unwrap() };
80 let a = c.acquire().unwrap();
81 let hit = Arc::new(AtomicBool::new(false));
82 let hit2 = hit.clone();
83 let (tx, rx) = mpsc::channel();
84 let t = thread::spawn(move || {
85 tx.send(()).unwrap();
86 let _b = c.acquire().unwrap();
87 hit2.store(true, Ordering::SeqCst);
88 });
89 rx.recv().unwrap();
90 assert!(!hit.load(Ordering::SeqCst));
91 drop(a);
92 t.join().unwrap();
93 assert!(hit.load(Ordering::SeqCst));
94 },
95 },
96 Test {
97 name: "acquire_raw",
98 make_args: &["-j2"],
99 rule: &|me| format!("+{}", me),
100 f: &|| {
101 let c = unsafe { Client::from_env().unwrap() };
102 c.acquire_raw().unwrap();
103 c.release_raw().unwrap();
104 },
105 },
106 ];
107
108 fn main() {
109 if let Ok(test) = env::var("TEST_TO_RUN") {
110 return (TESTS.iter().find(|t| t.name == test).unwrap().f)();
111 }
112
113 let me = t!(env::current_exe());
114 let me = me.to_str().unwrap();
115 let filter = env::args().nth(1);
116
117 let join_handles = TESTS
118 .iter()
119 .filter(|test| match filter {
120 Some(ref s) => test.name.contains(s),
121 None => true,
122 })
123 .map(|test| {
124 let td = t!(tempfile::tempdir());
125 let makefile = format!(
126 "\
127 all: export TEST_TO_RUN={}
128 all:
129 \t{}
130 ",
131 test.name,
132 (test.rule)(me)
133 );
134 t!(t!(File::create(td.path().join("Makefile"))).write_all(makefile.as_bytes()));
135 thread::spawn(move || {
136 let prog = env::var("MAKE").unwrap_or_else(|_| "make".to_string());
137 let mut cmd = Command::new(prog);
138 cmd.args(test.make_args);
139 cmd.current_dir(td.path());
140
141 (test, cmd.output().unwrap())
142 })
143 })
144 .collect::<Vec<_>>();
145
146 println!("\nrunning {} tests\n", join_handles.len());
147
148 let failures = join_handles
149 .into_iter()
150 .filter_map(|join_handle| {
151 let (test, output) = join_handle.join().unwrap();
152
153 if output.status.success() {
154 println!("test {} ... ok", test.name);
155 None
156 } else {
157 println!("test {} ... FAIL", test.name);
158 Some((test, output))
159 }
160 })
161 .collect::<Vec<_>>();
162
163 if failures.is_empty() {
164 println!("\ntest result: ok\n");
165 return;
166 }
167
168 println!("\n----------- failures");
169
170 for (test, output) in failures {
171 println!("test {}", test.name);
172 let stdout = String::from_utf8_lossy(&output.stdout);
173 let stderr = String::from_utf8_lossy(&output.stderr);
174
175 println!("\texit status: {}", output.status);
176 if !stdout.is_empty() {
177 println!("\tstdout ===");
178 for line in stdout.lines() {
179 println!("\t\t{}", line);
180 }
181 }
182
183 if !stderr.is_empty() {
184 println!("\tstderr ===");
185 for line in stderr.lines() {
186 println!("\t\t{}", line);
187 }
188 }
189 }
190
191 std::process::exit(4);
192 }