xref: /libuv/test/test-shutdown-twice.c (revision 011a1ac1)
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  * This is a regression test for issue #1113 (calling uv_shutdown twice will
24  * leave a ghost request in the system)
25  */
26 
27 #include "uv.h"
28 #include "task.h"
29 
30 static uv_shutdown_t req1;
31 static uv_shutdown_t req2;
32 
33 static int shutdown_cb_called = 0;
34 
close_cb(uv_handle_t * handle)35 static void close_cb(uv_handle_t* handle) {
36 
37 }
38 
shutdown_cb(uv_shutdown_t * req,int status)39 static void shutdown_cb(uv_shutdown_t* req, int status) {
40   ASSERT_PTR_EQ(req, &req1);
41   ASSERT_OK(status);
42   shutdown_cb_called++;
43   uv_close((uv_handle_t*) req->handle, close_cb);
44 }
45 
connect_cb(uv_connect_t * req,int status)46 static void connect_cb(uv_connect_t* req, int status) {
47   int r;
48 
49   ASSERT_OK(status);
50 
51   r = uv_shutdown(&req1, req->handle, shutdown_cb);
52   ASSERT_OK(r);
53   r = uv_shutdown(&req2, req->handle, shutdown_cb);
54   ASSERT(r);
55 
56 }
57 
TEST_IMPL(shutdown_twice)58 TEST_IMPL(shutdown_twice) {
59   struct sockaddr_in addr;
60   uv_loop_t* loop;
61   int r;
62   uv_tcp_t h;
63 
64   uv_connect_t connect_req;
65 
66   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
67   loop = uv_default_loop();
68 
69   r = uv_tcp_init(loop, &h);
70   ASSERT_OK(r);
71 
72   r = uv_tcp_connect(&connect_req,
73                      &h,
74                      (const struct sockaddr*) &addr,
75                      connect_cb);
76   ASSERT_OK(r);
77 
78   r = uv_run(loop, UV_RUN_DEFAULT);
79   ASSERT_OK(r);
80 
81   ASSERT_EQ(1, shutdown_cb_called);
82 
83   MAKE_VALGRIND_HAPPY(loop);
84   return 0;
85 }
86