|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* |
| 4 | + * This is a regression test for https://github.com/joyent/node/issues/15447 |
| 5 | + * and https://github.com/joyent/node/issues/9333. |
| 6 | + * |
| 7 | + * When a timer is added in another timer's callback, its underlying timer |
| 8 | + * handle was started with a timeout that was actually incorrect. |
| 9 | + * |
| 10 | + * The reason was that the value that represents the current time was not |
| 11 | + * updated between the time the original callback was called and the time |
| 12 | + * the added timer was processed by timers.listOnTimeout. That lead the |
| 13 | + * logic in timers.listOnTimeout to do an incorrect computation that made |
| 14 | + * the added timer fire with a timeout of scheduledTimeout + |
| 15 | + * timeSpentInCallback. |
| 16 | + * |
| 17 | + * This test makes sure that a timer added by another timer's callback |
| 18 | + * fire with the expected timeout. |
| 19 | + * |
| 20 | + * It makes sure that it works when the timers list for a given timeout is |
| 21 | + * empty (see testAddingTimerToEmptyTimersList) and when the timers list |
| 22 | + * is not empty (see testAddingTimerToNonEmptyTimersList). |
| 23 | + */ |
| 24 | + |
| 25 | +const assert = require('assert'); |
| 26 | +const common = require('../common'); |
| 27 | +const Timer = process.binding('timer_wrap').Timer; |
| 28 | + |
| 29 | +const TIMEOUT = 100; |
| 30 | + |
| 31 | +var nbBlockingCallbackCalls = 0; |
| 32 | +var latestDelay = 0; |
| 33 | +var timeCallbackScheduled = 0; |
| 34 | + |
| 35 | +function initTest() { |
| 36 | + nbBlockingCallbackCalls = 0; |
| 37 | + latestDelay = 0; |
| 38 | + timeCallbackScheduled = 0; |
| 39 | +} |
| 40 | + |
| 41 | +function blockingCallback(callback) { |
| 42 | + ++nbBlockingCallbackCalls; |
| 43 | + |
| 44 | + if (nbBlockingCallbackCalls > 1) { |
| 45 | + latestDelay = Timer.now() - timeCallbackScheduled; |
| 46 | + // Even if timers can fire later than when they've been scheduled |
| 47 | + // to fire, they should more than 50% later with a timeout of |
| 48 | + // 100ms. Firing later than that would mean that we hit the regression |
| 49 | + // highlighted in |
| 50 | + // https://github.com/nodejs/node-v0.x-archive/issues/15447 and |
| 51 | + // https://github.com/nodejs/node-v0.x-archive/issues/9333.. |
| 52 | + assert(latestDelay < TIMEOUT * 1.5); |
| 53 | + if (callback) |
| 54 | + return callback(); |
| 55 | + } else { |
| 56 | + // block by busy-looping to trigger the issue |
| 57 | + common.busyLoop(TIMEOUT); |
| 58 | + |
| 59 | + timeCallbackScheduled = Timer.now(); |
| 60 | + setTimeout(blockingCallback, TIMEOUT); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function testAddingTimerToEmptyTimersList(callback) { |
| 65 | + initTest(); |
| 66 | + // Call setTimeout just once to make sure the timers list is |
| 67 | + // empty when blockingCallback is called. |
| 68 | + setTimeout(blockingCallback.bind(null, callback), TIMEOUT); |
| 69 | +} |
| 70 | + |
| 71 | +function testAddingTimerToNonEmptyTimersList() { |
| 72 | + initTest(); |
| 73 | + // Call setTimeout twice with the same timeout to make |
| 74 | + // sure the timers list is not empty when blockingCallback is called. |
| 75 | + setTimeout(blockingCallback, TIMEOUT); |
| 76 | + setTimeout(blockingCallback, TIMEOUT); |
| 77 | +} |
| 78 | + |
| 79 | +// Run the test for the empty timers list case, and then for the non-empty |
| 80 | +// timers list one |
| 81 | +testAddingTimerToEmptyTimersList(testAddingTimerToNonEmptyTimersList); |
0 commit comments