test-close-fd.c
Go to the documentation of this file.
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 
22 #if !defined(_WIN32)
23 
24 #include "uv.h"
25 #include "task.h"
26 #include <fcntl.h>
27 #include <unistd.h>
28 
29 static unsigned int read_cb_called;
30 
31 static void alloc_cb(uv_handle_t *handle, size_t size, uv_buf_t *buf) {
32  static char slab[1];
33  buf->base = slab;
34  buf->len = sizeof(slab);
35 }
36 
37 static void read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) {
38  switch (++read_cb_called) {
39  case 1:
40  ASSERT(nread == 1);
42  break;
43  case 2:
44  ASSERT(nread == UV_EOF);
45  uv_close((uv_handle_t *) handle, NULL);
46  break;
47  default:
48  ASSERT(!"read_cb_called > 2");
49  }
50 }
51 
52 TEST_IMPL(close_fd) {
54  int fd[2];
55 
56  ASSERT(0 == pipe(fd));
58  ASSERT(0 == uv_pipe_open(&pipe_handle, fd[0]));
59  fd[0] = -1; /* uv_pipe_open() takes ownership of the file descriptor. */
60  ASSERT(1 == write(fd[1], "", 1));
61  ASSERT(0 == close(fd[1]));
62  fd[1] = -1;
65  ASSERT(1 == read_cb_called);
66  ASSERT(0 == uv_is_active((const uv_handle_t *) &pipe_handle));
69  ASSERT(2 == read_cb_called);
70  ASSERT(0 != uv_is_closing((const uv_handle_t *) &pipe_handle));
71 
73  return 0;
74 }
75 
76 #else
77 
78 typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
79 
80 #endif /* !_WIN32 */
alloc_cb
static void alloc_cb(uv_handle_t *handle, size_t size, uv_buf_t *buf)
Definition: test-close-fd.c:31
slab
static char slab[1]
Definition: test-watcher-cross-stop.c:37
TEST_IMPL
TEST_IMPL(close_fd)
Definition: test-close-fd.c:52
task.h
uv_pipe_init
UV_EXTERN int uv_pipe_init(uv_loop_t *, uv_pipe_t *handle, int ipc)
Definition: unix/pipe.c:33
write
#define write
Definition: test-fs.c:47
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
ASSERT
#define ASSERT(expr)
Definition: task.h:102
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_is_closing
UV_EXTERN int uv_is_closing(const uv_handle_t *handle)
Definition: unix/core.c:319
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_stream_s
Definition: uv.h:491
read_cb
static void read_cb(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf)
Definition: test-close-fd.c:37
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
ssize_t
intptr_t ssize_t
Definition: win.h:27
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_is_active
UV_EXTERN int uv_is_active(const uv_handle_t *handle)
Definition: unix/core.c:418
uv_read_start
UV_EXTERN int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: unix/stream.c:1555
close
#define close
Definition: test-fs.c:48
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_buf_t
Definition: unix.h:121
file_has_no_tests
int file_has_no_tests
Definition: test-fs-fd-hash.c:131
pipe_handle
static uv_pipe_t pipe_handle
Definition: test-pipe-connect-prepare.c:39
uv_pipe_s
Definition: uv.h:757
read_cb_called
static unsigned int read_cb_called
Definition: test-close-fd.c:29
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv_read_stop
UV_EXTERN int uv_read_stop(uv_stream_t *)
Definition: unix/stream.c:1590
uv_pipe_open
UV_EXTERN int uv_pipe_open(uv_pipe_t *, uv_file file)
Definition: unix/pipe.c:137


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:26