]> git.proxmox.com Git - rustc.git/blame - src/test/ui/tcp-stress.rs
New upstream version 1.47.0~beta.2+dfsg1
[rustc.git] / src / test / ui / tcp-stress.rs
CommitLineData
416331ca 1// run-pass
1a4d82fc 2// ignore-android needs extra network permissions
2c00a5a8
XL
3// ignore-cloudabi no global network namespace access
4// ignore-emscripten no threads or sockets support
c1a9b12d
SL
5// ignore-netbsd system ulimit (Too many open files)
6// ignore-openbsd system ulimit (Too many open files)
1a4d82fc 7
9346a6ac
AL
8use std::io::prelude::*;
9use std::net::{TcpListener, TcpStream};
10use std::process;
1a4d82fc 11use std::sync::mpsc::channel;
a7813a04 12use std::time::Duration;
c34b1796 13use std::thread::{self, Builder};
1a4d82fc 14
5bcae85e
SL
15const TARGET_CNT: usize = 200;
16
1a4d82fc
JJ
17fn main() {
18 // This test has a chance to time out, try to not let it time out
c34b1796 19 thread::spawn(move|| -> () {
a7813a04 20 thread::sleep(Duration::from_secs(30));
9346a6ac 21 process::exit(1);
1a4d82fc
JJ
22 });
23
a7813a04 24 let listener = TcpListener::bind("127.0.0.1:0").unwrap();
9346a6ac 25 let addr = listener.local_addr().unwrap();
c34b1796 26 thread::spawn(move || -> () {
1a4d82fc 27 loop {
9346a6ac
AL
28 let mut stream = match listener.accept() {
29 Ok(stream) => stream.0,
a7813a04 30 Err(_) => continue,
1a4d82fc 31 };
a7813a04
XL
32 let _ = stream.read(&mut [0]);
33 let _ = stream.write(&[2]);
1a4d82fc
JJ
34 }
35 });
1a4d82fc
JJ
36
37 let (tx, rx) = channel();
5bcae85e 38
a7813a04 39 let mut spawned_cnt = 0;
5bcae85e 40 for _ in 0..TARGET_CNT {
1a4d82fc 41 let tx = tx.clone();
a7813a04 42 let res = Builder::new().stack_size(64 * 1024).spawn(move|| {
1a4d82fc 43 match TcpStream::connect(addr) {
9346a6ac 44 Ok(mut stream) => {
a7813a04
XL
45 let _ = stream.write(&[1]);
46 let _ = stream.read(&mut [0]);
1a4d82fc 47 },
9346a6ac 48 Err(..) => {}
1a4d82fc
JJ
49 }
50 tx.send(()).unwrap();
51 });
a7813a04
XL
52 if let Ok(_) = res {
53 spawned_cnt += 1;
54 };
1a4d82fc
JJ
55 }
56
57 // Wait for all clients to exit, but don't wait for the server to exit. The
58 // server just runs infinitely.
59 drop(tx);
a7813a04 60 for _ in 0..spawned_cnt {
1a4d82fc
JJ
61 rx.recv().unwrap();
62 }
5bcae85e 63 assert_eq!(spawned_cnt, TARGET_CNT);
9346a6ac 64 process::exit(0);
1a4d82fc 65}