]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/tcp-connect-timeouts.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[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 #![allow(unstable)]
20 #![reexport_test_harness_main = "test_main"]
21
22 #![allow(unused_imports)]
23
24 use std::io::*;
25 use std::io::test::*;
26 use std::io;
27 use std::time::Duration;
28 use std::sync::mpsc::channel;
29 use std::thread::Thread;
30
31 #[cfg_attr(target_os = "freebsd", ignore)]
32 fn eventual_timeout() {
33     let addr = next_test_ip4();
34
35     let (tx1, rx1) = channel();
36     let (_tx2, rx2) = channel::<()>();
37     let _t = Thread::spawn(move|| {
38         let _l = TcpListener::bind(addr).unwrap().listen();
39         tx1.send(()).unwrap();
40         let _ = rx2.recv();
41     });
42     rx1.recv().unwrap();
43
44     let mut v = Vec::new();
45     for _ in range(0u, 10000) {
46         match TcpStream::connect_timeout(addr, Duration::milliseconds(100)) {
47             Ok(e) => v.push(e),
48             Err(ref e) if e.kind == io::TimedOut => return,
49             Err(e) => panic!("other error: {}", e),
50         }
51     }
52     panic!("never timed out!");
53 }
54
55 fn timeout_success() {
56     let addr = next_test_ip4();
57     let _l = TcpListener::bind(addr).unwrap().listen();
58
59     assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(1000)).is_ok());
60 }
61
62 fn timeout_error() {
63     let addr = next_test_ip4();
64
65     assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(1000)).is_err());
66 }
67
68 fn connect_timeout_zero() {
69     let addr = next_test_ip4();
70     assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(0)).is_err());
71 }
72
73 fn connect_timeout_negative() {
74     let addr = next_test_ip4();
75     assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(-1)).is_err());
76 }