test-osx-select.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 #include "uv.h"
23 #include "task.h"
24 
25 #ifdef __APPLE__
26 
27 #include <sys/ioctl.h>
28 #include <string.h>
29 
30 static int read_count;
31 
32 
33 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
34  static char slab[1024];
35  buf->base = slab;
36  buf->len = sizeof(slab);
37 }
38 
39 
40 static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
41  fprintf(stdout, "got data %d\n", ++read_count);
42  fflush(stdout);
43 
44  if (read_count == 3)
45  uv_close((uv_handle_t*) stream, NULL);
46 }
47 
48 
49 TEST_IMPL(osx_select) {
50  int r;
51  int fd;
52  size_t i;
53  size_t len;
54  const char* str;
55  uv_tty_t tty;
56 
57  fd = open("/dev/tty", O_RDONLY);
58  if (fd < 0) {
59  fprintf(stderr, "Cannot open /dev/tty as read-only: %s\n", strerror(errno));
60  fflush(stderr);
61  return TEST_SKIP;
62  }
63 
64  r = uv_tty_init(uv_default_loop(), &tty, fd, 1);
65  ASSERT(r == 0);
66 
68 
69  /* Emulate user-input */
70  str = "got some input\n"
71  "with a couple of lines\n"
72  "feel pretty happy\n";
73  for (i = 0, len = strlen(str); i < len; i++) {
74  r = ioctl(fd, TIOCSTI, str + i);
75  ASSERT(r == 0);
76  }
77 
79 
80  ASSERT(read_count == 3);
81 
83  return 0;
84 }
85 
86 
87 TEST_IMPL(osx_select_many_fds) {
88  int r;
89  int fd;
90  size_t i;
91  size_t len;
92  const char* str;
93  struct sockaddr_in addr;
94  uv_tty_t tty;
95  uv_tcp_t tcps[1500];
96 
97  TEST_FILE_LIMIT(ARRAY_SIZE(tcps) + 100);
98 
99  r = uv_ip4_addr("127.0.0.1", 0, &addr);
100  ASSERT(r == 0);
101 
102  for (i = 0; i < ARRAY_SIZE(tcps); i++) {
103  r = uv_tcp_init(uv_default_loop(), &tcps[i]);
104  ASSERT(r == 0);
105  r = uv_tcp_bind(&tcps[i], (const struct sockaddr *) &addr, 0);
106  ASSERT(r == 0);
107  uv_unref((uv_handle_t*) &tcps[i]);
108  }
109 
110  fd = open("/dev/tty", O_RDONLY);
111  if (fd < 0) {
112  fprintf(stderr, "Cannot open /dev/tty as read-only: %s\n", strerror(errno));
113  fflush(stderr);
114  return TEST_SKIP;
115  }
116 
117  r = uv_tty_init(uv_default_loop(), &tty, fd, 1);
118  ASSERT(r == 0);
119 
121  ASSERT(r == 0);
122 
123  /* Emulate user-input */
124  str = "got some input\n"
125  "with a couple of lines\n"
126  "feel pretty happy\n";
127  for (i = 0, len = strlen(str); i < len; i++) {
128  r = ioctl(fd, TIOCSTI, str + i);
129  ASSERT(r == 0);
130  }
131 
133 
134  ASSERT(read_count == 3);
135 
137  return 0;
138 }
139 
140 #endif /* __APPLE__ */
xds_interop_client.str
str
Definition: xds_interop_client.py:487
slab
static char slab[1]
Definition: test-watcher-cross-stop.c:37
ARRAY_SIZE
#define ARRAY_SIZE(array)
Definition: bloaty.cc:101
task.h
read_cb
static void read_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
Definition: benchmark-pound.c:138
demumble_test.stdout
stdout
Definition: demumble_test.py:38
TEST_IMPL
#define TEST_IMPL(name)
Definition: task.h:236
string.h
uv_tty_s
Definition: uv.h:704
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
ASSERT
#define ASSERT(expr)
Definition: task.h:102
alloc_cb
static void alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: benchmark-pound.c:84
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_unref
UV_EXTERN void uv_unref(uv_handle_t *)
Definition: uv-common.c:522
uv_tcp_bind
UV_EXTERN int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:277
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
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
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
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
tty
uv_tty_t tty
Definition: libuv/docs/code/tty/main.c:7
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
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
uv_tcp_init
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: unix/tcp.c:143
uv_tcp_s
Definition: uv.h:544
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
TEST_FILE_LIMIT
#define TEST_FILE_LIMIT(num)
Definition: task.h:271
uv_buf_t
Definition: unix.h:121
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_tty_init
UV_EXTERN int uv_tty_init(uv_loop_t *, uv_tty_t *, uv_file fd, int readable)
Definition: unix/tty.c:123
TEST_SKIP
@ TEST_SKIP
Definition: task.h:254
open
#define open
Definition: test-fs.c:46
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:30