Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/dom/resources/promise-rejection-events.js

Issue 1416163003: Reland "Don't report promise rejection events during the microtask checkpoint" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: updates Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Source/bindings/core/v8/RejectedPromises.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 'use strict'; 1 'use strict';
2 2
3 if (self.importScripts) { 3 if (self.importScripts) {
4 if ('ServiceWorkerGlobalScope' in self && self instanceof ServiceWorkerGlobalS cope) { 4 if ('ServiceWorkerGlobalScope' in self && self instanceof ServiceWorkerGlobalS cope) {
5 importScripts('../../serviceworker/resources/worker-testharness.js'); 5 importScripts('../../serviceworker/resources/worker-testharness.js');
6 } else { 6 } else {
7 importScripts('../../resources/testharness.js'); 7 importScripts('../../resources/testharness.js');
8 } 8 }
9 } 9 }
10 10
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 postMessageTask(function() { 264 postMessageTask(function() {
265 p = Promise.resolve().then(function() { 265 p = Promise.resolve().then(function() {
266 return Promise.reject(e); 266 return Promise.reject(e);
267 }) 267 })
268 .catch(function() {}); 268 .catch(function() {});
269 }); 269 });
270 }, 'no unhandledrejection/rejectionhandled: all inside a queued task, a rejectio n handler attached synchronously to ' + 270 }, 'no unhandledrejection/rejectionhandled: all inside a queued task, a rejectio n handler attached synchronously to ' +
271 'a promise created from returning a Promise.reject-created promise in a fulfi llment handler'); 271 'a promise created from returning a Promise.reject-created promise in a fulfi llment handler');
272 272
273 // 273 //
274 // Negative unhandledrejection/rejectionhandled tests with delayed attachment 274 // Negative unhandledrejection/rejectionhandled tests with microtask-delayed att achment
275 // 275 //
276 276
277 async_test(function(t) { 277 async_test(function(t) {
278 var e = new Error(); 278 var e = new Error();
279 var p; 279 var p;
280 280
281 onUnhandledFail(t, function() { return p; }); 281 onUnhandledFail(t, function() { return p; });
282 282
283 p = Promise.reject(e); 283 p = Promise.reject(e);
284 mutationObserverMicrotask(function() { 284 mutationObserverMicrotask(function() {
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 mutationObserverMicrotask(function() { 442 mutationObserverMicrotask(function() {
443 p.catch(function() {}); 443 p.catch(function() {});
444 }); 444 });
445 }); 445 });
446 }); 446 });
447 }); 447 });
448 }, 0); 448 }, 0);
449 }, 'microtask nesting: attaching a handler inside a combination of promise micro tasks + mutationObserverMicrotask, ' + 449 }, 'microtask nesting: attaching a handler inside a combination of promise micro tasks + mutationObserverMicrotask, ' +
450 'all inside a setTimeout'); 450 'all inside a setTimeout');
451 451
452
453 // For workers, postMessageTask() involves posting tasks to other threads, so
454 // the following tests don't work there.
455
456 if ('document' in self) {
457 //
458 // Negative unhandledrejection/rejectionhandled tests with task-delayed attach ment
459 //
460
461 async_test(function(t) {
462 var e = new Error();
463 var p;
464
465 onUnhandledFail(t, function() { return p; });
466
467 var _reject;
468 p = new Promise(function(_, reject) {
469 _reject = reject;
470 });
471 _reject(e);
472 postMessageTask(function() {
473 var unreached = t.unreached_func('promise should not be fulfilled');
474 p.then(unreached, function() {});
475 });
476 }, 'delayed handling: a task delay before attaching a handler prevents unhandl edrejection');
477
478 async_test(function(t) {
479 var e = new Error();
480 var p;
481
482 onUnhandledFail(t, function() { return p; });
483
484 p = Promise.reject(e);
485 postMessageTask(function() {
486 Promise.resolve().then(function() {
487 p.catch(function() {});
488 });
489 });
490 }, 'delayed handling: postMessageTask after promise creation/rejection, plus p romise microtasks, is not too late to ' +
491 'attach a rejection handler');
492
493 async_test(function(t) {
494 var e = new Error();
495 var p;
496
497 onUnhandledFail(t, function() { return p; });
498
499 postMessageTask(function() {
500 Promise.resolve().then(function() {
501 Promise.resolve().then(function() {
502 Promise.resolve().then(function() {
503 Promise.resolve().then(function() {
504 p.catch(function() {});
505 });
506 });
507 });
508 });
509 });
510 p = Promise.reject(e);
511 }, 'delayed handling: postMessageTask before promise creation/rejection, plus many promise microtasks, is not too ' +
512 'late to attach a rejection handler');
513
514 async_test(function(t) {
515 var e = new Error();
516 var p;
517
518 onUnhandledFail(t, function() { return p; });
519
520 p = Promise.reject(e);
521 postMessageTask(function() {
522 Promise.resolve().then(function() {
523 Promise.resolve().then(function() {
524 Promise.resolve().then(function() {
525 Promise.resolve().then(function() {
526 p.catch(function() {});
527 });
528 });
529 });
530 });
531 });
532 }, 'delayed handling: postMessageTask after promise creation/rejection, plus m any promise microtasks, is not too ' +
533 'late to attach a rejection handler');
534 }
535
452 // 536 //
453 // Positive unhandledrejection/rejectionhandled tests with delayed attachment 537 // Positive unhandledrejection/rejectionhandled tests with delayed attachment
454 // 538 //
455 539
456 async_test(function(t) { 540 async_test(function(t) {
457 var e = new Error(); 541 var e = new Error();
458 var p; 542 var p;
459 543
460 onUnhandledSucceed(t, e, function() { return p; }); 544 onUnhandledSucceed(t, e, function() { return p; });
461 545
462 var _reject; 546 var _reject;
463 p = new Promise(function(_, reject) { 547 p = new Promise(function(_, reject) {
464 _reject = reject; 548 _reject = reject;
465 }); 549 });
466 _reject(e); 550 _reject(e);
467 postMessageTask(function() { 551 postMessageTask(function() {
468 var unreached = t.unreached_func('promise should not be fulfilled'); 552 postMessageTask(function() {
469 p.then(unreached, function() {}); 553 var unreached = t.unreached_func('promise should not be fulfilled');
554 p.then(unreached, function() {});
555 });
470 }); 556 });
471 }, 'delayed handling: a task delay before attaching a handler does not prevent u nhandledrejection'); 557 }, 'delayed handling: a nested-task delay before attaching a handler causes unha ndledrejection');
472
473 async_test(function(t) {
474 var unhandledPromises = [];
475 var unhandledReasons = [];
476 var e = new Error();
477 var p;
478
479 var unhandled = function(ev) {
480 if (ev.promise === p) {
481 t.step(function() {
482 unhandledPromises.push(ev.promise);
483 unhandledReasons.push(ev.reason);
484 });
485 }
486 };
487 var handled = function(ev) {
488 if (ev.promise === p) {
489 t.step(function() {
490 assert_array_equals(unhandledPromises, [p]);
491 assert_array_equals(unhandledReasons, [e]);
492 assert_equals(ev.promise, p);
493 assert_equals(ev.reason, e);
494 });
495 }
496 };
497 addEventListener('unhandledrejection', unhandled);
498 addEventListener('rejectionhandled', handled);
499 ensureCleanup(t, unhandled, handled);
500
501 p = new Promise(function() {
502 throw e;
503 });
504 setTimeout(function() {
505 var unreached = t.unreached_func('promise should not be fulfilled');
506 p.then(unreached, function(reason) {
507 assert_equals(reason, e);
508 setTimeout(function() { t.done(); }, 10);
509 });
510 }, 10);
511 }, 'delayed handling: delaying handling by setTimeout(,10) will cause both event s to fire');
512 558
513 async_test(function(t) { 559 async_test(function(t) {
514 var e = new Error(); 560 var e = new Error();
515 var p; 561 var p;
516 562
517 onUnhandledSucceed(t, e, function() { return p; }); 563 onUnhandledSucceed(t, e, function() { return p; });
518 564
519 p = Promise.reject(e); 565 p = Promise.reject(e);
520 postMessageTask(function() { 566 postMessageTask(function() {
521 Promise.resolve().then(function() { 567 postMessageTask(function() {
522 p.catch(function() {}); 568 Promise.resolve().then(function() {
569 p.catch(function() {});
570 });
523 }); 571 });
524 }); 572 });
525 }, 'delayed handling: postMessageTask after promise creation/rejection, plus pro mise microtasks, is too late to ' + 573 }, 'delayed handling: a nested-postMessageTask after promise creation/rejection, plus promise microtasks, is too ' +
526 'attach a rejection handler'); 574 'late to attach a rejection handler');
527 575
528 async_test(function(t) { 576 async_test(function(t) {
529 var e = new Error(); 577 var e = new Error();
530 var p; 578 var p;
531 579
532 onUnhandledSucceed(t, e, function() { return p; }); 580 onUnhandledSucceed(t, e, function() { return p; });
581
533 postMessageTask(function() { 582 postMessageTask(function() {
534 Promise.resolve().then(function() { 583 postMessageTask(function() {
535 Promise.resolve().then(function() { 584 Promise.resolve().then(function() {
536 Promise.resolve().then(function() { 585 Promise.resolve().then(function() {
537 Promise.resolve().then(function() { 586 Promise.resolve().then(function() {
538 p.catch(function() {}); 587 Promise.resolve().then(function() {
588 p.catch(function() {});
589 });
539 }); 590 });
540 }); 591 });
541 }); 592 });
542 }); 593 });
543 }); 594 });
544 p = Promise.reject(e); 595 p = Promise.reject(e);
545 }, 'delayed handling: postMessageTask before promise creation/rejection, plus ma ny promise microtasks, is too late ' + 596 }, 'delayed handling: a nested-postMessageTask before promise creation/rejection , plus many promise microtasks, is ' +
546 'to attach a rejection handler'); 597 'too late to attach a rejection handler');
547 598
548 async_test(function(t) { 599 async_test(function(t) {
549 var e = new Error(); 600 var e = new Error();
550 var p; 601 var p;
551 602
552 onUnhandledSucceed(t, e, function() { return p; }); 603 onUnhandledSucceed(t, e, function() { return p; });
553 604
554 p = Promise.reject(e); 605 p = Promise.reject(e);
555 postMessageTask(function() { 606 postMessageTask(function() {
556 Promise.resolve().then(function() { 607 postMessageTask(function() {
557 Promise.resolve().then(function() { 608 Promise.resolve().then(function() {
558 Promise.resolve().then(function() { 609 Promise.resolve().then(function() {
559 Promise.resolve().then(function() { 610 Promise.resolve().then(function() {
560 p.catch(function() {}); 611 Promise.resolve().then(function() {
612 p.catch(function() {});
613 });
561 }); 614 });
562 }); 615 });
563 }); 616 });
564 }); 617 });
565 }); 618 });
566 }, 'delayed handling: postMessageTask after promise creation/rejection, plus man y promise microtasks, is too late ' + 619 }, 'delayed handling: a nested-postMessageTask after promise creation/rejection, plus many promise microtasks, is ' +
567 'to attach a rejection handler'); 620 'too late to attach a rejection handler');
621
622 async_test(function(t) {
623 var unhandledPromises = [];
624 var unhandledReasons = [];
625 var e = new Error();
626 var p;
627
628 var unhandled = function(ev) {
629 if (ev.promise === p) {
630 t.step(function() {
631 unhandledPromises.push(ev.promise);
632 unhandledReasons.push(ev.reason);
633 });
634 }
635 };
636 var handled = function(ev) {
637 if (ev.promise === p) {
638 t.step(function() {
639 assert_array_equals(unhandledPromises, [p]);
640 assert_array_equals(unhandledReasons, [e]);
641 assert_equals(ev.promise, p);
642 assert_equals(ev.reason, e);
643 });
644 }
645 };
646 addEventListener('unhandledrejection', unhandled);
647 addEventListener('rejectionhandled', handled);
648 ensureCleanup(t, unhandled, handled);
649
650 p = new Promise(function() {
651 throw e;
652 });
653 setTimeout(function() {
654 var unreached = t.unreached_func('promise should not be fulfilled');
655 p.then(unreached, function(reason) {
656 assert_equals(reason, e);
657 setTimeout(function() { t.done(); }, 10);
658 });
659 }, 10);
660 }, 'delayed handling: delaying handling by setTimeout(,10) will cause both event s to fire');
568 661
569 // 662 //
570 // Miscellaneous tests about integration with the rest of the platform 663 // Miscellaneous tests about integration with the rest of the platform
571 // 664 //
572 665
573 async_test(function(t) { 666 async_test(function(t) {
574 var e = new Error(); 667 var e = new Error();
575 var l = function(ev) { 668 var l = function(ev) {
576 var order = []; 669 var order = [];
577 mutationObserverMicrotask(function() { 670 mutationObserverMicrotask(function() {
578 order.push(1); 671 order.push(1);
579 }); 672 });
580 setTimeout(function() { 673 setTimeout(function() {
581 order.push(2); 674 order.push(2);
582 t.step(function() { 675 t.step(function() {
583 assert_array_equals(order, [1, 2]); 676 assert_array_equals(order, [1, 2]);
584 }); 677 });
585 t.done(); 678 t.done();
586 }, 1); 679 }, 1);
587 }; 680 };
588 addEventListener('unhandledrejection', l); 681 addEventListener('unhandledrejection', l);
589 ensureCleanup(t, l); 682 ensureCleanup(t, l);
590 Promise.reject(e); 683 Promise.reject(e);
591 }, 'mutationObserverMicrotask vs. postMessageTask ordering is not disturbed insi de unhandledrejection events'); 684 }, 'mutationObserverMicrotask vs. postMessageTask ordering is not disturbed insi de unhandledrejection events');
592 685
686 // For workers, postMessageTask() involves posting tasks to other threads, so
687 // the following tests don't work there.
688
689 if ('document' in self) {
690
691 // For the next two see https://github.com/domenic/unhandled-rejections-browse r-spec/issues/2#issuecomment-121121695
692 // and the following comments.
693
694 async_test(function(t) {
695 var sequenceOfEvents = [];
696
697 addEventListener('unhandledrejection', l);
698 ensureCleanup(t, l);
699
700 var p1 = Promise.reject();
701 var p2;
702 postMessageTask(function() {
703 p2 = Promise.reject();
704 postMessageTask(function() {
705 sequenceOfEvents.push('postMessageTask');
706 checkSequence();
707 });
708 });
709
710 function l(ev) {
711 if (ev.promise === p1 || ev.promise === p2) {
712 sequenceOfEvents.push(ev.promise);
713 checkSequence();
714 }
715 }
716
717 function checkSequence() {
718 if (sequenceOfEvents.length === 3) {
719 t.step(function() {
720 assert_array_equals(sequenceOfEvents, [p1, 'postMessageTask', p2]);
721 });
722 t.done();
723 }
724 }
725 }, 'postMessageTask ordering vs. the task queued for unhandled rejection notif ication (1)');
726
727 async_test(function(t) {
728 var sequenceOfEvents = [];
729
730 addEventListener('unhandledrejection', l);
731 ensureCleanup(t, l);
732
733 var p2;
734 postMessageTask(function() {
735 p2 = Promise.reject();
736 postMessageTask(function() {
737 sequenceOfEvents.push('postMessageTask');
738 checkSequence();
739 });
740 });
741
742 function l(ev) {
743 if (ev.promise == p2) {
744 sequenceOfEvents.push(ev.promise);
745 checkSequence();
746 }
747 }
748
749 function checkSequence() {
750 if (sequenceOfEvents.length === 2) {
751 t.step(function() {
752 assert_array_equals(sequenceOfEvents, ['postMessageTask', p2]);
753 });
754 t.done();
755 }
756 }
757 }, 'postMessageTask ordering vs. the task queued for unhandled rejection notif ication (2)');
758
759 async_test(function(t) {
760 var sequenceOfEvents = [];
761
762
763 addEventListener('unhandledrejection', unhandled);
764 addEventListener('rejectionhandled', handled);
765 ensureCleanup(t, unhandled, handled);
766
767 var p = Promise.reject();
768
769 setTimeout(function() {
770 postMessageTask(function() {
771 sequenceOfEvents.push('task before catch');
772 checkSequence();
773 });
774
775 p.catch(function() {
776 sequenceOfEvents.push('catch');
777 checkSequence();
778 });
779
780 postMessageTask(function() {
781 sequenceOfEvents.push('task after catch');
782 checkSequence();
783 });
784
785 sequenceOfEvents.push('after catch');
786 checkSequence();
787 }, 10);
788
789 function unhandled(ev) {
790 if (ev.promise === p) {
791 sequenceOfEvents.push('unhandled');
792 checkSequence();
793 }
794 }
795
796 function handled(ev) {
797 if (ev.promise === p) {
798 sequenceOfEvents.push('handled');
799 checkSequence();
800 }
801 }
802
803 function checkSequence() {
804 if (sequenceOfEvents.length === 6) {
805 t.step(function() {
806 assert_array_equals(sequenceOfEvents,
807 ['unhandled', 'after catch', 'catch', 'task before catch', 'handled' , 'task after catch']);
808 });
809 t.done();
810 }
811 }
812 }, 'rejectionhandled is dispatched from a queued task, and not immediately');
813 }
814
593 // 815 //
594 // HELPERS 816 // HELPERS
595 // 817 //
596 818
819 var globalPostMessageCounter = 0;
820
597 function postMessageTask(f) { 821 function postMessageTask(f) {
598 if ('document' in self) { 822 if ('document' in self) {
599 var l = function() { 823 var message = 'abusingpostmessageforfunandprofit' + globalPostMessageCounter ;
600 removeEventListener('message', l); 824 globalPostMessageCounter++;
601 f(); 825 var l = function(ev) {
826 if (ev.data === message) {
827 removeEventListener('message', l);
828 f();
829 }
602 }; 830 };
603 addEventListener('message', l); 831 addEventListener('message', l);
604 postMessage('abusingpostmessageforfunandprofit', '*'); 832 postMessage(message, '*');
605 } else { 833 } else {
606 var channel = new MessageChannel(); 834 var channel = new MessageChannel();
607 channel.port1.onmessage = function() { channel.port1.close(); f(); }; 835 channel.port1.onmessage = function() { channel.port1.close(); f(); };
608 channel.port2.postMessage('abusingpostmessageforfunandprofit'); 836 channel.port2.postMessage('abusingpostmessageforfunandprofit');
609 channel.port2.close(); 837 channel.port2.close();
610 } 838 }
611 } 839 }
612 840
613 function mutationObserverMicrotask(f) { 841 function mutationObserverMicrotask(f) {
614 if ('document' in self) { 842 if ('document' in self) {
(...skipping 18 matching lines...) Expand all
633 t.done(); 861 t.done();
634 } 862 }
635 }; 863 };
636 addEventListener('unhandledrejection', l); 864 addEventListener('unhandledrejection', l);
637 ensureCleanup(t, l); 865 ensureCleanup(t, l);
638 } 866 }
639 867
640 function onUnhandledFail(t, expectedPromiseGetter) { 868 function onUnhandledFail(t, expectedPromiseGetter) {
641 var unhandled = function(evt) { 869 var unhandled = function(evt) {
642 if (evt.promise === expectedPromiseGetter()) { 870 if (evt.promise === expectedPromiseGetter()) {
643 t.unreached_func('unhandledrejection event is not supposed to be triggered '); 871 t.step(function() {
872 assert_unreached('unhandledrejection event is not supposed to be trigger ed');
873 });
644 } 874 }
645 }; 875 };
646 var handled = function(evt) { 876 var handled = function(evt) {
647 if (evt.promise === expectedPromiseGetter()) { 877 if (evt.promise === expectedPromiseGetter()) {
648 t.unreached_func('rejectionhandled event is not supposed to be triggered') ; 878 t.step(function() {
879 assert_unreached('rejectionhandled event is not supposed to be triggered ');
880 });
649 } 881 }
650 }; 882 };
651 addEventListener('unhandledrejection', unhandled); 883 addEventListener('unhandledrejection', unhandled);
652 addEventListener('rejectionhandled', handled); 884 addEventListener('rejectionhandled', handled);
653 ensureCleanup(t, unhandled, handled); 885 ensureCleanup(t, unhandled, handled);
654 setTimeout(function() { 886 setTimeout(function() {
655 t.done(); 887 t.done();
656 }, 10); 888 }, 10);
657 } 889 }
658 890
659 function ensureCleanup(t, unhandled, handled) { 891 function ensureCleanup(t, unhandled, handled) {
660 t.add_cleanup(function() { 892 t.add_cleanup(function() {
661 if (unhandled) 893 if (unhandled)
662 removeEventListener('unhandledrejection', unhandled); 894 removeEventListener('unhandledrejection', unhandled);
663 if (handled) 895 if (handled)
664 removeEventListener('rejectionhandled', handled); 896 removeEventListener('rejectionhandled', handled);
665 }); 897 });
666 } 898 }
667 899
668 done(); 900 done();
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/bindings/core/v8/RejectedPromises.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698