Chromium Code Reviews| 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 import 'package:scheduled_test/src/mock_clock.dart' as mock_clock; |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 'schedule is handled', () { | 135 'schedule is handled', () { |
| 136 mock_clock.mock().run(); | 136 mock_clock.mock().run(); |
| 137 test('test', () { | 137 test('test', () { |
| 138 schedule(() { | 138 schedule(() { |
| 139 sleep(2).then(wrapAsync((_) => expect('foo', equals('bar')))); | 139 sleep(2).then(wrapAsync((_) => expect('foo', equals('bar')))); |
| 140 }); | 140 }); |
| 141 schedule(() => sleep(1)); | 141 schedule(() => sleep(1)); |
| 142 }); | 142 }); |
| 143 }); | 143 }); |
| 144 | 144 |
| 145 expectTestsFail('an out-of-band failure in wrapFuture is handled', () { | |
| 146 mock_clock.mock().run(); | |
| 147 test('test', () { | |
| 148 schedule(() { | |
| 149 wrapFuture(sleep(1).then((_) => expect('foo', equals('bar')))); | |
|
Bob Nystrom
2013/02/19 23:15:04
Using expect like this seems weird. How about just
nweiz
2013/02/20 00:23:12
I want to test against the failure conditions that
| |
| 150 }); | |
| 151 schedule(() => sleep(2)); | |
| 152 }); | |
| 153 }); | |
| 154 | |
| 155 expectTestsFail('an out-of-band failure in wrapFuture that finishes after ' | |
| 156 'the schedule is handled', () { | |
| 157 mock_clock.mock().run(); | |
| 158 test('test', () { | |
| 159 schedule(() { | |
| 160 wrapFuture(sleep(2).then((_) => expect('foo', equals('bar')))); | |
| 161 }); | |
| 162 schedule(() => sleep(1)); | |
| 163 }); | |
| 164 }); | |
| 165 | |
| 166 expectTestsPass("wrapFuture should return the value of the wrapped future", | |
| 167 () { | |
| 168 test('test', () { | |
| 169 schedule(() { | |
| 170 expect(wrapFuture(pumpEventQueue().then((_) => 'foo')), | |
| 171 completion(equals('foo'))); | |
| 172 }); | |
| 173 }); | |
| 174 }); | |
| 175 | |
| 176 expectTestsPass("wrapFuture should pass through the error of the wrapped " | |
| 177 "future", () { | |
| 178 var error; | |
| 179 test('test 1', () { | |
| 180 schedule(() { | |
| 181 wrapFuture(pumpEventQueue().then((_) { | |
| 182 throw 'error'; | |
| 183 })).catchError(wrapAsync((e) { | |
| 184 error = e.error; | |
| 185 })); | |
| 186 }); | |
| 187 }); | |
| 188 | |
| 189 test('test 2', () { | |
| 190 expect(error, equals('error')); | |
| 191 }); | |
| 192 }, passing: ['test 2']); | |
| 193 | |
| 194 expectTestsPass("scheduled blocks whose return values are passed to " | |
| 195 "wrapFuture should report exceptions once", () { | |
| 196 var errors; | |
| 197 test('test 1', () { | |
| 198 currentSchedule.onException.schedule(() { | |
| 199 errors = currentSchedule.errors; | |
| 200 }); | |
| 201 | |
| 202 wrapFuture(schedule(() { | |
| 203 throw 'error'; | |
| 204 })); | |
| 205 }); | |
| 206 | |
| 207 test('test 2', () { | |
| 208 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 209 expect(errors.map((e) => e.error), equals(['error'])); | |
| 210 }); | |
| 211 }, passing: ['test 2']); | |
| 212 | |
| 145 expectTestsFail('an out-of-band error reported via signalError is ' | 213 expectTestsFail('an out-of-band error reported via signalError is ' |
| 146 'handled', () { | 214 'handled', () { |
| 147 mock_clock.mock().run(); | 215 mock_clock.mock().run(); |
| 148 test('test', () { | 216 test('test', () { |
| 149 schedule(() { | 217 schedule(() { |
| 150 sleep(1).then((_) => currentSchedule.signalError('bad')); | 218 sleep(1).then((_) => currentSchedule.signalError('bad')); |
| 151 }); | 219 }); |
| 152 schedule(() => sleep(2)); | 220 schedule(() => sleep(2)); |
| 153 }); | 221 }); |
| 154 }); | 222 }); |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 517 })); | 585 })); |
| 518 }); | 586 }); |
| 519 | 587 |
| 520 test('test 2', () { | 588 test('test 2', () { |
| 521 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 589 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 522 expect(errors.map((e) => e.error), | 590 expect(errors.map((e) => e.error), |
| 523 orderedEquals(['error1', 'error2', 'error3', 'error4'])); | 591 orderedEquals(['error1', 'error2', 'error3', 'error4'])); |
| 524 }); | 592 }); |
| 525 }, passing: ['test 2']); | 593 }, passing: ['test 2']); |
| 526 | 594 |
| 595 expectTestsPass('currentSchedule.errors contains multiple out-of-band errors ' | |
| 596 'from both the main task queue and onException in onComplete reported ' | |
| 597 'via wrapFuture', () { | |
| 598 mock_clock.mock().run(); | |
| 599 var errors; | |
| 600 test('test 1', () { | |
| 601 currentSchedule.onComplete.schedule(() { | |
| 602 errors = currentSchedule.errors; | |
| 603 }); | |
| 604 | |
| 605 currentSchedule.onException.schedule(() { | |
| 606 wrapFuture(sleep(1).then((_) { | |
| 607 throw 'error3'; | |
| 608 })); | |
| 609 wrapFuture(sleep(2).then((_) { | |
| 610 throw 'error4'; | |
| 611 })); | |
| 612 }); | |
| 613 | |
| 614 wrapFuture(sleep(1).then((_) { | |
| 615 throw 'error1'; | |
| 616 })); | |
| 617 wrapFuture(sleep(2).then((_) { | |
| 618 throw 'error2'; | |
| 619 })); | |
| 620 }); | |
| 621 | |
| 622 test('test 2', () { | |
| 623 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 624 expect(errors.map((e) => e.error), | |
| 625 orderedEquals(['error1', 'error2', 'error3', 'error4'])); | |
| 626 }); | |
| 627 }, passing: ['test 2']); | |
| 628 | |
| 527 expectTestsPass('currentSchedule.errors contains both an out-of-band error ' | 629 expectTestsPass('currentSchedule.errors contains both an out-of-band error ' |
| 528 'and an error raised afterwards in a task', () { | 630 'and an error raised afterwards in a task', () { |
| 529 mock_clock.mock().run(); | 631 mock_clock.mock().run(); |
| 530 var errors; | 632 var errors; |
| 531 test('test 1', () { | 633 test('test 1', () { |
| 532 currentSchedule.onComplete.schedule(() { | 634 currentSchedule.onComplete.schedule(() { |
| 533 errors = currentSchedule.errors; | 635 errors = currentSchedule.errors; |
| 534 }); | 636 }); |
| 535 | 637 |
| 536 sleep(1).then(wrapAsync((_) { | 638 sleep(1).then(wrapAsync((_) { |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 910 return sleep(2).then((_) { | 1012 return sleep(2).then((_) { |
| 911 currentSchedule.heartbeat(); | 1013 currentSchedule.heartbeat(); |
| 912 return sleep(2); | 1014 return sleep(2); |
| 913 }); | 1015 }); |
| 914 }); | 1016 }); |
| 915 }); | 1017 }); |
| 916 }); | 1018 }); |
| 917 | 1019 |
| 918 // TODO(nweiz): test out-of-band post-timeout errors that are detected after | 1020 // 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). | 1021 // the test finishes once we can detect top-level errors (issue 8417). |
| 1022 | |
| 1023 expectTestsPass("nested schedule() runs its function immediately (but " | |
| 1024 "asynchronously)", () { | |
| 1025 test('test', () { | |
| 1026 schedule(() { | |
| 1027 var nestedScheduleRun = false; | |
| 1028 schedule(() { | |
| 1029 nestedScheduleRun = true; | |
| 1030 }); | |
| 1031 | |
|
Bob Nystrom
2013/02/19 23:15:04
Add expect(nestedScheduleRun, isFalse) here to tes
nweiz
2013/02/20 00:23:12
Done.
| |
| 1032 expect(pumpEventQueue().then((_) => nestedScheduleRun), | |
| 1033 completion(isTrue)); | |
| 1034 }); | |
| 1035 }); | |
| 1036 }); | |
| 1037 | |
| 1038 expectTestsPass("out-of-band schedule() runs its function immediately (but " | |
| 1039 "asynchronously)", () { | |
| 1040 mock_clock.mock().run(); | |
| 1041 test('test', () { | |
| 1042 schedule(() { | |
| 1043 wrapFuture(sleep(1).then((_) { | |
| 1044 var nestedScheduleRun = false; | |
| 1045 schedule(() { | |
| 1046 nestedScheduleRun = true; | |
| 1047 }); | |
| 1048 | |
|
Bob Nystrom
2013/02/19 23:15:04
Ditto.
nweiz
2013/02/20 00:23:12
Done.
| |
| 1049 expect(pumpEventQueue().then((_) => nestedScheduleRun), | |
| 1050 completion(isTrue)); | |
| 1051 })); | |
| 1052 }); | |
| 1053 }); | |
| 1054 }); | |
| 1055 | |
| 1056 expectTestsPass("nested schedule() calls don't wait for one another", () { | |
| 1057 mock_clock.mock().run(); | |
| 1058 test('test', () { | |
| 1059 var sleepFinished = false; | |
| 1060 schedule(() { | |
| 1061 schedule(() => sleep(1).then((_) { | |
| 1062 sleepFinished = true; | |
| 1063 })); | |
| 1064 schedule(() => expect(sleepFinished, isFalse)); | |
|
Bob Nystrom
2013/02/19 23:15:04
So child siblings are parallel but top-level sibli
nweiz
2013/02/20 00:23:12
I thought that was pretty clear from the [schedule
| |
| 1065 }); | |
| 1066 }); | |
| 1067 }); | |
| 1068 | |
| 1069 expectTestsPass("nested schedule() calls block their parent task", () { | |
| 1070 mock_clock.mock().run(); | |
| 1071 test('test', () { | |
| 1072 var sleepFinished = false; | |
| 1073 schedule(() { | |
| 1074 schedule(() => sleep(1).then((_) { | |
| 1075 sleepFinished = true; | |
| 1076 })); | |
| 1077 }); | |
| 1078 | |
| 1079 schedule(() => expect(sleepFinished, isTrue)); | |
| 1080 }); | |
| 1081 }); | |
| 1082 | |
| 1083 expectTestsPass("out-of-band schedule() calls block their parent queue", () { | |
| 1084 mock_clock.mock().run(); | |
| 1085 test('test', () { | |
| 1086 var scheduleRun = false; | |
| 1087 wrapFuture(sleep(1).then((_) { | |
| 1088 schedule(() => sleep(1).then((_) { | |
| 1089 scheduleRun = true; | |
| 1090 })); | |
| 1091 })); | |
| 1092 | |
| 1093 currentSchedule.onComplete.schedule(() => expect(scheduleRun, isTrue)); | |
| 1094 }); | |
| 1095 }); | |
| 1096 | |
| 1097 expectTestsPass("nested schedule() calls forward their Future values", () { | |
| 1098 mock_clock.mock().run(); | |
| 1099 test('test', () { | |
| 1100 schedule(() { | |
| 1101 expect(schedule(() => 'foo'), completion(equals('foo'))); | |
| 1102 }); | |
| 1103 }); | |
| 1104 }); | |
| 1105 | |
| 1106 expectTestsPass("errors in nested schedule() calls are properly registered", | |
| 1107 () { | |
| 1108 var errors; | |
| 1109 test('test 1', () { | |
| 1110 currentSchedule.onException.schedule(() { | |
| 1111 errors = currentSchedule.errors; | |
| 1112 }); | |
| 1113 | |
| 1114 schedule(() { | |
| 1115 schedule(() { | |
| 1116 throw 'error'; | |
| 1117 }); | |
| 1118 }); | |
| 1119 }); | |
| 1120 | |
| 1121 test('test 2', () { | |
| 1122 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 1123 expect(errors.map((e) => e.error), equals(['error'])); | |
| 1124 }); | |
| 1125 }, passing: ['test 2']); | |
| 1126 | |
| 1127 expectTestsPass("nested scheduled blocks whose return values are passed to " | |
| 1128 "wrapFuture should report exceptions once", () { | |
| 1129 var errors; | |
| 1130 test('test 1', () { | |
| 1131 currentSchedule.onException.schedule(() { | |
| 1132 errors = currentSchedule.errors; | |
| 1133 }); | |
| 1134 | |
| 1135 schedule(() { | |
| 1136 wrapFuture(schedule(() { | |
| 1137 throw 'error'; | |
| 1138 })); | |
| 1139 }); | |
| 1140 }); | |
| 1141 | |
| 1142 test('test 2', () { | |
| 1143 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | |
| 1144 expect(errors.map((e) => e.error), equals(['error'])); | |
| 1145 }); | |
| 1146 }, passing: ['test 2']); | |
| 1147 | |
| 1148 expectTestsPass("a nested task failing shouldn't short-circuit the parent " | |
| 1149 "task", () { | |
| 1150 var parentTaskFinishedBeforeOnComplete = false; | |
| 1151 test('test 1', () { | |
| 1152 var parentTaskFinished = false; | |
| 1153 currentSchedule.onComplete.schedule(() { | |
| 1154 parentTaskFinishedBeforeOnComplete = parentTaskFinished; | |
| 1155 }); | |
| 1156 | |
| 1157 schedule(() { | |
| 1158 schedule(() { | |
| 1159 throw 'error'; | |
| 1160 }); | |
| 1161 | |
| 1162 return sleep(1).then((_) { | |
| 1163 parentTaskFinished = true; | |
| 1164 }); | |
| 1165 }); | |
| 1166 }); | |
| 1167 | |
| 1168 test('test 2', () { | |
| 1169 expect(parentTaskFinishedBeforeOnComplete, isTrue); | |
| 1170 }); | |
| 1171 }, passing: ['test 2']); | |
| 920 } | 1172 } |
| OLD | NEW |