libuv-svace-build
445 строк · 10.6 Кб
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
22#include "uv.h"23#include "task.h"24
25#include <stdlib.h>26#include <string.h>27
28
29static uv_write_t write_req;30static uv_shutdown_t shutdown_req;31static uv_connect_t connect_req;32
33static char buffer[32767];34
35static int req_cb_called;36static int connect_cb_called;37static int write_cb_called;38static int shutdown_cb_called;39static int close_cb_called;40
41
42static void close_cb(uv_handle_t* handle) {43close_cb_called++;44}
45
46
47static void do_close(void* handle) {48close_cb_called = 0;49uv_close((uv_handle_t*)handle, close_cb);50ASSERT_OK(close_cb_called);51uv_run(uv_default_loop(), UV_RUN_DEFAULT);52ASSERT_EQ(1, close_cb_called);53}
54
55
56static void fail_cb(void) {57FATAL("fail_cb should not have been called");58}
59
60
61static void fail_cb2(void) {62ASSERT(0 && "fail_cb2 should not have been called");63}
64
65
66static void req_cb(uv_handle_t* req, int status) {67req_cb_called++;68}
69
70
71static void shutdown_cb(uv_shutdown_t* req, int status) {72ASSERT_PTR_EQ(req, &shutdown_req);73shutdown_cb_called++;74}
75
76
77static void write_cb(uv_write_t* req, int status) {78ASSERT_PTR_EQ(req, &write_req);79uv_shutdown(&shutdown_req, req->handle, shutdown_cb);80write_cb_called++;81}
82
83
84static void connect_and_write(uv_connect_t* req, int status) {85uv_buf_t buf = uv_buf_init(buffer, sizeof buffer);86ASSERT_PTR_EQ(req, &connect_req);87ASSERT_OK(status);88uv_write(&write_req, req->handle, &buf, 1, write_cb);89connect_cb_called++;90}
91
92
93
94static void connect_and_shutdown(uv_connect_t* req, int status) {95ASSERT_PTR_EQ(req, &connect_req);96ASSERT_OK(status);97uv_shutdown(&shutdown_req, req->handle, shutdown_cb);98connect_cb_called++;99}
100
101
102TEST_IMPL(ref) {103uv_run(uv_default_loop(), UV_RUN_DEFAULT);104MAKE_VALGRIND_HAPPY(uv_default_loop());105return 0;106}
107
108
109TEST_IMPL(idle_ref) {110uv_idle_t h;111uv_idle_init(uv_default_loop(), &h);112uv_idle_start(&h, (uv_idle_cb) fail_cb2);113uv_unref((uv_handle_t*)&h);114uv_run(uv_default_loop(), UV_RUN_DEFAULT);115do_close(&h);116MAKE_VALGRIND_HAPPY(uv_default_loop());117return 0;118}
119
120
121TEST_IMPL(async_ref) {122uv_async_t h;123uv_async_init(uv_default_loop(), &h, NULL);124uv_unref((uv_handle_t*)&h);125uv_run(uv_default_loop(), UV_RUN_DEFAULT);126do_close(&h);127MAKE_VALGRIND_HAPPY(uv_default_loop());128return 0;129}
130
131
132TEST_IMPL(prepare_ref) {133uv_prepare_t h;134uv_prepare_init(uv_default_loop(), &h);135uv_prepare_start(&h, (uv_prepare_cb) fail_cb2);136uv_unref((uv_handle_t*)&h);137uv_run(uv_default_loop(), UV_RUN_DEFAULT);138do_close(&h);139MAKE_VALGRIND_HAPPY(uv_default_loop());140return 0;141}
142
143
144TEST_IMPL(check_ref) {145uv_check_t h;146uv_check_init(uv_default_loop(), &h);147uv_check_start(&h, (uv_check_cb) fail_cb2);148uv_unref((uv_handle_t*)&h);149uv_run(uv_default_loop(), UV_RUN_DEFAULT);150do_close(&h);151MAKE_VALGRIND_HAPPY(uv_default_loop());152return 0;153}
154
155
156static void prepare_cb(uv_prepare_t* h) {157ASSERT_NOT_NULL(h);158uv_unref((uv_handle_t*)h);159}
160
161
162TEST_IMPL(unref_in_prepare_cb) {163uv_prepare_t h;164uv_prepare_init(uv_default_loop(), &h);165uv_prepare_start(&h, prepare_cb);166uv_run(uv_default_loop(), UV_RUN_DEFAULT);167do_close(&h);168MAKE_VALGRIND_HAPPY(uv_default_loop());169return 0;170}
171
172
173TEST_IMPL(timer_ref) {174uv_timer_t h;175uv_timer_init(uv_default_loop(), &h);176uv_unref((uv_handle_t*)&h);177uv_run(uv_default_loop(), UV_RUN_DEFAULT);178do_close(&h);179MAKE_VALGRIND_HAPPY(uv_default_loop());180return 0;181}
182
183
184TEST_IMPL(timer_ref2) {185uv_timer_t h;186uv_timer_init(uv_default_loop(), &h);187uv_timer_start(&h, (uv_timer_cb)fail_cb, 42, 42);188uv_unref((uv_handle_t*)&h);189uv_run(uv_default_loop(), UV_RUN_DEFAULT);190do_close(&h);191MAKE_VALGRIND_HAPPY(uv_default_loop());192return 0;193}
194
195
196TEST_IMPL(fs_event_ref) {197#if defined(NO_FS_EVENTS)198RETURN_SKIP(NO_FS_EVENTS);199#endif200uv_fs_event_t h;201uv_fs_event_init(uv_default_loop(), &h);202uv_fs_event_start(&h, (uv_fs_event_cb)fail_cb, ".", 0);203uv_unref((uv_handle_t*)&h);204uv_run(uv_default_loop(), UV_RUN_DEFAULT);205do_close(&h);206MAKE_VALGRIND_HAPPY(uv_default_loop());207return 0;208}
209
210
211TEST_IMPL(fs_poll_ref) {212uv_fs_poll_t h;213uv_fs_poll_init(uv_default_loop(), &h);214uv_fs_poll_start(&h, NULL, ".", 999);215uv_unref((uv_handle_t*)&h);216uv_run(uv_default_loop(), UV_RUN_DEFAULT);217do_close(&h);218MAKE_VALGRIND_HAPPY(uv_default_loop());219return 0;220}
221
222
223TEST_IMPL(tcp_ref) {224uv_tcp_t h;225uv_tcp_init(uv_default_loop(), &h);226uv_unref((uv_handle_t*)&h);227uv_run(uv_default_loop(), UV_RUN_DEFAULT);228do_close(&h);229MAKE_VALGRIND_HAPPY(uv_default_loop());230return 0;231}
232
233
234TEST_IMPL(tcp_ref2) {235uv_tcp_t h;236uv_tcp_init(uv_default_loop(), &h);237uv_listen((uv_stream_t*)&h, 128, (uv_connection_cb)fail_cb);238uv_unref((uv_handle_t*)&h);239uv_run(uv_default_loop(), UV_RUN_DEFAULT);240do_close(&h);241MAKE_VALGRIND_HAPPY(uv_default_loop());242return 0;243}
244
245
246TEST_IMPL(tcp_ref2b) {247uv_tcp_t h;248uv_tcp_init(uv_default_loop(), &h);249uv_listen((uv_stream_t*)&h, 128, (uv_connection_cb)fail_cb);250uv_unref((uv_handle_t*)&h);251uv_close((uv_handle_t*)&h, close_cb);252uv_run(uv_default_loop(), UV_RUN_DEFAULT);253ASSERT_EQ(1, close_cb_called);254MAKE_VALGRIND_HAPPY(uv_default_loop());255return 0;256}
257
258
259TEST_IMPL(tcp_ref3) {260struct sockaddr_in addr;261uv_tcp_t h;262ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));263uv_tcp_init(uv_default_loop(), &h);264uv_tcp_connect(&connect_req,265&h,266(const struct sockaddr*) &addr,267connect_and_shutdown);268uv_unref((uv_handle_t*)&h);269uv_run(uv_default_loop(), UV_RUN_DEFAULT);270ASSERT_EQ(1, connect_cb_called);271ASSERT_EQ(1, shutdown_cb_called);272do_close(&h);273MAKE_VALGRIND_HAPPY(uv_default_loop());274return 0;275}
276
277
278TEST_IMPL(tcp_ref4) {279struct sockaddr_in addr;280uv_tcp_t h;281ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));282uv_tcp_init(uv_default_loop(), &h);283uv_tcp_connect(&connect_req,284&h,285(const struct sockaddr*) &addr,286connect_and_write);287uv_unref((uv_handle_t*)&h);288uv_run(uv_default_loop(), UV_RUN_DEFAULT);289ASSERT_EQ(1, connect_cb_called);290ASSERT_EQ(1, write_cb_called);291ASSERT_EQ(1, shutdown_cb_called);292do_close(&h);293MAKE_VALGRIND_HAPPY(uv_default_loop());294return 0;295}
296
297
298TEST_IMPL(udp_ref) {299uv_udp_t h;300uv_udp_init(uv_default_loop(), &h);301uv_unref((uv_handle_t*)&h);302uv_run(uv_default_loop(), UV_RUN_DEFAULT);303do_close(&h);304MAKE_VALGRIND_HAPPY(uv_default_loop());305return 0;306}
307
308
309TEST_IMPL(udp_ref2) {310struct sockaddr_in addr;311uv_udp_t h;312ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));313uv_udp_init(uv_default_loop(), &h);314uv_udp_bind(&h, (const struct sockaddr*) &addr, 0);315uv_udp_recv_start(&h, (uv_alloc_cb)fail_cb, (uv_udp_recv_cb)fail_cb);316uv_unref((uv_handle_t*)&h);317uv_run(uv_default_loop(), UV_RUN_DEFAULT);318do_close(&h);319MAKE_VALGRIND_HAPPY(uv_default_loop());320return 0;321}
322
323
324TEST_IMPL(udp_ref3) {325struct sockaddr_in addr;326uv_buf_t buf = uv_buf_init("PING", 4);327uv_udp_send_t req;328uv_udp_t h;329
330ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));331uv_udp_init(uv_default_loop(), &h);332uv_udp_send(&req,333&h,334&buf,3351,336(const struct sockaddr*) &addr,337(uv_udp_send_cb) req_cb);338uv_unref((uv_handle_t*)&h);339uv_run(uv_default_loop(), UV_RUN_DEFAULT);340ASSERT_EQ(1, req_cb_called);341do_close(&h);342
343MAKE_VALGRIND_HAPPY(uv_default_loop());344return 0;345}
346
347
348TEST_IMPL(pipe_ref) {349uv_pipe_t h;350uv_pipe_init(uv_default_loop(), &h, 0);351uv_unref((uv_handle_t*)&h);352uv_run(uv_default_loop(), UV_RUN_DEFAULT);353do_close(&h);354MAKE_VALGRIND_HAPPY(uv_default_loop());355return 0;356}
357
358
359TEST_IMPL(pipe_ref2) {360uv_pipe_t h;361uv_pipe_init(uv_default_loop(), &h, 0);362uv_listen((uv_stream_t*)&h, 128, (uv_connection_cb)fail_cb);363uv_unref((uv_handle_t*)&h);364uv_run(uv_default_loop(), UV_RUN_DEFAULT);365do_close(&h);366MAKE_VALGRIND_HAPPY(uv_default_loop());367return 0;368}
369
370
371TEST_IMPL(pipe_ref3) {372uv_pipe_t h;373uv_pipe_init(uv_default_loop(), &h, 0);374uv_pipe_connect(&connect_req, &h, TEST_PIPENAME, connect_and_shutdown);375uv_unref((uv_handle_t*)&h);376uv_run(uv_default_loop(), UV_RUN_DEFAULT);377ASSERT_EQ(1, connect_cb_called);378ASSERT_EQ(1, shutdown_cb_called);379do_close(&h);380MAKE_VALGRIND_HAPPY(uv_default_loop());381return 0;382}
383
384
385TEST_IMPL(pipe_ref4) {386uv_pipe_t h;387uv_pipe_init(uv_default_loop(), &h, 0);388uv_pipe_connect(&connect_req, &h, TEST_PIPENAME, connect_and_write);389uv_unref((uv_handle_t*)&h);390uv_run(uv_default_loop(), UV_RUN_DEFAULT);391ASSERT_EQ(1, connect_cb_called);392ASSERT_EQ(1, write_cb_called);393ASSERT_EQ(1, shutdown_cb_called);394do_close(&h);395MAKE_VALGRIND_HAPPY(uv_default_loop());396return 0;397}
398
399
400TEST_IMPL(process_ref) {401/* spawn_helper4 blocks indefinitely. */402char *argv[] = { NULL, "spawn_helper4", NULL };403uv_process_options_t options;404size_t exepath_size;405char exepath[256];406uv_process_t h;407int r;408
409memset(&options, 0, sizeof(options));410exepath_size = sizeof(exepath);411
412r = uv_exepath(exepath, &exepath_size);413ASSERT_OK(r);414
415argv[0] = exepath;416options.file = exepath;417options.args = argv;418options.exit_cb = NULL;419
420r = uv_spawn(uv_default_loop(), &h, &options);421ASSERT_OK(r);422
423uv_unref((uv_handle_t*)&h);424uv_run(uv_default_loop(), UV_RUN_DEFAULT);425
426r = uv_process_kill(&h, /* SIGTERM */ 15);427ASSERT_OK(r);428
429do_close(&h);430
431MAKE_VALGRIND_HAPPY(uv_default_loop());432return 0;433}
434
435
436TEST_IMPL(has_ref) {437uv_idle_t h;438uv_idle_init(uv_default_loop(), &h);439uv_ref((uv_handle_t*)&h);440ASSERT_EQ(1, uv_has_ref((uv_handle_t*)&h));441uv_unref((uv_handle_t*)&h);442ASSERT_OK(uv_has_ref((uv_handle_t*)&h));443MAKE_VALGRIND_HAPPY(uv_default_loop());444return 0;445}
446