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)]
27 fn start(argc: int, argv: **u8) -> int {
28 green::start(argc, argv, rustuv::event_loop, __test::main)
32 { fn $name:ident() $b:block $(#[$a:meta])* } => (
34 #![allow(unused_imports)]
37 use std::io::net::tcp::*;
43 $(#[$a])* #[test] fn green() { f() }
44 $(#[$a])* #[test] fn native() {
46 let (tx, rx) = channel();
47 native::task::spawn(proc() { tx.send(f()) });
54 iotest!(fn eventual_timeout() {
56 let addr = next_test_ip4();
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();
66 let _ = rx2.recv_opt();
70 let mut v = Vec::new();
71 for _ in range(0, 10000) {
72 match TcpStream::connect_timeout(addr, 100) {
74 Err(ref e) if e.kind == io::TimedOut => return,
75 Err(e) => fail!("other error: {}", e),
78 fail!("never timed out!");
79 } #[ignore(cfg(target_os = "freebsd"))])
81 iotest!(fn timeout_success() {
82 let addr = next_test_ip4();
83 let _l = TcpListener::bind(addr).unwrap().listen();
85 assert!(TcpStream::connect_timeout(addr, 1000).is_ok());
88 iotest!(fn timeout_error() {
89 let addr = next_test_ip4();
91 assert!(TcpStream::connect_timeout(addr, 1000).is_err());