test-pipe-set-non-blocking.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (c) 2015, Ben Noordhuis <info@bnoordhuis.nl>
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "uv.h"
  16. #include "task.h"
  17. #ifdef _WIN32
  18. TEST_IMPL(pipe_set_non_blocking) {
  19. RETURN_SKIP("Test not implemented on Windows.");
  20. }
  21. #else /* !_WIN32 */
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <sys/socket.h>
  25. #include <sys/types.h>
  26. #include <sys/un.h>
  27. #include <unistd.h>
  28. struct thread_ctx {
  29. uv_barrier_t barrier;
  30. int fd;
  31. };
  32. static void thread_main(void* arg) {
  33. struct thread_ctx* ctx;
  34. char buf[4096];
  35. ssize_t n;
  36. ctx = arg;
  37. uv_barrier_wait(&ctx->barrier);
  38. do
  39. n = read(ctx->fd, buf, sizeof(buf));
  40. while (n > 0 || (n == -1 && errno == EINTR));
  41. ASSERT(n == 0);
  42. }
  43. TEST_IMPL(pipe_set_non_blocking) {
  44. struct thread_ctx ctx;
  45. uv_pipe_t pipe_handle;
  46. uv_thread_t thread;
  47. size_t nwritten;
  48. char data[4096];
  49. uv_buf_t buf;
  50. int fd[2];
  51. int n;
  52. ASSERT(0 == uv_pipe_init(uv_default_loop(), &pipe_handle, 0));
  53. ASSERT(0 == socketpair(AF_UNIX, SOCK_STREAM, 0, fd));
  54. ASSERT(0 == uv_pipe_open(&pipe_handle, fd[0]));
  55. ASSERT(0 == uv_stream_set_blocking((uv_stream_t*) &pipe_handle, 1));
  56. ctx.fd = fd[1];
  57. ASSERT(0 == uv_barrier_init(&ctx.barrier, 2));
  58. ASSERT(0 == uv_thread_create(&thread, thread_main, &ctx));
  59. uv_barrier_wait(&ctx.barrier);
  60. buf.len = sizeof(data);
  61. buf.base = data;
  62. memset(data, '.', sizeof(data));
  63. nwritten = 0;
  64. while (nwritten < 10 << 20) {
  65. /* The stream is in blocking mode so uv_try_write() should always succeed
  66. * with the exact number of bytes that we wanted written.
  67. */
  68. n = uv_try_write((uv_stream_t*) &pipe_handle, &buf, 1);
  69. ASSERT(n == sizeof(data));
  70. nwritten += n;
  71. }
  72. uv_close((uv_handle_t*) &pipe_handle, NULL);
  73. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  74. ASSERT(0 == uv_thread_join(&thread));
  75. ASSERT(0 == close(fd[1])); /* fd[0] is closed by uv_close(). */
  76. uv_barrier_destroy(&ctx.barrier);
  77. MAKE_VALGRIND_HAPPY();
  78. return 0;
  79. }
  80. #endif /* !_WIN32 */