test_promise_utils.html 8.6 KB

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