OLD | NEW |
(Empty) | |
| 1 <html> |
| 2 <head> |
| 3 <script src="../http/tests/inspector/inspector-test.js"></script> |
| 4 <script> |
| 5 |
| 6 function test() |
| 7 { |
| 8 var ProcessMock = function(name, runnable) |
| 9 { |
| 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; |
| 15 } |
| 16 |
| 17 ProcessMock.create = function(name, runnable) |
| 18 { |
| 19 var processMock = new ProcessMock(name, runnable); |
| 20 return processMock._call; |
| 21 } |
| 22 |
| 23 ProcessMock.prototype = { |
| 24 _call: function(finishCallback) |
| 25 { |
| 26 InspectorTest.addResult("Process '" + this._processName + "' STARTED
."); |
| 27 this._finishCallback = finishCallback; |
| 28 if (this._runnable) |
| 29 this._runnable.call(null); |
| 30 }, |
| 31 |
| 32 _finish: function() |
| 33 { |
| 34 InspectorTest.addResult("Process '" + this._processName + "' FINISHE
D."); |
| 35 this._finishCallback(); |
| 36 delete this._finishCallback(); |
| 37 }, |
| 38 } |
| 39 |
| 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); |
| 45 |
| 46 function testSimpleSchedule(next, runningProcess) |
| 47 { |
| 48 assertThrottlerIdle(); |
| 49 throttler.schedule(ProcessMock.create("operation #1"), false); |
| 50 var process = ProcessMock.create("operation #2"); |
| 51 throttler.schedule(process); |
| 52 if (runningProcess) |
| 53 runningProcess.finish(); |
| 54 |
| 55 assertThrottlerTimeout(); |
| 56 timeoutMock.fireAllTimers(); |
| 57 process.finish(); |
| 58 next(); |
| 59 } |
| 60 |
| 61 function testAsSoonAsPossibleOverrideTimeout(next, runningProcess) |
| 62 { |
| 63 assertThrottlerIdle(); |
| 64 throttler.schedule(ProcessMock.create("operation #1")); |
| 65 var process = ProcessMock.create("operation #2"); |
| 66 throttler.schedule(process, true); |
| 67 if (runningProcess) |
| 68 runningProcess.finish(); |
| 69 |
| 70 assertThrottlerTimeout(); |
| 71 timeoutMock.fireAllTimers(); |
| 72 process.finish(); |
| 73 next(); |
| 74 } |
| 75 |
| 76 function testAlwaysExecuteLastScheduled(next, runningProcess) |
| 77 { |
| 78 assertThrottlerIdle(); |
| 79 var process = null; |
| 80 for (var i = 0; i < 4; ++i) { |
| 81 process = ProcessMock.create("operation #" + i); |
| 82 throttler.schedule(process, i % 2 === 0); |
| 83 } |
| 84 if (runningProcess) |
| 85 runningProcess.finish(); |
| 86 |
| 87 assertThrottlerTimeout(); |
| 88 timeoutMock.fireAllTimers(); |
| 89 process.finish(); |
| 90 next(); |
| 91 } |
| 92 |
| 93 InspectorTest.runTestSuite([ |
| 94 testSimpleSchedule, |
| 95 |
| 96 testAsSoonAsPossibleOverrideTimeout, |
| 97 |
| 98 testAlwaysExecuteLastScheduled, |
| 99 |
| 100 function testSimpleScheduleDuringProcess(next) |
| 101 { |
| 102 var runningProcess = throttlerToRunningState(); |
| 103 testSimpleSchedule(next, runningProcess); |
| 104 }, |
| 105 |
| 106 function testAsSoonAsPossibleOverrideDuringProcess(next) |
| 107 { |
| 108 var runningProcess = throttlerToRunningState(); |
| 109 testAsSoonAsPossibleOverrideTimeout(next, runningProcess); |
| 110 }, |
| 111 |
| 112 function testAlwaysExecuteLastScheduledDuringProcess(next) |
| 113 { |
| 114 var runningProcess = throttlerToRunningState(); |
| 115 testAlwaysExecuteLastScheduled(next, runningProcess); |
| 116 }, |
| 117 |
| 118 function testScheduleFromProcess(next) |
| 119 { |
| 120 var nextProcess; |
| 121 assertThrottlerIdle(); |
| 122 var process = ProcessMock.create("operation #1", processBody); |
| 123 throttler.schedule(process); |
| 124 assertThrottlerTimeout(); |
| 125 timeoutMock.fireAllTimers(); |
| 126 process.finish(); |
| 127 assertThrottlerTimeout(); |
| 128 timeoutMock.fireAllTimers(); |
| 129 nextProcess.finish(); |
| 130 next(); |
| 131 |
| 132 function processBody() |
| 133 { |
| 134 nextProcess = ProcessMock.create("operation #2"); |
| 135 throttler.schedule(nextProcess, false); |
| 136 } |
| 137 }, |
| 138 ]); |
| 139 |
| 140 function throttlerToRunningState() |
| 141 { |
| 142 assertThrottlerIdle(); |
| 143 var process = ProcessMock.create("long operation"); |
| 144 throttler.schedule(process); |
| 145 assertThrottlerTimeout(); |
| 146 timeoutMock.fireAllTimers(); |
| 147 return process; |
| 148 } |
| 149 |
| 150 function assertThrottlerIdle() |
| 151 { |
| 152 var timeouts = timeoutMock.activeTimersTimeouts(); |
| 153 if (timeouts.length !== 0) { |
| 154 InspectorTest.addResult("ERROR: throttler is not in idle state. Sche
duled timers timeouts: [" + timeouts.sort().join(", ") + "]"); |
| 155 InspectorTest.completeTest(); |
| 156 return; |
| 157 } |
| 158 InspectorTest.addResult("Throttler is in IDLE state (doesn't have any ti
mers set up)"); |
| 159 } |
| 160 |
| 161 function assertThrottlerTimeout() |
| 162 { |
| 163 var timeouts = timeoutMock.activeTimersTimeouts(); |
| 164 if (timeouts.length === 0) { |
| 165 InspectorTest.addResult("ERROR: throttler is not in timeout state. S
cheduled timers timeouts are empty!"); |
| 166 InspectorTest.completeTest(); |
| 167 return; |
| 168 } |
| 169 InspectorTest.addResult("Throttler is in TIMEOUT state. Scheduled timers
timeouts: [" + timeouts.sort().join(", ") + "]"); |
| 170 } |
| 171 |
| 172 function logSchedule(operation, asSoonAsPossible) |
| 173 { |
| 174 InspectorTest.addResult("SCHEDULED: '" + operation.processName + "' asSo
onAsPossible: " + asSoonAsPossible); |
| 175 } |
| 176 } |
| 177 |
| 178 </script> |
| 179 </head> |
| 180 |
| 181 <body onload="runTest()"> |
| 182 <p>This test verifies throttler behavior.</p> |
| 183 </body> |
| 184 </html> |
OLD | NEW |