node/deps/uv/test/test-close-fd.c
cjihrig 78d6e62fd0 deps: upgrade to libuv 1.41.0
Notable changes:

- The IBM i platform has been promoted to a Tier 2 platform.
- libuv is now built with `-fno-strict-aliasing`, and recommends
  that projects using libuv do the same.
- `uv_fs_mkdir()` now returns `UV_EINVAL` for invalid directory
  names on Windows.
- `uv_uptime()` now returns the correct value on OpenVZ containers.
- Windows 8 is the new minimum supported version of Windows.
- Bind errors are now reported from `uv_tcp_connect()`.
- The `uv_pipe()` function has been added.
- The `uv_socketpair()` function has been added.
- `uv_read_start()` error handling has been unified across
  Windows and Unix.

PR-URL: https://github.com/nodejs/node/pull/37360
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
2021-02-15 23:09:54 +01:00

85 lines
2.8 KiB
C

/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "task.h"
#ifndef _WIN32
#include <unistd.h>
#endif
static unsigned int read_cb_called;
static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
static char slab[1];
buf->base = slab;
buf->len = sizeof(slab);
}
static void read_cb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
switch (++read_cb_called) {
case 1:
ASSERT(nread == 1);
uv_read_stop(handle);
break;
case 2:
ASSERT(nread == UV_EOF);
uv_close((uv_handle_t *) handle, NULL);
break;
default:
ASSERT(!"read_cb_called > 2");
}
}
TEST_IMPL(close_fd) {
uv_pipe_t pipe_handle;
uv_fs_t req;
uv_buf_t bufs[1];
uv_file fd[2];
bufs[0] = uv_buf_init("", 1);
ASSERT(0 == uv_pipe(fd, 0, 0));
ASSERT(0 == uv_pipe_init(uv_default_loop(), &pipe_handle, 0));
ASSERT(0 == uv_pipe_open(&pipe_handle, fd[0]));
/* uv_pipe_open() takes ownership of the file descriptor. */
fd[0] = -1;
ASSERT(1 == uv_fs_write(NULL, &req, fd[1], bufs, 1, -1, NULL));
ASSERT(1 == req.result);
uv_fs_req_cleanup(&req);
#ifdef _WIN32
ASSERT(0 == _close(fd[1]));
#else
ASSERT(0 == close(fd[1]));
#endif
fd[1] = -1;
ASSERT(0 == uv_read_start((uv_stream_t *) &pipe_handle, alloc_cb, read_cb));
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
ASSERT(1 == read_cb_called);
ASSERT(0 == uv_is_active((const uv_handle_t *) &pipe_handle));
ASSERT(0 == uv_read_start((uv_stream_t *) &pipe_handle, alloc_cb, read_cb));
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
ASSERT(2 == read_cb_called);
ASSERT(0 != uv_is_closing((const uv_handle_t *) &pipe_handle));
MAKE_VALGRIND_HAPPY();
return 0;
}