test-process-priority.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include "uv.h"
  22. #include "task.h"
  23. TEST_IMPL(process_priority) {
  24. int priority;
  25. int r;
  26. int i;
  27. #if defined(__MVS__)
  28. if (uv_os_setpriority(0, 0) == UV_ENOSYS)
  29. RETURN_SKIP("functionality not supported on zOS");
  30. #endif
  31. /* Verify that passing a NULL pointer returns UV_EINVAL. */
  32. r = uv_os_getpriority(0, NULL);
  33. ASSERT(r == UV_EINVAL);
  34. /* Verify that all valid values work. */
  35. for (i = UV_PRIORITY_HIGHEST; i <= UV_PRIORITY_LOW; i++) {
  36. r = uv_os_setpriority(0, i);
  37. /* If UV_EACCES is returned, the current user doesn't have permission to
  38. set this specific priority. */
  39. if (r == UV_EACCES)
  40. continue;
  41. ASSERT(r == 0);
  42. ASSERT(uv_os_getpriority(0, &priority) == 0);
  43. /* Verify that the priority values match on Unix, and are range mapped
  44. on Windows. */
  45. #ifndef _WIN32
  46. ASSERT(priority == i);
  47. #else
  48. /* On Windows, only elevated users can set UV_PRIORITY_HIGHEST. Other
  49. users will silently be set to UV_PRIORITY_HIGH. */
  50. if (i < UV_PRIORITY_HIGH)
  51. ASSERT(priority == UV_PRIORITY_HIGHEST || priority == UV_PRIORITY_HIGH);
  52. else if (i < UV_PRIORITY_ABOVE_NORMAL)
  53. ASSERT(priority == UV_PRIORITY_HIGH);
  54. else if (i < UV_PRIORITY_NORMAL)
  55. ASSERT(priority == UV_PRIORITY_ABOVE_NORMAL);
  56. else if (i < UV_PRIORITY_BELOW_NORMAL)
  57. ASSERT(priority == UV_PRIORITY_NORMAL);
  58. else if (i < UV_PRIORITY_LOW)
  59. ASSERT(priority == UV_PRIORITY_BELOW_NORMAL);
  60. else
  61. ASSERT(priority == UV_PRIORITY_LOW);
  62. #endif
  63. /* Verify that the current PID and 0 are equivalent. */
  64. ASSERT(uv_os_getpriority(uv_os_getpid(), &r) == 0);
  65. ASSERT(priority == r);
  66. }
  67. /* Verify that invalid priorities return UV_EINVAL. */
  68. ASSERT(uv_os_setpriority(0, UV_PRIORITY_HIGHEST - 1) == UV_EINVAL);
  69. ASSERT(uv_os_setpriority(0, UV_PRIORITY_LOW + 1) == UV_EINVAL);
  70. return 0;
  71. }