2019-07-27 00:54:25 +03:00
|
|
|
// run-pass
|
2014-03-10 21:27:34 -07:00
|
|
|
// ignore-android needs extra network permissions
|
2018-01-02 14:11:41 +01:00
|
|
|
// ignore-emscripten no threads or sockets support
|
2015-06-30 20:37:11 -07:00
|
|
|
// ignore-netbsd system ulimit (Too many open files)
|
|
|
|
// ignore-openbsd system ulimit (Too many open files)
|
2014-03-16 21:18:17 +08:00
|
|
|
|
2015-04-10 13:51:53 -07:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::net::{TcpListener, TcpStream};
|
|
|
|
use std::process;
|
2015-01-02 12:05:24 -08:00
|
|
|
use std::sync::mpsc::channel;
|
2016-05-14 18:57:09 +02:00
|
|
|
use std::time::Duration;
|
2015-03-31 18:59:36 -07:00
|
|
|
use std::thread::{self, Builder};
|
2014-04-05 22:18:52 -07:00
|
|
|
|
2016-08-01 09:43:19 +02:00
|
|
|
const TARGET_CNT: usize = 200;
|
2016-07-30 09:01:13 +02:00
|
|
|
|
2014-03-10 21:27:34 -07:00
|
|
|
fn main() {
|
|
|
|
// This test has a chance to time out, try to not let it time out
|
2015-03-31 18:59:36 -07:00
|
|
|
thread::spawn(move|| -> () {
|
2016-05-14 18:57:09 +02:00
|
|
|
thread::sleep(Duration::from_secs(30));
|
2015-04-10 13:51:53 -07:00
|
|
|
process::exit(1);
|
2015-01-05 21:59:45 -08:00
|
|
|
});
|
2014-03-10 21:27:34 -07:00
|
|
|
|
2016-05-14 18:57:09 +02:00
|
|
|
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
2015-04-10 13:51:53 -07:00
|
|
|
let addr = listener.local_addr().unwrap();
|
2015-03-31 18:59:36 -07:00
|
|
|
thread::spawn(move || -> () {
|
2014-03-10 21:27:34 -07:00
|
|
|
loop {
|
2015-04-10 13:51:53 -07:00
|
|
|
let mut stream = match listener.accept() {
|
|
|
|
Ok(stream) => stream.0,
|
2016-05-14 18:57:09 +02:00
|
|
|
Err(_) => continue,
|
2014-03-10 21:27:34 -07:00
|
|
|
};
|
2016-05-14 18:57:09 +02:00
|
|
|
let _ = stream.read(&mut [0]);
|
|
|
|
let _ = stream.write(&[2]);
|
2014-03-10 21:27:34 -07:00
|
|
|
}
|
2015-01-05 21:59:45 -08:00
|
|
|
});
|
2014-03-10 21:27:34 -07:00
|
|
|
|
2014-03-13 23:26:14 -05:00
|
|
|
let (tx, rx) = channel();
|
2016-07-30 09:01:13 +02:00
|
|
|
|
2016-05-14 18:56:20 +02:00
|
|
|
let mut spawned_cnt = 0;
|
2016-07-30 09:01:13 +02:00
|
|
|
for _ in 0..TARGET_CNT {
|
2014-03-13 23:26:14 -05:00
|
|
|
let tx = tx.clone();
|
2016-05-14 18:56:20 +02:00
|
|
|
let res = Builder::new().stack_size(64 * 1024).spawn(move|| {
|
2014-10-30 23:22:40 +03:00
|
|
|
match TcpStream::connect(addr) {
|
2015-04-10 13:51:53 -07:00
|
|
|
Ok(mut stream) => {
|
2016-05-14 18:57:09 +02:00
|
|
|
let _ = stream.write(&[1]);
|
|
|
|
let _ = stream.read(&mut [0]);
|
2014-03-10 21:27:34 -07:00
|
|
|
},
|
2015-04-10 13:51:53 -07:00
|
|
|
Err(..) => {}
|
2014-03-10 21:27:34 -07:00
|
|
|
}
|
2015-01-02 12:05:24 -08:00
|
|
|
tx.send(()).unwrap();
|
2015-01-05 21:59:45 -08:00
|
|
|
});
|
2016-05-14 18:56:20 +02:00
|
|
|
if let Ok(_) = res {
|
|
|
|
spawned_cnt += 1;
|
|
|
|
};
|
2014-03-10 21:27:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all clients to exit, but don't wait for the server to exit. The
|
|
|
|
// server just runs infinitely.
|
2014-03-13 23:26:14 -05:00
|
|
|
drop(tx);
|
2016-05-14 18:56:20 +02:00
|
|
|
for _ in 0..spawned_cnt {
|
2015-01-02 12:05:24 -08:00
|
|
|
rx.recv().unwrap();
|
2014-03-10 21:27:34 -07:00
|
|
|
}
|
2016-07-30 09:01:13 +02:00
|
|
|
assert_eq!(spawned_cnt, TARGET_CNT);
|
2015-04-10 13:51:53 -07:00
|
|
|
process::exit(0);
|
2014-03-10 21:27:34 -07:00
|
|
|
}
|