]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/tcp-stress.rs
rollup merge of #20608: nikomatsakis/assoc-types-method-dispatch
[rust.git] / src / test / run-pass / tcp-stress.rs
1 // Copyright 2012-2014 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-linux see joyent/libuv#1189
12 // ignore-android needs extra network permissions
13 // exec-env:RUST_LOG=debug
14
15 #![feature(phase)]
16 #[phase(plugin, link)]
17 extern crate log;
18 extern crate libc;
19
20 use std::sync::mpsc::channel;
21 use std::io::net::tcp::{TcpListener, TcpStream};
22 use std::io::{Acceptor, Listener};
23 use std::thread::{Builder, Thread};
24 use std::time::Duration;
25
26 fn main() {
27     // This test has a chance to time out, try to not let it time out
28     Thread::spawn(move|| -> () {
29         use std::io::timer;
30         timer::sleep(Duration::milliseconds(30 * 1000));
31         println!("timed out!");
32         unsafe { libc::exit(1) }
33     }).detach();
34
35     let (tx, rx) = channel();
36     Thread::spawn(move || -> () {
37         let mut listener = TcpListener::bind("127.0.0.1:0").unwrap();
38         tx.send(listener.socket_name().unwrap()).unwrap();
39         let mut acceptor = listener.listen();
40         loop {
41             let mut stream = match acceptor.accept() {
42                 Ok(stream) => stream,
43                 Err(error) => {
44                     debug!("accept panicked: {}", error);
45                     continue;
46                 }
47             };
48             stream.read_byte();
49             stream.write(&[2]);
50         }
51     }).detach();
52     let addr = rx.recv().unwrap();
53
54     let (tx, rx) = channel();
55     for _ in range(0u, 1000) {
56         let tx = tx.clone();
57         Builder::new().stack_size(64 * 1024).spawn(move|| {
58             match TcpStream::connect(addr) {
59                 Ok(stream) => {
60                     let mut stream = stream;
61                     stream.write(&[1]);
62                     let mut buf = [0];
63                     stream.read(&mut buf);
64                 },
65                 Err(e) => debug!("{}", e)
66             }
67             tx.send(()).unwrap();
68         }).detach();
69     }
70
71     // Wait for all clients to exit, but don't wait for the server to exit. The
72     // server just runs infinitely.
73     drop(tx);
74     for _ in range(0u, 1000) {
75         rx.recv().unwrap();
76     }
77     unsafe { libc::exit(0) }
78 }