-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
[Tests]: System time tampering effects on _.throttle and _.debounce methods
#2913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -390,22 +390,66 @@ | |||||
| var counter = 0; | ||||||
| var incr = function(){ counter++; }; | ||||||
| var throttledIncr = _.throttle(incr, 100); | ||||||
| var origNowFunc = _.now; | ||||||
| var originalNowFunc = Date.now; | ||||||
| var originalGetTimeFunc = Date.prototype.getTime; | ||||||
|
|
||||||
| throttledIncr(); | ||||||
| assert.strictEqual(counter, 1); | ||||||
| _.now = function() { | ||||||
| return new Date(2013, 0, 1, 1, 1, 1); | ||||||
| }; | ||||||
| assert.strictEqual(counter, 1, '_.throttle: incr was called immediately'); | ||||||
|
|
||||||
| Date.prototype.getTime = function() { | ||||||
| return +(new Date(2013, 0, 1, 1, 1, 1)); | ||||||
| } | ||||||
| Date.now = function() { | ||||||
| return +(new Date(2013, 0, 1, 1, 1, 1)); | ||||||
| } | ||||||
|
|
||||||
| _.delay(function() { | ||||||
| throttledIncr(); | ||||||
| assert.strictEqual(counter, 2); | ||||||
| assert.strictEqual(counter, 2, '_.throttle: incr was called successfully, with tampered system time'); | ||||||
| done(); | ||||||
| _.now = origNowFunc; | ||||||
| Date.now = originalNowFunc; | ||||||
| Date.prototype.getTime = originalGetTimeFunc; | ||||||
| }, 200); | ||||||
| }); | ||||||
|
|
||||||
| QUnit.test('throttle continues to function after system time is not accessible (or in invalid format)', function(assert) { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow! |
||||||
| assert.expect(3); | ||||||
| var done = assert.async(); | ||||||
| var counter = 0; | ||||||
| var incr = function(){ counter++; }; | ||||||
| var throttledIncr = _.throttle(incr, 100); | ||||||
| var originalNowFunc = Date.now; | ||||||
| var originalGetTimeFunc = Date.prototype.getTime; | ||||||
| var originalValueOfFunc = Date.prototype.valueOf; | ||||||
|
|
||||||
| throttledIncr(); | ||||||
| assert.strictEqual(counter, 1, '_.throttle: incr was called immediately'); | ||||||
|
|
||||||
| Date.prototype.valueOf = function() { | ||||||
| return null; | ||||||
| } | ||||||
| Date.prototype.getTime = function() { | ||||||
| return null; | ||||||
| } | ||||||
| Date.now = function() { | ||||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| _.delay(function() { | ||||||
| throttledIncr(); | ||||||
| assert.strictEqual(counter, 2, '_.throttle: incr was debounced successfully, with tampered system time'); | ||||||
|
||||||
| assert.strictEqual(counter, 2, '_.throttle: incr was debounced successfully, with tampered system time'); | |
| assert.strictEqual(counter, 2, 'incr was throttled successfully, with tampered system time'); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
likewise
| assert.strictEqual(counter, 3, '_.throttle: incr was debounced successfully, after system time method restoration'); | |
| assert.strictEqual(counter, 3, 'incr was throttled successfully, after system time method restoration'); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for brevity
| assert.strictEqual(counter, 2, '_.debounce: incr was debounced successfully, with tampered system time'); | |
| assert.strictEqual(counter, 2, 'incr was debounced successfully, with tampered system time'); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again for brevity
| assert.strictEqual(counter, 2, '_.debounce: incr was debounced successfully, with tampered system time'); | |
| assert.strictEqual(counter, 2, 'incr was debounced successfully, with tampered system time'); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and one last time.
| assert.strictEqual(counter, 3, '_.debounce: incr was debounced successfully, after system time method restoration'); | |
| assert.strictEqual(counter, 3, 'incr was debounced successfully, after system time method restoration'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good to add a comment to the assertion. Much clearer if it fails. 👍