| 1 |
use std::env; |
| 2 |
use std::fs::File; |
| 3 |
use std::io::prelude::*; |
| 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 |
#[test] |
| 22 |
fn server_smoke() { |
| 23 |
let c = t!(Client::new(1)); |
| 24 |
drop(c.acquire().unwrap()); |
| 25 |
drop(c.acquire().unwrap()); |
| 26 |
} |
| 27 |
|
| 28 |
#[test] |
| 29 |
fn server_multiple() { |
| 30 |
let c = t!(Client::new(2)); |
| 31 |
let a = c.acquire().unwrap(); |
| 32 |
let b = c.acquire().unwrap(); |
| 33 |
drop((a, b)); |
| 34 |
} |
| 35 |
|
| 36 |
#[test] |
| 37 |
fn server_available() { |
| 38 |
let c = t!(Client::new(10)); |
| 39 |
assert_eq!(c.available().unwrap(), 10); |
| 40 |
let a = c.acquire().unwrap(); |
| 41 |
assert_eq!(c.available().unwrap(), 9); |
| 42 |
drop(a); |
| 43 |
assert_eq!(c.available().unwrap(), 10); |
| 44 |
} |
| 45 |
|
| 46 |
#[test] |
| 47 |
fn server_none_available() { |
| 48 |
let c = t!(Client::new(2)); |
| 49 |
assert_eq!(c.available().unwrap(), 2); |
| 50 |
let a = c.acquire().unwrap(); |
| 51 |
assert_eq!(c.available().unwrap(), 1); |
| 52 |
let b = c.acquire().unwrap(); |
| 53 |
assert_eq!(c.available().unwrap(), 0); |
| 54 |
drop(a); |
| 55 |
assert_eq!(c.available().unwrap(), 1); |
| 56 |
drop(b); |
| 57 |
assert_eq!(c.available().unwrap(), 2); |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn server_blocks() { |
| 62 |
let c = t!(Client::new(1)); |
| 63 |
let a = c.acquire().unwrap(); |
| 64 |
let hit = Arc::new(AtomicBool::new(false)); |
| 65 |
let hit2 = hit.clone(); |
| 66 |
let (tx, rx) = mpsc::channel(); |
| 67 |
let t = thread::spawn(move || { |
| 68 |
tx.send(()).unwrap(); |
| 69 |
let _b = c.acquire().unwrap(); |
| 70 |
hit2.store(true, Ordering::SeqCst); |
| 71 |
}); |
| 72 |
rx.recv().unwrap(); |
| 73 |
assert!(!hit.load(Ordering::SeqCst)); |
| 74 |
drop(a); |
| 75 |
t.join().unwrap(); |
| 76 |
assert!(hit.load(Ordering::SeqCst)); |
| 77 |
} |
| 78 |
|
| 79 |
#[test] |
| 80 |
fn make_as_a_single_thread_client() { |
| 81 |
let c = t!(Client::new(1)); |
| 82 |
let td = tempfile::tempdir().unwrap(); |
| 83 |
|
| 84 |
let prog = env::var("MAKE").unwrap_or_else(|_| "make".to_string()); |
| 85 |
let mut cmd = Command::new(prog); |
| 86 |
cmd.current_dir(td.path()); |
| 87 |
|
| 88 |
t!(t!(File::create(td.path().join("Makefile"))).write_all( |
| 89 |
b" |
| 90 |
all: foo bar |
| 91 |
foo: |
| 92 |
\techo foo |
| 93 |
bar: |
| 94 |
\techo bar |
| 95 |
" |
| 96 |
)); |
| 97 |
|
| 98 |
// The jobserver protocol means that the `make` process itself "runs with a |
| 99 |
// token", so we acquire our one token to drain the jobserver, and this |
| 100 |
// should mean that `make` itself never has a second token available to it. |
| 101 |
let _a = c.acquire(); |
| 102 |
c.configure(&mut cmd); |
| 103 |
let output = t!(cmd.output()); |
| 104 |
println!( |
| 105 |
"\n\t=== stderr\n\t\t{}", |
| 106 |
String::from_utf8_lossy(&output.stderr).replace("\n", "\n\t\t") |
| 107 |
); |
| 108 |
println!( |
| 109 |
"\t=== stdout\n\t\t{}", |
| 110 |
String::from_utf8_lossy(&output.stdout).replace("\n", "\n\t\t") |
| 111 |
); |
| 112 |
|
| 113 |
assert!(output.status.success()); |
| 114 |
assert!(output.stderr.is_empty()); |
| 115 |
|
| 116 |
let stdout = String::from_utf8_lossy(&output.stdout).replace("\r\n", "\n"); |
| 117 |
let a = "\ |
| 118 |
echo foo |
| 119 |
foo |
| 120 |
echo bar |
| 121 |
bar |
| 122 |
"; |
| 123 |
let b = "\ |
| 124 |
echo bar |
| 125 |
bar |
| 126 |
echo foo |
| 127 |
foo |
| 128 |
"; |
| 129 |
|
| 130 |
assert!(stdout == a || stdout == b); |
| 131 |
} |
| 132 |
|
| 133 |
#[test] |
| 134 |
fn make_as_a_multi_thread_client() { |
| 135 |
let c = t!(Client::new(1)); |
| 136 |
let td = tempfile::tempdir().unwrap(); |
| 137 |
|
| 138 |
let prog = env::var("MAKE").unwrap_or_else(|_| "make".to_string()); |
| 139 |
let mut cmd = Command::new(prog); |
| 140 |
cmd.current_dir(td.path()); |
| 141 |
|
| 142 |
t!(t!(File::create(td.path().join("Makefile"))).write_all( |
| 143 |
b" |
| 144 |
all: foo bar |
| 145 |
foo: |
| 146 |
\techo foo |
| 147 |
bar: |
| 148 |
\techo bar |
| 149 |
" |
| 150 |
)); |
| 151 |
|
| 152 |
// We're leaking one extra token to `make` sort of violating the makefile |
| 153 |
// jobserver protocol. It has the desired effect though. |
| 154 |
c.configure(&mut cmd); |
| 155 |
let output = t!(cmd.output()); |
| 156 |
println!( |
| 157 |
"\n\t=== stderr\n\t\t{}", |
| 158 |
String::from_utf8_lossy(&output.stderr).replace("\n", "\n\t\t") |
| 159 |
); |
| 160 |
println!( |
| 161 |
"\t=== stdout\n\t\t{}", |
| 162 |
String::from_utf8_lossy(&output.stdout).replace("\n", "\n\t\t") |
| 163 |
); |
| 164 |
|
| 165 |
assert!(output.status.success()); |
| 166 |
} |
| 167 |
|
| 168 |
#[test] |
| 169 |
fn zero_client() { |
| 170 |
let client = t!(Client::new(0)); |
| 171 |
let (tx, rx) = mpsc::channel(); |
| 172 |
let helper = client |
| 173 |
.into_helper_thread(move |a| drop(tx.send(a))) |
| 174 |
.unwrap(); |
| 175 |
helper.request_token(); |
| 176 |
helper.request_token(); |
| 177 |
|
| 178 |
for _ in 0..1000 { |
| 179 |
assert!(rx.try_recv().is_err()); |
| 180 |
} |
| 181 |
} |