libuv 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -37,14 +37,15 @@
37
37
 
38
38
  static uv_fs_event_t fs_event;
39
39
  static const char file_prefix[] = "fsevent-";
40
+ static const int fs_event_file_count = 16;
40
41
  #if defined(__APPLE__) || defined(_WIN32)
41
42
  static const char file_prefix_in_subdir[] = "subdir";
42
43
  #endif
43
44
  static uv_timer_t timer;
44
45
  static int timer_cb_called;
45
46
  static int close_cb_called;
46
- static const int fs_event_file_count = 128;
47
47
  static int fs_event_created;
48
+ static int fs_event_removed;
48
49
  static int fs_event_cb_called;
49
50
  #if defined(PATH_MAX)
50
51
  static char fs_event_filename[PATH_MAX];
@@ -53,49 +54,45 @@ static char fs_event_filename[1024];
53
54
  #endif /* defined(PATH_MAX) */
54
55
  static int timer_cb_touch_called;
55
56
 
56
- static void fs_event_unlink_files(uv_timer_t* handle);
57
- static void fs_event_unlink_files_in_subdir(uv_timer_t* handle);
58
-
59
- static void create_dir(uv_loop_t* loop, const char* name) {
57
+ static void create_dir(const char* name) {
60
58
  int r;
61
59
  uv_fs_t req;
62
- r = uv_fs_mkdir(loop, &req, name, 0755, NULL);
60
+ r = uv_fs_mkdir(NULL, &req, name, 0755, NULL);
63
61
  ASSERT(r == 0 || r == UV_EEXIST);
64
62
  uv_fs_req_cleanup(&req);
65
63
  }
66
64
 
67
- static void create_file(uv_loop_t* loop, const char* name) {
65
+ static void create_file(const char* name) {
68
66
  int r;
69
67
  uv_file file;
70
68
  uv_fs_t req;
71
69
 
72
- r = uv_fs_open(loop, &req, name, O_WRONLY | O_CREAT,
73
- S_IWUSR | S_IRUSR, NULL);
70
+ r = uv_fs_open(NULL, &req, name, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR, NULL);
74
71
  ASSERT(r >= 0);
75
72
  file = r;
76
73
  uv_fs_req_cleanup(&req);
77
- r = uv_fs_close(loop, &req, file, NULL);
74
+ r = uv_fs_close(NULL, &req, file, NULL);
78
75
  ASSERT(r == 0);
79
76
  uv_fs_req_cleanup(&req);
80
77
  }
81
78
 
82
- static void touch_file(uv_loop_t* loop, const char* name) {
79
+ static void touch_file(const char* name) {
83
80
  int r;
84
81
  uv_file file;
85
82
  uv_fs_t req;
86
83
  uv_buf_t buf;
87
84
 
88
- r = uv_fs_open(loop, &req, name, O_RDWR, 0, NULL);
85
+ r = uv_fs_open(NULL, &req, name, O_RDWR, 0, NULL);
89
86
  ASSERT(r >= 0);
90
87
  file = r;
91
88
  uv_fs_req_cleanup(&req);
92
89
 
93
90
  buf = uv_buf_init("foo", 4);
94
- r = uv_fs_write(loop, &req, file, &buf, 1, -1, NULL);
91
+ r = uv_fs_write(NULL, &req, file, &buf, 1, -1, NULL);
95
92
  ASSERT(r >= 0);
96
93
  uv_fs_req_cleanup(&req);
97
94
 
98
- r = uv_fs_close(loop, &req, file, NULL);
95
+ r = uv_fs_close(NULL, &req, file, NULL);
99
96
  ASSERT(r == 0);
100
97
  uv_fs_req_cleanup(&req);
101
98
  }
@@ -123,6 +120,56 @@ static void fs_event_cb_dir(uv_fs_event_t* handle, const char* filename,
123
120
  uv_close((uv_handle_t*)handle, close_cb);
124
121
  }
125
122
 
123
+ static const char* fs_event_get_filename(int i) {
124
+ snprintf(fs_event_filename,
125
+ sizeof(fs_event_filename),
126
+ "watch_dir/%s%d",
127
+ file_prefix,
128
+ i);
129
+ return fs_event_filename;
130
+ }
131
+
132
+ static void fs_event_create_files(uv_timer_t* handle) {
133
+ /* Make sure we're not attempting to create files we do not intend */
134
+ ASSERT(fs_event_created < fs_event_file_count);
135
+
136
+ /* Create the file */
137
+ create_file(fs_event_get_filename(fs_event_created));
138
+
139
+ if (++fs_event_created < fs_event_file_count) {
140
+ /* Create another file on a different event loop tick. We do it this way
141
+ * to avoid fs events coalescing into one fs event. */
142
+ ASSERT(0 == uv_timer_start(&timer, fs_event_create_files, 1, 0));
143
+ }
144
+ }
145
+
146
+ static void fs_event_unlink_files(uv_timer_t* handle) {
147
+ int r;
148
+ int i;
149
+
150
+ /* NOTE: handle might be NULL if invoked not as timer callback */
151
+ if (handle == NULL) {
152
+ /* Unlink all files */
153
+ for (i = 0; i < 16; i++) {
154
+ r = remove(fs_event_get_filename(i));
155
+ if (handle != NULL)
156
+ ASSERT(r == 0);
157
+ }
158
+ } else {
159
+ /* Make sure we're not attempting to remove files we do not intend */
160
+ ASSERT(fs_event_removed < fs_event_file_count);
161
+
162
+ /* Remove the file */
163
+ ASSERT(0 == remove(fs_event_get_filename(fs_event_removed)));
164
+
165
+ if (++fs_event_removed < fs_event_file_count) {
166
+ /* Remove another file on a different event loop tick. We do it this way
167
+ * to avoid fs events coalescing into one fs event. */
168
+ ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files, 1, 0));
169
+ }
170
+ }
171
+ }
172
+
126
173
  static void fs_event_cb_dir_multi_file(uv_fs_event_t* handle,
127
174
  const char* filename,
128
175
  int events,
@@ -130,48 +177,21 @@ static void fs_event_cb_dir_multi_file(uv_fs_event_t* handle,
130
177
  fs_event_cb_called++;
131
178
  ASSERT(handle == &fs_event);
132
179
  ASSERT(status == 0);
133
- ASSERT(events == UV_RENAME);
180
+ ASSERT(events == UV_CHANGE || UV_RENAME);
134
181
  ASSERT(filename == NULL ||
135
182
  strncmp(filename, file_prefix, sizeof(file_prefix) - 1) == 0);
136
183
 
137
- /* Stop watching dir when received events about all files:
138
- * both create and close events */
139
- if (fs_event_cb_called == 2 * fs_event_file_count) {
140
- ASSERT(0 == uv_fs_event_stop(handle));
184
+ if (fs_event_created + fs_event_removed == fs_event_file_count) {
185
+ /* Once we've processed all create events, delete all files */
186
+ ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files, 1, 0));
187
+ } else if (fs_event_cb_called == 2 * fs_event_file_count) {
188
+ /* Once we've processed all create and delete events, stop watching */
189
+ uv_close((uv_handle_t*) &timer, close_cb);
141
190
  uv_close((uv_handle_t*) handle, close_cb);
142
191
  }
143
192
  }
144
193
 
145
194
  #if defined(__APPLE__) || defined(_WIN32)
146
- static void fs_event_cb_dir_multi_file_in_subdir(uv_fs_event_t* handle,
147
- const char* filename,
148
- int events,
149
- int status) {
150
- fs_event_cb_called++;
151
- ASSERT(handle == &fs_event);
152
- ASSERT(status == 0);
153
- ASSERT(events == UV_RENAME || events == UV_CHANGE);
154
- ASSERT(filename == NULL ||
155
- strncmp(filename, file_prefix_in_subdir, sizeof(file_prefix_in_subdir) - 1) == 0);
156
-
157
- /* Stop watching dir when received events about all files:
158
- * both create and close events */
159
- if (fs_event_cb_called == 2 * fs_event_file_count) {
160
- ASSERT(0 == uv_fs_event_stop(handle));
161
- uv_close((uv_handle_t*) handle, close_cb);
162
- }
163
- }
164
- #endif
165
-
166
- static const char* fs_event_get_filename(int i) {
167
- snprintf(fs_event_filename,
168
- sizeof(fs_event_filename),
169
- "watch_dir/%s%d",
170
- file_prefix,
171
- i);
172
- return fs_event_filename;
173
- }
174
-
175
195
  static const char* fs_event_get_filename_in_subdir(int i) {
176
196
  snprintf(fs_event_filename,
177
197
  sizeof(fs_event_filename),
@@ -181,75 +201,68 @@ static const char* fs_event_get_filename_in_subdir(int i) {
181
201
  return fs_event_filename;
182
202
  }
183
203
 
184
- static void fs_event_create_files(uv_timer_t* handle) {
185
- int i;
186
-
187
- /* Already created all files */
188
- if (fs_event_created == fs_event_file_count) {
189
- uv_close((uv_handle_t*) &timer, close_cb);
190
- return;
191
- }
192
-
193
- /* Create all files */
194
- for (i = 0; i < 16; i++, fs_event_created++)
195
- create_file(handle->loop, fs_event_get_filename(i));
196
-
197
- /* And unlink them */
198
- ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files, 50, 0));
199
- }
200
-
201
204
  static void fs_event_create_files_in_subdir(uv_timer_t* handle) {
202
- int i;
203
-
204
- /* Already created all files */
205
- if (fs_event_created == fs_event_file_count) {
206
- uv_close((uv_handle_t*) &timer, close_cb);
207
- return;
208
- }
205
+ /* Make sure we're not attempting to create files we do not intend */
206
+ ASSERT(fs_event_created < fs_event_file_count);
209
207
 
210
- /* Create all files */
211
- for (i = 0; i < 16; i++, fs_event_created++)
212
- create_file(handle->loop, fs_event_get_filename_in_subdir(i));
208
+ /* Create the file */
209
+ create_file(fs_event_get_filename_in_subdir(fs_event_created));
213
210
 
214
- /* And unlink them */
215
- ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files_in_subdir, 50, 0));
211
+ if (++fs_event_created < fs_event_file_count) {
212
+ /* Create another file on a different event loop tick. We do it this way
213
+ * to avoid fs events coalescing into one fs event. */
214
+ ASSERT(0 == uv_timer_start(&timer, fs_event_create_files_in_subdir, 1, 0));
215
+ }
216
216
  }
