File: timer-test.js

package info (click to toggle)
node-d3-timer 1.0.7-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 224 kB
  • sloc: makefile: 13; sh: 2
file content (432 lines) | stat: -rw-r--r-- 13,610 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
var tape = require("tape"),
    timer = require("../"),
    end = require("./end");

require("./inRange");
/*
tape("timer(callback) returns an instanceof timer", function(test) {
  var t = timer.timer(function() {});
  test.equal(t instanceof timer.timer, true);
  test.equal(t.constructor.name, "Timer");
  t.stop();
  end(test);
});

tape("timer(callback) verifies that callback is a function", function(test) {
  test.throws(function() { timer.timer(); }, TypeError);
  test.throws(function() { timer.timer("42"); }, TypeError);
  test.throws(function() { timer.timer(null); }, TypeError);
  test.end();
});

// It’s difficult to test the timing behavior reliably, since there can be small
// hiccups that cause a timer to be delayed. So we test only the mean rate.
tape("timer(callback) invokes the callback about every 17ms", function(test) {
  var then = timer.now(), count = 0;
  var t = timer.timer(function() {
    if (++count > 10) {
      t.stop();
      test.inRange(timer.now() - then, (17 - 5) * count, (17 + 5) * count);
      end(test);
    }
  });
});

tape("timer(callback) invokes the callback until the timer is stopped", function(test) {
  var count = 0;
  var t = timer.timer(function() {
    if (++count > 2) {
      t.stop();
      end(test);
    }
  });
});

tape("timer(callback) uses the global context for the callback", function(test) {
  var t = timer.timer(function() {
    test.equal(this, global);
    t.stop();
    end(test);
  });
});

tape("timer(callback) passes the callback the elapsed time", function(test) {
  var then = timer.now(), count = 0;
  var t = timer.timer(function(elapsed) {
    ++count;
    test.equal(elapsed, timer.now() - then);
    if (count > 10) {
      t.stop();
      end(test);
    }
  });
});

tape("timer(callback, delay) first invokes the callback after the specified delay", function(test) {
  var then = timer.now(), delay = 150;
  var t = timer.timer(function() {
    t.stop();
    test.inRange(timer.now() - then, delay - 10, delay + 10);
    end(test);
  }, delay);
});

tape("timer(callback, delay) computes the elapsed time relative to the delay", function(test) {
  var delay = 150;
  var t = timer.timer(function(elapsed) {
    t.stop();
    test.inRange(elapsed, 0, 10);
    end(test);
  }, delay);
});

tape("timer(callback, delay, time) computes the effective delay relative to the specified time", function(test) {
  var delay = 150, skew = 200;
  var t = timer.timer(function(elapsed) {
    t.stop();
    test.inRange(elapsed, skew - delay + 17 - 10, skew - delay + 17 + 10);
    end(test);
  }, delay, timer.now() - skew);
});

tape("timer(callback) invokes callbacks in scheduling order during synchronous flush", function(test) {
  var results = [];
  var t0 = timer.timer(function() { results.push(1); t0.stop(); });
  var t1 = timer.timer(function() { results.push(2); t1.stop(); });
  var t2 = timer.timer(function() { results.push(3); t2.stop(); });
  timer.timerFlush();
  test.deepEqual(results, [1, 2, 3]);
  end(test);
});

tape("timer(callback) invokes callbacks in scheduling order during asynchronous flush", function(test) {
  var results = [];
  var t0 = timer.timer(function() { results.push(1); t0.stop(); });
  var t1 = timer.timer(function() { results.push(2); t1.stop(); });
  var t2 = timer.timer(function() { results.push(3); t2.stop(); });
  var t3 = timer.timer(function() {
    t3.stop();
    test.deepEqual(results, [1, 2, 3]);
    end(test);
  });
});

// Even though these timers have different delays, they are still called back in
// scheduling order when they are simultaneously active.
tape("timer(callback, delay) invokes callbacks in scheduling order during asynchronous flush", function(test) {
  var then = timer.now(), results;
  var t0 = timer.timer(function() { results = [1]; t0.stop(); }, 60, then);
  var t1 = timer.timer(function() { if (results) results.push(2), t1.stop(); }, 40, then);
  var t2 = timer.timer(function() { if (results) results.push(3), t2.stop(); }, 80, then);
  var t3 = timer.timer(function() {
    t3.stop();
    test.deepEqual(results, [1, 2, 3]);
    end(test);
  }, 100, then);
});

tape("timer(callback) within a frame invokes the callback at the end of the same frame", function(test) {
  var then = timer.now();
  var t0 = timer.timer(function(elapsed, now) {
    var delay = timer.now() - then;
    var t1 = timer.timer(function(elapsed2, now2) {
      t1.stop();
      test.equal(elapsed2, 0);
      test.equal(now2, now);
      test.inRange(timer.now() - then, delay, delay + 3);
      end(test);
    }, 0, now);
    t0.stop();
  });
});

// Note: assumes that Node doesn’t support requestAnimationFrame, falling back to setTimeout.
tape("timer(callback, delay) within a timerFlush() does not request duplicate frames", function(test) {
  var setTimeout0 = setTimeout,
      frames = 0;

  // This requests a frame, too, so do it before the test starts.
  timer.now();

  setTimeout = function() {
    ++frames;
    return setTimeout0.apply(this, arguments);
  };

  var t0 = timer.timer(function(elapsed) {

    // 2. The first timer is invoked synchronously by timerFlush, so only the
    // first frame—when this timer was created—has been requested.
    test.equal(frames, 1);

    t0.stop();

    // 3. This timer was stopped during flush, so it doesn’t request a frame.
    test.equal(frames, 1);

    var t1 = timer.timer(function() {

      // 6. Still only one frame has been requested so far: the second timer has
      // a <17ms delay, and so was called back during the first frame requested
      // by the first timer on creation. If the second timer had a longer delay,
      // it might need another frame (or timeout) before invocation.
      test.equal(frames, 1);

      t1.stop();

      // 7. Stopping the second timer doesn’t immediately request a frame since
      // we’re now within an implicit flush (initiated by this timer).
      test.equal(frames, 1);

      setTimeout0(function() {

        // 8. Since the timer queue was empty when we stopped the second timer,
        // no additional frame was requested after the timers were flushed.
        test.equal(frames, 1);

        setTimeout = setTimeout0;
        end(test);
      }, 50);
    }, 1);

    // 4. Creating a second timer during flush also doesn’t immediately request
    // a frame; the request would happen AFTER all the timers are called back,
    // and we still have the request active from when the first timer was
    // created, since the first timer is invoked synchronously.
    test.equal(frames, 1);
  });

  // 1. Creating the first timer requests the first frame.
  test.equal(frames, 1);

  timer.timerFlush();

  // 5. Still only one frame active!
  test.equal(frames, 1);
});

// Note: assumes that Node doesn’t support requestAnimationFrame, falling back to setTimeout.
tape("timer(callback) switches to setTimeout for long delays", function(test) {
  var setTimeout0 = setTimeout,
      frames = 0,
      timeouts = 0;

  // This requests a frame, too, so do it before the test starts.
  timer.now();

  setTimeout = function(callback, delay) {
    delay === 17 ? ++frames : ++timeouts;
    return setTimeout0.apply(this, arguments);
  };

  var t0 = timer.timer(function() {

    // 2. The first timer had a delay >24ms, so after the first scheduling
    // frame, we used a longer timeout to wake up.
    test.equal(frames, 1);
    test.equal(timeouts, 1);

    t0.stop();

    // 3. Stopping a timer during flush doesn’t request a new frame.
    test.equal(frames, 1);
    test.equal(timeouts, 1);

    var t1 = timer.timer(function() {

      // 5. The second timer had a short delay, so it’s not immediately invoked
      // during the same tick as the first timer; it gets a new frame.
      test.equal(frames, 2);
      test.equal(timeouts, 1);

      t1.stop();

      // 6. Stopping a timer during flush doesn’t request a new frame.
      test.equal(frames, 2);
      test.equal(timeouts, 1);

      setTimeout0(function() {

        // 7. Since the timer queue was empty when we stopped the second timer,
        // no additional frame was requested after the timers were flushed.
        test.equal(frames, 2);
        test.equal(timeouts, 1);

        setTimeout = setTimeout0;
        end(test);
      }, 50);
    }, 1);

    // 4. Scheduling a new timer during flush doesn’t request a new frame;
    // that happens after all the timers have been invoked.
    test.equal(frames, 1);
    test.equal(timeouts, 1);
  }, 100);

  // 1. Creating the first timer requests the first frame. Even though the timer
  // has a long delay, we always use a frame to consolidate timer creation for
  // multiple timers. That way, if you schedule a bunch of timers with different
  // delays, we don’t thrash timeouts.
  test.equal(frames, 1);
  test.equal(timeouts, 0);
});

tape("timer.stop() immediately stops the timer", function(test) {
  var count = 0;
  var t = timer.timer(function() { ++count; });
  setTimeout(function() {
    t.stop();
    test.equal(count, 1);
    end(test);
  }, 24);
});

tape("timer.stop() recomputes the new wake time after one frame", function(test) {
  var setTimeout0 = setTimeout,
      delays = [];

  // This requests a frame, too, so do it before the test starts.
  timer.now();

  setTimeout = function(callback, delay) {
    delays.push(delay);
    return setTimeout0.apply(this, arguments);
  };

  var t0 = timer.timer(function() {}, 1000);
  var t1 = timer.timer(function() {}, 500);
  setTimeout0(function() {

    // 1. The two timers are scheduling in the first frame, and then the new
    // wake time is computed based on minimum relative time of active timers.
    test.equal(delays.length, 2);
    test.equal(delays[0], 17);
    test.inRange(delays[1], 500 - 17 - 10, 500 - 17 + 10);

    t1.stop();

    // 2. The second timer (with the smaller delay) was stopped, but the new
    // wake time isn’t computed until the next frame, since we stopped the timer
    // outside of a flush.
    test.equal(delays.length, 3);
    test.equal(delays[2], 17);

    setTimeout0(function() {

      // 3. The alarm was reset to wake for the long-delay timer.
      test.equal(delays.length, 4);
      test.inRange(delays[3], 1000 - 100 - 17 * 1.5 - 10, 1000 - 100 - 17 * 1.5 + 10);

      t0.stop();

      setTimeout0(function() {

        // 4. The alarm was cleared after the long-delay timer was cancelled.
        test.equal(delays.length, 5);
        test.equal(delays[4], 17);

        setTimeout = setTimeout0;
        end(test);
      }, 100);
    }, 100);
  }, 100);
});

tape("timer.restart(callback) verifies that callback is a function", function(test) {
  var t = timer.timer(function() {});
  test.throws(function() { t.restart(); }, TypeError);
  test.throws(function() { t.restart(null); }, TypeError);
  test.throws(function() { t.restart("42"); }, TypeError);
  t.stop();
  end(test);
});

tape("timer.restart(callback) implicitly uses zero delay and the current time", function(test) {
  var t = timer.timer(function() {}, 1000);
  t.restart(function(elapsed) {
    test.inRange(elapsed, 17 - 10, 17 + 10);
    t.stop();
    end(test);
  });
});

tape("timer.restart(callback, delay, time) recomputes the new wake time after one frame", function(test) {
  var then = timer.now(),
      setTimeout0 = setTimeout,
      delays = [];

  setTimeout = function(callback, delay) {
    delays.push(delay);
    return setTimeout0.apply(this, arguments);
  };

  var t = timer.timer(function() {}, 500, then);
  setTimeout0(function() {

    // 1. The timer is scheduled in the first frame, and then the new wake time
    // is computed based on its relative time.
    test.equal(delays.length, 2);
    test.equal(delays[0], 17);
    test.inRange(delays[1], 500 - 17 - 10, 500 - 17 + 10);

    t.restart(function() {}, 1000, then);

    // 2. The timer was delayed, but the new wake time isn’t computed until the
    // next frame, since we restarted the timer outside of a flush.
    test.equal(delays.length, 3);
    test.equal(delays[2], 17);

    setTimeout0(function() {

      // 3. The alarm was reset to wake for the longer delay.
      test.equal(delays.length, 4);
      test.inRange(delays[3], 1000 - 100 - 17 * 1.5 - 10, 1000 - 100 - 17 * 1.5 + 10);

      t.stop();

      setTimeout0(function() {

        // 4. The alarm was cleared after the timer was cancelled.
        test.equal(delays.length, 5);
        test.equal(delays[4], 17);

        setTimeout = setTimeout0;
        end(test);
      }, 100);
    }, 100);
  }, 100);
});

tape("timer.stop() immediately followed by timer.restart() doesn’t cause an infinite loop", function(test) {
  var t = timer.timer(function() {}), last;
  t.stop();
  t.restart(function(elapsed) {
    if (!last) return last = elapsed;
    if (elapsed === last) test.fail("repeated invocation");
    t.stop();
    test.end();
  });
});

tape("timer.stop() immediately followed by timer.restart() doesn’t cause an infinite loop (2)", function(test) {
  var t0 = timer.timer(function() {}), t1 = timer.timer(function() {}), last;
  t0.stop();
  t0.restart(function(elapsed) {
    if (!last) return last = elapsed;
    if (elapsed === last) test.fail("repeated invocation");
    t0.stop();
    test.end();
  });
  t1.stop();
});

tape("timer.stop() clears the internal _next field after a timeout", function(test) {
  var t0 = timer.timer(function() {}), t1 = timer.timer(function() {});
  t0.stop();
  setTimeout(function() {
    test.equal(!t0._next, true);
    t1.stop();
    test.end();
  }, 100);
});
*/