]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/tcp-stress.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / tcp-stress.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-android needs extra network permissions
12 // ignore-bitrig system ulimit (Too many open files)
13 // ignore-netbsd system ulimit (Too many open files)
14 // ignore-openbsd system ulimit (Too many open files)
15
16 use std::io::prelude::*;
17 use std::net::{TcpListener, TcpStream};
18 use std::process;
19 use std::sync::mpsc::channel;
20 use std::thread::{self, Builder};
21
22 fn main() {
23     // This test has a chance to time out, try to not let it time out
24     thread::spawn(move|| -> () {
25         thread::sleep_ms(30 * 1000);
26         process::exit(1);
27     });
28
29     let mut listener = TcpListener::bind("127.0.0.1:0").unwrap();
30     let addr = listener.local_addr().unwrap();
31     thread::spawn(move || -> () {
32         loop {
33             let mut stream = match listener.accept() {
34                 Ok(stream) => stream.0,
35                 Err(error) => continue,
36             };
37             stream.read(&mut [0]);
38             stream.write(&[2]);
39         }
40     });
41
42     let (tx, rx) = channel();
43     for _ in 0..1000 {
44         let tx = tx.clone();
45         Builder::new().stack_size(64 * 1024).spawn(move|| {
46             match TcpStream::connect(addr) {
47                 Ok(mut stream) => {
48                     stream.write(&[1]);
49                     stream.read(&mut [0]);
50                 },
51                 Err(..) => {}
52             }
53             tx.send(()).unwrap();
54         });
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);
60     for _ in 0..1000 {
61         rx.recv().unwrap();
62     }
63     process::exit(0);
64 }