| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library scheduled_test_test; | 5 library scheduled_test_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:scheduled_test/scheduled_test.dart'; | 9 import 'package:scheduled_test/scheduled_test.dart'; |
| 10 import 'package:scheduled_test/src/mock_clock.dart' as mock_clock; |
| 10 | 11 |
| 11 import 'metatest.dart'; | 12 import 'metatest.dart'; |
| 12 import 'utils.dart'; | 13 import 'utils.dart'; |
| 13 | 14 |
| 14 void main() { | 15 void main() { |
| 15 expectTestsPass('a scheduled test with a correct synchronous expectation ' | 16 expectTestsPass('a scheduled test with a correct synchronous expectation ' |
| 16 'should pass', () { | 17 'should pass', () { |
| 17 test('test', () { | 18 test('test', () { |
| 18 expect('foo', equals('foo')); | 19 expect('foo', equals('foo')); |
| 19 }); | 20 }); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 expectTestsFail('an error in a chained future in a scheduled block should be ' | 115 expectTestsFail('an error in a chained future in a scheduled block should be ' |
| 115 'registered', () { | 116 'registered', () { |
| 116 test('test', () { | 117 test('test', () { |
| 117 schedule(() => new Future.immediate(null).then((_) { | 118 schedule(() => new Future.immediate(null).then((_) { |
| 118 throw 'error'; | 119 throw 'error'; |
| 119 })); | 120 })); |
| 120 }); | 121 }); |
| 121 }); | 122 }); |
| 122 | 123 |
| 123 expectTestsFail('an out-of-band failure in wrapAsync is handled', () { | 124 expectTestsFail('an out-of-band failure in wrapAsync is handled', () { |
| 125 mock_clock.mock().run(); |
| 124 test('test', () { | 126 test('test', () { |
| 125 var waiter = new Waiter(); | |
| 126 schedule(() { | 127 schedule(() { |
| 127 waiter.wait(1).then(wrapAsync((_) => expect('foo', equals('bar')))); | 128 sleep(1).then(wrapAsync((_) => expect('foo', equals('bar')))); |
| 128 }); | 129 }); |
| 129 schedule(() => waiter.wait(2)); | 130 schedule(() => sleep(2)); |
| 130 }); | 131 }); |
| 131 }); | 132 }); |
| 132 | 133 |
| 133 expectTestsFail('an out-of-band failure in wrapAsync that finishes after the ' | 134 expectTestsFail('an out-of-band failure in wrapAsync that finishes after the ' |
| 134 'schedule is handled', () { | 135 'schedule is handled', () { |
| 136 mock_clock.mock().run(); |
| 135 test('test', () { | 137 test('test', () { |
| 136 var waiter = new Waiter(); | |
| 137 schedule(() { | 138 schedule(() { |
| 138 waiter.wait(2).then(wrapAsync((_) => expect('foo', equals('bar')))); | 139 sleep(2).then(wrapAsync((_) => expect('foo', equals('bar')))); |
| 139 }); | 140 }); |
| 140 schedule(() => waiter.wait(1)); | 141 schedule(() => sleep(1)); |
| 141 }); | 142 }); |
| 142 }); | 143 }); |
| 143 | 144 |
| 144 expectTestsFail('an out-of-band error reported via signalError is ' | 145 expectTestsFail('an out-of-band error reported via signalError is ' |
| 145 'handled', () { | 146 'handled', () { |
| 147 mock_clock.mock().run(); |
| 146 test('test', () { | 148 test('test', () { |
| 147 var waiter = new Waiter(); | |
| 148 schedule(() { | 149 schedule(() { |
| 149 waiter.wait(1).then((_) => currentSchedule.signalError('bad')); | 150 sleep(1).then((_) => currentSchedule.signalError('bad')); |
| 150 }); | 151 }); |
| 151 schedule(() => waiter.wait(2)); | 152 schedule(() => sleep(2)); |
| 152 }); | 153 }); |
| 153 }); | 154 }); |
| 154 | 155 |
| 155 expectTestsFail('an out-of-band error reported via signalError that finished ' | 156 expectTestsFail('an out-of-band error reported via signalError that finished ' |
| 156 'after the schedule is handled', () { | 157 'after the schedule is handled', () { |
| 158 mock_clock.mock().run(); |
| 157 test('test', () { | 159 test('test', () { |
| 158 var waiter = new Waiter(); | |
| 159 schedule(() { | 160 schedule(() { |
| 160 var done = wrapAsync((_) {}); | 161 var done = wrapAsync((_) {}); |
| 161 waiter.wait(2).then((_) { | 162 sleep(2).then((_) { |
| 162 currentSchedule.signalError('bad'); | 163 currentSchedule.signalError('bad'); |
| 163 done(null); | 164 done(null); |
| 164 }); | 165 }); |
| 165 }); | 166 }); |
| 166 schedule(() => waiter.wait(1)); | 167 schedule(() => sleep(1)); |
| 167 }); | 168 }); |
| 168 }); | 169 }); |
| 169 | 170 |
| 170 expectTestsFail('a synchronous error reported via signalError is handled', ()
{ | 171 expectTestsFail('a synchronous error reported via signalError is handled', ()
{ |
| 171 test('test', () { | 172 test('test', () { |
| 172 currentSchedule.signalError('bad'); | 173 currentSchedule.signalError('bad'); |
| 173 }); | 174 }); |
| 174 }); | 175 }); |
| 175 | 176 |
| 176 expectTestsPass('the onComplete queue is run if a test is successful', () { | 177 expectTestsPass('the onComplete queue is run if a test is successful', () { |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 }); | 486 }); |
| 486 | 487 |
| 487 test('test 2', () { | 488 test('test 2', () { |
| 488 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 489 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 489 expect(errors.map((e) => e.error), equals(['error1', 'error2'])); | 490 expect(errors.map((e) => e.error), equals(['error1', 'error2'])); |
| 490 }); | 491 }); |
| 491 }, passing: ['test 2']); | 492 }, passing: ['test 2']); |
| 492 | 493 |
| 493 expectTestsPass('currentSchedule.errors contains multiple out-of-band errors ' | 494 expectTestsPass('currentSchedule.errors contains multiple out-of-band errors ' |
| 494 'from both the main task queue and onException in onComplete', () { | 495 'from both the main task queue and onException in onComplete', () { |
| 496 mock_clock.mock().run(); |
| 495 var errors; | 497 var errors; |
| 496 test('test 1', () { | 498 test('test 1', () { |
| 497 currentSchedule.onComplete.schedule(() { | 499 currentSchedule.onComplete.schedule(() { |
| 498 errors = currentSchedule.errors; | 500 errors = currentSchedule.errors; |
| 499 }); | 501 }); |
| 500 | 502 |
| 501 currentSchedule.onException.schedule(() { | 503 currentSchedule.onException.schedule(() { |
| 502 var exceptionWaiter = new Waiter(); | 504 sleep(1).then(wrapAsync((_) { |
| 503 exceptionWaiter.wait(1).then(wrapAsync((_) { | |
| 504 throw 'error3'; | 505 throw 'error3'; |
| 505 })); | 506 })); |
| 506 exceptionWaiter.wait(2).then(wrapAsync((_) { | 507 sleep(2).then(wrapAsync((_) { |
| 507 throw 'error4'; | 508 throw 'error4'; |
| 508 })); | 509 })); |
| 509 }); | 510 }); |
| 510 | 511 |
| 511 var waiter = new Waiter(); | 512 sleep(1).then(wrapAsync((_) { |
| 512 waiter.wait(1).then(wrapAsync((_) { | |
| 513 throw 'error1'; | 513 throw 'error1'; |
| 514 })); | 514 })); |
| 515 waiter.wait(2).then(wrapAsync((_) { | 515 sleep(2).then(wrapAsync((_) { |
| 516 throw 'error2'; | 516 throw 'error2'; |
| 517 })); | 517 })); |
| 518 }); | 518 }); |
| 519 | 519 |
| 520 test('test 2', () { | 520 test('test 2', () { |
| 521 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 521 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 522 expect(errors.map((e) => e.error), | 522 expect(errors.map((e) => e.error), |
| 523 orderedEquals(['error1', 'error2', 'error3', 'error4'])); | 523 orderedEquals(['error1', 'error2', 'error3', 'error4'])); |
| 524 }); | 524 }); |
| 525 }, passing: ['test 2']); | 525 }, passing: ['test 2']); |
| 526 | 526 |
| 527 expectTestsPass('currentSchedule.errors contains both an out-of-band error ' | 527 expectTestsPass('currentSchedule.errors contains both an out-of-band error ' |
| 528 'and an error raised afterwards in a task', () { | 528 'and an error raised afterwards in a task', () { |
| 529 mock_clock.mock().run(); |
| 529 var errors; | 530 var errors; |
| 530 test('test 1', () { | 531 test('test 1', () { |
| 531 currentSchedule.onComplete.schedule(() { | 532 currentSchedule.onComplete.schedule(() { |
| 532 errors = currentSchedule.errors; | 533 errors = currentSchedule.errors; |
| 533 }); | 534 }); |
| 534 | 535 |
| 535 var waiter = new Waiter(); | 536 sleep(1).then(wrapAsync((_) { |
| 536 waiter.wait(1).then(wrapAsync((_) { | |
| 537 throw 'out-of-band'; | 537 throw 'out-of-band'; |
| 538 })); | 538 })); |
| 539 | 539 |
| 540 schedule(() => waiter.wait(2).then((_) { | 540 schedule(() => sleep(2).then((_) { |
| 541 throw 'in-band'; | 541 throw 'in-band'; |
| 542 })); | 542 })); |
| 543 }); | 543 }); |
| 544 | 544 |
| 545 test('test 2', () { | 545 test('test 2', () { |
| 546 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 546 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 547 expect(errors.map((e) => e.error), equals(['out-of-band', 'in-band'])); | 547 expect(errors.map((e) => e.error), equals(['out-of-band', 'in-band'])); |
| 548 }); | 548 }); |
| 549 }, passing: ['test 2']); | 549 }, passing: ['test 2']); |
| 550 | 550 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 | 585 |
| 586 expectTestsPass('currentSchedule.currentQueue returns the current queue while
' | 586 expectTestsPass('currentSchedule.currentQueue returns the current queue while
' |
| 587 'executing a task', () { | 587 'executing a task', () { |
| 588 test('test', () { | 588 test('test', () { |
| 589 schedule(() { | 589 schedule(() { |
| 590 expect(currentSchedule.currentQueue.name, equals('tasks')); | 590 expect(currentSchedule.currentQueue.name, equals('tasks')); |
| 591 }); | 591 }); |
| 592 }); | 592 }); |
| 593 }); | 593 }); |
| 594 | 594 |
| 595 expectTestsPass('currentSchedule.currentQueue is null before the schedule has
' | 595 expectTestsPass('currentSchedule.currentQueue is tasks before the schedule ' |
| 596 'started', () { | 596 'has started', () { |
| 597 test('test', () { | 597 test('test', () { |
| 598 schedule(() => expect('foo', equals('foo'))); | 598 schedule(() => expect('foo', equals('foo'))); |
| 599 | 599 |
| 600 expect(currentSchedule.currentQueue, isNull); | 600 expect(currentSchedule.currentQueue.name, equals('tasks')); |
| 601 }); | 601 }); |
| 602 }); | 602 }); |
| 603 | 603 |
| 604 expectTestsPass('currentSchedule.state starts out as SET_UP', () { |
| 605 test('test', () { |
| 606 expect(currentSchedule.state, equals(ScheduleState.SET_UP)); |
| 607 }); |
| 608 }); |
| 609 |
| 610 expectTestsPass('currentSchedule.state is RUNNING in tasks', () { |
| 611 test('test', () { |
| 612 schedule(() { |
| 613 expect(currentSchedule.state, equals(ScheduleState.RUNNING)); |
| 614 }); |
| 615 |
| 616 currentSchedule.onComplete.schedule(() { |
| 617 expect(currentSchedule.state, equals(ScheduleState.RUNNING)); |
| 618 }); |
| 619 }); |
| 620 }); |
| 621 |
| 622 expectTestsPass('currentSchedule.state is DONE after the test', () { |
| 623 var oldSchedule; |
| 624 test('test 1', () { |
| 625 oldSchedule = currentSchedule; |
| 626 }); |
| 627 |
| 628 test('test 2', () { |
| 629 expect(oldSchedule.state, equals(ScheduleState.DONE)); |
| 630 }); |
| 631 }); |
| 632 |
| 604 expectTestsPass('setUp is run before each test', () { | 633 expectTestsPass('setUp is run before each test', () { |
| 605 var setUpRun = false; | 634 var setUpRun = false; |
| 606 setUp(() { | 635 setUp(() { |
| 607 setUpRun = true; | 636 setUpRun = true; |
| 608 }); | 637 }); |
| 609 | 638 |
| 610 test('test 1', () { | 639 test('test 1', () { |
| 611 expect(setUpRun, isTrue); | 640 expect(setUpRun, isTrue); |
| 612 setUpRun = false; | 641 setUpRun = false; |
| 613 }); | 642 }); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 expect(setUpRun, isTrue); | 750 expect(setUpRun, isTrue); |
| 722 }); | 751 }); |
| 723 }); | 752 }); |
| 724 | 753 |
| 725 group('group 2', () { | 754 group('group 2', () { |
| 726 test('test 2', () { | 755 test('test 2', () { |
| 727 expect(setUpRun, isFalse); | 756 expect(setUpRun, isFalse); |
| 728 }); | 757 }); |
| 729 }); | 758 }); |
| 730 }); | 759 }); |
| 760 |
| 761 expectTestsPass("a single task that takes too long will cause a timeout " |
| 762 "error", () { |
| 763 mock_clock.mock().run(); |
| 764 var errors; |
| 765 test('test 1', () { |
| 766 currentSchedule.timeout = new Duration(milliseconds: 1); |
| 767 |
| 768 currentSchedule.onException.schedule(() { |
| 769 errors = currentSchedule.errors; |
| 770 }); |
| 771 |
| 772 schedule(() => sleep(2)); |
| 773 }); |
| 774 |
| 775 test('test 2', () { |
| 776 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 777 expect(errors.map((e) => e.error), equals(["The schedule timed out after " |
| 778 "1ms of inactivity."])); |
| 779 }); |
| 780 }, passing: ['test 2']); |
| 781 |
| 782 expectTestsPass("an out-of-band callback that takes too long will cause a " |
| 783 "timeout error", () { |
| 784 mock_clock.mock().run(); |
| 785 var errors; |
| 786 test('test 1', () { |
| 787 currentSchedule.timeout = new Duration(milliseconds: 1); |
| 788 |
| 789 currentSchedule.onException.schedule(() { |
| 790 errors = currentSchedule.errors; |
| 791 }); |
| 792 |
| 793 sleep(2).then(wrapAsync((_) => expect('foo', equals('foo')))); |
| 794 }); |
| 795 |
| 796 test('test 2', () { |
| 797 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 798 expect(errors.map((e) => e.error), equals(["The schedule timed out after " |
| 799 "1ms of inactivity."])); |
| 800 }); |
| 801 }, passing: ['test 2']); |
| 802 |
| 803 expectTestsPass("each task resets the timeout timer", () { |
| 804 mock_clock.mock().run(); |
| 805 test('test', () { |
| 806 currentSchedule.timeout = new Duration(milliseconds: 2); |
| 807 |
| 808 schedule(() => sleep(1)); |
| 809 schedule(() => sleep(1)); |
| 810 schedule(() => sleep(1)); |
| 811 }); |
| 812 }); |
| 813 |
| 814 expectTestsPass("setting up the test doesn't trigger a timeout", () { |
| 815 var clock = mock_clock.mock(); |
| 816 test('test', () { |
| 817 currentSchedule.timeout = new Duration(milliseconds: 1); |
| 818 |
| 819 clock.tick(2); |
| 820 schedule(() => expect('foo', equals('foo'))); |
| 821 }); |
| 822 }); |
| 823 |
| 824 expectTestsPass("an out-of-band error that's signaled after a timeout but " |
| 825 "before the test completes is registered", () { |
| 826 mock_clock.mock().run(); |
| 827 var errors; |
| 828 test('test 1', () { |
| 829 currentSchedule.timeout = new Duration(milliseconds: 3); |
| 830 |
| 831 currentSchedule.onException.schedule(() => sleep(2)); |
| 832 currentSchedule.onException.schedule(() { |
| 833 errors = currentSchedule.errors; |
| 834 }); |
| 835 |
| 836 sleep(4).then(wrapAsync((_) { |
| 837 throw 'out-of-band'; |
| 838 })); |
| 839 }); |
| 840 |
| 841 test('test 2', () { |
| 842 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 843 expect(errors.map((e) => e.error), equals([ |
| 844 "The schedule timed out after 3ms of inactivity.", |
| 845 "out-of-band" |
| 846 ])); |
| 847 }); |
| 848 }, passing: ['test 2']); |
| 849 |
| 850 expectTestsPass("an out-of-band error that's signaled after a timeout but " |
| 851 "before the test completes plays nicely with other out-of-band callbacks", |
| 852 () { |
| 853 mock_clock.mock().run(); |
| 854 var errors; |
| 855 var onExceptionCallbackRun = false; |
| 856 var onCompleteRunAfterOnExceptionCallback = false; |
| 857 test('test 1', () { |
| 858 currentSchedule.timeout = new Duration(milliseconds: 2); |
| 859 |
| 860 currentSchedule.onException.schedule(() { |
| 861 sleep(1).then(wrapAsync((_) { |
| 862 onExceptionCallbackRun = true; |
| 863 })); |
| 864 }); |
| 865 |
| 866 currentSchedule.onComplete.schedule(() { |
| 867 onCompleteRunAfterOnExceptionCallback = onExceptionCallbackRun; |
| 868 }); |
| 869 |
| 870 sleep(3).then(wrapAsync((_) { |
| 871 throw 'out-of-band'; |
| 872 })); |
| 873 }); |
| 874 |
| 875 test('test 2', () { |
| 876 expect(onCompleteRunAfterOnExceptionCallback, isTrue); |
| 877 }); |
| 878 }, passing: ['test 2']); |
| 879 |
| 880 expectTestsPass("a task that times out while waiting to handle an " |
| 881 "out-of-band error records both", () { |
| 882 mock_clock.mock().run(); |
| 883 var errors; |
| 884 test('test 1', () { |
| 885 currentSchedule.timeout = new Duration(milliseconds: 2); |
| 886 |
| 887 currentSchedule.onException.schedule(() { |
| 888 errors = currentSchedule.errors; |
| 889 }); |
| 890 |
| 891 schedule(() => sleep(4)); |
| 892 sleep(1).then((_) => currentSchedule.signalError('out-of-band')); |
| 893 }); |
| 894 |
| 895 test('test 2', () { |
| 896 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 897 expect(errors.map((e) => e.error), equals([ |
| 898 "out-of-band", |
| 899 "The schedule timed out after 2ms of inactivity." |
| 900 ])); |
| 901 }); |
| 902 }, passing: ['test 2']); |
| 903 |
| 904 expectTestsPass("currentSchedule.heartbeat resets the timeout timer", () { |
| 905 mock_clock.mock().run(); |
| 906 test('test', () { |
| 907 currentSchedule.timeout = new Duration(milliseconds: 3); |
| 908 |
| 909 schedule(() { |
| 910 return sleep(2).then((_) { |
| 911 currentSchedule.heartbeat(); |
| 912 return sleep(2); |
| 913 }); |
| 914 }); |
| 915 }); |
| 916 }); |
| 917 |
| 918 // TODO(nweiz): test out-of-band post-timeout errors that are detected after |
| 919 // the test finishes once we can detect top-level errors (issue 8417). |
| 731 } | 920 } |
| OLD | NEW |