]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/tcp-stress.rs
auto merge of #13704 : edwardw/rust/doc-hidden, r=alexcrichton
[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(syntax, link)]
17 extern crate log;
18 extern crate libc;
19 extern crate green;
20 extern crate rustuv;
21
22 use std::io::net::ip::{Ipv4Addr, SocketAddr};
23 use std::io::net::tcp::{TcpListener, TcpStream};
24 use std::io::{Acceptor, Listener};
25 use std::task::TaskBuilder;
26
27 #[start]
28 fn start(argc: int, argv: **u8) -> int {
29     green::start(argc, argv, rustuv::event_loop, main)
30 }
31
32 fn main() {
33     // This test has a chance to time out, try to not let it time out
34     spawn(proc() {
35         use std::io::timer;
36         timer::sleep(30 * 1000);
37         println!("timed out!");
38         unsafe { libc::exit(1) }
39     });
40
41     let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 0 };
42     let (tx, rx) = channel();
43     spawn(proc() {
44         let mut listener = TcpListener::bind(addr).unwrap();
45         tx.send(listener.socket_name().unwrap());
46         let mut acceptor = listener.listen();
47         loop {
48             let mut stream = match acceptor.accept() {
49                 Ok(stream) => stream,
50                 Err(error) => {
51                     debug!("accept failed: {:?}", error);
52                     continue;
53                 }
54             };
55             stream.read_byte();
56             stream.write([2]);
57         }
58     });
59     let addr = rx.recv();
60
61     let (tx, rx) = channel();
62     for _ in range(0, 1000) {
63         let tx = tx.clone();
64         let mut builder = TaskBuilder::new();
65         builder.opts.stack_size = Some(32 * 1024);
66         builder.spawn(proc() {
67             match TcpStream::connect(addr) {
68                 Ok(stream) => {
69                     let mut stream = stream;
70                     stream.write([1]);
71                     let mut buf = [0];
72                     stream.read(buf);
73                 },
74                 Err(e) => debug!("{:?}", e)
75             }
76             tx.send(());
77         });
78     }
79
80     // Wait for all clients to exit, but don't wait for the server to exit. The
81     // server just runs infinitely.
82     drop(tx);
83     for _ in range(0, 1000) {
84         rx.recv();
85     }
86     unsafe { libc::exit(0) }
87 }