| OLD | NEW |
| 1 <html> | 1 <html> |
| 2 <head> | 2 <head> |
| 3 <script src="../http/tests/inspector/inspector-test.js"></script> | 3 <script src="../http/tests/inspector/inspector-test.js"></script> |
| 4 <script> | 4 <script> |
| 5 | 5 |
| 6 function test() | 6 function test() |
| 7 { | 7 { |
| 8 var ProcessMock = function(name, runnable) | 8 var ProcessMock = function(name, runnable) |
| 9 { | 9 { |
| 10 this._runnable = runnable; | 10 this._runnable = runnable; |
| 11 this._processName = name; | 11 this.processName = name; |
| 12 this._call = this._call.bind(this); | 12 this.run = this.run.bind(this); |
| 13 this._call.finish = this._finish.bind(this); | 13 this.run.processName = name; |
| 14 this._call.processName = name; | 14 |
| 15 this.startPromise = new Promise(onStart.bind(this)); |
| 16 this.finishPromise = new Promise(onFinish.bind(this)); |
| 17 |
| 18 function onStart(startCallback) |
| 19 { |
| 20 this._startCallback = startCallback; |
| 21 } |
| 22 |
| 23 function onFinish(finishCallback) |
| 24 { |
| 25 this._finishCallback = finishCallback; |
| 26 } |
| 15 } | 27 } |
| 16 | 28 |
| 17 ProcessMock.create = function(name, runnable) | 29 ProcessMock.create = function(name, runnable) |
| 18 { | 30 { |
| 19 var processMock = new ProcessMock(name, runnable); | 31 return new ProcessMock(name, runnable); |
| 20 return processMock._call; | |
| 21 } | 32 } |
| 22 | 33 |
| 23 ProcessMock.prototype = { | 34 ProcessMock.prototype = { |
| 24 _call: function(finishCallback) | 35 run: function() |
| 25 { | 36 { |
| 26 InspectorTest.addResult("Process '" + this._processName + "' STARTED
."); | 37 InspectorTest.addResult("Process '" + this.processName + "' STARTED.
"); |
| 27 this._finishCallback = finishCallback; | 38 this._startCallback(); |
| 28 if (this._runnable) | 39 if (this._runnable) |
| 29 this._runnable.call(null); | 40 this._runnable.call(null); |
| 41 return this.finishPromise; |
| 30 }, | 42 }, |
| 31 | 43 |
| 32 _finish: function() | 44 finish: function() |
| 33 { | 45 { |
| 34 InspectorTest.addResult("Process '" + this._processName + "' FINISHE
D."); | 46 this.startPromise.then(onFinish.bind(this)); |
| 35 this._finishCallback(); | 47 |
| 36 delete this._finishCallback(); | 48 function onFinish() |
| 49 { |
| 50 InspectorTest.addResult("Process '" + this.processName + "' FINI
SHED."); |
| 51 this._finishCallback(); |
| 52 } |
| 37 }, | 53 }, |
| 38 } | 54 } |
| 39 | 55 |
| 40 var throttler = new WebInspector.Throttler(1989); | 56 var throttler = new WebInspector.Throttler(1989); |
| 41 var timeoutMock = new InspectorTest.TimeoutMock(); | 57 var timeoutMock = new InspectorTest.TimeoutMock(); |
| 42 throttler._setTimeout = timeoutMock.setTimeout; | 58 throttler._setTimeout = timeoutMock.setTimeout; |
| 43 throttler._clearTimeout = timeoutMock.clearTimeout; | 59 throttler._clearTimeout = timeoutMock.clearTimeout; |
| 44 InspectorTest.addSniffer(throttler, "schedule", logSchedule, true); | 60 InspectorTest.addSniffer(throttler, "schedule", logSchedule, true); |
| 45 | 61 |
| 46 function testSimpleSchedule(next, runningProcess) | 62 function testSimpleSchedule(next, runningProcess) |
| 47 { | 63 { |
| 48 assertThrottlerIdle(); | 64 assertThrottlerIdle(); |
| 49 throttler.schedule(ProcessMock.create("operation #1"), false); | 65 throttler.schedule(ProcessMock.create("operation #1").run, false); |
| 50 var process = ProcessMock.create("operation #2"); | 66 var process = ProcessMock.create("operation #2"); |
| 51 throttler.schedule(process); | 67 throttler.schedule(process.run); |
| 52 if (runningProcess) | 68 |
| 69 var promise = Promise.resolve(); |
| 70 if (runningProcess) { |
| 53 runningProcess.finish(); | 71 runningProcess.finish(); |
| 72 promise = waitForProcessFinish(); |
| 73 } |
| 54 | 74 |
| 55 assertThrottlerTimeout(); | 75 promise.then(function() { |
| 56 timeoutMock.fireAllTimers(); | 76 assertThrottlerTimeout(); |
| 57 process.finish(); | 77 timeoutMock.fireAllTimers(); |
| 58 next(); | 78 process.finish(); |
| 79 return waitForProcessFinish(); |
| 80 }).then(next); |
| 59 } | 81 } |
| 60 | 82 |
| 61 function testAsSoonAsPossibleOverrideTimeout(next, runningProcess) | 83 function testAsSoonAsPossibleOverrideTimeout(next, runningProcess) |
| 62 { | 84 { |
| 63 assertThrottlerIdle(); | 85 assertThrottlerIdle(); |
| 64 throttler.schedule(ProcessMock.create("operation #1")); | 86 throttler.schedule(ProcessMock.create("operation #1").run); |
| 65 var process = ProcessMock.create("operation #2"); | 87 var process = ProcessMock.create("operation #2"); |
| 66 throttler.schedule(process, true); | 88 throttler.schedule(process.run, true); |
| 67 if (runningProcess) | 89 |
| 90 var promise = Promise.resolve(); |
| 91 if (runningProcess) { |
| 68 runningProcess.finish(); | 92 runningProcess.finish(); |
| 93 promise = waitForProcessFinish(); |
| 94 } |
| 69 | 95 |
| 70 assertThrottlerTimeout(); | 96 promise.then(function() { |
| 71 timeoutMock.fireAllTimers(); | 97 assertThrottlerTimeout(); |
| 72 process.finish(); | 98 timeoutMock.fireAllTimers(); |
| 73 next(); | 99 process.finish(); |
| 100 return waitForProcessFinish(); |
| 101 }).then(next); |
| 74 } | 102 } |
| 75 | 103 |
| 76 function testAlwaysExecuteLastScheduled(next, runningProcess) | 104 function testAlwaysExecuteLastScheduled(next, runningProcess) |
| 77 { | 105 { |
| 78 assertThrottlerIdle(); | 106 assertThrottlerIdle(); |
| 79 var process = null; | 107 var process = null; |
| 80 for (var i = 0; i < 4; ++i) { | 108 for (var i = 0; i < 4; ++i) { |
| 81 process = ProcessMock.create("operation #" + i); | 109 process = ProcessMock.create("operation #" + i); |
| 82 throttler.schedule(process, i % 2 === 0); | 110 throttler.schedule(process.run, i % 2 === 0); |
| 83 } | 111 } |
| 84 if (runningProcess) | 112 var promise = Promise.resolve(); |
| 113 if (runningProcess) { |
| 85 runningProcess.finish(); | 114 runningProcess.finish(); |
| 86 | 115 promise = waitForProcessFinish(); |
| 87 assertThrottlerTimeout(); | 116 } |
| 88 timeoutMock.fireAllTimers(); | 117 promise.then(function() { |
| 89 process.finish(); | 118 assertThrottlerTimeout(); |
| 90 next(); | 119 timeoutMock.fireAllTimers(); |
| 120 process.finish(); |
| 121 return waitForProcessFinish(); |
| 122 }).then(next); |
| 91 } | 123 } |
| 92 | 124 |
| 93 InspectorTest.runTestSuite([ | 125 InspectorTest.runTestSuite([ |
| 94 testSimpleSchedule, | 126 testSimpleSchedule, |
| 95 | 127 |
| 96 testAsSoonAsPossibleOverrideTimeout, | 128 testAsSoonAsPossibleOverrideTimeout, |
| 97 | 129 |
| 98 testAlwaysExecuteLastScheduled, | 130 testAlwaysExecuteLastScheduled, |
| 99 | 131 |
| 100 function testSimpleScheduleDuringProcess(next) | 132 function testSimpleScheduleDuringProcess(next) |
| 101 { | 133 { |
| 102 var runningProcess = throttlerToRunningState(); | 134 var runningProcess = throttlerToRunningState(); |
| 103 testSimpleSchedule(next, runningProcess); | 135 runningProcess.startPromise.then(function() { |
| 136 testSimpleSchedule(next, runningProcess); |
| 137 }); |
| 104 }, | 138 }, |
| 105 | 139 |
| 106 function testAsSoonAsPossibleOverrideDuringProcess(next) | 140 function testAsSoonAsPossibleOverrideDuringProcess(next) |
| 107 { | 141 { |
| 108 var runningProcess = throttlerToRunningState(); | 142 var runningProcess = throttlerToRunningState(); |
| 109 testAsSoonAsPossibleOverrideTimeout(next, runningProcess); | 143 runningProcess.startPromise.then(function() { |
| 144 testAsSoonAsPossibleOverrideTimeout(next, runningProcess); |
| 145 }); |
| 110 }, | 146 }, |
| 111 | 147 |
| 112 function testAlwaysExecuteLastScheduledDuringProcess(next) | 148 function testAlwaysExecuteLastScheduledDuringProcess(next) |
| 113 { | 149 { |
| 114 var runningProcess = throttlerToRunningState(); | 150 var runningProcess = throttlerToRunningState(); |
| 115 testAlwaysExecuteLastScheduled(next, runningProcess); | 151 runningProcess.startPromise.then(function() { |
| 152 testAlwaysExecuteLastScheduled(next, runningProcess); |
| 153 }); |
| 116 }, | 154 }, |
| 117 | 155 |
| 118 function testScheduleFromProcess(next) | 156 function testScheduleFromProcess(next) |
| 119 { | 157 { |
| 120 var nextProcess; | 158 var nextProcess; |
| 121 assertThrottlerIdle(); | 159 assertThrottlerIdle(); |
| 122 var process = ProcessMock.create("operation #1", processBody); | 160 var process = ProcessMock.create("operation #1", processBody); |
| 123 throttler.schedule(process); | 161 throttler.schedule(process.run); |
| 124 assertThrottlerTimeout(); | 162 assertThrottlerTimeout(); |
| 125 timeoutMock.fireAllTimers(); | 163 timeoutMock.fireAllTimers(); |
| 126 process.finish(); | 164 process.finish(); |
| 127 assertThrottlerTimeout(); | 165 waitForProcessFinish().then(function() { |
| 128 timeoutMock.fireAllTimers(); | 166 assertThrottlerTimeout(); |
| 129 nextProcess.finish(); | 167 timeoutMock.fireAllTimers(); |
| 130 next(); | 168 nextProcess.finish(); |
| 169 return waitForProcessFinish(); |
| 170 }).then(next); |
| 131 | 171 |
| 132 function processBody() | 172 function processBody() |
| 133 { | 173 { |
| 134 nextProcess = ProcessMock.create("operation #2"); | 174 nextProcess = ProcessMock.create("operation #2"); |
| 135 throttler.schedule(nextProcess, false); | 175 throttler.schedule(nextProcess.run, false); |
| 136 } | 176 } |
| 137 }, | 177 }, |
| 138 | 178 |
| 139 function testExceptionFromProcess(next) | 179 function testExceptionFromProcess(next) |
| 140 { | 180 { |
| 141 var process = ProcessMock.create("operation #1", processBody); | 181 var process = ProcessMock.create("operation #1", processBody); |
| 142 throttler.schedule(process); | 182 throttler.schedule(process.run); |
| 143 timeoutMock.fireAllTimers(); | 183 timeoutMock.fireAllTimers(); |
| 144 assertThrottlerIdle(); | 184 waitForProcessFinish().then(function() { |
| 145 next(); | 185 assertThrottlerIdle(); |
| 186 next(); |
| 187 }); |
| 146 | 188 |
| 147 function processBody() | 189 function processBody() |
| 148 { | 190 { |
| 149 throw new Error("Exception during process execution."); | 191 throw new Error("Exception during process execution."); |
| 150 } | 192 } |
| 151 } | 193 } |
| 152 ]); | 194 ]); |
| 153 | 195 |
| 196 function waitForProcessFinish() |
| 197 { |
| 198 var promiseResolve; |
| 199 var hasFinished; |
| 200 InspectorTest.addSniffer(WebInspector.Throttler.prototype, "_processComp
letedForTests", onFinished); |
| 201 function onFinished() |
| 202 { |
| 203 hasFinished = true; |
| 204 if (promiseResolve) |
| 205 promiseResolve(); |
| 206 } |
| 207 return new Promise(function(success) { |
| 208 promiseResolve = success; |
| 209 if (hasFinished) |
| 210 success(); |
| 211 }); |
| 212 } |
| 213 |
| 154 function throttlerToRunningState() | 214 function throttlerToRunningState() |
| 155 { | 215 { |
| 156 assertThrottlerIdle(); | 216 assertThrottlerIdle(); |
| 157 var process = ProcessMock.create("long operation"); | 217 var process = ProcessMock.create("long operation"); |
| 158 throttler.schedule(process); | 218 throttler.schedule(process.run); |
| 159 assertThrottlerTimeout(); | 219 assertThrottlerTimeout(); |
| 160 timeoutMock.fireAllTimers(); | 220 timeoutMock.fireAllTimers(); |
| 161 return process; | 221 return process; |
| 162 } | 222 } |
| 163 | 223 |
| 164 function assertThrottlerIdle() | 224 function assertThrottlerIdle() |
| 165 { | 225 { |
| 166 var timeouts = timeoutMock.activeTimersTimeouts(); | 226 var timeouts = timeoutMock.activeTimersTimeouts(); |
| 167 if (timeouts.length !== 0) { | 227 if (timeouts.length !== 0) { |
| 168 InspectorTest.addResult("ERROR: throttler is not in idle state. Sche
duled timers timeouts: [" + timeouts.sort().join(", ") + "]"); | 228 InspectorTest.addResult("ERROR: throttler is not in idle state. Sche
duled timers timeouts: [" + timeouts.sort().join(", ") + "]"); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 189 } | 249 } |
| 190 } | 250 } |
| 191 | 251 |
| 192 </script> | 252 </script> |
| 193 </head> | 253 </head> |
| 194 | 254 |
| 195 <body onload="runTest()"> | 255 <body onload="runTest()"> |
| 196 <p>This test verifies throttler behavior.</p> | 256 <p>This test verifies throttler behavior.</p> |
| 197 </body> | 257 </body> |
| 198 </html> | 258 </html> |
| OLD | NEW |