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.
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.
12 // compile-flags:--test
13 // exec-env:RUST_TEST_TASKS=1
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
20 #![reexport_test_harness_main = "test_main"]
22 #![allow(unused_imports)]
27 use std::time::Duration;
28 use std::sync::mpsc::channel;
29 use std::thread::Thread;
31 #[cfg_attr(target_os = "freebsd", ignore)]
32 fn eventual_timeout() {
33 let addr = next_test_ip4();
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();
44 let mut v = Vec::new();
45 for _ in range(0u, 10000) {
46 match TcpStream::connect_timeout(addr, Duration::milliseconds(100)) {
48 Err(ref e) if e.kind == io::TimedOut => return,
49 Err(e) => panic!("other error: {}", e),
52 panic!("never timed out!");
55 fn timeout_success() {
56 let addr = next_test_ip4();
57 let _l = TcpListener::bind(addr).unwrap().listen();
59 assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(1000)).is_ok());
63 let addr = next_test_ip4();
65 assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(1000)).is_err());
68 fn connect_timeout_zero() {
69 let addr = next_test_ip4();
70 assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(0)).is_err());
73 fn connect_timeout_negative() {
74 let addr = next_test_ip4();
75 assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(-1)).is_err());