libuv-svace-build
116 строк · 3.1 Кб
1/* Copyright libuv project 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#ifndef _WIN32
22
23#include "uv.h"
24#include "task.h"
25
26#include <string.h>
27#include <unistd.h>
28
29static uv_loop_t loop;
30static uv_signal_t signal_hdl;
31static uv_pipe_t pipe_hdl;
32static uv_write_t write_req;
33static char* buf;
34static int close_cb_called;
35
36
37static void stop_loop_cb(uv_signal_t* signal, int signum) {
38ASSERT_EQ(signum, SIGPIPE);
39uv_stop(signal->loop);
40}
41
42static void signal_cb(uv_signal_t* signal, int signum) {
43ASSERT(0);
44}
45
46static void close_cb(uv_handle_t *handle) {
47close_cb_called++;
48}
49
50
51static void write_cb(uv_write_t* req, int status) {
52ASSERT_NOT_NULL(req);
53ASSERT_EQ(status, UV_EPIPE);
54free(buf);
55uv_close((uv_handle_t *) &pipe_hdl, close_cb);
56uv_close((uv_handle_t *) &signal_hdl, close_cb);
57}
58
59
60TEST_IMPL(signal_pending_on_close) {
61int pipefds[2];
62uv_buf_t buffer;
63int r;
64
65ASSERT_OK(uv_loop_init(&loop));
66
67ASSERT_OK(uv_signal_init(&loop, &signal_hdl));
68
69ASSERT_OK(uv_signal_start(&signal_hdl, signal_cb, SIGPIPE));
70
71ASSERT_OK(pipe(pipefds));
72
73ASSERT_OK(uv_pipe_init(&loop, &pipe_hdl, 0));
74
75ASSERT_OK(uv_pipe_open(&pipe_hdl, pipefds[1]));
76
77/* Write data large enough so it needs loop iteration */
78buf = malloc(1<<24);
79ASSERT_NOT_NULL(buf);
80memset(buf, '.', 1<<24);
81buffer = uv_buf_init(buf, 1<<24);
82
83r = uv_write(&write_req, (uv_stream_t *) &pipe_hdl, &buffer, 1, write_cb);
84ASSERT_OK(r);
85
86/* cause a SIGPIPE on write in next iteration */
87close(pipefds[0]);
88
89ASSERT_OK(uv_run(&loop, UV_RUN_DEFAULT));
90
91ASSERT_EQ(2, close_cb_called);
92
93MAKE_VALGRIND_HAPPY(&loop);
94return 0;
95}
96
97
98TEST_IMPL(signal_close_loop_alive) {
99ASSERT_OK(uv_loop_init(&loop));
100ASSERT_OK(uv_signal_init(&loop, &signal_hdl));
101ASSERT_OK(uv_signal_start(&signal_hdl, stop_loop_cb, SIGPIPE));
102uv_unref((uv_handle_t*) &signal_hdl);
103
104ASSERT_OK(uv_kill(uv_os_getpid(), SIGPIPE));
105ASSERT_OK(uv_run(&loop, UV_RUN_DEFAULT));
106uv_close((uv_handle_t*) &signal_hdl, close_cb);
107ASSERT_EQ(1, uv_loop_alive(&loop));
108
109ASSERT_OK(uv_run(&loop, UV_RUN_DEFAULT));
110ASSERT_EQ(1, close_cb_called);
111
112MAKE_VALGRIND_HAPPY(&loop);
113return 0;
114}
115
116#endif
117