1
0

test-iocp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef _WIN32
  22. /* This test makes no sense outside the context of windows. */
  23. #else
  24. #include "uv.h"
  25. #include "task.h"
  26. #include <fcntl.h>
  27. #include <direct.h>
  28. #include <io.h>
  29. #define unlink _unlink
  30. int iocp_cb_count;
  31. static uv_loop_t* loop;
  32. static uv_fs_t open_req;
  33. static uv_fs_t write_req;
  34. static uv_fs_t close_req;
  35. static uv_iocp_t iocp_handle;
  36. static char test_buf[] = "test-buffer\n";
  37. static void iocp_cb(uv_iocp_t* handle)
  38. {
  39. iocp_cb_count++;
  40. uv_stop(loop);
  41. }
  42. TEST_IMPL(iocp) {
  43. int r;
  44. HANDLE file;
  45. unlink("test_file");
  46. loop = uv_default_loop();
  47. r = uv_fs_open(loop, &open_req, "test_file", O_WRONLY | O_CREAT,
  48. S_IWUSR | S_IRUSR, NULL);
  49. ASSERT(r >= 0);
  50. ASSERT(open_req.result >= 0);
  51. uv_fs_req_cleanup(&open_req);
  52. r = uv_fs_write(loop, &write_req, open_req.result, test_buf,
  53. sizeof(test_buf), -1, NULL);
  54. ASSERT(r >= 0);
  55. ASSERT(write_req.result >= 0);
  56. uv_fs_req_cleanup(&write_req);
  57. r = uv_fs_close(loop, &close_req, open_req.result, NULL);
  58. ASSERT(r == 0);
  59. ASSERT(close_req.result == 0);
  60. uv_fs_req_cleanup(&close_req);
  61. /* Test. */
  62. file = CreateFile("test_file",
  63. GENERIC_READ | GENERIC_WRITE,
  64. 0,
  65. 0,
  66. OPEN_EXISTING,
  67. FILE_FLAG_OVERLAPPED,
  68. 0);
  69. uv_iocp_start(loop, &iocp_handle, file, iocp_cb);
  70. if (!ReadFile(file,
  71. test_buf,
  72. sizeof(test_buf),
  73. NULL,
  74. (OVERLAPPED*) iocp_handle.overlapped)) {
  75. ASSERT(GetLastError() == ERROR_IO_PENDING);
  76. }
  77. uv_run(loop, UV_RUN_DEFAULT);
  78. ASSERT(iocp_cb_count == 1);
  79. CloseHandle(file);
  80. unlink("test_file");
  81. MAKE_VALGRIND_HAPPY();
  82. return 0;
  83. }
  84. #endif