test-fs-poll.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 #include <string.h>
26 
27 #define FIXTURE "testfile"
28 
29 static void timer_cb(uv_timer_t* handle);
30 static void close_cb(uv_handle_t* handle);
31 static void poll_cb(uv_fs_poll_t* handle,
32  int status,
33  const uv_stat_t* prev,
34  const uv_stat_t* curr);
35 
36 static void poll_cb_fail(uv_fs_poll_t* handle,
37  int status,
38  const uv_stat_t* prev,
39  const uv_stat_t* curr);
40 static void poll_cb_noop(uv_fs_poll_t* handle,
41  int status,
42  const uv_stat_t* prev,
43  const uv_stat_t* curr);
44 
47 static uv_loop_t* loop;
48 
49 static int poll_cb_called;
50 static int timer_cb_called;
51 static int close_cb_called;
52 
53 
54 static void touch_file(const char* path) {
55  static int count;
56  FILE* fp;
57  int i;
58 
59  ASSERT((fp = fopen(FIXTURE, "w+")));
60 
61  /* Need to change the file size because the poller may not pick up
62  * sub-second mtime changes.
63  */
64  i = ++count;
65 
66  while (i--)
67  fputc('*', fp);
68 
69  fclose(fp);
70 }
71 
72 
73 static void close_cb(uv_handle_t* handle) {
75 }
76 
77 
78 static void timer_cb(uv_timer_t* handle) {
81 }
82 
83 
85  int status,
86  const uv_stat_t* prev,
87  const uv_stat_t* curr) {
88  ASSERT(0 && "fail_cb called");
89 }
90 
92  int status,
93  const uv_stat_t* prev,
94  const uv_stat_t* curr) {
95 }
96 
97 
99  int status,
100  const uv_stat_t* prev,
101  const uv_stat_t* curr) {
103 
104  memset(&zero_statbuf, 0, sizeof(zero_statbuf));
105 
108  ASSERT(prev != NULL);
109  ASSERT(curr != NULL);
110 
111  switch (poll_cb_called++) {
112  case 0:
113  ASSERT(status == UV_ENOENT);
114  ASSERT(0 == memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
115  ASSERT(0 == memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
117  break;
118 
119  case 1:
120  ASSERT(status == 0);
121  ASSERT(0 == memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
122  ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
123  ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 20, 0));
124  break;
125 
126  case 2:
127  ASSERT(status == 0);
128  ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
129  ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
130  ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 200, 0));
131  break;
132 
133  case 3:
134  ASSERT(status == 0);
135  ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
136  ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
137  remove(FIXTURE);
138  break;
139 
140  case 4:
141  ASSERT(status == UV_ENOENT);
142  ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
143  ASSERT(0 == memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
145  break;
146 
147  default:
148  ASSERT(0);
149  }
150 }
151 
152 
153 TEST_IMPL(fs_poll) {
154  loop = uv_default_loop();
155 
156  remove(FIXTURE);
157 
162 
163  ASSERT(poll_cb_called == 5);
164  ASSERT(timer_cb_called == 2);
165  ASSERT(close_cb_called == 1);
166 
168  return 0;
169 }
170 
171 
172 TEST_IMPL(fs_poll_getpath) {
173  char buf[1024];
174  size_t len;
175  loop = uv_default_loop();
176 
177  remove(FIXTURE);
178 
180  len = sizeof buf;
181  ASSERT(UV_EINVAL == uv_fs_poll_getpath(&poll_handle, buf, &len));
183  len = sizeof buf;
185  ASSERT(buf[len - 1] != 0);
186  ASSERT(buf[len] == '\0');
187  ASSERT(0 == memcmp(buf, FIXTURE, len));
188 
190 
192 
193  ASSERT(close_cb_called == 1);
194 
196  return 0;
197 }
198 
199 
200 TEST_IMPL(fs_poll_close_request) {
201  uv_loop_t loop;
203 
204  remove(FIXTURE);
205 
206  ASSERT(0 == uv_loop_init(&loop));
207 
211  while (close_cb_called == 0)
213  ASSERT(close_cb_called == 1);
214 
215  ASSERT(0 == uv_loop_close(&loop));
216 
218  return 0;
219 }
220 
221 TEST_IMPL(fs_poll_close_request_multi_start_stop) {
222  uv_loop_t loop;
224  int i;
225 
226  remove(FIXTURE);
227 
228  ASSERT(0 == uv_loop_init(&loop));
229 
231 
232  for (i = 0; i < 10; ++i) {
235  }
237  while (close_cb_called == 0)
239  ASSERT(close_cb_called == 1);
240 
241  ASSERT(0 == uv_loop_close(&loop));
242 
244  return 0;
245 }
246 
247 TEST_IMPL(fs_poll_close_request_multi_stop_start) {
248  uv_loop_t loop;
250  int i;
251 
252  remove(FIXTURE);
253 
254  ASSERT(0 == uv_loop_init(&loop));
255 
257 
258  for (i = 0; i < 10; ++i) {
261  }
263  while (close_cb_called == 0)
265  ASSERT(close_cb_called == 1);
266 
267  ASSERT(0 == uv_loop_close(&loop));
268 
270  return 0;
271 }
272 
273 TEST_IMPL(fs_poll_close_request_stop_when_active) {
274  /* Regression test for https://github.com/libuv/libuv/issues/2287. */
275  uv_loop_t loop;
277 
278  remove(FIXTURE);
279 
280  ASSERT(0 == uv_loop_init(&loop));
281 
282  /* Set up all handles. */
286 
287  /* Close the timer handle, and do not crash. */
290 
291  /* Clean up after the test. */
294  ASSERT(close_cb_called == 1);
295 
296  ASSERT(0 == uv_loop_close(&loop));
297 
299  return 0;
300 }
close_cb_called
static int close_cb_called
Definition: test-fs-poll.c:51
uv_fs_poll_init
UV_EXTERN int uv_fs_poll_init(uv_loop_t *loop, uv_fs_poll_t *handle)
Definition: fs-poll.c:59
timer_cb
static void timer_cb(uv_timer_t *handle)
Definition: test-fs-poll.c:78
poll_cb_fail
static void poll_cb_fail(uv_fs_poll_t *handle, int status, const uv_stat_t *prev, const uv_stat_t *curr)
Definition: test-fs-poll.c:84
task.h
memset
return memset(p, 0, total)
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
poll_cb
static void poll_cb(uv_fs_poll_t *handle, int status, const uv_stat_t *prev, const uv_stat_t *curr)
Definition: test-fs-poll.c:98
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
check_documentation.path
path
Definition: check_documentation.py:57
uv_fs_poll_stop
UV_EXTERN int uv_fs_poll_stop(uv_fs_poll_t *handle)
Definition: fs-poll.c:116
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_loop_close
UV_EXTERN int uv_loop_close(uv_loop_t *loop)
Definition: uv-common.c:761
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
zero_statbuf
static uv_stat_t zero_statbuf
Definition: fs-poll.c:56
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
timer_cb_called
static int timer_cb_called
Definition: test-fs-poll.c:50
touch_file
static void touch_file(const char *path)
Definition: test-fs-poll.c:54
uv_is_active
UV_EXTERN int uv_is_active(const uv_handle_t *handle)
Definition: unix/core.c:418
uv_loop_init
UV_EXTERN int uv_loop_init(uv_loop_t *loop)
Definition: loop.c:30
uv_fs_poll_start
UV_EXTERN int uv_fs_poll_start(uv_fs_poll_t *handle, uv_fs_poll_cb poll_cb, const char *path, unsigned int interval)
Definition: fs-poll.c:66
uv_timer_s
Definition: uv.h:850
loop
static uv_loop_t * loop
Definition: test-fs-poll.c:47
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
close_cb
static void close_cb(uv_handle_t *handle)
Definition: test-fs-poll.c:73
benchmark.FILE
FILE
Definition: benchmark.py:21
uv_stat_t
Definition: uv.h:346
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
uv_fs_poll_s
Definition: uv.h:1544
grpc::fclose
fclose(creds_file)
FIXTURE
#define FIXTURE
Definition: test-fs-poll.c:27
UV_RUN_ONCE
@ UV_RUN_ONCE
Definition: uv.h:255
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_timer_start
UV_EXTERN int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
Definition: timer.c:66
poll_cb_called
static int poll_cb_called
Definition: test-fs-poll.c:49
uv_loop_s
Definition: uv.h:1767
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
uv_timer_init
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58
poll_cb_noop
static void poll_cb_noop(uv_fs_poll_t *handle, int status, const uv_stat_t *prev, const uv_stat_t *curr)
Definition: test-fs-poll.c:91
poll_handle
static uv_fs_poll_t poll_handle
Definition: test-fs-poll.c:45
timer_handle
static uv_timer_t timer_handle
Definition: test-fs-poll.c:46
TEST_IMPL
TEST_IMPL(fs_poll)
Definition: test-fs-poll.c:153
uv_fs_poll_getpath
UV_EXTERN int uv_fs_poll_getpath(uv_fs_poll_t *handle, char *buffer, size_t *size)
Definition: fs-poll.c:138
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


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