Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 109 lines (84 sloc) 3.103 kb
422a898 @piscisaureus Tests: verify that shutdown_cb is always called
piscisaureus authored
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);
3ee4d3f @bnoordhuis unix, windows: return error codes directly
bnoordhuis authored
41 ASSERT(status == 0 || status == UV_ECANCELED);
422a898 @piscisaureus Tests: verify that shutdown_cb is always called
piscisaureus authored
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);
d7115f0 @bnoordhuis unix, windows: make uv_is_*() always return 0 or 1
bnoordhuis authored
59 ASSERT(0 == uv_is_closing((uv_handle_t*) req->handle));
422a898 @piscisaureus Tests: verify that shutdown_cb is always called
piscisaureus authored
60 uv_close((uv_handle_t*) req->handle, close_cb);
d7115f0 @bnoordhuis unix, windows: make uv_is_*() always return 0 or 1
bnoordhuis authored
61 ASSERT(1 == uv_is_closing((uv_handle_t*) req->handle));
422a898 @piscisaureus Tests: verify that shutdown_cb is always called
piscisaureus authored
62
63 connect_cb_called++;
64 }
65
66
67 TEST_IMPL(shutdown_close_tcp) {
8184076 @bnoordhuis include: uv_ip[46]_addr now takes sockaddr_in*
bnoordhuis authored
68 struct sockaddr_in addr;
422a898 @piscisaureus Tests: verify that shutdown_cb is always called
piscisaureus authored
69 uv_tcp_t h;
70 int r;
71
8184076 @bnoordhuis include: uv_ip[46]_addr now takes sockaddr_in*
bnoordhuis authored
72 ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
422a898 @piscisaureus Tests: verify that shutdown_cb is always called
piscisaureus authored
73 r = uv_tcp_init(uv_default_loop(), &h);
74 ASSERT(r == 0);
5c675c4 @bnoordhuis include: merge uv_tcp_connect and uv_tcp_connect6
bnoordhuis authored
75 r = uv_tcp_connect(&connect_req,
76 &h,
77 (const struct sockaddr*) &addr,
78 connect_cb);
422a898 @piscisaureus Tests: verify that shutdown_cb is always called
piscisaureus authored
79 ASSERT(r == 0);
4ba03dd @bnoordhuis unix, windows: rename uv_run2 to uv_run
bnoordhuis authored
80 r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
422a898 @piscisaureus Tests: verify that shutdown_cb is always called
piscisaureus authored
81 ASSERT(r == 0);
82
83 ASSERT(connect_cb_called == 1);
84 ASSERT(shutdown_cb_called == 1);
85 ASSERT(close_cb_called == 1);
86
47eb034 @piscisaureus test: move loop cleanup code to the individual tests
piscisaureus authored
87 MAKE_VALGRIND_HAPPY();
422a898 @piscisaureus Tests: verify that shutdown_cb is always called
piscisaureus authored
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);
4ba03dd @bnoordhuis unix, windows: rename uv_run2 to uv_run
bnoordhuis authored
99 r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
422a898 @piscisaureus Tests: verify that shutdown_cb is always called
piscisaureus authored
100 ASSERT(r == 0);
101
102 ASSERT(connect_cb_called == 1);
103 ASSERT(shutdown_cb_called == 1);
104 ASSERT(close_cb_called == 1);
105
47eb034 @piscisaureus test: move loop cleanup code to the individual tests
piscisaureus authored
106 MAKE_VALGRIND_HAPPY();
422a898 @piscisaureus Tests: verify that shutdown_cb is always called
piscisaureus authored
107 return 0;
108 }
Something went wrong with that request. Please try again.