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._call
= this._call
.bind(this);
13 this._call
.finish
= this._finish
.bind(this);
14 this._call
.processName
= name
;
17 ProcessMock
.create = function(name
, runnable
)
19 var processMock
= new ProcessMock(name
, runnable
);
20 return processMock
._call
;
23 ProcessMock
.prototype = {
24 _call: function(finishCallback
)
26 InspectorTest
.addResult("Process '" + this._processName
+ "' STARTED.");
27 this._finishCallback
= finishCallback
;
29 this._runnable
.call(null);
34 InspectorTest
.addResult("Process '" + this._processName
+ "' FINISHED.");
35 this._finishCallback();
36 delete this._finishCallback();
40 var throttler
= new WebInspector
.Throttler(1989);
41 var timeoutMock
= new InspectorTest
.TimeoutMock();
42 throttler
._setTimeout
= timeoutMock
.setTimeout
;
43 throttler
._clearTimeout
= timeoutMock
.clearTimeout
;
44 InspectorTest
.addSniffer(throttler
, "schedule", logSchedule
, true);
46 function testSimpleSchedule(next
, runningProcess
)
48 assertThrottlerIdle();
49 throttler
.schedule(ProcessMock
.create("operation #1"), false);
50 var process
= ProcessMock
.create("operation #2");
51 throttler
.schedule(process
);
53 runningProcess
.finish();
55 assertThrottlerTimeout();
56 timeoutMock
.fireAllTimers();
61 function testAsSoonAsPossibleOverrideTimeout(next
, runningProcess
)
63 assertThrottlerIdle();
64 throttler
.schedule(ProcessMock
.create("operation #1"));
65 var process
= ProcessMock
.create("operation #2");
66 throttler
.schedule(process
, true);
68 runningProcess
.finish();
70 assertThrottlerTimeout();
71 timeoutMock
.fireAllTimers();
76 function testAlwaysExecuteLastScheduled(next
, runningProcess
)
78 assertThrottlerIdle();
80 for (var i
= 0; i
< 4; ++i
) {
81 process
= ProcessMock
.create("operation #" + i
);
82 throttler
.schedule(process
, i
% 2 === 0);
85 runningProcess
.finish();
87 assertThrottlerTimeout();
88 timeoutMock
.fireAllTimers();
93 InspectorTest
.runTestSuite([
96 testAsSoonAsPossibleOverrideTimeout
,
98 testAlwaysExecuteLastScheduled
,
100 function testSimpleScheduleDuringProcess(next
)
102 var runningProcess
= throttlerToRunningState();
103 testSimpleSchedule(next
, runningProcess
);
106 function testAsSoonAsPossibleOverrideDuringProcess(next
)
108 var runningProcess
= throttlerToRunningState();
109 testAsSoonAsPossibleOverrideTimeout(next
, runningProcess
);
112 function testAlwaysExecuteLastScheduledDuringProcess(next
)
114 var runningProcess
= throttlerToRunningState();
115 testAlwaysExecuteLastScheduled(next
, runningProcess
);
118 function testScheduleFromProcess(next
)
121 assertThrottlerIdle();
122 var process
= ProcessMock
.create("operation #1", processBody
);
123 throttler
.schedule(process
);
124 assertThrottlerTimeout();
125 timeoutMock
.fireAllTimers();
127 assertThrottlerTimeout();
128 timeoutMock
.fireAllTimers();
129 nextProcess
.finish();
132 function processBody()
134 nextProcess
= ProcessMock
.create("operation #2");
135 throttler
.schedule(nextProcess
, false);
139 function testExceptionFromProcess(next
)
141 var process
= ProcessMock
.create("operation #1", processBody
);
142 throttler
.schedule(process
);
143 timeoutMock
.fireAllTimers();
144 assertThrottlerIdle();
147 function processBody()
149 throw new Error("Exception during process execution.");
154 function throttlerToRunningState()
156 assertThrottlerIdle();
157 var process
= ProcessMock
.create("long operation");
158 throttler
.schedule(process
);
159 assertThrottlerTimeout();
160 timeoutMock
.fireAllTimers();
164 function assertThrottlerIdle()
166 var timeouts
= timeoutMock
.activeTimersTimeouts();
167 if (timeouts
.length
!== 0) {
168 InspectorTest
.addResult("ERROR: throttler is not in idle state. Scheduled timers timeouts: [" + timeouts
.sort().join(", ") + "]");
169 InspectorTest
.completeTest();
172 InspectorTest
.addResult("Throttler is in IDLE state (doesn't have any timers set up)");
175 function assertThrottlerTimeout()
177 var timeouts
= timeoutMock
.activeTimersTimeouts();
178 if (timeouts
.length
=== 0) {
179 InspectorTest
.addResult("ERROR: throttler is not in timeout state. Scheduled timers timeouts are empty!");
180 InspectorTest
.completeTest();
183 InspectorTest
.addResult("Throttler is in TIMEOUT state. Scheduled timers timeouts: [" + timeouts
.sort().join(", ") + "]");
186 function logSchedule(operation
, asSoonAsPossible
)
188 InspectorTest
.addResult("SCHEDULED: '" + operation
.processName
+ "' asSoonAsPossible: " + asSoonAsPossible
);
195 <body onload=
"runTest()">
196 <p>This test verifies throttler behavior.
</p>