1
0

test-ref.c 10 KB

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