test-poll-closesocket.c 2.5 KB

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