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
19 #![feature(macro_rules, globs)]
20 #![allow(experimental)]
21 #![reexport_test_harness_main = "test_main"]
23 #![allow(unused_imports)]
28 use std::time::Duration;
29 use std::sync::mpsc::channel;
30 use std::thread::Thread;
32 #[cfg_attr(target_os = "freebsd", ignore)]
33 fn eventual_timeout() {
34 let addr = next_test_ip4();
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();
45 let mut v = Vec::new();
46 for _ in range(0u, 10000) {
47 match TcpStream::connect_timeout(addr, Duration::milliseconds(100)) {
49 Err(ref e) if e.kind == io::TimedOut => return,
50 Err(e) => panic!("other error: {}", e),
53 panic!("never timed out!");
56 fn timeout_success() {
57 let addr = next_test_ip4();
58 let _l = TcpListener::bind(addr).unwrap().listen();
60 assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(1000)).is_ok());
64 let addr = next_test_ip4();
66 assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(1000)).is_err());
69 fn connect_timeout_zero() {
70 let addr = next_test_ip4();
71 assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(0)).is_err());
74 fn connect_timeout_negative() {
75 let addr = next_test_ip4();
76 assert!(TcpStream::connect_timeout(addr, Duration::milliseconds(-1)).is_err());