]> git.lizzy.rs Git - rust.git/blob - src/libstd/io/test.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / libstd / io / test.rs
1 // Copyright 2013 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 //! Various utility functions useful for writing I/O tests
12
13 #![macro_escape]
14
15 use prelude::v1::*;
16
17 use libc;
18 use os;
19 use std::io::net::ip::*;
20 use sync::atomic::{AtomicUint, ATOMIC_UINT_INIT, Ordering};
21
22 /// Get a port number, starting at 9600, for use in tests
23 pub fn next_test_port() -> u16 {
24     static NEXT_OFFSET: AtomicUint = ATOMIC_UINT_INIT;
25     base_port() + NEXT_OFFSET.fetch_add(1, Ordering::Relaxed) as u16
26 }
27
28 /// Get a temporary path which could be the location of a unix socket
29 pub fn next_test_unix() -> Path {
30     static COUNT: AtomicUint = ATOMIC_UINT_INIT;
31     // base port and pid are an attempt to be unique between multiple
32     // test-runners of different configurations running on one
33     // buildbot, the count is to be unique within this executable.
34     let string = format!("rust-test-unix-path-{}-{}-{}",
35                          base_port(),
36                          unsafe {libc::getpid()},
37                          COUNT.fetch_add(1, Ordering::Relaxed));
38     if cfg!(unix) {
39         os::tmpdir().join(string)
40     } else {
41         Path::new(format!("{}{}", r"\\.\pipe\", string))
42     }
43 }
44
45 /// Get a unique IPv4 localhost:port pair starting at 9600
46 pub fn next_test_ip4() -> SocketAddr {
47     SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: next_test_port() }
48 }
49
50 /// Get a unique IPv6 localhost:port pair starting at 9600
51 pub fn next_test_ip6() -> SocketAddr {
52     SocketAddr { ip: Ipv6Addr(0, 0, 0, 0, 0, 0, 0, 1), port: next_test_port() }
53 }
54
55 /*
56 XXX: Welcome to MegaHack City.
57
58 The bots run multiple builds at the same time, and these builds
59 all want to use ports. This function figures out which workspace
60 it is running in and assigns a port range based on it.
61 */
62 fn base_port() -> u16 {
63
64     let base = 9600u16;
65     let range = 1000u16;
66
67     let bases = [
68         ("32-opt", base + range * 1),
69         ("32-nopt", base + range * 2),
70         ("64-opt", base + range * 3),
71         ("64-nopt", base + range * 4),
72         ("64-opt-vg", base + range * 5),
73         ("all-opt", base + range * 6),
74         ("snap3", base + range * 7),
75         ("dist", base + range * 8)
76     ];
77
78     // FIXME (#9639): This needs to handle non-utf8 paths
79     let path = os::getcwd().unwrap();
80     let path_s = path.as_str().unwrap();
81
82     let mut final_base = base;
83
84     for &(dir, base) in bases.iter() {
85         if path_s.contains(dir) {
86             final_base = base;
87             break;
88         }
89     }
90
91     return final_base;
92 }
93
94 /// Raises the file descriptor limit when running tests if necessary
95 pub fn raise_fd_limit() {
96     unsafe { darwin_fd_limit::raise_fd_limit() }
97 }
98
99 /// darwin_fd_limit exists to work around an issue where launchctl on Mac OS X defaults the rlimit
100 /// maxfiles to 256/unlimited. The default soft limit of 256 ends up being far too low for our
101 /// multithreaded scheduler testing, depending on the number of cores available.
102 ///
103 /// This fixes issue #7772.
104 #[cfg(target_os="macos")]
105 #[allow(non_camel_case_types)]
106 mod darwin_fd_limit {
107     use libc;
108     type rlim_t = libc::uint64_t;
109     #[repr(C)]
110     struct rlimit {
111         rlim_cur: rlim_t,
112         rlim_max: rlim_t
113     }
114     extern {
115         // name probably doesn't need to be mut, but the C function doesn't specify const
116         fn sysctl(name: *mut libc::c_int, namelen: libc::c_uint,
117                   oldp: *mut libc::c_void, oldlenp: *mut libc::size_t,
118                   newp: *mut libc::c_void, newlen: libc::size_t) -> libc::c_int;
119         fn getrlimit(resource: libc::c_int, rlp: *mut rlimit) -> libc::c_int;
120         fn setrlimit(resource: libc::c_int, rlp: *const rlimit) -> libc::c_int;
121     }
122     static CTL_KERN: libc::c_int = 1;
123     static KERN_MAXFILESPERPROC: libc::c_int = 29;
124     static RLIMIT_NOFILE: libc::c_int = 8;
125
126     pub unsafe fn raise_fd_limit() {
127         // The strategy here is to fetch the current resource limits, read the kern.maxfilesperproc
128         // sysctl value, and bump the soft resource limit for maxfiles up to the sysctl value.
129         use ptr::null_mut;
130         use mem::size_of_val;
131         use os::last_os_error;
132
133         // Fetch the kern.maxfilesperproc value
134         let mut mib: [libc::c_int; 2] = [CTL_KERN, KERN_MAXFILESPERPROC];
135         let mut maxfiles: libc::c_int = 0;
136         let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t;
137         if sysctl(&mut mib[0], 2, &mut maxfiles as *mut libc::c_int as *mut libc::c_void, &mut size,
138                   null_mut(), 0) != 0 {
139             let err = last_os_error();
140             panic!("raise_fd_limit: error calling sysctl: {}", err);
141         }
142
143         // Fetch the current resource limits
144         let mut rlim = rlimit{rlim_cur: 0, rlim_max: 0};
145         if getrlimit(RLIMIT_NOFILE, &mut rlim) != 0 {
146             let err = last_os_error();
147             panic!("raise_fd_limit: error calling getrlimit: {}", err);
148         }
149
150         // Bump the soft limit to the smaller of kern.maxfilesperproc and the hard limit
151         rlim.rlim_cur = ::cmp::min(maxfiles as rlim_t, rlim.rlim_max);
152
153         // Set our newly-increased resource limit
154         if setrlimit(RLIMIT_NOFILE, &rlim) != 0 {
155             let err = last_os_error();
156             panic!("raise_fd_limit: error calling setrlimit: {}", err);
157         }
158     }
159 }
160
161 #[cfg(not(target_os="macos"))]
162 mod darwin_fd_limit {
163     pub unsafe fn raise_fd_limit() {}
164 }