| 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')))); |
| 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 |
| 1032 expect(nestedScheduleRun, isFalse); |
| 1033 expect(pumpEventQueue().then((_) => nestedScheduleRun), |
| 1034 completion(isTrue)); |
| 1035 }); |
| 1036 }); |
| 1037 }); |
| 1038 |
| 1039 expectTestsPass("out-of-band schedule() runs its function immediately (but " |
| 1040 "asynchronously)", () { |
| 1041 mock_clock.mock().run(); |
| 1042 test('test', () { |
| 1043 schedule(() { |
| 1044 wrapFuture(sleep(1).then((_) { |
| 1045 var nestedScheduleRun = false; |
| 1046 schedule(() { |
| 1047 nestedScheduleRun = true; |
| 1048 }); |
| 1049 |
| 1050 expect(nestedScheduleRun, isFalse); |
| 1051 expect(pumpEventQueue().then((_) => nestedScheduleRun), |
| 1052 completion(isTrue)); |
| 1053 })); |
| 1054 }); |
| 1055 }); |
| 1056 }); |
| 1057 |
| 1058 expectTestsPass("nested schedule() calls don't wait for one another", () { |
| 1059 mock_clock.mock().run(); |
| 1060 test('test', () { |
| 1061 var sleepFinished = false; |
| 1062 schedule(() { |
| 1063 schedule(() => sleep(1).then((_) { |
| 1064 sleepFinished = true; |
| 1065 })); |
| 1066 schedule(() => expect(sleepFinished, isFalse)); |
| 1067 }); |
| 1068 }); |
| 1069 }); |
| 1070 |
| 1071 expectTestsPass("nested schedule() calls block their parent task", () { |
| 1072 mock_clock.mock().run(); |
| 1073 test('test', () { |
| 1074 var sleepFinished = false; |
| 1075 schedule(() { |
| 1076 schedule(() => sleep(1).then((_) { |
| 1077 sleepFinished = true; |
| 1078 })); |
| 1079 }); |
| 1080 |
| 1081 schedule(() => expect(sleepFinished, isTrue)); |
| 1082 }); |
| 1083 }); |
| 1084 |
| 1085 expectTestsPass("out-of-band schedule() calls block their parent queue", () { |
| 1086 mock_clock.mock().run(); |
| 1087 test('test', () { |
| 1088 var scheduleRun = false; |
| 1089 wrapFuture(sleep(1).then((_) { |
| 1090 schedule(() => sleep(1).then((_) { |
| 1091 scheduleRun = true; |
| 1092 })); |
| 1093 })); |
| 1094 |
| 1095 currentSchedule.onComplete.schedule(() => expect(scheduleRun, isTrue)); |
| 1096 }); |
| 1097 }); |
| 1098 |
| 1099 expectTestsPass("nested schedule() calls forward their Future values", () { |
| 1100 mock_clock.mock().run(); |
| 1101 test('test', () { |
| 1102 schedule(() { |
| 1103 expect(schedule(() => 'foo'), completion(equals('foo'))); |
| 1104 }); |
| 1105 }); |
| 1106 }); |
| 1107 |
| 1108 expectTestsPass("errors in nested schedule() calls are properly registered", |
| 1109 () { |
| 1110 var errors; |
| 1111 test('test 1', () { |
| 1112 currentSchedule.onException.schedule(() { |
| 1113 errors = currentSchedule.errors; |
| 1114 }); |
| 1115 |
| 1116 schedule(() { |
| 1117 schedule(() { |
| 1118 throw 'error'; |
| 1119 }); |
| 1120 }); |
| 1121 }); |
| 1122 |
| 1123 test('test 2', () { |
| 1124 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 1125 expect(errors.map((e) => e.error), equals(['error'])); |
| 1126 }); |
| 1127 }, passing: ['test 2']); |
| 1128 |
| 1129 expectTestsPass("nested scheduled blocks whose return values are passed to " |
| 1130 "wrapFuture should report exceptions once", () { |
| 1131 var errors; |
| 1132 test('test 1', () { |
| 1133 currentSchedule.onException.schedule(() { |
| 1134 errors = currentSchedule.errors; |
| 1135 }); |
| 1136 |
| 1137 schedule(() { |
| 1138 wrapFuture(schedule(() { |
| 1139 throw 'error'; |
| 1140 })); |
| 1141 |
| 1142 return pumpEventQueue(); |
| 1143 }); |
| 1144 }); |
| 1145 |
| 1146 test('test 2', () { |
| 1147 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
| 1148 expect(errors.map((e) => e.error), equals(['error'])); |
| 1149 }); |
| 1150 }, passing: ['test 2']); |
| 1151 |
| 1152 expectTestsPass("a nested task failing shouldn't short-circuit the parent " |
| 1153 "task", () { |
| 1154 var parentTaskFinishedBeforeOnComplete = false; |
| 1155 test('test 1', () { |
| 1156 var parentTaskFinished = false; |
| 1157 currentSchedule.onComplete.schedule(() { |
| 1158 parentTaskFinishedBeforeOnComplete = parentTaskFinished; |
| 1159 }); |
| 1160 |
| 1161 schedule(() { |
| 1162 schedule(() { |
| 1163 throw 'error'; |
| 1164 }); |
| 1165 |
| 1166 return sleep(1).then((_) { |
| 1167 parentTaskFinished = true; |
| 1168 }); |
| 1169 }); |
| 1170 }); |
| 1171 |
| 1172 test('test 2', () { |
| 1173 expect(parentTaskFinishedBeforeOnComplete, isTrue); |
| 1174 }); |
| 1175 }, passing: ['test 2']); |
| 920 } | 1176 } |
| OLD | NEW |