|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* |
| 4 | + * This is a regression test for https://github.com/nodejs/node/issues/7722. |
| 5 | + * |
| 6 | + * When nested timers have the same timeout, calling clearTimeout on the |
| 7 | + * older timer after it has fired causes the list the newer timer is in |
| 8 | + * to be deleted. Since the newer timer was not cleared, it still blocks |
| 9 | + * the event loop completing for the duration of its timeout, however, since |
| 10 | + * no reference exists to it in its list, it cannot be canceled and its |
| 11 | + * callback is not called when the timeout elapses. |
| 12 | + */ |
| 13 | + |
| 14 | +constcommon=require('../common'); |
| 15 | +constassert=require('assert'); |
| 16 | +constTimer=process.binding('timer_wrap').Timer; |
| 17 | + |
| 18 | +constTIMEOUT=common.platformTimeout(100); |
| 19 | +conststart=Timer.now(); |
| 20 | + |
| 21 | +// This bug also prevents the erroneously dereferenced timer's callback |
| 22 | +// from being called, so we can't use it's execution or lack thereof |
| 23 | +// to assert that the bug is fixed. |
| 24 | +process.on('exit',function(){ |
| 25 | +constend=Timer.now(); |
| 26 | +assert.equal(end-start<TIMEOUT*2,true, |
| 27 | +'Elapsed time does not include second timer\'s timeout.'); |
| 28 | +}); |
| 29 | + |
| 30 | +consthandle1=setTimeout(common.mustCall(function(){ |
| 31 | +// Cause the old TIMEOUT list to be deleted |
| 32 | +clearTimeout(handle1); |
| 33 | + |
| 34 | +// Cause a new list with the same key (TIMEOUT) to be created for this timer |
| 35 | +consthandle2=setTimeout(function(){ |
| 36 | +common.fail('Inner callback is not called'); |
| 37 | +},TIMEOUT); |
| 38 | + |
| 39 | +setTimeout(common.mustCall(function(){ |
| 40 | +// Attempt to cancel the second timer. Fix for this bug will keep the |
| 41 | +// newer timer from being dereferenced by keeping its list from being |
| 42 | +// erroneously deleted. If we are able to cancel the timer successfully, |
| 43 | +// the bug is fixed. |
| 44 | +clearTimeout(handle2); |
| 45 | +setImmediate(common.mustCall(function(){ |
| 46 | +setImmediate(common.mustCall(function(){ |
| 47 | +constactiveHandles=process._getActiveHandles(); |
| 48 | +constactiveTimers=activeHandles.filter(function(handle){ |
| 49 | +returnhandleinstanceofTimer; |
| 50 | +}); |
| 51 | + |
| 52 | +// Make sure our clearTimeout succeeded. One timer finished and |
| 53 | +// the other was canceled, so none should be active. |
| 54 | +assert.equal(activeTimers.length,0,'No Timers remain.'); |
| 55 | +})); |
| 56 | +})); |
| 57 | +}),10); |
| 58 | + |
| 59 | +// Make sure our timers got added to the list. |
| 60 | +constactiveHandles=process._getActiveHandles(); |
| 61 | +constactiveTimers=activeHandles.filter(function(handle){ |
| 62 | +returnhandleinstanceofTimer; |
| 63 | +}); |
| 64 | +constshortTimer=activeTimers.find(function(handle){ |
| 65 | +returnhandle._list.msecs===10; |
| 66 | +}); |
| 67 | +constlongTimers=activeTimers.filter(function(handle){ |
| 68 | +returnhandle._list.msecs===TIMEOUT; |
| 69 | +}); |
| 70 | + |
| 71 | +// Make sure our clearTimeout succeeded. One timer finished and |
| 72 | +// the other was canceled, so none should be active. |
| 73 | +assert.equal(activeTimers.length,3,'There are 3 timers in the list.'); |
| 74 | +assert(shortTimerinstanceofTimer,'The shorter timer is in the list.'); |
| 75 | +assert.equal(longTimers.length,2,'Both longer timers are in the list.'); |
| 76 | + |
| 77 | +// When this callback completes, `listOnTimeout` should now look at the |
| 78 | +// correct list and refrain from removing the new TIMEOUT list which |
| 79 | +// contains the reference to the newer timer. |
| 80 | +}),TIMEOUT); |
0 commit comments