summaryrefslogtreecommitdiff
path: root/test/sys/test_select.rs
blob: 6b7eac9a70b8fc8395043605829ba1082be96b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use nix::sys::select::{FdSet, FD_SETSIZE, select};
use nix::sys::time::TimeVal;
use nix::unistd::{read, write, pipe};

#[test]
fn test_fdset() {
    let mut fd_set = FdSet::new();

    for i in 0..FD_SETSIZE {
        assert!(!fd_set.contains(i));
    }

    fd_set.insert(7);

    assert!(fd_set.contains(7));

    fd_set.remove(7);

    for i in 0..FD_SETSIZE {
        assert!(!fd_set.contains(i));
    }

    fd_set.insert(1);
    fd_set.insert(FD_SETSIZE / 2);
    fd_set.insert(FD_SETSIZE - 1);

    fd_set.clear();

    for i in 0..FD_SETSIZE {
        assert!(!fd_set.contains(i));
    }
}

#[test]
fn test_select() {
    let (r1, w1) = pipe().unwrap();
    write(w1, b"hi!").unwrap();
    let (r2, w2) = pipe().unwrap();

    let mut fd_set = FdSet::new();
    fd_set.insert(r1);
    fd_set.insert(r2);

    let mut timeout = TimeVal::seconds(1);
    assert_eq!(1, select(r2 + 1,
                         Some(&mut fd_set),
                         None,
                         None,
                         &mut timeout).unwrap());
    assert!(fd_set.contains(r1));
    assert!(!fd_set.contains(r2));
}