testUVHandlePtr.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <functional>
  2. #include <iostream>
  3. #include <memory>
  4. #include <cm3p/uv.h>
  5. #include "cmGetPipes.h"
  6. #include "cmUVHandlePtr.h"
  7. static bool testBool()
  8. {
  9. cm::uv_async_ptr async;
  10. cm::uv_handle_ptr handle;
  11. cm::uv_idle_ptr idle;
  12. cm::uv_pipe_ptr pipe;
  13. cm::uv_process_ptr process;
  14. cm::uv_signal_ptr signal;
  15. cm::uv_stream_ptr stream;
  16. cm::uv_timer_ptr timer;
  17. cm::uv_tty_ptr tty;
  18. return !async && !handle && !idle && !pipe && !process && !signal &&
  19. !stream && !timer && !tty;
  20. }
  21. static bool testIdle()
  22. {
  23. bool idled = false;
  24. cm::uv_loop_ptr loop;
  25. loop.init();
  26. auto cb = [](uv_idle_t* handle) {
  27. auto idledPtr = static_cast<bool*>(handle->data);
  28. *idledPtr = true;
  29. uv_idle_stop(handle);
  30. };
  31. cm::uv_idle_ptr idle;
  32. idle.init(*loop, &idled);
  33. idle.start(cb);
  34. uv_run(loop, UV_RUN_DEFAULT);
  35. if (!idled) {
  36. std::cerr << "uv_idle_ptr did not trigger callback" << std::endl;
  37. return false;
  38. }
  39. idled = false;
  40. idle.start(cb);
  41. idle.stop();
  42. uv_run(loop, UV_RUN_DEFAULT);
  43. if (idled) {
  44. std::cerr << "uv_idle_ptr::stop did not stop callback" << std::endl;
  45. return false;
  46. }
  47. return true;
  48. }
  49. static bool testTimer()
  50. {
  51. bool timed = false;
  52. cm::uv_loop_ptr loop;
  53. loop.init();
  54. auto cb = [](uv_timer_t* handle) {
  55. auto timedPtr = static_cast<bool*>(handle->data);
  56. *timedPtr = true;
  57. uv_timer_stop(handle);
  58. };
  59. cm::uv_timer_ptr timer;
  60. timer.init(*loop, &timed);
  61. timer.start(cb, 10, 0, cm::uv_update_time::no);
  62. uv_run(loop, UV_RUN_DEFAULT);
  63. if (!timed) {
  64. std::cerr << "uv_timer_ptr did not trigger callback" << std::endl;
  65. return false;
  66. }
  67. timed = false;
  68. timer.start(cb, 10, 0, cm::uv_update_time::no);
  69. timer.stop();
  70. uv_run(loop, UV_RUN_DEFAULT);
  71. if (timed) {
  72. std::cerr << "uv_timer_ptr::stop did not stop callback" << std::endl;
  73. return false;
  74. }
  75. return true;
  76. }
  77. static bool testWriteCallback()
  78. {
  79. int pipe[] = { -1, -1 };
  80. if (cmGetPipes(pipe) < 0) {
  81. std::cout << "cmGetPipes() returned an error" << std::endl;
  82. return false;
  83. }
  84. cm::uv_loop_ptr loop;
  85. loop.init();
  86. cm::uv_pipe_ptr pipeRead;
  87. pipeRead.init(*loop, 0);
  88. uv_pipe_open(pipeRead, pipe[0]);
  89. cm::uv_pipe_ptr pipeWrite;
  90. pipeWrite.init(*loop, 0);
  91. uv_pipe_open(pipeWrite, pipe[1]);
  92. char c = '.';
  93. uv_buf_t buf = uv_buf_init(&c, sizeof(c));
  94. int status = -1;
  95. auto cb = std::make_shared<std::function<void(int)>>(
  96. [&status](int s) { status = s; });
  97. // Test getting a callback after the write is done.
  98. cm::uv_write(pipeWrite, &buf, 1, cb);
  99. uv_run(loop, UV_RUN_DEFAULT);
  100. if (status != 0) {
  101. std::cout << "cm::uv_write non-zero status: " << status << std::endl;
  102. return false;
  103. }
  104. // Test deleting the callback before it is made.
  105. status = -1;
  106. cm::uv_write(pipeWrite, &buf, 1, cb);
  107. cb.reset();
  108. uv_run(loop, UV_RUN_DEFAULT);
  109. if (status != -1) {
  110. std::cout << "cm::uv_write callback incorrectly called with status: "
  111. << status << std::endl;
  112. return false;
  113. }
  114. return true;
  115. }
  116. int testUVHandlePtr(int, char** const)
  117. {
  118. bool passed = true;
  119. passed = testBool() && passed;
  120. passed = testIdle() && passed;
  121. passed = testTimer() && passed;
  122. passed = testWriteCallback() && passed;
  123. return passed ? 0 : -1;
  124. }