test-threadpool-cancel.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* Copyright Joyent, Inc. and other Node 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. #define INIT_CANCEL_INFO(ci, what) \
  24. do { \
  25. (ci)->reqs = (what); \
  26. (ci)->nreqs = ARRAY_SIZE(what); \
  27. (ci)->stride = sizeof((what)[0]); \
  28. } \
  29. while (0)
  30. struct cancel_info {
  31. void* reqs;
  32. unsigned nreqs;
  33. unsigned stride;
  34. uv_timer_t timer_handle;
  35. };
  36. static uv_cond_t signal_cond;
  37. static uv_mutex_t signal_mutex;
  38. static uv_mutex_t wait_mutex;
  39. static unsigned num_threads;
  40. static unsigned fs_cb_called;
  41. static unsigned work_cb_called;
  42. static unsigned done_cb_called;
  43. static unsigned done2_cb_called;
  44. static unsigned timer_cb_called;
  45. static unsigned getaddrinfo_cb_called;
  46. static void work_cb(uv_work_t* req) {
  47. uv_mutex_lock(&signal_mutex);
  48. uv_cond_signal(&signal_cond);
  49. uv_mutex_unlock(&signal_mutex);
  50. uv_mutex_lock(&wait_mutex);
  51. uv_mutex_unlock(&wait_mutex);
  52. work_cb_called++;
  53. }
  54. static void done_cb(uv_work_t* req, int status) {
  55. done_cb_called++;
  56. free(req);
  57. }
  58. static void saturate_threadpool(void) {
  59. uv_work_t* req;
  60. ASSERT(0 == uv_cond_init(&signal_cond));
  61. ASSERT(0 == uv_mutex_init(&signal_mutex));
  62. ASSERT(0 == uv_mutex_init(&wait_mutex));
  63. uv_mutex_lock(&signal_mutex);
  64. uv_mutex_lock(&wait_mutex);
  65. for (num_threads = 0; /* empty */; num_threads++) {
  66. req = malloc(sizeof(*req));
  67. ASSERT(req != NULL);
  68. ASSERT(0 == uv_queue_work(uv_default_loop(), req, work_cb, done_cb));
  69. /* Expect to get signalled within 350 ms, otherwise assume that
  70. * the thread pool is saturated. As with any timing dependent test,
  71. * this is obviously not ideal.
  72. */
  73. if (uv_cond_timedwait(&signal_cond, &signal_mutex, 350 * 1e6)) {
  74. ASSERT(0 == uv_cancel((uv_req_t*) req));
  75. break;
  76. }
  77. }
  78. }
  79. static void unblock_threadpool(void) {
  80. uv_mutex_unlock(&signal_mutex);
  81. uv_mutex_unlock(&wait_mutex);
  82. }
  83. static void cleanup_threadpool(void) {
  84. ASSERT(done_cb_called == num_threads + 1); /* +1 == cancelled work req. */
  85. ASSERT(work_cb_called == num_threads);
  86. uv_cond_destroy(&signal_cond);
  87. uv_mutex_destroy(&signal_mutex);
  88. uv_mutex_destroy(&wait_mutex);
  89. }
  90. static void fs_cb(uv_fs_t* req) {
  91. ASSERT(req->result == UV_ECANCELED);
  92. uv_fs_req_cleanup(req);
  93. fs_cb_called++;
  94. }
  95. static void getaddrinfo_cb(uv_getaddrinfo_t* req,
  96. int status,
  97. struct addrinfo* res) {
  98. ASSERT(status == UV_EAI_CANCELED);
  99. ASSERT(res == NULL);
  100. uv_freeaddrinfo(res); /* Should not crash. */
  101. getaddrinfo_cb_called++;
  102. }
  103. static void work2_cb(uv_work_t* req) {
  104. ASSERT(0 && "work2_cb called");
  105. }
  106. static void done2_cb(uv_work_t* req, int status) {
  107. ASSERT(status == UV_ECANCELED);
  108. done2_cb_called++;
  109. }
  110. static void timer_cb(uv_timer_t* handle, int status) {
  111. struct cancel_info* ci;
  112. uv_req_t* req;
  113. unsigned i;
  114. ci = container_of(handle, struct cancel_info, timer_handle);
  115. for (i = 0; i < ci->nreqs; i++) {
  116. req = (uv_req_t*) ((char*) ci->reqs + i * ci->stride);
  117. ASSERT(0 == uv_cancel(req));
  118. }
  119. uv_close((uv_handle_t*) &ci->timer_handle, NULL);
  120. unblock_threadpool();
  121. timer_cb_called++;
  122. }
  123. static void nop_work_cb(uv_work_t* req) {
  124. }
  125. static void nop_done_cb(uv_work_t* req, int status) {
  126. req->data = "OK";
  127. }
  128. TEST_IMPL(threadpool_cancel_getaddrinfo) {
  129. uv_getaddrinfo_t reqs[4];
  130. struct cancel_info ci;
  131. struct addrinfo hints;
  132. uv_loop_t* loop;
  133. int r;
  134. INIT_CANCEL_INFO(&ci, reqs);
  135. loop = uv_default_loop();
  136. saturate_threadpool();
  137. r = uv_getaddrinfo(loop, reqs + 0, getaddrinfo_cb, "fail", NULL, NULL);
  138. ASSERT(r == 0);
  139. r = uv_getaddrinfo(loop, reqs + 1, getaddrinfo_cb, NULL, "fail", NULL);
  140. ASSERT(r == 0);
  141. r = uv_getaddrinfo(loop, reqs + 2, getaddrinfo_cb, "fail", "fail", NULL);
  142. ASSERT(r == 0);
  143. r = uv_getaddrinfo(loop, reqs + 3, getaddrinfo_cb, "fail", NULL, &hints);
  144. ASSERT(r == 0);
  145. ASSERT(0 == uv_timer_init(loop, &ci.timer_handle));
  146. ASSERT(0 == uv_timer_start(&ci.timer_handle, timer_cb, 10, 0));
  147. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  148. ASSERT(1 == timer_cb_called);
  149. cleanup_threadpool();
  150. MAKE_VALGRIND_HAPPY();
  151. return 0;
  152. }
  153. TEST_IMPL(threadpool_cancel_work) {
  154. struct cancel_info ci;
  155. uv_work_t reqs[16];
  156. uv_loop_t* loop;
  157. unsigned i;
  158. INIT_CANCEL_INFO(&ci, reqs);
  159. loop = uv_default_loop();
  160. saturate_threadpool();
  161. for (i = 0; i < ARRAY_SIZE(reqs); i++)
  162. ASSERT(0 == uv_queue_work(loop, reqs + i, work2_cb, done2_cb));
  163. ASSERT(0 == uv_timer_init(loop, &ci.timer_handle));
  164. ASSERT(0 == uv_timer_start(&ci.timer_handle, timer_cb, 10, 0));
  165. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  166. ASSERT(1 == timer_cb_called);
  167. ASSERT(ARRAY_SIZE(reqs) == done2_cb_called);
  168. cleanup_threadpool();
  169. MAKE_VALGRIND_HAPPY();
  170. return 0;
  171. }
  172. TEST_IMPL(threadpool_cancel_fs) {
  173. struct cancel_info ci;
  174. uv_fs_t reqs[25];
  175. uv_loop_t* loop;
  176. unsigned n;
  177. INIT_CANCEL_INFO(&ci, reqs);
  178. loop = uv_default_loop();
  179. saturate_threadpool();
  180. /* Needs to match ARRAY_SIZE(fs_reqs). */
  181. n = 0;
  182. ASSERT(0 == uv_fs_chmod(loop, reqs + n++, "/", 0, fs_cb));
  183. ASSERT(0 == uv_fs_chown(loop, reqs + n++, "/", 0, 0, fs_cb));
  184. ASSERT(0 == uv_fs_close(loop, reqs + n++, 0, fs_cb));
  185. ASSERT(0 == uv_fs_fchmod(loop, reqs + n++, 0, 0, fs_cb));
  186. ASSERT(0 == uv_fs_fchown(loop, reqs + n++, 0, 0, 0, fs_cb));
  187. ASSERT(0 == uv_fs_fdatasync(loop, reqs + n++, 0, fs_cb));
  188. ASSERT(0 == uv_fs_fstat(loop, reqs + n++, 0, fs_cb));
  189. ASSERT(0 == uv_fs_fsync(loop, reqs + n++, 0, fs_cb));
  190. ASSERT(0 == uv_fs_ftruncate(loop, reqs + n++, 0, 0, fs_cb));
  191. ASSERT(0 == uv_fs_futime(loop, reqs + n++, 0, 0, 0, fs_cb));
  192. ASSERT(0 == uv_fs_link(loop, reqs + n++, "/", "/", fs_cb));
  193. ASSERT(0 == uv_fs_lstat(loop, reqs + n++, "/", fs_cb));
  194. ASSERT(0 == uv_fs_mkdir(loop, reqs + n++, "/", 0, fs_cb));
  195. ASSERT(0 == uv_fs_open(loop, reqs + n++, "/", 0, 0, fs_cb));
  196. ASSERT(0 == uv_fs_read(loop, reqs + n++, 0, NULL, 0, 0, fs_cb));
  197. ASSERT(0 == uv_fs_readdir(loop, reqs + n++, "/", 0, fs_cb));
  198. ASSERT(0 == uv_fs_readlink(loop, reqs + n++, "/", fs_cb));
  199. ASSERT(0 == uv_fs_rename(loop, reqs + n++, "/", "/", fs_cb));
  200. ASSERT(0 == uv_fs_mkdir(loop, reqs + n++, "/", 0, fs_cb));
  201. ASSERT(0 == uv_fs_sendfile(loop, reqs + n++, 0, 0, 0, 0, fs_cb));
  202. ASSERT(0 == uv_fs_stat(loop, reqs + n++, "/", fs_cb));
  203. ASSERT(0 == uv_fs_symlink(loop, reqs + n++, "/", "/", 0, fs_cb));
  204. ASSERT(0 == uv_fs_unlink(loop, reqs + n++, "/", fs_cb));
  205. ASSERT(0 == uv_fs_utime(loop, reqs + n++, "/", 0, 0, fs_cb));
  206. ASSERT(0 == uv_fs_write(loop, reqs + n++, 0, NULL, 0, 0, fs_cb));
  207. ASSERT(n == ARRAY_SIZE(reqs));
  208. ASSERT(0 == uv_timer_init(loop, &ci.timer_handle));
  209. ASSERT(0 == uv_timer_start(&ci.timer_handle, timer_cb, 10, 0));
  210. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  211. ASSERT(n == fs_cb_called);
  212. ASSERT(1 == timer_cb_called);
  213. cleanup_threadpool();
  214. MAKE_VALGRIND_HAPPY();
  215. return 0;
  216. }
  217. TEST_IMPL(threadpool_cancel_single) {
  218. uv_loop_t* loop;
  219. uv_work_t req;
  220. int cancelled;
  221. int i;
  222. loop = uv_default_loop();
  223. for (i = 0; i < 5000; i++) {
  224. req.data = NULL;
  225. ASSERT(0 == uv_queue_work(loop, &req, nop_work_cb, nop_done_cb));
  226. cancelled = uv_cancel((uv_req_t*) &req);
  227. if (cancelled == 0)
  228. break;
  229. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  230. }
  231. if (cancelled != 0) {
  232. fputs("Failed to cancel a work req in 5,000 iterations, giving up.\n",
  233. stderr);
  234. return 1;
  235. }
  236. ASSERT(req.data == NULL);
  237. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  238. ASSERT(req.data != NULL); /* Should have been updated by nop_done_cb(). */
  239. MAKE_VALGRIND_HAPPY();
  240. return 0;
  241. }