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