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#include <errno.h>
24
25#include "uv.h"
26#include "task.h"
27
28uv_os_sock_t sock;
29uv_poll_t handle;
30
31#ifdef _WIN32
32static int close_cb_called = 0;
33
34
35static void close_cb(uv_handle_t* h) {
36 close_cb_called++;
37}
38
39
40static void poll_cb(uv_poll_t* h, int status, int events) {
41 int r;
42
43 ASSERT(status == 0);
44 ASSERT(h == &handle);
45
46 r = uv_poll_start(&handle, UV_READABLE, poll_cb);
47 ASSERT(r == 0);
48
49 closesocket(sock);
50 uv_close((uv_handle_t*) &handle, close_cb);
51
52}
53#endif
54
55
56TEST_IMPL(poll_closesocket) {
57#ifndef _WIN32
58 RETURN_SKIP("Test only relevant on Windows");
59#else
60 struct WSAData wsa_data;
61 int r;
62 unsigned long on;
63 struct sockaddr_in addr;
64
65 r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
66 ASSERT(r == 0);
67
68 sock = socket(AF_INET, SOCK_STREAM, 0);
69 ASSERT(sock != INVALID_SOCKET);
70 on = 1;
71 r = ioctlsocket(sock, FIONBIO, &on);
72 ASSERT(r == 0);
73
74 r = uv_ip4_addr("127.0.0.1", TEST_PORT, &addr);
75 ASSERT(r == 0);
76
77 r = connect(sock, (const struct sockaddr*) &addr, sizeof addr);
78 ASSERT(r != 0);
79 ASSERT(WSAGetLastError() == WSAEWOULDBLOCK);
80
81 r = uv_poll_init_socket(uv_default_loop(), &handle, sock);
82 ASSERT(r == 0);
83 r = uv_poll_start(&handle, UV_WRITABLE, poll_cb);
84 ASSERT(r == 0);
85
86 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
87
88 ASSERT(close_cb_called == 1);
89
90 MAKE_VALGRIND_HAPPY();
91 return 0;
92#endif
93}
94