Blame test/test-pipe-set-non-blocking.c

Packit Service 7c31a4
/* Copyright (c) 2015, Ben Noordhuis <info@bnoordhuis.nl>
Packit Service 7c31a4
 *
Packit Service 7c31a4
 * Permission to use, copy, modify, and/or distribute this software for any
Packit Service 7c31a4
 * purpose with or without fee is hereby granted, provided that the above
Packit Service 7c31a4
 * copyright notice and this permission notice appear in all copies.
Packit Service 7c31a4
 *
Packit Service 7c31a4
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
Packit Service 7c31a4
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
Packit Service 7c31a4
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
Packit Service 7c31a4
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
Packit Service 7c31a4
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Packit Service 7c31a4
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Packit Service 7c31a4
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Packit Service 7c31a4
 */
Packit Service 7c31a4
Packit Service 7c31a4
#include "uv.h"
Packit Service 7c31a4
#include "task.h"
Packit Service 7c31a4
Packit Service 7c31a4
#ifdef _WIN32
Packit Service 7c31a4
Packit Service 7c31a4
TEST_IMPL(pipe_set_non_blocking) {
Packit Service 7c31a4
  RETURN_SKIP("Test not implemented on Windows.");
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
#else  /* !_WIN32 */
Packit Service 7c31a4
Packit Service e2ebee
#include <string.h> /* memset */
Packit Service e2ebee
#include <unistd.h> /* close */
Packit Service 7c31a4
#include <sys/types.h>
Packit Service e2ebee
#include <sys/socket.h>
Packit Service 7c31a4
Packit Service 7c31a4
struct thread_ctx {
Packit Service 7c31a4
  uv_barrier_t barrier;
Packit Service e2ebee
  uv_file fd;
Packit Service 7c31a4
};
Packit Service 7c31a4
Packit Service 7c31a4
static void thread_main(void* arg) {
Packit Service 7c31a4
  struct thread_ctx* ctx;
Packit Service e2ebee
  uv_fs_t req;
Packit Service e2ebee
  uv_buf_t bufs[1];
Packit Service 7c31a4
  char buf[4096];
Packit Service 7c31a4
  ssize_t n;
Packit Service e2ebee
  int uv_errno;
Packit Service e2ebee
Packit Service e2ebee
  bufs[0] = uv_buf_init(buf, sizeof(buf));
Packit Service 7c31a4
Packit Service 7c31a4
  ctx = arg;
Packit Service 7c31a4
  uv_barrier_wait(&ctx->barrier);
Packit Service 7c31a4
Packit Service e2ebee
  uv_sleep(100); /* make sure we are forcing the writer to block a bit */
Packit Service e2ebee
  do {
Packit Service e2ebee
    uv_errno = uv_fs_read(NULL, &req, ctx->fd, bufs, 1, -1, NULL);
Packit Service e2ebee
    n = req.result;
Packit Service e2ebee
    uv_fs_req_cleanup(&req;;
Packit Service e2ebee
  } while (n > 0 || (n == -1 && uv_errno == UV_EINTR));
Packit Service 7c31a4
Packit Service 7c31a4
  ASSERT(n == 0);
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
TEST_IMPL(pipe_set_non_blocking) {
Packit Service 7c31a4
  struct thread_ctx ctx;
Packit Service 7c31a4
  uv_pipe_t pipe_handle;
Packit Service 7c31a4
  uv_thread_t thread;
Packit Service 7c31a4
  size_t nwritten;
Packit Service 7c31a4
  char data[4096];
Packit Service 7c31a4
  uv_buf_t buf;
Packit Service e2ebee
  uv_file fd[2];
Packit Service 7c31a4
  int n;
Packit Service 7c31a4
Packit Service 7c31a4
  ASSERT(0 == uv_pipe_init(uv_default_loop(), &pipe_handle, 0));
Packit Service 7c31a4
  ASSERT(0 == socketpair(AF_UNIX, SOCK_STREAM, 0, fd));
Packit Service e2ebee
  ASSERT(0 == uv_pipe_open(&pipe_handle, fd[1]));
Packit Service 7c31a4
  ASSERT(0 == uv_stream_set_blocking((uv_stream_t*) &pipe_handle, 1));
Packit Service e2ebee
  fd[1] = -1; /* fd[1] is owned by pipe_handle now. */
Packit Service 7c31a4
Packit Service e2ebee
  ctx.fd = fd[0];
Packit Service 7c31a4
  ASSERT(0 == uv_barrier_init(&ctx.barrier, 2));
Packit Service 7c31a4
  ASSERT(0 == uv_thread_create(&thread, thread_main, &ctx));
Packit Service 7c31a4
  uv_barrier_wait(&ctx.barrier);
Packit Service 7c31a4
Packit Service 7c31a4
  buf.len = sizeof(data);
Packit Service 7c31a4
  buf.base = data;
Packit Service 7c31a4
  memset(data, '.', sizeof(data));
Packit Service 7c31a4
Packit Service 7c31a4
  nwritten = 0;
Packit Service 7c31a4
  while (nwritten < 10 << 20) {
Packit Service 7c31a4
    /* The stream is in blocking mode so uv_try_write() should always succeed
Packit Service 7c31a4
     * with the exact number of bytes that we wanted written.
Packit Service 7c31a4
     */
Packit Service 7c31a4
    n = uv_try_write((uv_stream_t*) &pipe_handle, &buf, 1);
Packit Service 7c31a4
    ASSERT(n == sizeof(data));
Packit Service 7c31a4
    nwritten += n;
Packit Service 7c31a4
  }
Packit Service 7c31a4
Packit Service 7c31a4
  uv_close((uv_handle_t*) &pipe_handle, NULL);
Packit Service 7c31a4
  ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
Packit Service 7c31a4
Packit Service 7c31a4
  ASSERT(0 == uv_thread_join(&thread));
Packit Service e2ebee
  ASSERT(0 == close(fd[0]));  /* fd[1] is closed by uv_close(). */
Packit Service e2ebee
  fd[0] = -1;
Packit Service 7c31a4
  uv_barrier_destroy(&ctx.barrier);
Packit Service 7c31a4
Packit Service 7c31a4
  MAKE_VALGRIND_HAPPY();
Packit Service 7c31a4
  return 0;
Packit Service 7c31a4
}
Packit Service 7c31a4
Packit Service 7c31a4
#endif  /* !_WIN32 */