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 22#include <errno.h> 23 24#ifndef _WIN32 25# include <fcntl.h> 26# include <sys/socket.h> 27# include <unistd.h> 28#endif 29 30#include "uv.h" 31#include "task.h" 32 33 34static int close_cb_called = 0; 35 36 37static void close_cb(uv_handle_t* handle) { 38 close_cb_called++; 39} 40 41static void poll_cb(uv_poll_t* handle, int status, int events) { 42 /* Not a bound socket, linux immediately reports UV_READABLE, other OS do not */ 43 ASSERT(events == UV_READABLE); 44} 45 46TEST_IMPL(poll_multiple_handles) { 47 uv_os_sock_t sock; 48 uv_poll_t first_poll_handle, second_poll_handle; 49 50#ifdef _WIN32 51 { 52 struct WSAData wsa_data; 53 int r = WSAStartup(MAKEWORD(2, 2), &wsa_data); 54 ASSERT(r == 0); 55 } 56#endif 57 58 sock = socket(AF_INET, SOCK_STREAM, 0); 59#ifdef _WIN32 60 ASSERT(sock != INVALID_SOCKET); 61#else 62 ASSERT(sock != -1); 63#endif 64 ASSERT(0 == uv_poll_init_socket(uv_default_loop(), &first_poll_handle, sock)); 65 ASSERT(0 == uv_poll_init_socket(uv_default_loop(), &second_poll_handle, sock)); 66 67 ASSERT(0 == uv_poll_start(&first_poll_handle, UV_READABLE, poll_cb)); 68 69 /* We may not start polling while another polling handle is active 70 * on that fd. 71 */ 72#ifndef _WIN32 73 /* We do not track handles in an O(1) lookupable way on Windows, 74 * so not checking that here. 75 */ 76 ASSERT(uv_poll_start(&second_poll_handle, UV_READABLE, poll_cb) == UV_EEXIST); 77#endif 78 79 /* After stopping the other polling handle, we now should be able to poll */ 80 ASSERT(0 == uv_poll_stop(&first_poll_handle)); 81 ASSERT(0 == uv_poll_start(&second_poll_handle, UV_READABLE, poll_cb)); 82 83 /* Closing an already stopped polling handle is safe in any case */ 84 uv_close((uv_handle_t*) &first_poll_handle, close_cb); 85 86 uv_unref((uv_handle_t*) &second_poll_handle); 87 ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT)); 88 ASSERT(close_cb_called == 1); 89 uv_ref((uv_handle_t*) &second_poll_handle); 90 91 ASSERT(uv_is_active((uv_handle_t*) &second_poll_handle)); 92 uv_close((uv_handle_t*) &second_poll_handle, close_cb); 93 94 ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT)); 95 ASSERT(close_cb_called == 2); 96 97 MAKE_VALGRIND_HAPPY(); 98 return 0; 99} 100