]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/tcp-connect-timeouts.rs
Honor hidden doc attribute of derivable trait methods
[rust.git] / src / test / run-pass / tcp-connect-timeouts.rs
1 // Copyright 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-pretty
12 // compile-flags:--test
13 // exec-env:RUST_TEST_TASKS=1
14
15 // Tests for the connect_timeout() function on a TcpStream. This runs with only
16 // one test task to ensure that errors are timeouts, not file descriptor
17 // exhaustion.
18
19 #![feature(macro_rules, globs)]
20 #![allow(experimental)]
21
22 extern crate native;
23 extern crate green;
24 extern crate rustuv;
25
26 #[cfg(test)] #[start]
27 fn start(argc: int, argv: **u8) -> int {
28     green::start(argc, argv, rustuv::event_loop, __test::main)
29 }
30
31 macro_rules! iotest (
32     { fn $name:ident() $b:block $($a:attr)* } => (
33         mod $name {
34             #![allow(unused_imports)]
35
36             use std::io::*;
37             use std::io::net::tcp::*;
38             use std::io::test::*;
39             use std::io;
40
41             fn f() $b
42
43             $($a)* #[test] fn green() { f() }
44             $($a)* #[test] fn native() {
45                 use native;
46                 let (tx, rx) = channel();
47                 native::task::spawn(proc() { tx.send(f()) });
48                 rx.recv();
49             }
50         }
51     )
52 )
53
54 iotest!(fn eventual_timeout() {
55     use native;
56     let addr = next_test_ip4();
57
58     // Use a native task to receive connections because it turns out libuv is
59     // really good at accepting connections and will likely run out of file
60     // descriptors before timing out.
61     let (tx1, rx1) = channel();
62     let (_tx2, rx2) = channel::<()>();
63     native::task::spawn(proc() {
64         let _l = TcpListener::bind(addr).unwrap().listen();
65         tx1.send(());
66         let _ = rx2.recv_opt();
67     });
68     rx1.recv();
69
70     let mut v = Vec::new();
71     for _ in range(0, 10000) {
72         match TcpStream::connect_timeout(addr, 100) {
73             Ok(e) => v.push(e),
74             Err(ref e) if e.kind == io::TimedOut => return,
75             Err(e) => fail!("other error: {}", e),
76         }
77     }
78     fail!("never timed out!");
79 })
80
81 iotest!(fn timeout_success() {
82     let addr = next_test_ip4();
83     let _l = TcpListener::bind(addr).unwrap().listen();
84
85     assert!(TcpStream::connect_timeout(addr, 1000).is_ok());
86 })
87
88 iotest!(fn timeout_error() {
89     let addr = next_test_ip4();
90
91     assert!(TcpStream::connect_timeout(addr, 1000).is_err());
92 })