3 <script src=
"../http/tests/inspector/inspector-test.js"></script>
8 var ProcessMock = function(name
, runnable
)
10 this._runnable
= runnable
;
11 this.processName
= name
;
12 this.run
= this.run
.bind(this);
13 this.run
.processName
= name
;
15 this.startPromise
= new Promise(onStart
.bind(this));
16 this.finishPromise
= new Promise(onFinish
.bind(this));
18 function onStart(startCallback
)
20 this._startCallback
= startCallback
;
23 function onFinish(finishCallback
)
25 this._finishCallback
= finishCallback
;
29 ProcessMock
.create = function(name
, runnable
)
31 return new ProcessMock(name
, runnable
);
34 ProcessMock
.prototype = {
37 InspectorTest
.addResult("Process '" + this.processName
+ "' STARTED.");
38 this._startCallback();
40 this._runnable
.call(null);
41 return this.finishPromise
;
46 this.startPromise
.then(onFinish
.bind(this));
50 InspectorTest
.addResult("Process '" + this.processName
+ "' FINISHED.");
51 this._finishCallback();
56 var throttler
= new WebInspector
.Throttler(1989);
57 var timeoutMock
= new InspectorTest
.TimeoutMock();
58 throttler
._setTimeout
= timeoutMock
.setTimeout
;
59 throttler
._clearTimeout
= timeoutMock
.clearTimeout
;
60 InspectorTest
.addSniffer(throttler
, "schedule", logSchedule
, true);
62 function testSimpleSchedule(next
, runningProcess
)
64 assertThrottlerIdle();
65 throttler
.schedule(ProcessMock
.create("operation #1").run
, false);
66 var process
= ProcessMock
.create("operation #2");
67 throttler
.schedule(process
.run
);
69 var promise
= Promise
.resolve();
71 runningProcess
.finish();
72 promise
= waitForProcessFinish();
75 promise
.then(function() {
76 assertThrottlerTimeout();
77 timeoutMock
.fireAllTimers();
79 return waitForProcessFinish();
83 function testAsSoonAsPossibleOverrideTimeout(next
, runningProcess
)
85 assertThrottlerIdle();
86 throttler
.schedule(ProcessMock
.create("operation #1").run
);
87 var process
= ProcessMock
.create("operation #2");
88 throttler
.schedule(process
.run
, true);
90 var promise
= Promise
.resolve();
92 runningProcess
.finish();
93 promise
= waitForProcessFinish();
96 promise
.then(function() {
97 assertThrottlerTimeout();
98 timeoutMock
.fireAllTimers();
100 return waitForProcessFinish();
104 function testAlwaysExecuteLastScheduled(next
, runningProcess
)
106 assertThrottlerIdle();
108 for (var i
= 0; i
< 4; ++i
) {
109 process
= ProcessMock
.create("operation #" + i
);
110 throttler
.schedule(process
.run
, i
% 2 === 0);
112 var promise
= Promise
.resolve();
113 if (runningProcess
) {
114 runningProcess
.finish();
115 promise
= waitForProcessFinish();
117 promise
.then(function() {
118 assertThrottlerTimeout();
119 timeoutMock
.fireAllTimers();
121 return waitForProcessFinish();
125 InspectorTest
.runTestSuite([
128 testAsSoonAsPossibleOverrideTimeout
,
130 testAlwaysExecuteLastScheduled
,
132 function testSimpleScheduleDuringProcess(next
)
134 var runningProcess
= throttlerToRunningState();
135 runningProcess
.startPromise
.then(function() {
136 testSimpleSchedule(next
, runningProcess
);
140 function testAsSoonAsPossibleOverrideDuringProcess(next
)
142 var runningProcess
= throttlerToRunningState();
143 runningProcess
.startPromise
.then(function() {
144 testAsSoonAsPossibleOverrideTimeout(next
, runningProcess
);
148 function testAlwaysExecuteLastScheduledDuringProcess(next
)
150 var runningProcess
= throttlerToRunningState();
151 runningProcess
.startPromise
.then(function() {
152 testAlwaysExecuteLastScheduled(next
, runningProcess
);
156 function testScheduleFromProcess(next
)
159 assertThrottlerIdle();
160 var process
= ProcessMock
.create("operation #1", processBody
);
161 throttler
.schedule(process
.run
);
162 assertThrottlerTimeout();
163 timeoutMock
.fireAllTimers();
165 waitForProcessFinish().then(function() {
166 assertThrottlerTimeout();
167 timeoutMock
.fireAllTimers();
168 nextProcess
.finish();
169 return waitForProcessFinish();
172 function processBody()
174 nextProcess
= ProcessMock
.create("operation #2");
175 throttler
.schedule(nextProcess
.run
, false);
179 function testExceptionFromProcess(next
)
181 var process
= ProcessMock
.create("operation #1", processBody
);
182 throttler
.schedule(process
.run
);
183 timeoutMock
.fireAllTimers();
184 waitForProcessFinish().then(function() {
185 assertThrottlerIdle();
189 function processBody()
191 throw new Error("Exception during process execution.");
196 function waitForProcessFinish()
200 InspectorTest
.addSniffer(WebInspector
.Throttler
.prototype, "_processCompletedForTests", onFinished
);
201 function onFinished()
207 return new Promise(function(success
) {
208 promiseResolve
= success
;
214 function throttlerToRunningState()
216 assertThrottlerIdle();
217 var process
= ProcessMock
.create("long operation");
218 throttler
.schedule(process
.run
);
219 assertThrottlerTimeout();
220 timeoutMock
.fireAllTimers();
224 function assertThrottlerIdle()
226 var timeouts
= timeoutMock
.activeTimersTimeouts();
227 if (timeouts
.length
!== 0) {
228 InspectorTest
.addResult("ERROR: throttler is not in idle state. Scheduled timers timeouts: [" + timeouts
.sort().join(", ") + "]");
229 InspectorTest
.completeTest();
232 InspectorTest
.addResult("Throttler is in IDLE state (doesn't have any timers set up)");
235 function assertThrottlerTimeout()
237 var timeouts
= timeoutMock
.activeTimersTimeouts();
238 if (timeouts
.length
=== 0) {
239 InspectorTest
.addResult("ERROR: throttler is not in timeout state. Scheduled timers timeouts are empty!");
240 InspectorTest
.completeTest();
243 InspectorTest
.addResult("Throttler is in TIMEOUT state. Scheduled timers timeouts: [" + timeouts
.sort().join(", ") + "]");
246 function logSchedule(operation
, asSoonAsPossible
)
248 InspectorTest
.addResult("SCHEDULED: '" + operation
.processName
+ "' asSoonAsPossible: " + asSoonAsPossible
);
255 <body onload=
"runTest()">
256 <p>This test verifies throttler behavior.
</p>