]> git.lizzy.rs Git - rust.git/blob - src/libstd/io/test.rs
std: Delete rt::test
[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 /// Get a port number, starting at 9600, for use in tests
12 pub fn next_test_port() -> u16 {
13     use unstable::atomics::{AtomicUint, INIT_ATOMIC_UINT, Relaxed};
14     static mut next_offset: AtomicUint = INIT_ATOMIC_UINT;
15     unsafe {
16         base_port() + next_offset.fetch_add(1, Relaxed) as u16
17     }
18 }
19
20 /// Get a temporary path which could be the location of a unix socket
21 pub fn next_test_unix() -> Path {
22     if cfg!(unix) {
23         os::tmpdir().join(rand::task_rng().gen_ascii_str(20))
24     } else {
25         Path::new(r"\\.\pipe\" + rand::task_rng().gen_ascii_str(20))
26     }
27 }
28
29 /// Get a unique IPv4 localhost:port pair starting at 9600
30 pub fn next_test_ip4() -> SocketAddr {
31     SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: next_test_port() }
32 }
33
34 /// Get a unique IPv6 localhost:port pair starting at 9600
35 pub fn next_test_ip6() -> SocketAddr {
36     SocketAddr { ip: Ipv6Addr(0, 0, 0, 0, 0, 0, 0, 1), port: next_test_port() }
37 }
38
39 /*
40 XXX: Welcome to MegaHack City.
41
42 The bots run multiple builds at the same time, and these builds
43 all want to use ports. This function figures out which workspace
44 it is running in and assigns a port range based on it.
45 */
46 fn base_port() -> u16 {
47     use os;
48     use str::StrSlice;
49     use vec::ImmutableVector;
50
51     let base = 9600u16;
52     let range = 1000u16;
53
54     let bases = [
55         ("32-opt", base + range * 1),
56         ("32-noopt", base + range * 2),
57         ("64-opt", base + range * 3),
58         ("64-noopt", base + range * 4),
59         ("64-opt-vg", base + range * 5),
60         ("all-opt", base + range * 6),
61         ("snap3", base + range * 7),
62         ("dist", base + range * 8)
63     ];
64
65     // FIXME (#9639): This needs to handle non-utf8 paths
66     let path = os::getcwd();
67     let path_s = path.as_str().unwrap();
68
69     let mut final_base = base;
70
71     for &(dir, base) in bases.iter() {
72         if path_s.contains(dir) {
73             final_base = base;
74             break;
75         }
76     }
77
78     return final_base;
79 }