Permalink
Browse files

Make timers work when Date has been stubbed or modified.

  • Loading branch information...
austinbv committed Jul 14, 2012
1 parent db59c84 commit 53bf5c883d8746fdc7d82c7aeb44e8067af63601
Showing with 12 additions and 0 deletions.
  1. +3 −0 lib/timers.js
  2. +9 −0 test/pummel/test-timers.js
View
@@ -26,6 +26,9 @@ var assert = require('assert').ok;
// Timeout values > TIMEOUT_MAX are set to 1.
var TIMEOUT_MAX = 2147483647; // 2^31-1
+//Hold reference to the global date so if stubbed the times still work
+var Date = this.Date;
@isaacs

isaacs Jul 14, 2012

Shouldn't this be global.Date? (In node modules this === exports.)

+
var debug;
if (process.env.NODE_DEBUG && /timer/.test(process.env.NODE_DEBUG)) {
debug = function() { require('util').error.apply(this, arguments); };
@@ -44,6 +44,15 @@ setTimeout(function() {
setTimeout_called = true;
}, 1000);
+assert.doesNotThrow(function() {
+ var copy = Date;
+ Date = 'Not a Date';
+ interval_count = 0;
+ setInterval(function() { clearInterval(this); }, 100);
+ setTimeout(function() { return true; }, 100);
+ Date = copy;
+}, TypeError);
+
// this timer shouldn't execute
var id = setTimeout(function() { assert.equal(true, false); }, 500);
clearTimeout(id);

0 comments on commit 53bf5c8

Please sign in to comment.