xref: /libuv/test/test-process-priority.c (revision 011a1ac1)
1 /* Copyright libuv contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include "uv.h"
23 #include "task.h"
24 
25 
TEST_IMPL(process_priority)26 TEST_IMPL(process_priority) {
27   int priority;
28   int r;
29   int i;
30 
31 #if defined(__MVS__)
32   if (uv_os_setpriority(0, 0) == UV_ENOSYS)
33     RETURN_SKIP("functionality not supported on zOS");
34 #endif
35 
36   /* Verify that passing a NULL pointer returns UV_EINVAL. */
37   r = uv_os_getpriority(0, NULL);
38   ASSERT_EQ(r, UV_EINVAL);
39 
40   /* Verify that all valid values work. */
41   for (i = UV_PRIORITY_HIGHEST; i <= UV_PRIORITY_LOW; i++) {
42     r = uv_os_setpriority(0, i);
43 
44     /* If UV_EACCES is returned, the current user doesn't have permission to
45        set this specific priority. */
46     if (r == UV_EACCES)
47       continue;
48 
49     ASSERT_OK(r);
50     ASSERT_OK(uv_os_getpriority(0, &priority));
51 
52     /* Verify that the priority values match on Unix, and are range mapped
53        on Windows. */
54 #ifndef _WIN32
55     ASSERT_EQ(priority, i);
56 #else
57     /* On Windows, only elevated users can set UV_PRIORITY_HIGHEST. Other
58        users will silently be set to UV_PRIORITY_HIGH. */
59     if (i < UV_PRIORITY_HIGH)
60       ASSERT(priority == UV_PRIORITY_HIGHEST || priority == UV_PRIORITY_HIGH);
61     else if (i < UV_PRIORITY_ABOVE_NORMAL)
62       ASSERT_EQ(priority, UV_PRIORITY_HIGH);
63     else if (i < UV_PRIORITY_NORMAL)
64       ASSERT_EQ(priority, UV_PRIORITY_ABOVE_NORMAL);
65     else if (i < UV_PRIORITY_BELOW_NORMAL)
66       ASSERT_EQ(priority, UV_PRIORITY_NORMAL);
67     else if (i < UV_PRIORITY_LOW)
68       ASSERT_EQ(priority, UV_PRIORITY_BELOW_NORMAL);
69     else
70       ASSERT_EQ(priority, UV_PRIORITY_LOW);
71 #endif
72 
73     /* Verify that the current PID and 0 are equivalent. */
74     ASSERT_OK(uv_os_getpriority(uv_os_getpid(), &r));
75     ASSERT_EQ(priority, r);
76   }
77 
78   /* Verify that invalid priorities return UV_EINVAL. */
79   ASSERT_EQ(uv_os_setpriority(0, UV_PRIORITY_HIGHEST - 1), UV_EINVAL);
80   ASSERT_EQ(uv_os_setpriority(0, UV_PRIORITY_LOW + 1), UV_EINVAL);
81 
82   return 0;
83 }
84