|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +/* |
| 23 | + * This is a regression test for https://github.com/joyent/node/issues/15447 |
| 24 | + * and https://github.com/joyent/node/issues/9333. |
| 25 | + * |
| 26 | + * When a timer is added in another timer's callback, its underlying timer |
| 27 | + * handle was started with a timeout that was actually incorrect. |
| 28 | + * |
| 29 | + * The reason was that the value that represents the current time was not |
| 30 | + * updated between the time the original callback was called and the time |
| 31 | + * the added timer was processed by timers.listOnTimeout. That lead the |
| 32 | + * logic in timers.listOnTimeout to do an incorrect computation that made |
| 33 | + * the added timer fire with a timeout of scheduledTimeout + |
| 34 | + * timeSpentInCallback. |
| 35 | + * |
| 36 | + * This test makes sure that a timer added by another timer's callback |
| 37 | + * fire with the expected timeout. |
| 38 | + * |
| 39 | + * It makes sure that it works when the timers list for a given timeout is |
| 40 | + * empty (see testAddingTimerToEmptyTimersList) and when the timers list |
| 41 | + * is not empty (see testAddingTimerToNonEmptyTimersList). |
| 42 | + */ |
| 43 | + |
| 44 | +var assert = require('assert'); |
| 45 | +var common = require('../common'); |
| 46 | + |
| 47 | +var TIMEOUT = 100; |
| 48 | + |
| 49 | +var nbBlockingCallbackCalls = 0; |
| 50 | +var latestDelay = 0; |
| 51 | +var timeCallbackScheduled = 0; |
| 52 | + |
| 53 | +function initTest() { |
| 54 | + nbBlockingCallbackCalls = 0; |
| 55 | + latestDelay = 0; |
| 56 | + timeCallbackScheduled = 0; |
| 57 | +} |
| 58 | + |
| 59 | +function blockingCallback(callback) { |
| 60 | + ++nbBlockingCallbackCalls; |
| 61 | + |
| 62 | + if (nbBlockingCallbackCalls > 1) { |
| 63 | + latestDelay = new Date().getTime() - timeCallbackScheduled; |
| 64 | + // Even if timers can fire later than when they've been scheduled |
| 65 | + // to fire, they should more than 50% later with a timeout of |
| 66 | + // 100ms. Firing later than that would mean that we hit the regression |
| 67 | + // highlighted in |
| 68 | + // https://github.com/joyent/node/issues/15447 and |
| 69 | + // https://github.com/joyent/node/issues/9333. |
| 70 | + assert(latestDelay < TIMEOUT * 1.5); |
| 71 | + if (callback) |
| 72 | + return callback(); |
| 73 | + } else { |
| 74 | + // block by busy-looping to trigger the issue |
| 75 | + common.busyLoop(TIMEOUT); |
| 76 | + |
| 77 | + timeCallbackScheduled = new Date().getTime(); |
| 78 | + setTimeout(blockingCallback, TIMEOUT); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function testAddingTimerToEmptyTimersList(callback) { |
| 83 | + initTest(); |
| 84 | + // Call setTimeout just once to make sure the timers list is |
| 85 | + // empty when blockingCallback is called. |
| 86 | + setTimeout(blockingCallback.bind(global, callback), TIMEOUT); |
| 87 | +} |
| 88 | + |
| 89 | +function testAddingTimerToNonEmptyTimersList() { |
| 90 | + initTest(); |
| 91 | + // Call setTimeout twice with the same timeout to make |
| 92 | + // sure the timers list is not empty when blockingCallback is called. |
| 93 | + setTimeout(blockingCallback, TIMEOUT); |
| 94 | + setTimeout(blockingCallback, TIMEOUT); |
| 95 | +} |
| 96 | + |
| 97 | +// Run the test for the empty timers list case, and then for the non-empty |
| 98 | +// timers list one |
| 99 | +testAddingTimerToEmptyTimersList(testAddingTimerToNonEmptyTimersList); |
0 commit comments