test-ref.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. #include <stdlib.h>
  24. #include <string.h>
  25. static uv_write_t write_req;
  26. static uv_shutdown_t shutdown_req;
  27. static uv_connect_t connect_req;
  28. static char buffer[32767];
  29. static int req_cb_called;
  30. static int connect_cb_called;
  31. static int write_cb_called;
  32. static int shutdown_cb_called;
  33. static int close_cb_called;
  34. static void close_cb(uv_handle_t* handle) {
  35. close_cb_called++;
  36. }
  37. static void do_close(void* handle) {
  38. close_cb_called = 0;
  39. uv_close((uv_handle_t*)handle, close_cb);
  40. ASSERT(close_cb_called == 0);
  41. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  42. ASSERT(close_cb_called == 1);
  43. }
  44. static void fail_cb(void) {
  45. FATAL("fail_cb should not have been called");
  46. }
  47. static void fail_cb2(void) {
  48. ASSERT(0 && "fail_cb2 should not have been called");
  49. }
  50. static void req_cb(uv_handle_t* req, int status) {
  51. req_cb_called++;
  52. }
  53. static void shutdown_cb(uv_shutdown_t* req, int status) {
  54. ASSERT(req == &shutdown_req);
  55. shutdown_cb_called++;
  56. }
  57. static void write_cb(uv_write_t* req, int status) {
  58. ASSERT(req == &write_req);
  59. uv_shutdown(&shutdown_req, req->handle, shutdown_cb);
  60. write_cb_called++;
  61. }
  62. static void connect_and_write(uv_connect_t* req, int status) {
  63. uv_buf_t buf = uv_buf_init(buffer, sizeof buffer);
  64. ASSERT(req == &connect_req);
  65. ASSERT(status == 0);
  66. uv_write(&write_req, req->handle, &buf, 1, write_cb);
  67. connect_cb_called++;
  68. }
  69. static void connect_and_shutdown(uv_connect_t* req, int status) {
  70. ASSERT(req == &connect_req);
  71. ASSERT(status == 0);
  72. uv_shutdown(&shutdown_req, req->handle, shutdown_cb);
  73. connect_cb_called++;
  74. }
  75. TEST_IMPL(ref) {
  76. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  77. MAKE_VALGRIND_HAPPY();
  78. return 0;
  79. }
  80. TEST_IMPL(idle_ref) {
  81. uv_idle_t h;
  82. uv_idle_init(uv_default_loop(), &h);
  83. uv_idle_start(&h, (uv_idle_cb) fail_cb2);
  84. uv_unref((uv_handle_t*)&h);
  85. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  86. do_close(&h);
  87. MAKE_VALGRIND_HAPPY();
  88. return 0;
  89. }
  90. TEST_IMPL(async_ref) {
  91. uv_async_t h;
  92. uv_async_init(uv_default_loop(), &h, NULL);
  93. uv_unref((uv_handle_t*)&h);
  94. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  95. do_close(&h);
  96. MAKE_VALGRIND_HAPPY();
  97. return 0;
  98. }
  99. TEST_IMPL(prepare_ref) {
  100. uv_prepare_t h;
  101. uv_prepare_init(uv_default_loop(), &h);
  102. uv_prepare_start(&h, (uv_prepare_cb) fail_cb2);
  103. uv_unref((uv_handle_t*)&h);
  104. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  105. do_close(&h);
  106. MAKE_VALGRIND_HAPPY();
  107. return 0;
  108. }
  109. TEST_IMPL(check_ref) {
  110. uv_check_t h;
  111. uv_check_init(uv_default_loop(), &h);
  112. uv_check_start(&h, (uv_check_cb) fail_cb2);
  113. uv_unref((uv_handle_t*)&h);
  114. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  115. do_close(&h);
  116. MAKE_VALGRIND_HAPPY();
  117. return 0;
  118. }
  119. static void prepare_cb(uv_prepare_t* h, int status) {
  120. ASSERT(h != NULL);
  121. ASSERT(status == 0);
  122. uv_unref((uv_handle_t*)h);
  123. }
  124. TEST_IMPL(unref_in_prepare_cb) {
  125. uv_prepare_t h;
  126. uv_prepare_init(uv_default_loop(), &h);
  127. uv_prepare_start(&h, prepare_cb);
  128. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  129. do_close(&h);
  130. MAKE_VALGRIND_HAPPY();
  131. return 0;
  132. }
  133. TEST_IMPL(timer_ref) {
  134. uv_timer_t h;
  135. uv_timer_init(uv_default_loop(), &h);
  136. uv_unref((uv_handle_t*)&h);
  137. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  138. do_close(&h);
  139. MAKE_VALGRIND_HAPPY();
  140. return 0;
  141. }
  142. TEST_IMPL(timer_ref2) {
  143. uv_timer_t h;
  144. uv_timer_init(uv_default_loop(), &h);
  145. uv_timer_start(&h, (uv_timer_cb)fail_cb, 42, 42);
  146. uv_unref((uv_handle_t*)&h);
  147. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  148. do_close(&h);
  149. MAKE_VALGRIND_HAPPY();
  150. return 0;
  151. }
  152. TEST_IMPL(fs_event_ref) {
  153. uv_fs_event_t h;
  154. uv_fs_event_init(uv_default_loop(), &h, ".", (uv_fs_event_cb)fail_cb, 0);
  155. uv_unref((uv_handle_t*)&h);
  156. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  157. do_close(&h);
  158. MAKE_VALGRIND_HAPPY();
  159. return 0;
  160. }
  161. TEST_IMPL(fs_poll_ref) {
  162. uv_fs_poll_t h;
  163. uv_fs_poll_init(uv_default_loop(), &h);
  164. uv_fs_poll_start(&h, NULL, ".", 999);
  165. uv_unref((uv_handle_t*)&h);
  166. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  167. do_close(&h);
  168. MAKE_VALGRIND_HAPPY();
  169. return 0;
  170. }
  171. TEST_IMPL(tcp_ref) {
  172. uv_tcp_t h;
  173. uv_tcp_init(uv_default_loop(), &h);
  174. uv_unref((uv_handle_t*)&h);
  175. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  176. do_close(&h);
  177. MAKE_VALGRIND_HAPPY();
  178. return 0;
  179. }
  180. TEST_IMPL(tcp_ref2) {
  181. uv_tcp_t h;
  182. uv_tcp_init(uv_default_loop(), &h);
  183. uv_listen((uv_stream_t*)&h, 128, (uv_connection_cb)fail_cb);
  184. uv_unref((uv_handle_t*)&h);
  185. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  186. do_close(&h);
  187. MAKE_VALGRIND_HAPPY();
  188. return 0;
  189. }
  190. TEST_IMPL(tcp_ref2b) {
  191. uv_tcp_t h;
  192. uv_tcp_init(uv_default_loop(), &h);
  193. uv_listen((uv_stream_t*)&h, 128, (uv_connection_cb)fail_cb);
  194. uv_unref((uv_handle_t*)&h);
  195. uv_close((uv_handle_t*)&h, close_cb);
  196. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  197. ASSERT(close_cb_called == 1);
  198. MAKE_VALGRIND_HAPPY();
  199. return 0;
  200. }
  201. TEST_IMPL(tcp_ref3) {
  202. struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  203. uv_tcp_t h;
  204. uv_tcp_init(uv_default_loop(), &h);
  205. uv_tcp_connect(&connect_req, &h, addr, connect_and_shutdown);
  206. uv_unref((uv_handle_t*)&h);
  207. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  208. ASSERT(connect_cb_called == 1);
  209. ASSERT(shutdown_cb_called == 1);
  210. do_close(&h);
  211. MAKE_VALGRIND_HAPPY();
  212. return 0;
  213. }
  214. TEST_IMPL(tcp_ref4) {
  215. struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  216. uv_tcp_t h;
  217. uv_tcp_init(uv_default_loop(), &h);
  218. uv_tcp_connect(&connect_req, &h, addr, connect_and_write);
  219. uv_unref((uv_handle_t*)&h);
  220. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  221. ASSERT(connect_cb_called == 1);
  222. ASSERT(write_cb_called == 1);
  223. ASSERT(shutdown_cb_called == 1);
  224. do_close(&h);
  225. MAKE_VALGRIND_HAPPY();
  226. return 0;
  227. }
  228. TEST_IMPL(udp_ref) {
  229. uv_udp_t h;
  230. uv_udp_init(uv_default_loop(), &h);
  231. uv_unref((uv_handle_t*)&h);
  232. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  233. do_close(&h);
  234. MAKE_VALGRIND_HAPPY();
  235. return 0;
  236. }
  237. TEST_IMPL(udp_ref2) {
  238. struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  239. uv_udp_t h;
  240. uv_udp_init(uv_default_loop(), &h);
  241. uv_udp_bind(&h, addr, 0);
  242. uv_udp_recv_start(&h, (uv_alloc_cb)fail_cb, (uv_udp_recv_cb)fail_cb);
  243. uv_unref((uv_handle_t*)&h);
  244. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  245. do_close(&h);
  246. MAKE_VALGRIND_HAPPY();
  247. return 0;
  248. }
  249. TEST_IMPL(udp_ref3) {
  250. struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  251. uv_buf_t buf = uv_buf_init("PING", 4);
  252. uv_udp_send_t req;
  253. uv_udp_t h;
  254. uv_udp_init(uv_default_loop(), &h);
  255. uv_udp_send(&req, &h, &buf, 1, addr, (uv_udp_send_cb)req_cb);
  256. uv_unref((uv_handle_t*)&h);
  257. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  258. ASSERT(req_cb_called == 1);
  259. do_close(&h);
  260. MAKE_VALGRIND_HAPPY();
  261. return 0;
  262. }
  263. TEST_IMPL(pipe_ref) {
  264. uv_pipe_t h;
  265. uv_pipe_init(uv_default_loop(), &h, 0);
  266. uv_unref((uv_handle_t*)&h);
  267. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  268. do_close(&h);
  269. MAKE_VALGRIND_HAPPY();
  270. return 0;
  271. }
  272. TEST_IMPL(pipe_ref2) {
  273. uv_pipe_t h;
  274. uv_pipe_init(uv_default_loop(), &h, 0);
  275. uv_listen((uv_stream_t*)&h, 128, (uv_connection_cb)fail_cb);
  276. uv_unref((uv_handle_t*)&h);
  277. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  278. do_close(&h);
  279. MAKE_VALGRIND_HAPPY();
  280. return 0;
  281. }
  282. TEST_IMPL(pipe_ref3) {
  283. uv_pipe_t h;
  284. uv_pipe_init(uv_default_loop(), &h, 0);
  285. uv_pipe_connect(&connect_req, &h, TEST_PIPENAME, connect_and_shutdown);
  286. uv_unref((uv_handle_t*)&h);
  287. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  288. ASSERT(connect_cb_called == 1);
  289. ASSERT(shutdown_cb_called == 1);
  290. do_close(&h);
  291. MAKE_VALGRIND_HAPPY();
  292. return 0;
  293. }
  294. TEST_IMPL(pipe_ref4) {
  295. uv_pipe_t h;
  296. uv_pipe_init(uv_default_loop(), &h, 0);
  297. uv_pipe_connect(&connect_req, &h, TEST_PIPENAME, connect_and_write);
  298. uv_unref((uv_handle_t*)&h);
  299. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  300. ASSERT(connect_cb_called == 1);
  301. ASSERT(write_cb_called == 1);
  302. ASSERT(shutdown_cb_called == 1);
  303. do_close(&h);
  304. MAKE_VALGRIND_HAPPY();
  305. return 0;
  306. }
  307. TEST_IMPL(process_ref) {
  308. /* spawn_helper4 blocks indefinitely. */
  309. char *argv[] = { NULL, "spawn_helper4", NULL };
  310. uv_process_options_t options;
  311. size_t exepath_size;
  312. char exepath[256];
  313. uv_process_t h;
  314. int r;
  315. memset(&options, 0, sizeof(options));
  316. exepath_size = sizeof(exepath);
  317. r = uv_exepath(exepath, &exepath_size);
  318. ASSERT(r == 0);
  319. argv[0] = exepath;
  320. options.file = exepath;
  321. options.args = argv;
  322. options.exit_cb = NULL;
  323. r = uv_spawn(uv_default_loop(), &h, options);
  324. ASSERT(r == 0);
  325. uv_unref((uv_handle_t*)&h);
  326. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  327. r = uv_process_kill(&h, /* SIGTERM */ 15);
  328. ASSERT(r == 0);
  329. do_close(&h);
  330. MAKE_VALGRIND_HAPPY();
  331. return 0;
  332. }