217
217
 
218
- void fs_event_unlink_files_in_subdir(uv_timer_t* handle) {
218
+ static void fs_event_unlink_files_in_subdir(uv_timer_t* handle) {
219
219
  int r;
220
220
  int i;
221
221
 
222
222
  /* NOTE: handle might be NULL if invoked not as timer callback */
223
+ if (handle == NULL) {
224
+ /* Unlink all files */
225
+ for (i = 0; i < 16; i++) {
226
+ r = remove(fs_event_get_filename_in_subdir(i));
227
+ if (handle != NULL)
228
+ ASSERT(r == 0);
229
+ }
230
+ } else {
231
+ /* Make sure we're not attempting to remove files we do not intend */
232
+ ASSERT(fs_event_removed < fs_event_file_count);
223
233
 
224
- /* Unlink all files */
225
- for (i = 0; i < 16; i++) {
226
- r = remove(fs_event_get_filename_in_subdir(i));
227
- if (handle != NULL)
228
- ASSERT(r == 0);
229
- }
234
+ /* Remove the file */
235
+ ASSERT(0 == remove(fs_event_get_filename_in_subdir(fs_event_removed)));
230
236
 
231
- /* And create them again */
232
- if (handle != NULL)
233
- ASSERT(0 == uv_timer_start(&timer, fs_event_create_files_in_subdir, 50, 0));
237
+ if (++fs_event_removed < fs_event_file_count) {
238
+ /* Remove another file on a different event loop tick. We do it this way
239
+ * to avoid fs events coalescing into one fs event. */
240
+ ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files_in_subdir, 1, 0));
241
+ }
242
+ }
234
243
  }
