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