|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -require('../common'); |
| 3 | +const common = require('../common'); |
4 | 4 | const assert = require('assert'); |
5 | 5 |
|
6 | | -let interval_fired = false; |
7 | | -let timeout_fired = false; |
8 | 6 | let unref_interval = false; |
9 | 7 | let unref_timer = false; |
10 | | -let unref_callbacks = 0; |
11 | 8 | let checks = 0; |
12 | 9 |
|
13 | 10 | const LONG_TIME = 10 * 1000; |
14 | 11 | const SHORT_TIME = 100; |
15 | 12 |
|
16 | | -assert.doesNotThrow(function() { |
| 13 | +assert.doesNotThrow(() => { |
17 | 14 | setTimeout(() => {}, 10).unref().ref().unref(); |
18 | 15 | }, 'ref and unref are chainable'); |
19 | 16 |
|
20 | | -assert.doesNotThrow(function() { |
| 17 | +assert.doesNotThrow(() => { |
21 | 18 | setInterval(() => {}, 10).unref().ref().unref(); |
22 | 19 | }, 'ref and unref are chainable'); |
23 | 20 |
|
24 | | -setInterval(function() { |
25 | | - interval_fired = true; |
26 | | -}, LONG_TIME).unref(); |
| 21 | +setInterval(common.mustNotCall('Interval should not fire'), LONG_TIME).unref(); |
| 22 | +setTimeout(common.mustNotCall('Timer should not fire'), LONG_TIME).unref(); |
27 | 23 |
|
28 | | -setTimeout(function() { |
29 | | - timeout_fired = true; |
30 | | -}, LONG_TIME).unref(); |
31 | | - |
32 | | -const interval = setInterval(function() { |
| 24 | +const interval = setInterval(common.mustCall(() => { |
33 | 25 | unref_interval = true; |
34 | 26 | clearInterval(interval); |
35 | | -}, SHORT_TIME); |
| 27 | +}), SHORT_TIME); |
36 | 28 | interval.unref(); |
37 | 29 |
|
38 | | -setTimeout(function() { |
| 30 | +setTimeout(common.mustCall(() => { |
39 | 31 | unref_timer = true; |
40 | | -}, SHORT_TIME).unref(); |
| 32 | +}), SHORT_TIME).unref(); |
41 | 33 |
|
42 | | -const check_unref = setInterval(function() { |
| 34 | +const check_unref = setInterval(() => { |
43 | 35 | if (checks > 5 || (unref_interval && unref_timer)) |
44 | 36 | clearInterval(check_unref); |
45 | 37 | checks += 1; |
46 | 38 | }, 100); |
47 | 39 |
|
48 | | -setTimeout(function() { |
49 | | - unref_callbacks++; |
50 | | - this.unref(); |
51 | | -}, SHORT_TIME); |
| 40 | +{ |
| 41 | + const timeout = |
| 42 | + setTimeout(common.mustCall(() => { |
| 43 | + timeout.unref(); |
| 44 | + }), SHORT_TIME); |
| 45 | +} |
52 | 46 |
|
53 | | -// Should not timeout the test |
54 | | -setInterval(function() { |
55 | | - this.unref(); |
56 | | -}, SHORT_TIME); |
| 47 | +{ |
| 48 | + // Should not timeout the test |
| 49 | + const timeout = |
| 50 | + setInterval(() => timeout.unref(), SHORT_TIME); |
| 51 | +} |
57 | 52 |
|
58 | 53 | // Should not assert on args.Holder()->InternalFieldCount() > 0. See #4261. |
59 | 54 | { |
60 | 55 | const t = setInterval(() => {}, 1); |
61 | 56 | process.nextTick(t.unref.bind({})); |
62 | 57 | process.nextTick(t.unref.bind(t)); |
63 | 58 | } |
64 | | - |
65 | | -process.on('exit', function() { |
66 | | - assert.strictEqual(interval_fired, false, |
67 | | - 'Interval should not fire'); |
68 | | - assert.strictEqual(timeout_fired, false, |
69 | | - 'Timeout should not fire'); |
70 | | - assert.strictEqual(unref_timer, true, |
71 | | - 'An unrefd timeout should still fire'); |
72 | | - assert.strictEqual(unref_interval, true, |
73 | | - 'An unrefd interval should still fire'); |
74 | | - assert.strictEqual(unref_callbacks, 1, |
75 | | - 'Callback should only run once'); |
76 | | -}); |
0 commit comments