noderb 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/ext/noderb_extension/extconf.rb +15 -11
  2. data/ext/noderb_extension/libuv/AUTHORS +3 -0
  3. data/ext/noderb_extension/libuv/{README → README.md} +48 -6
  4. data/ext/noderb_extension/libuv/common.gypi +6 -1
  5. data/ext/noderb_extension/libuv/config-unix.mk +1 -1
  6. data/ext/noderb_extension/libuv/include/uv-private/uv-linux.h +29 -0
  7. data/ext/noderb_extension/libuv/include/uv-private/uv-unix.h +9 -0
  8. data/ext/noderb_extension/libuv/include/uv-private/uv-win.h +38 -3
  9. data/ext/noderb_extension/libuv/include/uv.h +55 -3
  10. data/ext/noderb_extension/libuv/src/unix/cares.c +1 -0
  11. data/ext/noderb_extension/libuv/src/unix/core.c +20 -4
  12. data/ext/noderb_extension/libuv/src/unix/cygwin.c +16 -0
  13. data/ext/noderb_extension/libuv/src/unix/darwin.c +18 -0
  14. data/ext/noderb_extension/libuv/src/unix/freebsd.c +18 -1
  15. data/ext/noderb_extension/libuv/src/unix/fs.c +18 -9
  16. data/ext/noderb_extension/libuv/src/unix/internal.h +24 -0
  17. data/ext/noderb_extension/libuv/src/unix/linux.c +133 -1
  18. data/ext/noderb_extension/libuv/src/unix/netbsd.c +18 -1
  19. data/ext/noderb_extension/libuv/src/unix/stream.c +21 -7
  20. data/ext/noderb_extension/libuv/src/unix/sunos.c +18 -1
  21. data/ext/noderb_extension/libuv/src/unix/tty.c +41 -0
  22. data/ext/noderb_extension/libuv/src/unix/udp.c +1 -0
  23. data/ext/noderb_extension/libuv/src/win/core.c +3 -0
  24. data/ext/noderb_extension/libuv/src/win/fs-event.c +384 -0
  25. data/ext/noderb_extension/libuv/src/win/getaddrinfo.c +7 -2
  26. data/ext/noderb_extension/libuv/src/win/handle.c +41 -0
  27. data/ext/noderb_extension/libuv/src/win/internal.h +36 -0
  28. data/ext/noderb_extension/libuv/src/win/pipe.c +3 -0
  29. data/ext/noderb_extension/libuv/src/win/process.c +7 -2
  30. data/ext/noderb_extension/libuv/src/win/req.c +10 -0
  31. data/ext/noderb_extension/libuv/src/win/stream.c +10 -3
  32. data/ext/noderb_extension/libuv/src/win/tcp.c +3 -1
  33. data/ext/noderb_extension/libuv/src/win/tty.c +1559 -5
  34. data/ext/noderb_extension/libuv/test/benchmark-getaddrinfo.c +2 -0
  35. data/ext/noderb_extension/libuv/test/runner-unix.c +2 -0
  36. data/ext/noderb_extension/libuv/test/test-fs-event.c +217 -0
  37. data/ext/noderb_extension/libuv/test/test-fs.c +0 -4
  38. data/ext/noderb_extension/libuv/test/test-getaddrinfo.c +7 -3
  39. data/ext/noderb_extension/libuv/test/test-list.h +23 -0
  40. data/ext/noderb_extension/libuv/test/test-tcp-close.c +47 -0
  41. data/ext/noderb_extension/libuv/test/test-tcp-write-error.c +168 -0
  42. data/ext/noderb_extension/libuv/test/test-timer.c +40 -0
  43. data/ext/noderb_extension/libuv/test/test-tty.c +56 -0
  44. data/ext/noderb_extension/libuv/uv.gyp +17 -18
  45. data/ext/noderb_extension/noderb.c +0 -2
  46. data/ext/noderb_extension/noderb_dns.c +6 -7
  47. data/ext/noderb_extension/noderb_fs.c +11 -10
  48. data/ext/noderb_extension/noderb_timers.c +5 -5
  49. data/lib/noderb/version.rb +1 -1
  50. metadata +8 -3
