rbuv 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +0 -1
- data/README.md +6 -1
- data/Rakefile +42 -0
- data/deps/libuv/.gitignore +34 -0
- data/deps/libuv/.mailmap +16 -0
- data/deps/libuv/AUTHORS +81 -0
- data/deps/libuv/ChangeLog +45 -0
- data/deps/libuv/LICENSE +41 -0
- data/deps/libuv/Makefile +53 -0
- data/deps/libuv/README.md +118 -0
- data/deps/libuv/build.mk +164 -0
- data/deps/libuv/checksparse.sh +230 -0
- data/deps/libuv/common.gypi +197 -0
- data/deps/libuv/config-mingw.mk +48 -0
- data/deps/libuv/config-unix.mk +167 -0
- data/deps/libuv/gyp_uv +98 -0
- data/deps/libuv/include/uv-private/ngx-queue.h +129 -0
- data/deps/libuv/include/uv-private/stdint-msvc2008.h +247 -0
- data/deps/libuv/include/uv-private/tree.h +768 -0
- data/deps/libuv/include/uv-private/uv-bsd.h +34 -0
- data/deps/libuv/include/uv-private/uv-darwin.h +61 -0
- data/deps/libuv/include/uv-private/uv-linux.h +34 -0
- data/deps/libuv/include/uv-private/uv-sunos.h +44 -0
- data/deps/libuv/include/uv-private/uv-unix.h +332 -0
- data/deps/libuv/include/uv-private/uv-win.h +585 -0
- data/deps/libuv/include/uv.h +1987 -0
- data/deps/libuv/src/fs-poll.c +248 -0
- data/deps/libuv/src/inet.c +298 -0
- data/deps/libuv/src/unix/aix.c +393 -0
- data/deps/libuv/src/unix/async.c +281 -0
- data/deps/libuv/src/unix/core.c +714 -0
- data/deps/libuv/src/unix/cygwin.c +93 -0
- data/deps/libuv/src/unix/darwin-proctitle.m +78 -0
- data/deps/libuv/src/unix/darwin.c +431 -0
- data/deps/libuv/src/unix/dl.c +83 -0
- data/deps/libuv/src/unix/error.c +109 -0
- data/deps/libuv/src/unix/freebsd.c +343 -0
- data/deps/libuv/src/unix/fs.c +869 -0
- data/deps/libuv/src/unix/fsevents.c +299 -0
- data/deps/libuv/src/unix/getaddrinfo.c +159 -0
- data/deps/libuv/src/unix/internal.h +259 -0
- data/deps/libuv/src/unix/kqueue.c +347 -0
- data/deps/libuv/src/unix/linux-core.c +724 -0
- data/deps/libuv/src/unix/linux-inotify.c +236 -0
- data/deps/libuv/src/unix/linux-syscalls.c +388 -0
- data/deps/libuv/src/unix/linux-syscalls.h +150 -0
- data/deps/libuv/src/unix/loop-watcher.c +64 -0
- data/deps/libuv/src/unix/loop.c +114 -0
- data/deps/libuv/src/unix/netbsd.c +353 -0
- data/deps/libuv/src/unix/openbsd.c +304 -0
- data/deps/libuv/src/unix/pipe.c +261 -0
- data/deps/libuv/src/unix/poll.c +108 -0
- data/deps/libuv/src/unix/process.c +501 -0
- data/deps/libuv/src/unix/proctitle.c +103 -0
- data/deps/libuv/src/unix/signal.c +455 -0
- data/deps/libuv/src/unix/stream.c +1380 -0
- data/deps/libuv/src/unix/sunos.c +647 -0
- data/deps/libuv/src/unix/tcp.c +357 -0
- data/deps/libuv/src/unix/thread.c +431 -0
- data/deps/libuv/src/unix/threadpool.c +286 -0
- data/deps/libuv/src/unix/timer.c +153 -0
- data/deps/libuv/src/unix/tty.c +179 -0
- data/deps/libuv/src/unix/udp.c +715 -0
- data/deps/libuv/src/uv-common.c +431 -0
- data/deps/libuv/src/uv-common.h +204 -0
- data/deps/libuv/src/version.c +60 -0
- data/deps/libuv/src/win/async.c +99 -0
- data/deps/libuv/src/win/atomicops-inl.h +56 -0
- data/deps/libuv/src/win/core.c +310 -0
- data/deps/libuv/src/win/dl.c +86 -0
- data/deps/libuv/src/win/error.c +164 -0
- data/deps/libuv/src/win/fs-event.c +506 -0
- data/deps/libuv/src/win/fs.c +1951 -0
- data/deps/libuv/src/win/getaddrinfo.c +365 -0
- data/deps/libuv/src/win/handle-inl.h +164 -0
- data/deps/libuv/src/win/handle.c +153 -0
- data/deps/libuv/src/win/internal.h +346 -0
- data/deps/libuv/src/win/loop-watcher.c +124 -0
- data/deps/libuv/src/win/pipe.c +1656 -0
- data/deps/libuv/src/win/poll.c +615 -0
- data/deps/libuv/src/win/process-stdio.c +503 -0
- data/deps/libuv/src/win/process.c +1048 -0
- data/deps/libuv/src/win/req-inl.h +224 -0
- data/deps/libuv/src/win/req.c +25 -0
- data/deps/libuv/src/win/signal.c +354 -0
- data/deps/libuv/src/win/stream-inl.h +67 -0
- data/deps/libuv/src/win/stream.c +198 -0
- data/deps/libuv/src/win/tcp.c +1422 -0
- data/deps/libuv/src/win/thread.c +666 -0
- data/deps/libuv/src/win/threadpool.c +82 -0
- data/deps/libuv/src/win/timer.c +230 -0
- data/deps/libuv/src/win/tty.c +1857 -0
- data/deps/libuv/src/win/udp.c +744 -0
- data/deps/libuv/src/win/util.c +946 -0
- data/deps/libuv/src/win/winapi.c +152 -0
- data/deps/libuv/src/win/winapi.h +4476 -0
- data/deps/libuv/src/win/winsock.c +560 -0
- data/deps/libuv/src/win/winsock.h +171 -0
- data/deps/libuv/test/benchmark-async-pummel.c +119 -0
- data/deps/libuv/test/benchmark-async.c +139 -0
- data/deps/libuv/test/benchmark-fs-stat.c +136 -0
- data/deps/libuv/test/benchmark-getaddrinfo.c +91 -0
- data/deps/libuv/test/benchmark-list.h +163 -0
- data/deps/libuv/test/benchmark-loop-count.c +90 -0
- data/deps/libuv/test/benchmark-million-async.c +112 -0
- data/deps/libuv/test/benchmark-million-timers.c +77 -0
- data/deps/libuv/test/benchmark-multi-accept.c +432 -0
- data/deps/libuv/test/benchmark-ping-pongs.c +212 -0
- data/deps/libuv/test/benchmark-pound.c +325 -0
- data/deps/libuv/test/benchmark-pump.c +459 -0
- data/deps/libuv/test/benchmark-sizes.c +45 -0
- data/deps/libuv/test/benchmark-spawn.c +163 -0
- data/deps/libuv/test/benchmark-tcp-write-batch.c +141 -0
- data/deps/libuv/test/benchmark-thread.c +64 -0
- data/deps/libuv/test/benchmark-udp-pummel.c +238 -0
- data/deps/libuv/test/blackhole-server.c +118 -0
- data/deps/libuv/test/dns-server.c +329 -0
- data/deps/libuv/test/echo-server.c +384 -0
- data/deps/libuv/test/fixtures/empty_file +0 -0
- data/deps/libuv/test/fixtures/load_error.node +1 -0
- data/deps/libuv/test/run-benchmarks.c +64 -0
- data/deps/libuv/test/run-tests.c +159 -0
- data/deps/libuv/test/runner-unix.c +328 -0
- data/deps/libuv/test/runner-unix.h +36 -0
- data/deps/libuv/test/runner-win.c +318 -0
- data/deps/libuv/test/runner-win.h +43 -0
- data/deps/libuv/test/runner.c +394 -0
- data/deps/libuv/test/runner.h +165 -0
- data/deps/libuv/test/task.h +122 -0
- data/deps/libuv/test/test-active.c +83 -0
- data/deps/libuv/test/test-async.c +136 -0
- data/deps/libuv/test/test-barrier.c +98 -0
- data/deps/libuv/test/test-callback-order.c +77 -0
- data/deps/libuv/test/test-callback-stack.c +204 -0
- data/deps/libuv/test/test-condvar.c +173 -0
- data/deps/libuv/test/test-connection-fail.c +150 -0
- data/deps/libuv/test/test-cwd-and-chdir.c +64 -0
- data/deps/libuv/test/test-delayed-accept.c +189 -0
- data/deps/libuv/test/test-dlerror.c +58 -0
- data/deps/libuv/test/test-embed.c +136 -0
- data/deps/libuv/test/test-error.c +59 -0
- data/deps/libuv/test/test-fail-always.c +29 -0
- data/deps/libuv/test/test-fs-event.c +504 -0
- data/deps/libuv/test/test-fs-poll.c +148 -0
- data/deps/libuv/test/test-fs.c +1899 -0
- data/deps/libuv/test/test-get-currentexe.c +63 -0
- data/deps/libuv/test/test-get-loadavg.c +36 -0
- data/deps/libuv/test/test-get-memory.c +38 -0
- data/deps/libuv/test/test-getaddrinfo.c +120 -0
- data/deps/libuv/test/test-getsockname.c +344 -0
- data/deps/libuv/test/test-hrtime.c +54 -0
- data/deps/libuv/test/test-idle.c +82 -0
- data/deps/libuv/test/test-ipc-send-recv.c +218 -0
- data/deps/libuv/test/test-ipc.c +625 -0
- data/deps/libuv/test/test-list.h +492 -0
- data/deps/libuv/test/test-loop-handles.c +337 -0
- data/deps/libuv/test/test-loop-stop.c +73 -0
- data/deps/libuv/test/test-multiple-listen.c +103 -0
- data/deps/libuv/test/test-mutexes.c +63 -0
- data/deps/libuv/test/test-pass-always.c +28 -0
- data/deps/libuv/test/test-ping-pong.c +250 -0
- data/deps/libuv/test/test-pipe-bind-error.c +144 -0
- data/deps/libuv/test/test-pipe-connect-error.c +98 -0
- data/deps/libuv/test/test-platform-output.c +87 -0
- data/deps/libuv/test/test-poll-close.c +73 -0
- data/deps/libuv/test/test-poll.c +575 -0
- data/deps/libuv/test/test-process-title.c +49 -0
- data/deps/libuv/test/test-ref.c +415 -0
- data/deps/libuv/test/test-run-nowait.c +46 -0
- data/deps/libuv/test/test-run-once.c +49 -0
- data/deps/libuv/test/test-semaphore.c +111 -0
- data/deps/libuv/test/test-shutdown-close.c +105 -0
- data/deps/libuv/test/test-shutdown-eof.c +184 -0
- data/deps/libuv/test/test-signal-multiple-loops.c +270 -0
- data/deps/libuv/test/test-signal.c +152 -0
- data/deps/libuv/test/test-spawn.c +938 -0
- data/deps/libuv/test/test-stdio-over-pipes.c +250 -0
- data/deps/libuv/test/test-tcp-bind-error.c +198 -0
- data/deps/libuv/test/test-tcp-bind6-error.c +159 -0
- data/deps/libuv/test/test-tcp-close-while-connecting.c +81 -0
- data/deps/libuv/test/test-tcp-close.c +130 -0
- data/deps/libuv/test/test-tcp-connect-error-after-write.c +96 -0
- data/deps/libuv/test/test-tcp-connect-error.c +71 -0
- data/deps/libuv/test/test-tcp-connect-timeout.c +86 -0
- data/deps/libuv/test/test-tcp-connect6-error.c +69 -0
- data/deps/libuv/test/test-tcp-flags.c +52 -0
- data/deps/libuv/test/test-tcp-open.c +175 -0
- data/deps/libuv/test/test-tcp-read-stop.c +73 -0
- data/deps/libuv/test/test-tcp-shutdown-after-write.c +132 -0
- data/deps/libuv/test/test-tcp-unexpected-read.c +114 -0
- data/deps/libuv/test/test-tcp-write-to-half-open-connection.c +136 -0
- data/deps/libuv/test/test-tcp-writealot.c +171 -0
- data/deps/libuv/test/test-thread.c +183 -0
- data/deps/libuv/test/test-threadpool-cancel.c +311 -0
- data/deps/libuv/test/test-threadpool.c +77 -0
- data/deps/libuv/test/test-timer-again.c +142 -0
- data/deps/libuv/test/test-timer.c +266 -0
- data/deps/libuv/test/test-tty.c +111 -0
- data/deps/libuv/test/test-udp-dgram-too-big.c +87 -0
- data/deps/libuv/test/test-udp-ipv6.c +158 -0
- data/deps/libuv/test/test-udp-multicast-join.c +140 -0
- data/deps/libuv/test/test-udp-multicast-ttl.c +87 -0
- data/deps/libuv/test/test-udp-open.c +154 -0
- data/deps/libuv/test/test-udp-options.c +87 -0
- data/deps/libuv/test/test-udp-send-and-recv.c +210 -0
- data/deps/libuv/test/test-util.c +97 -0
- data/deps/libuv/test/test-walk-handles.c +78 -0
- data/deps/libuv/uv.gyp +431 -0
- data/deps/libuv/vcbuild.bat +128 -0
- data/ext/rbuv/debug.h +27 -0
- data/ext/rbuv/error.c +7 -0
- data/ext/rbuv/error.h +10 -0
- data/ext/rbuv/extconf.rb +35 -0
- data/ext/rbuv/handle.c +40 -0
- data/ext/rbuv/handle.h +14 -0
- data/ext/rbuv/libuv.mk +12 -0
- data/ext/rbuv/loop.c +50 -0
- data/ext/rbuv/loop.h +13 -0
- data/ext/rbuv/rbuv.c +15 -0
- data/ext/rbuv/rbuv.h +27 -0
- data/ext/rbuv/timer.c +133 -0
- data/ext/rbuv/timer.h +13 -0
- data/lib/rbuv/timer.rb +7 -0
- data/lib/rbuv/version.rb +1 -1
- data/lib/rbuv.rb +24 -2
- data/rbuv.gemspec +5 -1
- data/spec/spec_helper.rb +22 -0
- data/spec/timer_spec.rb +144 -0
- metadata +278 -9
@@ -0,0 +1,504 @@
|
|
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
|
+
#include <fcntl.h>
|
27
|
+
|
28
|
+
#ifndef HAVE_KQUEUE
|
29
|
+
# if defined(__APPLE__) || \
|
30
|
+
defined(__DragonFly__) || \
|
31
|
+
defined(__FreeBSD__) || \
|
32
|
+
defined(__OpenBSD__) || \
|
33
|
+
defined(__NetBSD__)
|
34
|
+
# define HAVE_KQUEUE 1
|
35
|
+
# endif
|
36
|
+
#endif
|
37
|
+
|
38
|
+
static uv_fs_event_t fs_event;
|
39
|
+
static uv_timer_t timer;
|
40
|
+
static int timer_cb_called = 0;
|
41
|
+
static int close_cb_called = 0;
|
42
|
+
static int fs_event_cb_called = 0;
|
43
|
+
static int timer_cb_touch_called = 0;
|
44
|
+
|
45
|
+
static void create_dir(uv_loop_t* loop, const char* name) {
|
46
|
+
int r;
|
47
|
+
uv_fs_t req;
|
48
|
+
r = uv_fs_mkdir(loop, &req, name, 0755, NULL);
|
49
|
+
ASSERT(r == 0 || uv_last_error(loop).code == UV_EEXIST);
|
50
|
+
uv_fs_req_cleanup(&req);
|
51
|
+
}
|
52
|
+
|
53
|
+
static void create_file(uv_loop_t* loop, const char* name) {
|
54
|
+
int r;
|
55
|
+
uv_file file;
|
56
|
+
uv_fs_t req;
|
57
|
+
|
58
|
+
r = uv_fs_open(loop, &req, name, O_WRONLY | O_CREAT,
|
59
|
+
S_IWRITE | S_IREAD, NULL);
|
60
|
+
ASSERT(r != -1);
|
61
|
+
file = r;
|
62
|
+
uv_fs_req_cleanup(&req);
|
63
|
+
r = uv_fs_close(loop, &req, file, NULL);
|
64
|
+
ASSERT(r == 0);
|
65
|
+
uv_fs_req_cleanup(&req);
|
66
|
+
}
|
67
|
+
|
68
|
+
static void touch_file(uv_loop_t* loop, const char* name) {
|
69
|
+
int r;
|
70
|
+
uv_file file;
|
71
|
+
uv_fs_t req;
|
72
|
+
|
73
|
+
r = uv_fs_open(loop, &req, name, O_RDWR, 0, NULL);
|
74
|
+
ASSERT(r != -1);
|
75
|
+
file = r;
|
76
|
+
uv_fs_req_cleanup(&req);
|
77
|
+
|
78
|
+
r = uv_fs_write(loop, &req, file, "foo", 4, -1, NULL);
|
79
|
+
ASSERT(r != -1);
|
80
|
+
uv_fs_req_cleanup(&req);
|
81
|
+
|
82
|
+
r = uv_fs_close(loop, &req, file, NULL);
|
83
|
+
ASSERT(r != -1);
|
84
|
+
uv_fs_req_cleanup(&req);
|
85
|
+
}
|
86
|
+
|
87
|
+
static void close_cb(uv_handle_t* handle) {
|
88
|
+
ASSERT(handle != NULL);
|
89
|
+
close_cb_called++;
|
90
|
+
}
|
91
|
+
|
92
|
+
static void fail_cb(uv_fs_event_t* handle,
|
93
|
+
const char* path,
|
94
|
+
int events,
|
95
|
+
int status) {
|
96
|
+
ASSERT(0 && "fail_cb called");
|
97
|
+
}
|
98
|
+
|
99
|
+
static void fs_event_cb_dir(uv_fs_event_t* handle, const char* filename,
|
100
|
+
int events, int status) {
|
101
|
+
++fs_event_cb_called;
|
102
|
+
ASSERT(handle == &fs_event);
|
103
|
+
ASSERT(status == 0);
|
104
|
+
ASSERT(events == UV_RENAME);
|
105
|
+
ASSERT(filename == NULL || strcmp(filename, "file1") == 0);
|
106
|
+
uv_close((uv_handle_t*)handle, close_cb);
|
107
|
+
}
|
108
|
+
|
109
|
+
static void fs_event_cb_file(uv_fs_event_t* handle, const char* filename,
|
110
|
+
int events, int status) {
|
111
|
+
++fs_event_cb_called;
|
112
|
+
ASSERT(handle == &fs_event);
|
113
|
+
ASSERT(status == 0);
|
114
|
+
ASSERT(events == UV_CHANGE);
|
115
|
+
ASSERT(filename == NULL || strcmp(filename, "file2") == 0);
|
116
|
+
uv_close((uv_handle_t*)handle, close_cb);
|
117
|
+
}
|
118
|
+
|
119
|
+
static void timer_cb_close_handle(uv_timer_t* timer, int status) {
|
120
|
+
uv_handle_t* handle;
|
121
|
+
|
122
|
+
ASSERT(timer != NULL);
|
123
|
+
ASSERT(status == 0);
|
124
|
+
handle = timer->data;
|
125
|
+
|
126
|
+
uv_close((uv_handle_t*)timer, NULL);
|
127
|
+
uv_close((uv_handle_t*)handle, close_cb);
|
128
|
+
}
|
129
|
+
|
130
|
+
static void fs_event_cb_file_current_dir(uv_fs_event_t* handle,
|
131
|
+
const char* filename, int events, int status) {
|
132
|
+
ASSERT(fs_event_cb_called == 0);
|
133
|
+
++fs_event_cb_called;
|
134
|
+
|
135
|
+
ASSERT(handle == &fs_event);
|
136
|
+
ASSERT(status == 0);
|
137
|
+
ASSERT(events == UV_CHANGE);
|
138
|
+
ASSERT(filename == NULL || strcmp(filename, "watch_file") == 0);
|
139
|
+
|
140
|
+
/* Regression test for SunOS: touch should generate just one event. */
|
141
|
+
{
|
142
|
+
static uv_timer_t timer;
|
143
|
+
uv_timer_init(handle->loop, &timer);
|
144
|
+
timer.data = handle;
|
145
|
+
uv_timer_start(&timer, timer_cb_close_handle, 250, 0);
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
static void timer_cb_dir(uv_timer_t* handle, int status) {
|
150
|
+
++timer_cb_called;
|
151
|
+
create_file(handle->loop, "watch_dir/file1");
|
152
|
+
uv_close((uv_handle_t*)handle, close_cb);
|
153
|
+
}
|
154
|
+
|
155
|
+
static void timer_cb_file(uv_timer_t* handle, int status) {
|
156
|
+
++timer_cb_called;
|
157
|
+
|
158
|
+
if (timer_cb_called == 1) {
|
159
|
+
touch_file(handle->loop, "watch_dir/file1");
|
160
|
+
} else {
|
161
|
+
touch_file(handle->loop, "watch_dir/file2");
|
162
|
+
uv_close((uv_handle_t*)handle, close_cb);
|
163
|
+
}
|
164
|
+
}
|
165
|
+
|
166
|
+
static void timer_cb_touch(uv_timer_t* timer, int status) {
|
167
|
+
ASSERT(status == 0);
|
168
|
+
uv_close((uv_handle_t*)timer, NULL);
|
169
|
+
touch_file(timer->loop, "watch_file");
|
170
|
+
timer_cb_touch_called++;
|
171
|
+
}
|
172
|
+
|
173
|
+
static void timer_cb_watch_twice(uv_timer_t* handle, int status) {
|
174
|
+
uv_fs_event_t* handles = handle->data;
|
175
|
+
uv_close((uv_handle_t*) (handles + 0), NULL);
|
176
|
+
uv_close((uv_handle_t*) (handles + 1), NULL);
|
177
|
+
uv_close((uv_handle_t*) handle, NULL);
|
178
|
+
}
|
179
|
+
|
180
|
+
TEST_IMPL(fs_event_watch_dir) {
|
181
|
+
uv_loop_t* loop = uv_default_loop();
|
182
|
+
int r;
|
183
|
+
|
184
|
+
/* Setup */
|
185
|
+
remove("watch_dir/file2");
|
186
|
+
remove("watch_dir/file1");
|
187
|
+
remove("watch_dir/");
|
188
|
+
create_dir(loop, "watch_dir");
|
189
|
+
|
190
|
+
r = uv_fs_event_init(loop, &fs_event, "watch_dir", fs_event_cb_dir, 0);
|
191
|
+
ASSERT(r != -1);
|
192
|
+
r = uv_timer_init(loop, &timer);
|
193
|
+
ASSERT(r != -1);
|
194
|
+
r = uv_timer_start(&timer, timer_cb_dir, 100, 0);
|
195
|
+
ASSERT(r != -1);
|
196
|
+
|
197
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
198
|
+
|
199
|
+
ASSERT(fs_event_cb_called == 1);
|
200
|
+
ASSERT(timer_cb_called == 1);
|
201
|
+
ASSERT(close_cb_called == 2);
|
202
|
+
|
203
|
+
/* Cleanup */
|
204
|
+
remove("watch_dir/file2");
|
205
|
+
remove("watch_dir/file1");
|
206
|
+
remove("watch_dir/");
|
207
|
+
|
208
|
+
MAKE_VALGRIND_HAPPY();
|
209
|
+
return 0;
|
210
|
+
}
|
211
|
+
|
212
|
+
TEST_IMPL(fs_event_watch_file) {
|
213
|
+
uv_loop_t* loop = uv_default_loop();
|
214
|
+
int r;
|
215
|
+
|
216
|
+
/* Setup */
|
217
|
+
remove("watch_dir/file2");
|
218
|
+
remove("watch_dir/file1");
|
219
|
+
remove("watch_dir/");
|
220
|
+
create_dir(loop, "watch_dir");
|
221
|
+
create_file(loop, "watch_dir/file1");
|
222
|
+
create_file(loop, "watch_dir/file2");
|
223
|
+
|
224
|
+
r = uv_fs_event_init(loop, &fs_event, "watch_dir/file2", fs_event_cb_file, 0);
|
225
|
+
ASSERT(r != -1);
|
226
|
+
r = uv_timer_init(loop, &timer);
|
227
|
+
ASSERT(r != -1);
|
228
|
+
r = uv_timer_start(&timer, timer_cb_file, 100, 100);
|
229
|
+
ASSERT(r != -1);
|
230
|
+
|
231
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
232
|
+
|
233
|
+
ASSERT(fs_event_cb_called == 1);
|
234
|
+
ASSERT(timer_cb_called == 2);
|
235
|
+
ASSERT(close_cb_called == 2);
|
236
|
+
|
237
|
+
/* Cleanup */
|
238
|
+
remove("watch_dir/file2");
|
239
|
+
remove("watch_dir/file1");
|
240
|
+
remove("watch_dir/");
|
241
|
+
|
242
|
+
MAKE_VALGRIND_HAPPY();
|
243
|
+
return 0;
|
244
|
+
}
|
245
|
+
|
246
|
+
TEST_IMPL(fs_event_watch_file_twice) {
|
247
|
+
const char path[] = "test/fixtures/empty_file";
|
248
|
+
uv_fs_event_t watchers[2];
|
249
|
+
uv_timer_t timer;
|
250
|
+
uv_loop_t* loop;
|
251
|
+
|
252
|
+
loop = uv_default_loop();
|
253
|
+
timer.data = watchers;
|
254
|
+
|
255
|
+
ASSERT(0 == uv_fs_event_init(loop, watchers + 0, path, fail_cb, 0));
|
256
|
+
ASSERT(0 == uv_fs_event_init(loop, watchers + 1, path, fail_cb, 0));
|
257
|
+
ASSERT(0 == uv_timer_init(loop, &timer));
|
258
|
+
ASSERT(0 == uv_timer_start(&timer, timer_cb_watch_twice, 10, 0));
|
259
|
+
ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
|
260
|
+
|
261
|
+
MAKE_VALGRIND_HAPPY();
|
262
|
+
return 0;
|
263
|
+
}
|
264
|
+
|
265
|
+
TEST_IMPL(fs_event_watch_file_current_dir) {
|
266
|
+
uv_timer_t timer;
|
267
|
+
uv_loop_t* loop;
|
268
|
+
int r;
|
269
|
+
|
270
|
+
loop = uv_default_loop();
|
271
|
+
|
272
|
+
/* Setup */
|
273
|
+
remove("watch_file");
|
274
|
+
create_file(loop, "watch_file");
|
275
|
+
|
276
|
+
r = uv_fs_event_init(loop, &fs_event, "watch_file",
|
277
|
+
fs_event_cb_file_current_dir, 0);
|
278
|
+
ASSERT(r != -1);
|
279
|
+
|
280
|
+
r = uv_timer_init(loop, &timer);
|
281
|
+
ASSERT(r == 0);
|
282
|
+
|
283
|
+
r = uv_timer_start(&timer, timer_cb_touch, 1, 0);
|
284
|
+
ASSERT(r == 0);
|
285
|
+
|
286
|
+
ASSERT(timer_cb_touch_called == 0);
|
287
|
+
ASSERT(fs_event_cb_called == 0);
|
288
|
+
ASSERT(close_cb_called == 0);
|
289
|
+
|
290
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
291
|
+
|
292
|
+
ASSERT(timer_cb_touch_called == 1);
|
293
|
+
ASSERT(fs_event_cb_called == 1);
|
294
|
+
ASSERT(close_cb_called == 1);
|
295
|
+
|
296
|
+
/* Cleanup */
|
297
|
+
remove("watch_file");
|
298
|
+
|
299
|
+
MAKE_VALGRIND_HAPPY();
|
300
|
+
return 0;
|
301
|
+
}
|
302
|
+
|
303
|
+
TEST_IMPL(fs_event_no_callback_after_close) {
|
304
|
+
uv_loop_t* loop = uv_default_loop();
|
305
|
+
int r;
|
306
|
+
|
307
|
+
/* Setup */
|
308
|
+
remove("watch_dir/file1");
|
309
|
+
remove("watch_dir/");
|
310
|
+
create_dir(loop, "watch_dir");
|
311
|
+
create_file(loop, "watch_dir/file1");
|
312
|
+
|
313
|
+
r = uv_fs_event_init(loop,
|
314
|
+
&fs_event,
|
315
|
+
"watch_dir/file1",
|
316
|
+
fs_event_cb_file,
|
317
|
+
0);
|
318
|
+
ASSERT(r != -1);
|
319
|
+
|
320
|
+
uv_close((uv_handle_t*)&fs_event, close_cb);
|
321
|
+
touch_file(loop, "watch_dir/file1");
|
322
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
323
|
+
|
324
|
+
ASSERT(fs_event_cb_called == 0);
|
325
|
+
ASSERT(close_cb_called == 1);
|
326
|
+
|
327
|
+
/* Cleanup */
|
328
|
+
remove("watch_dir/file1");
|
329
|
+
remove("watch_dir/");
|
330
|
+
|
331
|
+
MAKE_VALGRIND_HAPPY();
|
332
|
+
return 0;
|
333
|
+
}
|
334
|
+
|
335
|
+
TEST_IMPL(fs_event_no_callback_on_close) {
|
336
|
+
uv_loop_t* loop = uv_default_loop();
|
337
|
+
int r;
|
338
|
+
|
339
|
+
/* Setup */
|
340
|
+
remove("watch_dir/file1");
|
341
|
+
remove("watch_dir/");
|
342
|
+
create_dir(loop, "watch_dir");
|
343
|
+
create_file(loop, "watch_dir/file1");
|
344
|
+
|
345
|
+
r = uv_fs_event_init(loop,
|
346
|
+
&fs_event,
|
347
|
+
"watch_dir/file1",
|
348
|
+
fs_event_cb_file,
|
349
|
+
0);
|
350
|
+
ASSERT(r != -1);
|
351
|
+
|
352
|
+
uv_close((uv_handle_t*)&fs_event, close_cb);
|
353
|
+
|
354
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
355
|
+
|
356
|
+
ASSERT(fs_event_cb_called == 0);
|
357
|
+
ASSERT(close_cb_called == 1);
|
358
|
+
|
359
|
+
/* Cleanup */
|
360
|
+
remove("watch_dir/file1");
|
361
|
+
remove("watch_dir/");
|
362
|
+
|
363
|
+
MAKE_VALGRIND_HAPPY();
|
364
|
+
return 0;
|
365
|
+
}
|
366
|
+
|
367
|
+
|
368
|
+
static void fs_event_fail(uv_fs_event_t* handle, const char* filename,
|
369
|
+
int events, int status) {
|
370
|
+
ASSERT(0 && "should never be called");
|
371
|
+
}
|
372
|
+
|
373
|
+
|
374
|
+
static void timer_cb(uv_timer_t* handle, int status) {
|
375
|
+
int r;
|
376
|
+
|
377
|
+
ASSERT(status == 0);
|
378
|
+
|
379
|
+
r = uv_fs_event_init(handle->loop, &fs_event, ".", fs_event_fail, 0);
|
380
|
+
ASSERT(r == 0);
|
381
|
+
|
382
|
+
uv_close((uv_handle_t*)&fs_event, close_cb);
|
383
|
+
uv_close((uv_handle_t*)handle, close_cb);
|
384
|
+
}
|
385
|
+
|
386
|
+
|
387
|
+
TEST_IMPL(fs_event_immediate_close) {
|
388
|
+
uv_timer_t timer;
|
389
|
+
uv_loop_t* loop;
|
390
|
+
int r;
|
391
|
+
|
392
|
+
loop = uv_default_loop();
|
393
|
+
|
394
|
+
r = uv_timer_init(loop, &timer);
|
395
|
+
ASSERT(r == 0);
|
396
|
+
|
397
|
+
r = uv_timer_start(&timer, timer_cb, 1, 0);
|
398
|
+
ASSERT(r == 0);
|
399
|
+
|
400
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
401
|
+
|
402
|
+
ASSERT(close_cb_called == 2);
|
403
|
+
|
404
|
+
MAKE_VALGRIND_HAPPY();
|
405
|
+
return 0;
|
406
|
+
}
|
407
|
+
|
408
|
+
|
409
|
+
TEST_IMPL(fs_event_close_with_pending_event) {
|
410
|
+
uv_loop_t* loop;
|
411
|
+
int r;
|
412
|
+
|
413
|
+
loop = uv_default_loop();
|
414
|
+
|
415
|
+
create_dir(loop, "watch_dir");
|
416
|
+
create_file(loop, "watch_dir/file");
|
417
|
+
|
418
|
+
r = uv_fs_event_init(loop, &fs_event, "watch_dir", fs_event_fail, 0);
|
419
|
+
ASSERT(r == 0);
|
420
|
+
|
421
|
+
/* Generate an fs event. */
|
422
|
+
touch_file(loop, "watch_dir/file");
|
423
|
+
|
424
|
+
uv_close((uv_handle_t*)&fs_event, close_cb);
|
425
|
+
|
426
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
427
|
+
|
428
|
+
ASSERT(close_cb_called == 1);
|
429
|
+
|
430
|
+
/* Clean up */
|
431
|
+
remove("watch_dir/file");
|
432
|
+
remove("watch_dir/");
|
433
|
+
|
434
|
+
MAKE_VALGRIND_HAPPY();
|
435
|
+
return 0;
|
436
|
+
}
|
437
|
+
|
438
|
+
#if defined(HAVE_KQUEUE)
|
439
|
+
|
440
|
+
/* kqueue doesn't register fs events if you don't have an active watcher.
|
441
|
+
* The file descriptor needs to be part of the kqueue set of interest and
|
442
|
+
* that's not the case until we actually enter the event loop.
|
443
|
+
*/
|
444
|
+
TEST_IMPL(fs_event_close_in_callback) {
|
445
|
+
fprintf(stderr, "Skipping test, doesn't work with kqueue.\n");
|
446
|
+
return 0;
|
447
|
+
}
|
448
|
+
|
449
|
+
#else /* !HAVE_KQUEUE */
|
450
|
+
|
451
|
+
static void fs_event_cb_close(uv_fs_event_t* handle, const char* filename,
|
452
|
+
int events, int status) {
|
453
|
+
ASSERT(status == 0);
|
454
|
+
|
455
|
+
ASSERT(fs_event_cb_called < 3);
|
456
|
+
++fs_event_cb_called;
|
457
|
+
|
458
|
+
if (fs_event_cb_called == 3) {
|
459
|
+
uv_close((uv_handle_t*) handle, close_cb);
|
460
|
+
}
|
461
|
+
}
|
462
|
+
|
463
|
+
|
464
|
+
TEST_IMPL(fs_event_close_in_callback) {
|
465
|
+
uv_loop_t* loop;
|
466
|
+
int r;
|
467
|
+
|
468
|
+
loop = uv_default_loop();
|
469
|
+
|
470
|
+
create_dir(loop, "watch_dir");
|
471
|
+
create_file(loop, "watch_dir/file1");
|
472
|
+
create_file(loop, "watch_dir/file2");
|
473
|
+
create_file(loop, "watch_dir/file3");
|
474
|
+
create_file(loop, "watch_dir/file4");
|
475
|
+
create_file(loop, "watch_dir/file5");
|
476
|
+
|
477
|
+
r = uv_fs_event_init(loop, &fs_event, "watch_dir", fs_event_cb_close, 0);
|
478
|
+
ASSERT(r == 0);
|
479
|
+
|
480
|
+
/* Generate a couple of fs events. */
|
481
|
+
touch_file(loop, "watch_dir/file1");
|
482
|
+
touch_file(loop, "watch_dir/file2");
|
483
|
+
touch_file(loop, "watch_dir/file3");
|
484
|
+
touch_file(loop, "watch_dir/file4");
|
485
|
+
touch_file(loop, "watch_dir/file5");
|
486
|
+
|
487
|
+
uv_run(loop, UV_RUN_DEFAULT);
|
488
|
+
|
489
|
+
ASSERT(close_cb_called == 1);
|
490
|
+
ASSERT(fs_event_cb_called == 3);
|
491
|
+
|
492
|
+
/* Clean up */
|
493
|
+
remove("watch_dir/file1");
|
494
|
+
remove("watch_dir/file2");
|
495
|
+
remove("watch_dir/file3");
|
496
|
+
remove("watch_dir/file4");
|
497
|
+
remove("watch_dir/file5");
|
498
|
+
remove("watch_dir/");
|
499
|
+
|
500
|
+
MAKE_VALGRIND_HAPPY();
|
501
|
+
return 0;
|
502
|
+
}
|
503
|
+
|
504
|
+
#endif /* HAVE_KQUEUE */
|
@@ -0,0 +1,148 @@
|
|
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, int status);
|
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_statbuf_t* prev,
|
34
|
+
const uv_statbuf_t* curr);
|
35
|
+
|
36
|
+
static uv_fs_poll_t poll_handle;
|
37
|
+
static uv_timer_t timer_handle;
|
38
|
+
static uv_loop_t* loop;
|
39
|
+
|
40
|
+
static int poll_cb_called;
|
41
|
+
static int timer_cb_called;
|
42
|
+
static int close_cb_called;
|
43
|
+
|
44
|
+
|
45
|
+
static void touch_file(const char* path) {
|
46
|
+
static int count;
|
47
|
+
FILE* fp;
|
48
|
+
int i;
|
49
|
+
|
50
|
+
ASSERT((fp = fopen(FIXTURE, "w+")));
|
51
|
+
|
52
|
+
/* Need to change the file size because the poller may not pick up
|
53
|
+
* sub-second mtime changes.
|
54
|
+
*/
|
55
|
+
i = ++count;
|
56
|
+
|
57
|
+
while (i--)
|
58
|
+
fputc('*', fp);
|
59
|
+
|
60
|
+
fclose(fp);
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
static void close_cb(uv_handle_t* handle) {
|
65
|
+
close_cb_called++;
|
66
|
+
}
|
67
|
+
|
68
|
+
|
69
|
+
static void timer_cb(uv_timer_t* handle, int status) {
|
70
|
+
touch_file(FIXTURE);
|
71
|
+
timer_cb_called++;
|
72
|
+
}
|
73
|
+
|
74
|
+
|
75
|
+
static void poll_cb(uv_fs_poll_t* handle,
|
76
|
+
int status,
|
77
|
+
const uv_statbuf_t* prev,
|
78
|
+
const uv_statbuf_t* curr) {
|
79
|
+
uv_statbuf_t zero_statbuf;
|
80
|
+
|
81
|
+
memset(&zero_statbuf, 0, sizeof(zero_statbuf));
|
82
|
+
|
83
|
+
ASSERT(handle == &poll_handle);
|
84
|
+
ASSERT(uv_is_active((uv_handle_t*)handle));
|
85
|
+
ASSERT(prev != NULL);
|
86
|
+
ASSERT(curr != NULL);
|
87
|
+
|
88
|
+
switch (poll_cb_called++) {
|
89
|
+
case 0:
|
90
|
+
ASSERT(status == -1);
|
91
|
+
ASSERT(uv_last_error(loop).code == UV_ENOENT);
|
92
|
+
ASSERT(0 == memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
|
93
|
+
ASSERT(0 == memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
|
94
|
+
touch_file(FIXTURE);
|
95
|
+
break;
|
96
|
+
|
97
|
+
case 1:
|
98
|
+
ASSERT(status == 0);
|
99
|
+
ASSERT(0 == memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
|
100
|
+
ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
|
101
|
+
ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 20, 0));
|
102
|
+
break;
|
103
|
+
|
104
|
+
case 2:
|
105
|
+
ASSERT(status == 0);
|
106
|
+
ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
|
107
|
+
ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
|
108
|
+
ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 200, 0));
|
109
|
+
break;
|
110
|
+
|
111
|
+
case 3:
|
112
|
+
ASSERT(status == 0);
|
113
|
+
ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
|
114
|
+
ASSERT(0 != memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
|
115
|
+
remove(FIXTURE);
|
116
|
+
break;
|
117
|
+
|
118
|
+
case 4:
|
119
|
+
ASSERT(status == -1);
|
120
|
+
ASSERT(0 != memcmp(prev, &zero_statbuf, sizeof(zero_statbuf)));
|
121
|
+
ASSERT(0 == memcmp(curr, &zero_statbuf, sizeof(zero_statbuf)));
|
122
|
+
ASSERT(uv_last_error(loop).code == UV_ENOENT);
|
123
|
+
uv_close((uv_handle_t*)handle, close_cb);
|
124
|
+
break;
|
125
|
+
|
126
|
+
default:
|
127
|
+
ASSERT(0);
|
128
|
+
}
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
TEST_IMPL(fs_poll) {
|
133
|
+
loop = uv_default_loop();
|
134
|
+
|
135
|
+
remove(FIXTURE);
|
136
|
+
|
137
|
+
ASSERT(0 == uv_timer_init(loop, &timer_handle));
|
138
|
+
ASSERT(0 == uv_fs_poll_init(loop, &poll_handle));
|
139
|
+
ASSERT(0 == uv_fs_poll_start(&poll_handle, poll_cb, FIXTURE, 100));
|
140
|
+
ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
|
141
|
+
|
142
|
+
ASSERT(poll_cb_called == 5);
|
143
|
+
ASSERT(timer_cb_called == 2);
|
144
|
+
ASSERT(close_cb_called == 1);
|
145
|
+
|
146
|
+
MAKE_VALGRIND_HAPPY();
|
147
|
+
return 0;
|
148
|
+
}
|