Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | <!-- |
michael@0 | 2 | Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | --> |
michael@0 | 5 | <html> |
michael@0 | 6 | <head> |
michael@0 | 7 | <title>Test for Promise.all, Promise.race</title> |
michael@0 | 8 | <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 9 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
michael@0 | 10 | </head> |
michael@0 | 11 | <body> |
michael@0 | 12 | <p id="display"></p> |
michael@0 | 13 | <div id="content" style="display: none"> |
michael@0 | 14 | |
michael@0 | 15 | </div> |
michael@0 | 16 | <pre id="test"> |
michael@0 | 17 | <script type="application/javascript"><!-- |
michael@0 | 18 | |
michael@0 | 19 | function promiseUtilitiesDefined() { |
michael@0 | 20 | ok(Promise.all, "Promise.all must be defined when Promise is enabled."); |
michael@0 | 21 | ok(Promise.race, "Promise.race must be defined when Promise is enabled."); |
michael@0 | 22 | runTest(); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | function promiseAllEmptyArray() { |
michael@0 | 26 | var p = Promise.all([]); |
michael@0 | 27 | ok(p instanceof Promise, "Return value of Promise.all should be a Promise."); |
michael@0 | 28 | p.then(function(values) { |
michael@0 | 29 | ok(Array.isArray(values), "Resolved value should be an array."); |
michael@0 | 30 | is(values.length, 0, "Resolved array length should match iterable's length."); |
michael@0 | 31 | runTest(); |
michael@0 | 32 | }, function() { |
michael@0 | 33 | ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises."); |
michael@0 | 34 | runTest(); |
michael@0 | 35 | }); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | function promiseAllArray() { |
michael@0 | 39 | var p = Promise.all([1, new Date(), Promise.resolve("firefox")]); |
michael@0 | 40 | ok(p instanceof Promise, "Return value of Promise.all should be a Promise."); |
michael@0 | 41 | p.then(function(values) { |
michael@0 | 42 | ok(Array.isArray(values), "Resolved value should be an array."); |
michael@0 | 43 | is(values.length, 3, "Resolved array length should match iterable's length."); |
michael@0 | 44 | is(values[0], 1, "Array values should match."); |
michael@0 | 45 | ok(values[1] instanceof Date, "Array values should match."); |
michael@0 | 46 | is(values[2], "firefox", "Array values should match."); |
michael@0 | 47 | runTest(); |
michael@0 | 48 | }, function() { |
michael@0 | 49 | ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises."); |
michael@0 | 50 | runTest(); |
michael@0 | 51 | }); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | function promiseAllIterable() { |
michael@0 | 55 | function* promiseGen() { |
michael@0 | 56 | var i = 3; |
michael@0 | 57 | while (--i) { |
michael@0 | 58 | yield Promise.resolve(i); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | yield new Promise(function(resolve) { |
michael@0 | 62 | setTimeout(resolve, 10); |
michael@0 | 63 | }); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | Promise.all(promiseGen()).then(function(values) { |
michael@0 | 67 | is(values.length, 3, "Resolved array length should match iterable's length."); |
michael@0 | 68 | is(values[0], 2, "Array values should match."); |
michael@0 | 69 | is(values[1], 1, "Array values should match."); |
michael@0 | 70 | is(values[2], undefined, "Array values should match."); |
michael@0 | 71 | runTest(); |
michael@0 | 72 | }, function(e) { |
michael@0 | 73 | ok(false, "Promise.all shouldn't fail when an iterable is passed."); |
michael@0 | 74 | runTest(); |
michael@0 | 75 | }); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | function promiseAllWaitsForAllPromises() { |
michael@0 | 79 | var arr = [ |
michael@0 | 80 | new Promise(function(resolve) { |
michael@0 | 81 | setTimeout(resolve.bind(undefined, 1), 50); |
michael@0 | 82 | }), |
michael@0 | 83 | new Promise(function(resolve) { |
michael@0 | 84 | setTimeout(resolve.bind(undefined, 2), 10); |
michael@0 | 85 | }), |
michael@0 | 86 | new Promise(function(resolve) { |
michael@0 | 87 | setTimeout(resolve.bind(undefined, new Promise(function(resolve2) { |
michael@0 | 88 | resolve2(3); |
michael@0 | 89 | })), 10); |
michael@0 | 90 | }), |
michael@0 | 91 | new Promise(function(resolve) { |
michael@0 | 92 | setTimeout(resolve.bind(undefined, 4), 20); |
michael@0 | 93 | }) |
michael@0 | 94 | ]; |
michael@0 | 95 | |
michael@0 | 96 | var p = Promise.all(arr); |
michael@0 | 97 | p.then(function(values) { |
michael@0 | 98 | ok(Array.isArray(values), "Resolved value should be an array."); |
michael@0 | 99 | is(values.length, 4, "Resolved array length should match iterable's length."); |
michael@0 | 100 | is(values[0], 1, "Array values should match."); |
michael@0 | 101 | is(values[1], 2, "Array values should match."); |
michael@0 | 102 | is(values[2], 3, "Array values should match."); |
michael@0 | 103 | is(values[3], 4, "Array values should match."); |
michael@0 | 104 | runTest(); |
michael@0 | 105 | }, function() { |
michael@0 | 106 | ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises."); |
michael@0 | 107 | runTest(); |
michael@0 | 108 | }); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | function promiseAllRejectFails() { |
michael@0 | 112 | var arr = [ |
michael@0 | 113 | new Promise(function(resolve) { |
michael@0 | 114 | setTimeout(resolve.bind(undefined, 1), 50); |
michael@0 | 115 | }), |
michael@0 | 116 | new Promise(function(resolve, reject) { |
michael@0 | 117 | setTimeout(reject.bind(undefined, 2), 10); |
michael@0 | 118 | }), |
michael@0 | 119 | new Promise(function(resolve) { |
michael@0 | 120 | setTimeout(resolve.bind(undefined, 3), 10); |
michael@0 | 121 | }), |
michael@0 | 122 | new Promise(function(resolve) { |
michael@0 | 123 | setTimeout(resolve.bind(undefined, 4), 20); |
michael@0 | 124 | }) |
michael@0 | 125 | ]; |
michael@0 | 126 | |
michael@0 | 127 | var p = Promise.all(arr); |
michael@0 | 128 | p.then(function(values) { |
michael@0 | 129 | ok(false, "Promise.all shouldn't resolve when iterable has rejected Promises."); |
michael@0 | 130 | runTest(); |
michael@0 | 131 | }, function(e) { |
michael@0 | 132 | ok(true, "Promise.all should reject when iterable has rejected Promises."); |
michael@0 | 133 | is(e, 2, "Rejection value should match."); |
michael@0 | 134 | runTest(); |
michael@0 | 135 | }); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | function promiseAllCastError() { |
michael@0 | 139 | var p = Promise.all([Promise.resolve(2), { then: function() { foo(); } }]); |
michael@0 | 140 | ok(p instanceof Promise, "Should cast to a Promise."); |
michael@0 | 141 | p.then(function(v) { |
michael@0 | 142 | ok(false, "promiseAllCastError: should've rejected."); |
michael@0 | 143 | runTest(); |
michael@0 | 144 | }, function(e) { |
michael@0 | 145 | ok(e instanceof ReferenceError, "promiseCastThenableError"); |
michael@0 | 146 | runTest(); |
michael@0 | 147 | }); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | // Check that the resolved array is enumerable. |
michael@0 | 151 | function promiseAllEnumerable() { |
michael@0 | 152 | var p = Promise.all([1, new Date(), Promise.resolve("firefox")]); |
michael@0 | 153 | p.then(function(v) { |
michael@0 | 154 | var count = 0; |
michael@0 | 155 | for (key in v) { |
michael@0 | 156 | ++count; |
michael@0 | 157 | ok(v[key] === 1 || v[key] instanceof Date || v[key] === "firefox", |
michael@0 | 158 | "Enumerated properties don't match."); |
michael@0 | 159 | } |
michael@0 | 160 | is(count, 3, "Resolved array from Promise.all should be enumerable"); |
michael@0 | 161 | runTest(); |
michael@0 | 162 | }, function(e) { |
michael@0 | 163 | ok(false, "promiseAllEnumerable: should've resolved."); |
michael@0 | 164 | runTest(); |
michael@0 | 165 | }); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | function promiseRaceEmpty() { |
michael@0 | 169 | var p = Promise.race([]); |
michael@0 | 170 | ok(p instanceof Promise, "Should return a Promise."); |
michael@0 | 171 | p.then(function() { |
michael@0 | 172 | ok(false, "Should not resolve"); |
michael@0 | 173 | }, function() { |
michael@0 | 174 | ok(false, "Should not reject"); |
michael@0 | 175 | }); |
michael@0 | 176 | // Per spec, An empty race never resolves or rejects. |
michael@0 | 177 | setTimeout(function() { |
michael@0 | 178 | ok(true); |
michael@0 | 179 | runTest(); |
michael@0 | 180 | }, 50); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | function promiseRaceValuesArray() { |
michael@0 | 184 | var p = Promise.race([true, new Date(), 3]); |
michael@0 | 185 | ok(p instanceof Promise, "Should return a Promise."); |
michael@0 | 186 | p.then(function(winner) { |
michael@0 | 187 | is(winner, true, "First value should win."); |
michael@0 | 188 | runTest(); |
michael@0 | 189 | }, function(err) { |
michael@0 | 190 | ok(false, "Should not fail " + err + "."); |
michael@0 | 191 | runTest(); |
michael@0 | 192 | }); |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | function promiseRacePromiseArray() { |
michael@0 | 196 | function timeoutPromise(n) { |
michael@0 | 197 | return new Promise(function(resolve) { |
michael@0 | 198 | setTimeout(function() { |
michael@0 | 199 | resolve(n); |
michael@0 | 200 | }, n); |
michael@0 | 201 | }); |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | var arr = [ |
michael@0 | 205 | new Promise(function(resolve) { |
michael@0 | 206 | resolve("first"); |
michael@0 | 207 | }), |
michael@0 | 208 | Promise.resolve("second"), |
michael@0 | 209 | new Promise(function() {}), |
michael@0 | 210 | new Promise(function(resolve) { |
michael@0 | 211 | setTimeout(function() { |
michael@0 | 212 | setTimeout(function() { |
michael@0 | 213 | resolve("fourth"); |
michael@0 | 214 | }, 0); |
michael@0 | 215 | }, 0); |
michael@0 | 216 | }), |
michael@0 | 217 | ]; |
michael@0 | 218 | |
michael@0 | 219 | var p = Promise.race(arr); |
michael@0 | 220 | p.then(function(winner) { |
michael@0 | 221 | is(winner, "first", "First queued resolution should win the race."); |
michael@0 | 222 | runTest(); |
michael@0 | 223 | }); |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | function promiseRaceIterable() { |
michael@0 | 227 | function* participants() { |
michael@0 | 228 | yield new Promise(function(resolve) { |
michael@0 | 229 | setTimeout(resolve, 10, 10); |
michael@0 | 230 | }); |
michael@0 | 231 | yield new Promise(function(resolve) { |
michael@0 | 232 | setTimeout(resolve, 20, 20); |
michael@0 | 233 | }); |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | Promise.race(participants()).then(function(winner) { |
michael@0 | 237 | is(winner, 10, "Winner should be the one that finished earlier."); |
michael@0 | 238 | runTest(); |
michael@0 | 239 | }, function(e) { |
michael@0 | 240 | ok(false, "Promise.race shouldn't throw when an iterable is passed!"); |
michael@0 | 241 | runTest(); |
michael@0 | 242 | }); |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | function promiseRaceReject() { |
michael@0 | 246 | var p = Promise.race([ |
michael@0 | 247 | Promise.reject(new Error("Fail bad!")), |
michael@0 | 248 | new Promise(function(resolve) { |
michael@0 | 249 | setTimeout(resolve, 0); |
michael@0 | 250 | }) |
michael@0 | 251 | ]); |
michael@0 | 252 | |
michael@0 | 253 | p.then(function() { |
michael@0 | 254 | ok(false, "Should not resolve when winning Promise rejected."); |
michael@0 | 255 | runTest(); |
michael@0 | 256 | }, function(e) { |
michael@0 | 257 | ok(true, "Should be rejected"); |
michael@0 | 258 | ok(e instanceof Error, "Should reject with Error."); |
michael@0 | 259 | ok(e.message == "Fail bad!", "Message should match."); |
michael@0 | 260 | runTest(); |
michael@0 | 261 | }); |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | function promiseRaceThrow() { |
michael@0 | 265 | var p = Promise.race([ |
michael@0 | 266 | new Promise(function(resolve) { |
michael@0 | 267 | nonExistent(); |
michael@0 | 268 | }), |
michael@0 | 269 | new Promise(function(resolve) { |
michael@0 | 270 | setTimeout(resolve, 0); |
michael@0 | 271 | }) |
michael@0 | 272 | ]); |
michael@0 | 273 | |
michael@0 | 274 | p.then(function() { |
michael@0 | 275 | ok(false, "Should not resolve when winning Promise had an error."); |
michael@0 | 276 | runTest(); |
michael@0 | 277 | }, function(e) { |
michael@0 | 278 | ok(true, "Should be rejected"); |
michael@0 | 279 | ok(e instanceof ReferenceError, "Should reject with ReferenceError for function nonExistent()."); |
michael@0 | 280 | runTest(); |
michael@0 | 281 | }); |
michael@0 | 282 | } |
michael@0 | 283 | |
michael@0 | 284 | var tests = [ |
michael@0 | 285 | promiseUtilitiesDefined, |
michael@0 | 286 | promiseAllEmptyArray, |
michael@0 | 287 | promiseAllArray, |
michael@0 | 288 | promiseAllIterable, |
michael@0 | 289 | promiseAllWaitsForAllPromises, |
michael@0 | 290 | promiseAllRejectFails, |
michael@0 | 291 | promiseAllCastError, |
michael@0 | 292 | promiseAllEnumerable, |
michael@0 | 293 | |
michael@0 | 294 | promiseRaceEmpty, |
michael@0 | 295 | promiseRaceValuesArray, |
michael@0 | 296 | promiseRacePromiseArray, |
michael@0 | 297 | promiseRaceIterable, |
michael@0 | 298 | promiseRaceReject, |
michael@0 | 299 | promiseRaceThrow, |
michael@0 | 300 | ]; |
michael@0 | 301 | |
michael@0 | 302 | function runTest() { |
michael@0 | 303 | if (!tests.length) { |
michael@0 | 304 | SimpleTest.finish(); |
michael@0 | 305 | return; |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | var test = tests.shift(); |
michael@0 | 309 | test(); |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 313 | runTest(); |
michael@0 | 314 | // --> |
michael@0 | 315 | </script> |
michael@0 | 316 | </pre> |
michael@0 | 317 | </body> |
michael@0 | 318 | </html> |
michael@0 | 319 |