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 | /* |
23 | * These tests verify that the uv_shutdown callback is always made, even when |
24 | * it is immediately followed by an uv_close call. |
25 | */ |
26 | |
27 | #include "uv.h" |
28 | #include "task.h" |
29 | |
30 | |
31 | static uv_shutdown_t shutdown_req; |
32 | static uv_connect_t connect_req; |
33 | |
34 | static int connect_cb_called = 0; |
35 | static int shutdown_cb_called = 0; |
36 | static int close_cb_called = 0; |
37 | |
38 | |
39 | static void shutdown_cb(uv_shutdown_t* req, int status) { |
40 | ASSERT(req == &shutdown_req); |
41 | ASSERT(status == 0 || status == UV_ECANCELED); |
42 | shutdown_cb_called++; |
43 | } |
44 | |
45 | |
46 | static void close_cb(uv_handle_t* handle) { |
47 | close_cb_called++; |
48 | } |
49 | |
50 | |
51 | static void connect_cb(uv_connect_t* req, int status) { |
52 | int r; |
53 | |
54 | ASSERT(req == &connect_req); |
55 | ASSERT(status == 0); |
56 | |
57 | r = uv_shutdown(&shutdown_req, req->handle, shutdown_cb); |
58 | ASSERT(r == 0); |
59 | ASSERT(0 == uv_is_closing((uv_handle_t*) req->handle)); |
60 | uv_close((uv_handle_t*) req->handle, close_cb); |
61 | ASSERT(1 == uv_is_closing((uv_handle_t*) req->handle)); |
62 | |
63 | connect_cb_called++; |
64 | } |
65 | |
66 | |
67 | TEST_IMPL(shutdown_close_tcp) { |
68 | struct sockaddr_in addr; |
69 | uv_tcp_t h; |
70 | int r; |
71 | |
72 | ASSERT(0 == uv_ip4_addr("127.0.0.1" , TEST_PORT, &addr)); |
73 | r = uv_tcp_init(uv_default_loop(), &h); |
74 | ASSERT(r == 0); |
75 | r = uv_tcp_connect(&connect_req, |
76 | &h, |
77 | (const struct sockaddr*) &addr, |
78 | connect_cb); |
79 | ASSERT(r == 0); |
80 | r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
81 | ASSERT(r == 0); |
82 | |
83 | ASSERT(connect_cb_called == 1); |
84 | ASSERT(shutdown_cb_called == 1); |
85 | ASSERT(close_cb_called == 1); |
86 | |
87 | MAKE_VALGRIND_HAPPY(); |
88 | return 0; |
89 | } |
90 | |
91 | |
92 | TEST_IMPL(shutdown_close_pipe) { |
93 | uv_pipe_t h; |
94 | int r; |
95 | |
96 | r = uv_pipe_init(uv_default_loop(), &h, 0); |
97 | ASSERT(r == 0); |
98 | uv_pipe_connect(&connect_req, &h, TEST_PIPENAME, connect_cb); |
99 | r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); |
100 | ASSERT(r == 0); |
101 | |
102 | ASSERT(connect_cb_called == 1); |
103 | ASSERT(shutdown_cb_called == 1); |
104 | ASSERT(close_cb_called == 1); |
105 | |
106 | MAKE_VALGRIND_HAPPY(); |
107 | return 0; |
108 | } |
109 | |