Commit 6b6934da authored by Andrey Lushnikov's avatar Andrey Lushnikov Committed by Commit Bot

DevTools: unflake http/tests/devtools/components/throttler.js

The test was mocking time only partially: the window.performance.now()
should be mocked as well.

BUG=610464
TBR=dgozman

Change-Id: If9c81662235e43ee2ffafef2ed772158f3a74968
Reviewed-on: https://chromium-review.googlesource.com/828443Reviewed-by: default avatarAndrey Lushnikov <lushnikov@chromium.org>
Commit-Queue: Andrey Lushnikov <lushnikov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#524327}
parent 6fd1b88c
...@@ -1822,7 +1822,6 @@ crbug.com/611658 [ Win7 ] fast/writing-mode/english-lr-text.html [ Failure ] ...@@ -1822,7 +1822,6 @@ crbug.com/611658 [ Win7 ] fast/writing-mode/english-lr-text.html [ Failure ]
crbug.com/605059 [ Retina ] fast/text/international/rtl-negative-letter-spacing.html [ Failure ] crbug.com/605059 [ Retina ] fast/text/international/rtl-negative-letter-spacing.html [ Failure ]
crbug.com/610464 [ Linux Win7 Debug ] http/tests/devtools/components/throttler.js [ Failure Pass ]
crbug.com/654477 [ Win ] compositing/video/video-controls-layer-creation.html [ Pass Failure ] crbug.com/654477 [ Win ] compositing/video/video-controls-layer-creation.html [ Pass Failure ]
crbug.com/654477 fast/hidpi/video-controls-in-hidpi.html [ Failure ] crbug.com/654477 fast/hidpi/video-controls-in-hidpi.html [ Failure ]
crbug.com/654477 fast/layers/video-layer.html [ Failure ] crbug.com/654477 fast/layers/video-layer.html [ Failure ]
......
...@@ -8,91 +8,91 @@ ...@@ -8,91 +8,91 @@
class TimeoutMock { class TimeoutMock {
constructor() { constructor() {
this._timeoutId = 0; this._timeoutId = 0;
this._timeoutIdToProcess = {}; this._timeoutIdToProcess = new Map();
this._timeoutIdToMillis = {}; this._timeoutIdToMillis = new Map();
this._time = 1;
this.setTimeout = this.setTimeout.bind(this); this.setTimeout = this.setTimeout.bind(this);
this.clearTimeout = this.clearTimeout.bind(this); this.clearTimeout = this.clearTimeout.bind(this);
this.getTime = this.getTime.bind(this);
} }
/** /**
* @param {!Function} operation * @param {!Function} operation
* @param {number} timeout * @param {number} timeout
*/ */
setTimeout(operation, timeout) { setTimeout(operation, timeout) {
this._timeoutIdToProcess[++this._timeoutId] = operation; this._timeoutIdToProcess.set(++this._timeoutId, operation);
this._timeoutIdToMillis[this._timeoutId] = timeout; this._timeoutIdToMillis.set(this._timeoutId, timeout);
return this._timeoutId; return this._timeoutId;
} }
/** /**
* *
* @param {number} timeoutId * @param {number} timeoutId
*/ */
clearTimeout(timeoutId) { clearTimeout(timeoutId) {
delete this._timeoutIdToProcess[timeoutId]; this._timeoutIdToProcess.delete(timeoutId);
delete this._timeoutIdToMillis[timeoutId]; this._timeoutIdToMillis.delete(timeoutId);
} }
/** /**
* @return {!Array<number>} * @return {!Array<number>}
*/ */
activeTimersTimeouts() { activeTimersTimeouts() {
return Object.values(this._timeoutIdToMillis); return Array.from(this._timeoutIdToMillis.values());
}
getTime() {
return this._time;
} }
fireAllTimers() { fireAllTimers() {
for (const timeoutId in this._timeoutIdToProcess) this._time = Math.max(...this.activeTimersTimeouts()) + 1;
this._timeoutIdToProcess[timeoutId].call(window); for (const timeoutId of this._timeoutIdToProcess.keys())
this._timeoutIdToProcess = {}; this._timeoutIdToProcess.get(timeoutId).call(window);
this._timeoutIdToMillis = {}; this._timeoutIdToProcess.clear();
this._timeoutIdToMillis.clear();
} }
} }
var ProcessMock = function(name, runnable) { class ProcessMock {
this._runnable = runnable; constructor(name, runnable) {
this.processName = name; this._runnable = runnable;
this.run = this.run.bind(this); this.processName = name;
this.run.processName = name; this.run = this.run.bind(this);
this.run.processName = name;
this.startPromise = new Promise(onStart.bind(this));
this.finishPromise = new Promise(onFinish.bind(this));
function onStart(startCallback) { this.startPromise = new Promise(fulfill => this._startCallback = fulfill);
this._startCallback = startCallback; this.finishPromise = new Promise(fulfill => this._finishCallback = fulfill);
} }
function onFinish(finishCallback) { run() {
this._finishCallback = finishCallback;
}
};
ProcessMock.create = function(name, runnable) {
return new ProcessMock(name, runnable);
};
ProcessMock.prototype = {
run: function() {
TestRunner.addResult('Process \'' + this.processName + '\' STARTED.'); TestRunner.addResult('Process \'' + this.processName + '\' STARTED.');
this._startCallback(); this._startCallback();
if (this._runnable) if (this._runnable)
this._runnable.call(null); this._runnable.call(null);
return this.finishPromise; return this.finishPromise;
}, }
finish: function() { finish() {
this.startPromise.then(onFinish.bind(this)); this.startPromise.then(onFinish.bind(this));
function onFinish() { function onFinish() {
TestRunner.addResult('Process \'' + this.processName + '\' FINISHED.'); TestRunner.addResult('Process \'' + this.processName + '\' FINISHED.');
this._finishCallback(); this._finishCallback();
} }
}, }
};
static create(name, runnable) {
return new ProcessMock(name, runnable);
}
}
var throttler = new Common.Throttler(1989); var throttler = new Common.Throttler(1989);
var timeoutMock = new TimeoutMock(); var timeoutMock = new TimeoutMock();
throttler._setTimeout = timeoutMock.setTimeout; throttler._setTimeout = timeoutMock.setTimeout;
throttler._clearTimeout = timeoutMock.clearTimeout; throttler._clearTimeout = timeoutMock.clearTimeout;
throttler._getTime = timeoutMock.getTime;
TestRunner.addSniffer(throttler, 'schedule', logSchedule, true); TestRunner.addSniffer(throttler, 'schedule', logSchedule, true);
function testSimpleSchedule(next, runningProcess) { function testSimpleSchedule(next, runningProcess) {
...@@ -107,14 +107,12 @@ ...@@ -107,14 +107,12 @@
promise = waitForProcessFinish(); promise = waitForProcessFinish();
} }
promise promise.then(() => {
.then(function() { assertThrottlerTimeout();
assertThrottlerTimeout(); timeoutMock.fireAllTimers();
timeoutMock.fireAllTimers(); process.finish();
process.finish(); return waitForProcessFinish();
return waitForProcessFinish(); }).then(next);
})
.then(next);
} }
function testAsSoonAsPossibleOverrideTimeout(next, runningProcess) { function testAsSoonAsPossibleOverrideTimeout(next, runningProcess) {
......
...@@ -18,7 +18,7 @@ Common.Throttler = class { ...@@ -18,7 +18,7 @@ Common.Throttler = class {
} }
_processCompleted() { _processCompleted() {
this._lastCompleteTime = window.performance.now(); this._lastCompleteTime = this._getTime();
this._isRunningProcess = false; this._isRunningProcess = false;
if (this._process) if (this._process)
this._innerSchedule(false); this._innerSchedule(false);
...@@ -48,7 +48,7 @@ Common.Throttler = class { ...@@ -48,7 +48,7 @@ Common.Throttler = class {
// Run the first scheduled task instantly. // Run the first scheduled task instantly.
var hasScheduledTasks = !!this._processTimeout || this._isRunningProcess; var hasScheduledTasks = !!this._processTimeout || this._isRunningProcess;
var okToFire = window.performance.now() - this._lastCompleteTime > this._timeout; var okToFire = this._getTime() - this._lastCompleteTime > this._timeout;
asSoonAsPossible = !!asSoonAsPossible || (!hasScheduledTasks && okToFire); asSoonAsPossible = !!asSoonAsPossible || (!hasScheduledTasks && okToFire);
var forceTimerUpdate = asSoonAsPossible && !this._asSoonAsPossible; var forceTimerUpdate = asSoonAsPossible && !this._asSoonAsPossible;
...@@ -87,6 +87,13 @@ Common.Throttler = class { ...@@ -87,6 +87,13 @@ Common.Throttler = class {
_setTimeout(operation, timeout) { _setTimeout(operation, timeout) {
return setTimeout(operation, timeout); return setTimeout(operation, timeout);
} }
/**
* @return {number}
*/
_getTime() {
return window.performance.now();
}
}; };
/** @typedef {function(!Error=)} */ /** @typedef {function(!Error=)} */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment