summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/tests/process/priority.rs
blob: 7feb1467a614f9af4e177c931dc1bfc6fd43ef8c (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use rustix::process::nice;
#[cfg(not(target_os = "redox"))]
use rustix::process::{getpriority_process, setpriority_process};

#[cfg(not(target_os = "freebsd"))] // FreeBSD's nice(3) doesn't return the old value.
#[test]
fn test_priorities() {
    let old = nice(0).unwrap();

    #[cfg(not(target_os = "redox"))]
    {
        let get_prio = getpriority_process(None).unwrap();
        assert_eq!(get_prio, old);
    }

    // Lower the priority by one.
    let new = nice(1).unwrap();

    // If the test wasn't running with the lowest priority initially, test that
    // we were able to lower the priority.
    if old < 19 {
        assert_eq!(old + 1, new);
    }

    let get = nice(0).unwrap();
    assert_eq!(new, get);

    #[cfg(not(target_os = "redox"))]
    {
        let get_prio = getpriority_process(None).unwrap();
        assert_eq!(get_prio, new);

        setpriority_process(None, get + 1).unwrap();
        let now = getpriority_process(None).unwrap();

        // If the test wasn't running with the lowest priority initially, test
        // that we were able to lower the priority.
        if get < 19 {
            assert_eq!(get + 1, now);
        }
        setpriority_process(None, get + 10000).unwrap();
        let now = getpriority_process(None).unwrap();
        // Linux's max is 19; Darwin's max is 20.
        assert!(now >= 19 && now <= 20);
        // Darwin appears to return `EPERM` on an out of range `nice`.
        if let Ok(again) = nice(1) {
            assert_eq!(now, again);
        }
    }
}

/// FreeBSD's `nice` doesn't return the new nice value, so use a specialized
/// test.
#[cfg(target_os = "freebsd")]
#[test]
fn test_priorities() {
    let start = getpriority_process(None).unwrap();

    let _ = nice(0).unwrap();

    let now = getpriority_process(None).unwrap();
    assert_eq!(start, now);

    let _ = nice(1).unwrap();

    let now = getpriority_process(None).unwrap();
    assert_eq!(start + 1, now);

    setpriority_process(None, start + 2).unwrap();

    let now = getpriority_process(None).unwrap();
    assert_eq!(start + 2, now);

    setpriority_process(None, 10000).unwrap();

    let now = getpriority_process(None).unwrap();
    assert_eq!(now, 20);

    let _ = nice(1).unwrap();

    let now = getpriority_process(None).unwrap();
    assert_eq!(now, 20);
}