235
244
 
236
- void fs_event_unlink_files(uv_timer_t* handle) {
237
- int r;
238
- int i;
239
-
240
- /* NOTE: handle might be NULL if invoked not as timer callback */
245
+ static void fs_event_cb_dir_multi_file_in_subdir(uv_fs_event_t* handle,
246
+ const char* filename,
247
+ int events,
248
+ int status) {
249
+ fs_event_cb_called++;
250
+ ASSERT(handle == &fs_event);
251
+ ASSERT(status == 0);
252
+ ASSERT(events == UV_CHANGE || UV_RENAME);
253
+ ASSERT(filename == NULL ||
254
+ strncmp(filename, file_prefix_in_subdir, sizeof(file_prefix_in_subdir) - 1) == 0);
241
255
 
242
- /* Unlink all files */
243
- for (i = 0; i < 16; i++) {
244
- r = remove(fs_event_get_filename(i));
245
- if (handle != NULL)
246
- ASSERT(r == 0);
256
+ if (fs_event_created + fs_event_removed == fs_event_file_count) {
257
+ /* Once we've processed all create events, delete all files */
258
+ ASSERT(0 == uv_timer_start(&timer, fs_event_unlink_files_in_subdir, 1, 0));
259
+ } else if (fs_event_cb_called == 2 * fs_event_file_count) {
260
+ /* Once we've processed all create and delete events, stop watching */
261
+ uv_close((uv_handle_t*) &timer, close_cb);
262
+ uv_close((uv_handle_t*) handle, close_cb);
247
263
  }
248
-
249
- /* And create them again */
250
- if (handle != NULL)
251
- ASSERT(0 == uv_timer_start(&timer, fs_event_create_files, 50, 0));
252
264
  }
265
+ #endif
253
266
 
254
267
  static void fs_event_cb_file(uv_fs_event_t* handle, const char* filename,
255
268
  int events, int status) {
@@ -295,16 +308,16 @@ static void timer_cb_file(uv_timer_t* handle) {
295
308
  ++timer_cb_called;
296
309
 
297
310
  if (timer_cb_called == 1) {
298
- touch_file(handle->loop, "watch_dir/file1");
311
+ touch_file("watch_dir/file1");
299
312
  } else {
300
- touch_file(handle->loop, "watch_dir/file2");
313
+ touch_file("watch_dir/file2");
301
314
  uv_close((uv_handle_t*)handle, close_cb);
302
315
  }
303
316
  }
304
317
 
305
318
  static void timer_cb_touch(uv_timer_t* timer) {
306
319
  uv_close((uv_handle_t*)timer, NULL);
307
- touch_file(timer->loop, "watch_file");
320
+ touch_file("watch_file");
308
321
  timer_cb_touch_called++;
309
322
  }
310
323
 
@@ -324,7 +337,7 @@ TEST_IMPL(fs_event_watch_dir) {
324
337
  remove("watch_dir/file2");
325
338
  remove("watch_dir/file1");
326
339
  remove("watch_dir/");
327
- create_dir(loop, "watch_dir");
340
+ create_dir("watch_dir");
328
341
 
329
342
  r = uv_fs_event_init(loop, &fs_event);
330
343
  ASSERT(r == 0);
@@ -337,8 +350,7 @@ TEST_IMPL(fs_event_watch_dir) {
337
350
 
338
351
  uv_run(loop, UV_RUN_DEFAULT);
339
352
 
340
- ASSERT(fs_event_cb_called == 2 * fs_event_file_count);
341
- ASSERT(fs_event_created == fs_event_file_count);
353
+ ASSERT(fs_event_cb_called == fs_event_created + fs_event_removed);
342
354
  ASSERT(close_cb_called == 2);
343
355
 
344
356
  /* Cleanup */
@@ -363,8 +375,8 @@ TEST_IMPL(fs_event_watch_dir_recursive) {
363
375
  remove("watch_dir/file1");
364
376
  remove("watch_dir/subdir");
365
377
  remove("watch_dir/");
366
- create_dir(loop, "watch_dir");
367
- create_dir(loop, "watch_dir/subdir");
378
+ create_dir("watch_dir");
379
+ create_dir("watch_dir/subdir");
368
380
 
369
381
  r = uv_fs_event_init(loop, &fs_event);
370
382
  ASSERT(r == 0);
@@ -377,8 +389,7 @@ TEST_IMPL(fs_event_watch_dir_recursive) {
377
389
 
378
390
  uv_run(loop, UV_RUN_DEFAULT);
379
391
 
380
- ASSERT(fs_event_cb_called == 2 * fs_event_file_count);
381
- ASSERT(fs_event_created == fs_event_file_count);
392
+ ASSERT(fs_event_cb_called == fs_event_created + fs_event_removed);
382
393
  ASSERT(close_cb_called == 2);
383
394
 
384
395
  /* Cleanup */
@@ -404,9 +415,9 @@ TEST_IMPL(fs_event_watch_file) {
404
415
  remove("watch_dir/file2");
405
416
  remove("watch_dir/file1");
406
417
  remove("watch_dir/");
407
- create_dir(loop, "watch_dir");
408
- create_file(loop, "watch_dir/file1");
409
- create_file(loop, "watch_dir/file2");
418
+ create_dir("watch_dir");
419
+ create_file("watch_dir/file1");
420
+ create_file("watch_dir/file2");
410
421
 
411
422
  r = uv_fs_event_init(loop, &fs_event);
412
423
  ASSERT(r == 0);
@@ -462,7 +473,7 @@ TEST_IMPL(fs_event_watch_file_current_dir) {
462
473
 
463
474
  /* Setup */
464
475
  remove("watch_file");
465
- create_file(loop, "watch_file");
476
+ create_file("watch_file");
466
477
 
467
478
  r = uv_fs_event_init(loop, &fs_event);
468
479
  ASSERT(r == 0);
@@ -503,8 +514,8 @@ TEST_IMPL(fs_event_no_callback_after_close) {
503
514
  /* Setup */
504
515
  remove("watch_dir/file1");
505
516
  remove("watch_dir/");
506
- create_dir(loop, "watch_dir");
507
- create_file(loop, "watch_dir/file1");
517
+ create_dir("watch_dir");
518
+ create_file("watch_dir/file1");
508
519
 
509
520
  r = uv_fs_event_init(loop, &fs_event);
510
521
  ASSERT(r == 0);
@@ -516,7 +527,7 @@ TEST_IMPL(fs_event_no_callback_after_close) {
516
527
 
517
528
 
518
529
  uv_close((uv_handle_t*)&fs_event, close_cb);
519
- touch_file(loop, "watch_dir/file1");
530
+ touch_file("watch_dir/file1");
520
531
  uv_run(loop, UV_RUN_DEFAULT);
521
532
 
522
533
  ASSERT(fs_event_cb_called == 0);
@@ -537,8 +548,8 @@ TEST_IMPL(fs_event_no_callback_on_close) {
537
548
  /* Setup */
538
549
  remove("watch_dir/file1");
539
550
  remove("watch_dir/");
540
- create_dir(loop, "watch_dir");
541
- create_file(loop, "watch_dir/file1");
551
+ create_dir("watch_dir");
552
+ create_file("watch_dir/file1");
542
553
 
543
554
  r = uv_fs_event_init(loop, &fs_event);
544
555
  ASSERT(r == 0);
@@ -611,8 +622,8 @@ TEST_IMPL(fs_event_close_with_pending_event) {
611
622
 
612
623
  loop = uv_default_loop();
613
624
 
614
- create_dir(loop, "watch_dir");
615
- create_file(loop, "watch_dir/file");
625
+ create_dir("watch_dir");
626
+ create_file("watch_dir/file");
616
627
 
617
628
  r = uv_fs_event_init(loop, &fs_event);
618
629
  ASSERT(r == 0);
@@ -620,7 +631,7 @@ TEST_IMPL(fs_event_close_with_pending_event) {
620
631
  ASSERT(r == 0);
621
632
 
622
633
  /* Generate an fs event. */
623
- touch_file(loop, "watch_dir/file");
634
+ touch_file("watch_dir/file");
624
635
 
625
636
  uv_close((uv_handle_t*)&fs_event, close_cb);
626
637
 
@@ -668,12 +679,12 @@ TEST_IMPL(fs_event_close_in_callback) {
668
679
 
669
680
  loop = uv_default_loop();
670
681
 
671
- create_dir(loop, "watch_dir");
672
- create_file(loop, "watch_dir/file1");
673
- create_file(loop, "watch_dir/file2");
674
- create_file(loop, "watch_dir/file3");
675
- create_file(loop, "watch_dir/file4");
676
- create_file(loop, "watch_dir/file5");
682
+ create_dir("watch_dir");
683
+ create_file("watch_dir/file1");
684
+ create_file("watch_dir/file2");
685
+ create_file("watch_dir/file3");
686
+ create_file("watch_dir/file4");
687
+ create_file("watch_dir/file5");
677
688
 
678
689
  r = uv_fs_event_init(loop, &fs_event);
679
690
  ASSERT(r == 0);
@@ -681,11 +692,11 @@ TEST_IMPL(fs_event_close_in_callback) {
681
692
  ASSERT(r == 0);
682
693
 
683
694
  /* Generate a couple of fs events. */
684
- touch_file(loop, "watch_dir/file1");
685
- touch_file(loop, "watch_dir/file2");
686
- touch_file(loop, "watch_dir/file3");
687
- touch_file(loop, "watch_dir/file4");
688
- touch_file(loop, "watch_dir/file5");
695
+ touch_file("watch_dir/file1");
696
+ touch_file("watch_dir/file2");
697
+ touch_file("watch_dir/file3");
698
+ touch_file("watch_dir/file4");
699
+ touch_file("watch_dir/file5");
689
700
 
690
701
  uv_run(loop, UV_RUN_DEFAULT);
691
702
 
@@ -714,7 +725,7 @@ TEST_IMPL(fs_event_start_and_close) {
714
725
 
715
726
  loop = uv_default_loop();
716
727
 
717
- create_dir(loop, "watch_dir");
728
+ create_dir("watch_dir");
718
729
 
719
730
  r = uv_fs_event_init(loop, &fs_event1);
720
731
  ASSERT(r == 0);
@@ -744,7 +755,7 @@ TEST_IMPL(fs_event_getpath) {
744
755
  char buf[1024];
745
756
  size_t len;
746
757
 
747
- create_dir(loop, "watch_dir");
758
+ create_dir("watch_dir");
748
759
 
749
760
  r = uv_fs_event_init(loop, &fs_event);
750
761
  ASSERT(r == 0);
@@ -806,7 +817,7 @@ TEST_IMPL(fs_event_error_reporting) {
806
817
  TEST_FILE_LIMIT(ARRAY_SIZE(loops) * 3);
807
818
 
808
819
  remove("watch_dir/");
809
- create_dir(uv_default_loop(), "watch_dir");
820
+ create_dir("watch_dir");
810
821
 
811
822
  /* Create a lot of loops, and start FSEventStream in each of them.
812
823
  * Eventually, this should create enough streams to make FSEventStreamStart()