test-ref.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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) {
  120. ASSERT(h != NULL);
  121. uv_unref((uv_handle_t*)h);
  122. }
  123. TEST_IMPL(unref_in_prepare_cb) {
  124. uv_prepare_t h;
  125. uv_prepare_init(uv_default_loop(), &h);
  126. uv_prepare_start(&h, prepare_cb);
  127. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  128. do_close(&h);
  129. MAKE_VALGRIND_HAPPY();
  130. return 0;
  131. }
  132. TEST_IMPL(timer_ref) {
  133. uv_timer_t h;
  134. uv_timer_init(uv_default_loop(), &h);
  135. uv_unref((uv_handle_t*)&h);
  136. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  137. do_close(&h);
  138. MAKE_VALGRIND_HAPPY();
  139. return 0;
  140. }
  141. TEST_IMPL(timer_ref2) {
  142. uv_timer_t h;
  143. uv_timer_init(uv_default_loop(), &h);
  144. uv_timer_start(&h, (uv_timer_cb)fail_cb, 42, 42);
  145. uv_unref((uv_handle_t*)&h);
  146. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  147. do_close(&h);
  148. MAKE_VALGRIND_HAPPY();
  149. return 0;
  150. }
  151. TEST_IMPL(fs_event_ref) {
  152. #if defined(NO_FS_EVENTS)
  153. RETURN_SKIP(NO_FS_EVENTS);
  154. #endif
  155. uv_fs_event_t h;
  156. uv_fs_event_init(uv_default_loop(), &h);
  157. uv_fs_event_start(&h, (uv_fs_event_cb)fail_cb, ".", 0);
  158. uv_unref((uv_handle_t*)&h);
  159. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  160. do_close(&h);
  161. MAKE_VALGRIND_HAPPY();
  162. return 0;
  163. }
  164. TEST_IMPL(fs_poll_ref) {
  165. uv_fs_poll_t h;
  166. uv_fs_poll_init(uv_default_loop(), &h);
  167. uv_fs_poll_start(&h, NULL, ".", 999);
  168. uv_unref((uv_handle_t*)&h);
  169. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  170. do_close(&h);
  171. MAKE_VALGRIND_HAPPY();
  172. return 0;
  173. }
  174. TEST_IMPL(tcp_ref) {
  175. uv_tcp_t h;
  176. uv_tcp_init(uv_default_loop(), &h);
  177. uv_unref((uv_handle_t*)&h);
  178. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  179. do_close(&h);
  180. MAKE_VALGRIND_HAPPY();
  181. return 0;
  182. }
  183. TEST_IMPL(tcp_ref2) {
  184. uv_tcp_t h;
  185. uv_tcp_init(uv_default_loop(), &h);
  186. uv_listen((uv_stream_t*)&h, 128, (uv_connection_cb)fail_cb);
  187. uv_unref((uv_handle_t*)&h);
  188. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  189. do_close(&h);
  190. MAKE_VALGRIND_HAPPY();
  191. return 0;
  192. }
  193. TEST_IMPL(tcp_ref2b) {
  194. uv_tcp_t h;
  195. uv_tcp_init(uv_default_loop(), &h);
  196. uv_listen((uv_stream_t*)&h, 128, (uv_connection_cb)fail_cb);
  197. uv_unref((uv_handle_t*)&h);
  198. uv_close((uv_handle_t*)&h, close_cb);
  199. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  200. ASSERT(close_cb_called == 1);
  201. MAKE_VALGRIND_HAPPY();
  202. return 0;
  203. }
  204. TEST_IMPL(tcp_ref3) {
  205. struct sockaddr_in addr;
  206. uv_tcp_t h;
  207. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  208. uv_tcp_init(uv_default_loop(), &h);
  209. uv_tcp_connect(&connect_req,
  210. &h,
  211. (const struct sockaddr*) &addr,
  212. connect_and_shutdown);
  213. uv_unref((uv_handle_t*)&h);
  214. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  215. ASSERT(connect_cb_called == 1);
  216. ASSERT(shutdown_cb_called == 1);
  217. do_close(&h);
  218. MAKE_VALGRIND_HAPPY();
  219. return 0;
  220. }
  221. TEST_IMPL(tcp_ref4) {
  222. struct sockaddr_in addr;
  223. uv_tcp_t h;
  224. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  225. uv_tcp_init(uv_default_loop(), &h);
  226. uv_tcp_connect(&connect_req,
  227. &h,
  228. (const struct sockaddr*) &addr,
  229. connect_and_write);
  230. uv_unref((uv_handle_t*)&h);
  231. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  232. ASSERT(connect_cb_called == 1);
  233. ASSERT(write_cb_called == 1);
  234. ASSERT(shutdown_cb_called == 1);
  235. do_close(&h);
  236. MAKE_VALGRIND_HAPPY();
  237. return 0;
  238. }
  239. TEST_IMPL(udp_ref) {
  240. uv_udp_t h;
  241. uv_udp_init(uv_default_loop(), &h);
  242. uv_unref((uv_handle_t*)&h);
  243. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  244. do_close(&h);
  245. MAKE_VALGRIND_HAPPY();
  246. return 0;
  247. }
  248. TEST_IMPL(udp_ref2) {
  249. struct sockaddr_in addr;
  250. uv_udp_t h;
  251. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  252. uv_udp_init(uv_default_loop(), &h);
  253. uv_udp_bind(&h, (const struct sockaddr*) &addr, 0);
  254. uv_udp_recv_start(&h, (uv_alloc_cb)fail_cb, (uv_udp_recv_cb)fail_cb);
  255. uv_unref((uv_handle_t*)&h);
  256. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  257. do_close(&h);
  258. MAKE_VALGRIND_HAPPY();
  259. return 0;
  260. }
  261. TEST_IMPL(udp_ref3) {
  262. struct sockaddr_in addr;
  263. uv_buf_t buf = uv_buf_init("PING", 4);
  264. uv_udp_send_t req;
  265. uv_udp_t h;
  266. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  267. uv_udp_init(uv_default_loop(), &h);
  268. uv_udp_send(&req,
  269. &h,
  270. &buf,
  271. 1,
  272. (const struct sockaddr*) &addr,
  273. (uv_udp_send_cb) req_cb);
  274. uv_unref((uv_handle_t*)&h);
  275. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  276. ASSERT(req_cb_called == 1);
  277. do_close(&h);
  278. MAKE_VALGRIND_HAPPY();
  279. return 0;
  280. }
  281. TEST_IMPL(pipe_ref) {
  282. uv_pipe_t h;
  283. uv_pipe_init(uv_default_loop(), &h, 0);
  284. uv_unref((uv_handle_t*)&h);
  285. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  286. do_close(&h);
  287. MAKE_VALGRIND_HAPPY();
  288. return 0;
  289. }
  290. TEST_IMPL(pipe_ref2) {
  291. uv_pipe_t h;
  292. uv_pipe_init(uv_default_loop(), &h, 0);
  293. uv_listen((uv_stream_t*)&h, 128, (uv_connection_cb)fail_cb);
  294. uv_unref((uv_handle_t*)&h);
  295. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  296. do_close(&h);
  297. MAKE_VALGRIND_HAPPY();
  298. return 0;
  299. }
  300. TEST_IMPL(pipe_ref3) {
  301. uv_pipe_t h;
  302. uv_pipe_init(uv_default_loop(), &h, 0);
  303. uv_pipe_connect(&connect_req, &h, TEST_PIPENAME, connect_and_shutdown);
  304. uv_unref((uv_handle_t*)&h);
  305. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  306. ASSERT(connect_cb_called == 1);
  307. ASSERT(shutdown_cb_called == 1);
  308. do_close(&h);
  309. MAKE_VALGRIND_HAPPY();
  310. return 0;
  311. }
  312. TEST_IMPL(pipe_ref4) {
  313. uv_pipe_t h;
  314. uv_pipe_init(uv_default_loop(), &h, 0);
  315. uv_pipe_connect(&connect_req, &h, TEST_PIPENAME, connect_and_write);
  316. uv_unref((uv_handle_t*)&h);
  317. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  318. ASSERT(connect_cb_called == 1);
  319. ASSERT(write_cb_called == 1);
  320. ASSERT(shutdown_cb_called == 1);
  321. do_close(&h);
  322. MAKE_VALGRIND_HAPPY();
  323. return 0;
  324. }
  325. TEST_IMPL(process_ref) {
  326. /* spawn_helper4 blocks indefinitely. */
  327. char *argv[] = { NULL, "spawn_helper4", NULL };
  328. uv_process_options_t options;
  329. size_t exepath_size;
  330. char exepath[256];
  331. uv_process_t h;
  332. int r;
  333. memset(&options, 0, sizeof(options));
  334. exepath_size = sizeof(exepath);
  335. r = uv_exepath(exepath, &exepath_size);
  336. ASSERT(r == 0);
  337. argv[0] = exepath;
  338. options.file = exepath;
  339. options.args = argv;
  340. options.exit_cb = NULL;
  341. r = uv_spawn(uv_default_loop(), &h, &options);
  342. ASSERT(r == 0);
  343. uv_unref((uv_handle_t*)&h);
  344. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  345. r = uv_process_kill(&h, /* SIGTERM */ 15);
  346. ASSERT(r == 0);
  347. do_close(&h);
  348. MAKE_VALGRIND_HAPPY();
  349. return 0;
  350. }
  351. TEST_IMPL(has_ref) {
  352. uv_idle_t h;
  353. uv_idle_init(uv_default_loop(), &h);
  354. uv_ref((uv_handle_t*)&h);
  355. ASSERT(uv_has_ref((uv_handle_t*)&h) == 1);
  356. uv_unref((uv_handle_t*)&h);
  357. ASSERT(uv_has_ref((uv_handle_t*)&h) == 0);
  358. MAKE_VALGRIND_HAPPY();
  359. return 0;
  360. }