@@ -52,6 +52,8 @@ static void getaddrinfo_cb(uv_getaddrinfo_t* handle, int status,
52
52
  if (calls_initiated < TOTAL_CALLS) {
53
53
  getaddrinfo_initiate(handle);
54
54
  }
55
+
56
+ uv_freeaddrinfo(res);
55
57
  }
56
58
 
57
59
 
@@ -65,6 +65,8 @@ void platform_init(int argc, char **argv) {
65
65
  #else
66
66
  strcpy(executable_path, argv[0]);
67
67
  #endif
68
+
69
+ signal(SIGPIPE, SIG_IGN);
68
70
  }
69
71
 
70
72
 
@@ -0,0 +1,217 @@
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
+ uv_fs_event_t fs_event;
29
+ uv_timer_t timer;
30
+ int timer_cb_called;
31
+ int close_cb_called;
32
+ int fs_event_cb_called;
33
+
34
+ static void create_dir(uv_loop_t* loop, const char* name) {
35
+ int r;
36
+ uv_fs_t req;
37
+ r = uv_fs_mkdir(loop, &req, name, 0755, NULL);
38
+ ASSERT(r == 0);
39
+ uv_fs_req_cleanup(&req);
40
+ }
41
+
42
+ static void create_file(uv_loop_t* loop, const char* name) {
43
+ int r;
44
+ uv_file file;
45
+ uv_fs_t req;
46
+
47
+ r = uv_fs_open(loop, &req, name, O_WRONLY | O_CREAT,
48
+ S_IWRITE | S_IREAD, NULL);
49
+ ASSERT(r != -1);
50
+ file = r;
51
+ uv_fs_req_cleanup(&req);
52
+ r = uv_fs_close(loop, &req, file, NULL);
53
+ ASSERT(r == 0);
54
+ uv_fs_req_cleanup(&req);
55
+ }
56
+
57
+ static void touch_file(uv_loop_t* loop, const char* name) {
58
+ int r;
59
+ uv_file file;
60
+ uv_fs_t req;
61
+
62
+ r = uv_fs_open(loop, &req, name, O_RDWR, 0, NULL);
63
+ ASSERT(r != -1);
64
+ file = r;
65
+ uv_fs_req_cleanup(&req);
66
+
67
+ r = uv_fs_write(loop, &req, file, "foo", 4, -1, NULL);
68
+ ASSERT(r != -1);
69
+ uv_fs_req_cleanup(&req);
70
+
71
+ r = uv_fs_close(loop, &req, file, NULL);
72
+ ASSERT(r != -1);
73
+ uv_fs_req_cleanup(&req);
74
+ }
75
+
76
+ static void close_cb(uv_handle_t* handle) {
77
+ ASSERT(handle != NULL);
78
+ close_cb_called++;
79
+ }
80
+
81
+ static void fs_event_cb_dir(uv_fs_event_t* handle, const char* filename,
82
+ int events, int status) {
83
+ ++fs_event_cb_called;
84
+ ASSERT(handle == &fs_event);
85
+ ASSERT(status == 0);
86
+ ASSERT(events == UV_RENAME);
87
+ ASSERT(strcmp(filename, "file1") == 0);
88
+ uv_close((uv_handle_t*)handle, close_cb);
89
+ }
90
+
91
+ static void fs_event_cb_file(uv_fs_event_t* handle, const char* filename,
92
+ int events, int status) {
93
+ ++fs_event_cb_called;
94
+ ASSERT(handle == &fs_event);
95
+ ASSERT(status == 0);
96
+ ASSERT(events == UV_CHANGE);
97
+ ASSERT(strcmp(filename, "file2") == 0);
98
+ uv_close((uv_handle_t*)handle, close_cb);
99
+ }
100
+
101
+ static void fs_event_cb_file_current_dir(uv_fs_event_t* handle,
102
+ const char* filename, int events, int status) {
103
+ ++fs_event_cb_called;
104
+ ASSERT(handle == &fs_event);
105
+ ASSERT(status == 0);
106
+ ASSERT(events == UV_CHANGE);
107
+ ASSERT(strcmp(filename, "watch_file") == 0);
108
+ uv_close((uv_handle_t*)handle, close_cb);
109
+ }
110
+
111
+ static void timer_cb_dir(uv_timer_t* handle, int status) {
112
+ ++timer_cb_called;
113
+ create_file(handle->loop, "watch_dir/file1");
114
+ uv_close((uv_handle_t*)handle, close_cb);
115
+ }
116
+
117
+ static void timer_cb_file(uv_timer_t* handle, int status) {
118
+ ++timer_cb_called;
119
+
120
+ if (timer_cb_called == 1) {
121
+ touch_file(handle->loop, "watch_dir/file1");
122
+ } else {
123
+ touch_file(handle->loop, "watch_dir/file2");
124
+ uv_close((uv_handle_t*)handle, close_cb);
125
+ }
126
+ }
127
+
128
+ TEST_IMPL(fs_event_watch_dir) {
129
+ uv_fs_t fs_req;
130
+ uv_loop_t* loop = uv_default_loop();
131
+ int r;
132
+
133
+ /* Setup */
134
+ uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL);
135
+ uv_fs_unlink(loop, &fs_req, "watch_dir/file2", NULL);
136
+ uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL);
137
+ create_dir(loop, "watch_dir");
138
+
139
+ r = uv_fs_event_init(loop, &fs_event, "watch_dir", fs_event_cb_dir);
140
+ ASSERT(r != -1);
141
+ r = uv_timer_init(loop, &timer);
142
+ ASSERT(r != -1);
143
+ r = uv_timer_start(&timer, timer_cb_dir, 100, 0);
144
+ ASSERT(r != -1);
145
+
146
+ uv_run(loop);
147
+
148
+ ASSERT(fs_event_cb_called == 1);
149
+ ASSERT(timer_cb_called == 1);
150
+ ASSERT(close_cb_called == 2);
151
+
152
+ /* Cleanup */
153
+ r = uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL);
154
+ r = uv_fs_unlink(loop, &fs_req, "watch_dir/file2", NULL);
155
+ r = uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL);
156
+
157
+ return 0;
158
+ }
159
+
160
+ TEST_IMPL(fs_event_watch_file) {
161
+ uv_fs_t fs_req;
162
+ uv_loop_t* loop = uv_default_loop();
163
+ int r;
164
+
165
+ /* Setup */
166
+ uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL);
167
+ uv_fs_unlink(loop, &fs_req, "watch_dir/file2", NULL);
168
+ uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL);
169
+ create_dir(loop, "watch_dir");
170
+ create_file(loop, "watch_dir/file1");
171
+ create_file(loop, "watch_dir/file2");
172
+
173
+ r = uv_fs_event_init(loop, &fs_event, "watch_dir/file2", fs_event_cb_file);
174
+ ASSERT(r != -1);
175
+ r = uv_timer_init(loop, &timer);
176
+ ASSERT(r != -1);
177
+ r = uv_timer_start(&timer, timer_cb_file, 100, 100);
178
+ ASSERT(r != -1);
179
+
180
+ uv_run(loop);
181
+
182
+ ASSERT(fs_event_cb_called == 1);
183
+ ASSERT(timer_cb_called == 2);
184
+ ASSERT(close_cb_called == 2);
185
+
186
+ /* Cleanup */
187
+ r = uv_fs_unlink(loop, &fs_req, "watch_dir/file1", NULL);
188
+ r = uv_fs_unlink(loop, &fs_req, "watch_dir/file2", NULL);
189
+ r = uv_fs_rmdir(loop, &fs_req, "watch_dir", NULL);
190
+
191
+ return 0;
192
+ }
193
+
194
+ TEST_IMPL(fs_event_watch_file_current_dir) {
195
+ uv_fs_t fs_req;
196
+ uv_loop_t* loop = uv_default_loop();
197
+ int r;
198
+
199
+ /* Setup */
200
+ uv_fs_unlink(loop, &fs_req, "watch_file", NULL);
201
+ create_file(loop, "watch_file");
202
+
203
+ r = uv_fs_event_init(loop, &fs_event, "watch_file",
204
+ fs_event_cb_file_current_dir);
205
+ ASSERT(r != -1);
206
+
207
+ touch_file(loop, "watch_file");
208
+
209
+ uv_run(loop);
210
+
211
+ ASSERT(fs_event_cb_called == 1);
212
+ ASSERT(close_cb_called == 1);
213
+
214
+ /* Cleanup */
215
+ r = uv_fs_unlink(loop, &fs_req, "watch_file", NULL);
216
+ return 0;
217
+ }
@@ -424,14 +424,10 @@ static void check_utime(const char* path, double atime, double mtime) {
424
424
  ASSERT(s->st_mtime == mtime);
425
425
  #elif !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
426
426
  ASSERT(s->st_atimespec.tv_sec == atime);
427
- ASSERT(s->st_atimespec.tv_nsec == 0); /* FIXME check sub-second precision */
428
427
  ASSERT(s->st_mtimespec.tv_sec == mtime);
429
- ASSERT(s->st_mtimespec.tv_nsec == 0); /* FIXME check sub-second precision */
430
428
  #else
431
429
  ASSERT(s->st_atim.tv_sec == atime);
432
- ASSERT(s->st_atim.tv_nsec == 0); /* FIXME check sub-second precision */
433
430
  ASSERT(s->st_mtim.tv_sec == mtime);
434
- ASSERT(s->st_mtim.tv_nsec == 0); /* FIXME check sub-second precision */
435
431
  #endif
436
432
 
437
433
  uv_fs_req_cleanup(&req);
@@ -31,10 +31,10 @@
31
31
 
32
32
  static const char* name = "localhost";
33
33
 
34
- static uv_getaddrinfo_t getaddrinfo_handle;
35
34
  static int getaddrinfo_cbs = 0;
36
35
 
37
36
  /* data used for running multiple calls concurrently */
37
+ static uv_getaddrinfo_t* getaddrinfo_handle;
38
38
  static uv_getaddrinfo_t getaddrinfo_handles[CONCURRENT_COUNT];
39
39
  static int callback_counts[CONCURRENT_COUNT];
40
40
 
@@ -42,8 +42,10 @@ static int callback_counts[CONCURRENT_COUNT];
42
42
  static void getaddrinfo_basic_cb(uv_getaddrinfo_t* handle,
43
43
  int status,
44
44
  struct addrinfo* res) {
45
- ASSERT(handle == &getaddrinfo_handle);
45
+ ASSERT(handle == getaddrinfo_handle);
46
46
  getaddrinfo_cbs++;
47
+ free(handle);
48
+ uv_freeaddrinfo(res);
47
49
  }
48
50
 
49
51
 
@@ -64,6 +66,7 @@ static void getaddrinfo_cuncurrent_cb(uv_getaddrinfo_t* handle,
64
66
  ASSERT (i < CONCURRENT_COUNT);
65
67
 
66
68
  free(data);
69
+ uv_freeaddrinfo(res);
67
70
 
68
71
  getaddrinfo_cbs++;
69
72
  }
@@ -71,9 +74,10 @@ static void getaddrinfo_cuncurrent_cb(uv_getaddrinfo_t* handle,
71
74
 
72
75
  TEST_IMPL(getaddrinfo_basic) {
73
76
  int r;
77
+ getaddrinfo_handle = (uv_getaddrinfo_t*)malloc(sizeof(uv_getaddrinfo_t));
74
78
 
75
79
  r = uv_getaddrinfo(uv_default_loop(),
76
- &getaddrinfo_handle,
80
+ getaddrinfo_handle,
77
81
  &getaddrinfo_basic_cb,
78
82
  name,
79
83
  NULL,
@@ -19,8 +19,11 @@
19
19
  * IN THE SOFTWARE.
20
20
  */
21
21
 
22
+ TEST_DECLARE (tty)
22
23
  TEST_DECLARE (tcp_ping_pong)
23
24
  TEST_DECLARE (tcp_ping_pong_v6)
25
+ TEST_DECLARE (tcp_ref)
26
+ TEST_DECLARE (tcp_ref2)
24
27
  TEST_DECLARE (pipe_ping_pong)
25
28
  TEST_DECLARE (delayed_accept)
26
29
  TEST_DECLARE (tcp_writealot)
@@ -32,6 +35,7 @@ TEST_DECLARE (tcp_bind_error_inval)
32
35
  TEST_DECLARE (tcp_bind_localhost_ok)
33
36
  TEST_DECLARE (tcp_listen_without_bind)
34
37
  TEST_DECLARE (tcp_close)
38
+ TEST_DECLARE (tcp_write_error)
35
39
  TEST_DECLARE (tcp_bind6_error_addrinuse)
36
40
  TEST_DECLARE (tcp_bind6_error_addrnotavail)
37
41
  TEST_DECLARE (tcp_bind6_error_fault)
@@ -50,6 +54,8 @@ TEST_DECLARE (connection_fail_doesnt_auto_close)
50
54
  TEST_DECLARE (shutdown_eof)
51
55
  TEST_DECLARE (callback_stack)
52
56
  TEST_DECLARE (timer)
57
+ TEST_DECLARE (timer_ref)
58
+ TEST_DECLARE (timer_ref2)
53
59
  TEST_DECLARE (timer_again)
54
60
  TEST_DECLARE (idle_starvation)
55
61
  TEST_DECLARE (loop_handles)
@@ -85,6 +91,9 @@ TEST_DECLARE (fs_link)
85
91
  TEST_DECLARE (fs_symlink)
86
92
  TEST_DECLARE (fs_utime)
87
93
  TEST_DECLARE (fs_futime)
94
+ TEST_DECLARE (fs_event_watch_dir)
95
+ TEST_DECLARE (fs_event_watch_file)
96
+ TEST_DECLARE (fs_event_watch_file_current_dir)
88
97
  TEST_DECLARE (threadpool_queue_work_simple)
89
98
  #ifdef _WIN32
90
99
  TEST_DECLARE (spawn_detect_pipe_name_collisions_on_windows)
@@ -97,6 +106,14 @@ HELPER_DECLARE (pipe_echo_server)
97
106
 
98
107
 
99
108
  TASK_LIST_START
109
+ TEST_ENTRY (tty)
110
+
111
+
112
+ TEST_ENTRY (tcp_ref)
113
+
114
+ TEST_ENTRY (tcp_ref2)
115
+ TEST_HELPER (tcp_ref2, tcp4_echo_server)
116
+
100
117
  TEST_ENTRY (tcp_ping_pong)
101
118
  TEST_HELPER (tcp_ping_pong, tcp4_echo_server)
102
119
 
@@ -119,6 +136,7 @@ TASK_LIST_START
119
136
  TEST_ENTRY (tcp_bind_localhost_ok)
120
137
  TEST_ENTRY (tcp_listen_without_bind)
121
138
  TEST_ENTRY (tcp_close)
139
+ TEST_ENTRY (tcp_write_error)
122
140
 
123
141
  TEST_ENTRY (tcp_bind6_error_addrinuse)
124
142
  TEST_ENTRY (tcp_bind6_error_addrnotavail)
@@ -146,6 +164,8 @@ TASK_LIST_START
146
164
  TEST_HELPER (callback_stack, tcp4_echo_server)
147
165
 
148
166
  TEST_ENTRY (timer)
167
+ TEST_ENTRY (timer_ref)
168
+ TEST_ENTRY (timer_ref2)
149
169
 
150
170
  TEST_ENTRY (timer_again)
151
171
 
@@ -196,6 +216,9 @@ TASK_LIST_START
196
216
  TEST_ENTRY (fs_utime)
197
217
  TEST_ENTRY (fs_futime)
198
218
  TEST_ENTRY (fs_symlink)
219
+ TEST_ENTRY (fs_event_watch_dir)
220
+ TEST_ENTRY (fs_event_watch_file)
221
+ TEST_ENTRY (fs_event_watch_file_current_dir)
199
222
 
200
223
  TEST_ENTRY (threadpool_queue_work_simple)
201
224
 
@@ -127,3 +127,50 @@ TEST_IMPL(tcp_close) {
127
127
 
128
128
  return 0;
129
129
  }
130
+
131
+
132
+ TEST_IMPL(tcp_ref) {
133
+ uv_tcp_t never;
134
+ int r;
135
+
136
+ /* A tcp just initialized should count as one reference. */
137
+ r = uv_tcp_init(uv_default_loop(), &never);
138
+ ASSERT(r == 0);
139
+
140
+ /* One unref should set the loop ref count to zero. */
141
+ uv_unref(uv_default_loop());
142
+
143
+ /* Therefore this does not block */
144
+ uv_run(uv_default_loop());
145
+
146
+ return 0;
147
+ }
148
+
149
+
150
+ static void never_cb(uv_connect_t* conn_req, int status) {
151
+ FATAL("never_cb should never be called");
152
+ }
153
+
154
+
155
+ TEST_IMPL(tcp_ref2) {
156
+ uv_tcp_t never;
157
+ int r;
158
+
159
+ /* A tcp just initialized should count as one reference. */
160
+ r = uv_tcp_init(uv_default_loop(), &never);
161
+ ASSERT(r == 0);
162
+
163
+ r = uv_tcp_connect(&connect_req,
164
+ &never,
165
+ uv_ip4_addr("127.0.0.1", TEST_PORT),
166
+ never_cb);
167
+ ASSERT(r == 0);
168
+
169
+ /* One unref should set the loop ref count to zero. */
170
+ uv_unref(uv_default_loop());
171
+
172
+ /* Therefore this does not block */
173
+ uv_run(uv_default_loop());
174
+
175
+ return 0;
176
+ }
@@ -0,0 +1,168 @@
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 <stdio.h>
26
+ #include <stdlib.h>
27
+ #include <string.h>
28
+
29
+ static void connection_cb(uv_stream_t* server, int status);
30
+ static void connect_cb(uv_connect_t* req, int status);
31
+ static void write_cb(uv_write_t* req, int status);
32
+ static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf);
33
+ static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size);
34
+
35
+ static uv_tcp_t tcp_server;
36
+ static uv_tcp_t tcp_client;
37
+ static uv_tcp_t tcp_peer; /* client socket as accept()-ed by server */
38
+ static uv_connect_t connect_req;
39
+
40
+ static int write_cb_called;
41
+ static int write_cb_error_called;
42
+
43
+ typedef struct {
44
+ uv_write_t req;
45
+ uv_buf_t buf;
46
+ } write_req_t;
47
+
48
+
49
+ static void connection_cb(uv_stream_t* server, int status) {
50
+ int r;
51
+
52
+ ASSERT(server == (uv_stream_t*)&tcp_server);
53
+ ASSERT(status == 0);
54
+
55
+ r = uv_tcp_init(server->loop, &tcp_peer);
56
+ ASSERT(r == 0);
57
+
58
+ r = uv_accept(server, (uv_stream_t*)&tcp_peer);
59
+ ASSERT(r == 0);
60
+
61
+ r = uv_read_start((uv_stream_t*)&tcp_peer, alloc_cb, read_cb);
62
+ ASSERT(r == 0);
63
+ }
64
+
65
+
66
+ static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
67
+ static char slab[1024];
68
+ return uv_buf_init(slab, sizeof slab);
69
+ }
70
+
71
+
72
+ static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) {
73
+ uv_close((uv_handle_t*)&tcp_server, NULL);
74
+ uv_close((uv_handle_t*)&tcp_peer, NULL);
75
+ }
76
+
77
+
78
+ static void connect_cb(uv_connect_t* req, int status) {
79
+ uv_buf_t buf;
80
+ size_t size;
81
+ char* data;
82
+ int r;
83
+ write_req_t* wr;
84
+
85
+ ASSERT(req == &connect_req);
86
+ ASSERT(status == 0);
87
+
88
+ while (1) {
89
+ size = 10 * 1024 * 1024;
90
+ data = malloc(size);
91
+ ASSERT(data != NULL);
92
+
93
+ memset(data, '$', size);
94
+ buf = uv_buf_init(data, size);
95
+
96
+ wr = (write_req_t*) malloc(sizeof *wr);
97
+ wr->buf = buf;
98
+ wr->req.data = data;
99
+
100
+ r = uv_write(&(wr->req), req->handle, &wr->buf, 1, write_cb);
101
+ ASSERT(r == 0);
102
+
103
+ if (req->handle->write_queue_size > 0) {
104
+ break;
105
+ }
106
+ }
107
+ }
108
+
109
+
110
+ static void write_cb(uv_write_t* req, int status) {
111
+ write_req_t* wr;
112
+ wr = (write_req_t*) req;
113
+
114
+ if (status == -1) {
115
+ write_cb_error_called++;
116
+ }
117
+
118
+ if (req->handle->write_queue_size == 0) {
119
+ uv_close((uv_handle_t*)&tcp_client, NULL);
120
+ }
121
+
122
+ free(wr->buf.base);
123
+ free(wr);
124
+
125
+ write_cb_called++;
126
+ }
127
+
128
+
129
+ /*
130
+ * Assert that a failing write does not leave
131
+ * the stream's write_queue_size in an inconsistent state.
132
+ */
133
+ TEST_IMPL(tcp_write_error) {
134
+ uv_loop_t* loop;
135
+ int r;
136
+
137
+ loop = uv_default_loop();
138
+ ASSERT(loop != NULL);
139
+
140
+ r = uv_tcp_init(loop, &tcp_server);
141
+ ASSERT(r == 0);
142
+
143
+ r = uv_tcp_bind(&tcp_server, uv_ip4_addr("127.0.0.1", TEST_PORT));
144
+ ASSERT(r == 0);
145
+
146
+ r = uv_listen((uv_stream_t*)&tcp_server, 1, connection_cb);
147
+ ASSERT(r == 0);
148
+
149
+ r = uv_tcp_init(loop, &tcp_client);
150
+ ASSERT(r == 0);
151
+
152
+ r = uv_tcp_connect(&connect_req,
153
+ &tcp_client,
154
+ uv_ip4_addr("127.0.0.1", TEST_PORT),
155
+ connect_cb);
156
+ ASSERT(r == 0);
157
+
158
+ ASSERT(write_cb_called == 0);
159
+
160
+ r = uv_run(loop);
161
+ ASSERT(r == 0);
162
+
163
+ ASSERT(write_cb_called > 0);
164
+ ASSERT(write_cb_error_called == 1);
165
+ ASSERT(tcp_client.write_queue_size == 0);
166
+
167
+ return 0;
168
+ }
@@ -130,3 +130,43 @@ TEST_IMPL(timer) {
130
130
 
131
131
  return 0;
132
132
  }
