|
| 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-fast |
| 13 | +// ignore-android needs extra network permissions |
| 14 | +// exec-env:RUST_LOG=debug |
| 15 | + |
| 16 | +use std::libc; |
| 17 | +use std::io::net::ip::{Ipv4Addr, SocketAddr}; |
| 18 | +use std::io::net::tcp::{TcpListener, TcpStream}; |
| 19 | +use std::io::{Acceptor, Listener}; |
| 20 | + |
| 21 | +fn main() { |
| 22 | + // This test has a chance to time out, try to not let it time out |
| 23 | + spawn(proc() { |
| 24 | + use std::io::timer; |
| 25 | + timer::sleep(30 * 1000); |
| 26 | + println!("timed out!"); |
| 27 | + unsafe { libc::exit(1) } |
| 28 | + }); |
| 29 | + |
| 30 | + let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 0 }; |
| 31 | + let (p, c) = Chan::new(); |
| 32 | + spawn(proc() { |
| 33 | + let mut listener = TcpListener::bind(addr).unwrap(); |
| 34 | + c.send(listener.socket_name().unwrap()); |
| 35 | + let mut acceptor = listener.listen(); |
| 36 | + loop { |
| 37 | + let mut stream = match acceptor.accept() { |
| 38 | + Ok(stream) => stream, |
| 39 | + Err(error) => { |
| 40 | + debug!("accept failed: {:?}", error); |
| 41 | + continue; |
| 42 | + } |
| 43 | + }; |
| 44 | + stream.read_byte(); |
| 45 | + stream.write([2]); |
| 46 | + } |
| 47 | + }); |
| 48 | + let addr = p.recv(); |
| 49 | + |
| 50 | + let (p, c) = Chan::new(); |
| 51 | + for _ in range(0, 1000) { |
| 52 | + let c = c.clone(); |
| 53 | + spawn(proc() { |
| 54 | + match TcpStream::connect(addr) { |
| 55 | + Ok(stream) => { |
| 56 | + let mut stream = stream; |
| 57 | + stream.write([1]); |
| 58 | + let mut buf = [0]; |
| 59 | + stream.read(buf); |
| 60 | + }, |
| 61 | + Err(e) => debug!("{:?}", e) |
| 62 | + } |
| 63 | + c.send(()); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + // Wait for all clients to exit, but don't wait for the server to exit. The |
| 68 | + // server just runs infinitely. |
| 69 | + drop(c); |
| 70 | + for _ in range(0, 1000) { |
| 71 | + p.recv(); |
| 72 | + } |
| 73 | + unsafe { libc::exit(0) } |
| 74 | +} |
0 commit comments