133
+
134
+
135
+ TEST_IMPL(timer_ref) {
136
+ uv_timer_t never;
137
+ int r;
138
+
139
+ /* A timer just initialized should count as one reference. */
140
+ r = uv_timer_init(uv_default_loop(), &never);
141
+ ASSERT(r == 0);
142
+
143
+ /* One unref should set the loop ref count to zero. */
144
+ uv_unref(uv_default_loop());
145
+
146
+ /* Therefore this does not block */
147
+ uv_run(uv_default_loop());
148
+
149
+ return 0;
150
+ }
151
+
152
+
153
+ TEST_IMPL(timer_ref2) {
154
+ uv_timer_t never;
155
+ int r;
156
+
157
+ /* A timer just initialized should count as one reference. */
158
+ r = uv_timer_init(uv_default_loop(), &never);
159
+ ASSERT(r == 0);
160
+
161
+ /* We start the timer, this should not effect the ref count. */
162
+ r = uv_timer_start(&never, never_cb, 1000, 1000);
163
+ ASSERT(r == 0);
164
+
165
+ /* One unref should set the loop ref count to zero. */
166
+ uv_unref(uv_default_loop());
167
+
168
+ /* Therefore this does not block */
169
+ uv_run(uv_default_loop());
170
+
171
+ return 0;
172
+ }
@@ -0,0 +1,56 @@
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
+ TEST_IMPL(tty) {
26
+ int r, width, height;
27
+ uv_tty_t tty;
28
+ uv_loop_t* loop = uv_default_loop();
29
+
30
+ /*
31
+ * Not necessarally a problem if this assert goes off. E.G you are piping
32
+ * this test to a file. 0 == stdin.
33
+ */
34
+ ASSERT(UV_TTY == uv_guess_handle(0));
35
+
36
+ r = uv_tty_init(uv_default_loop(), &tty, 0);
37
+ ASSERT(r == 0);
38
+
39
+ r = uv_tty_get_winsize(&tty, &width, &height);
40
+ ASSERT(r == 0);
41
+
42
+ printf("width=%d height=%d\n", width, height);
43
+
44
+ /*
45
+ * Is it a safe assumption that most people have terminals larger than
46
+ * 10x10?
47
+ */
48
+ ASSERT(width > 10);
49
+ ASSERT(height > 10);
50
+
51
+ uv_close((uv_handle_t*)&tty, NULL);
52
+
53
+ uv_run(loop);
54
+
55
+ return 0;
56
+ }