| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 library futures_test; | |
| 6 | |
| 7 import 'package:async_helper/async_helper.dart'; | |
| 8 import "package:expect/expect.dart"; | |
| 9 import 'dart:async'; | |
| 10 | |
| 11 Future testWaitEmpty() { | |
| 12 List<Future> futures = new List<Future>(); | |
| 13 return Future.wait(futures); | |
| 14 } | |
| 15 | |
| 16 Future testCompleteAfterWait() { | |
| 17 List<Future> futures = new List<Future>(); | |
| 18 Completer<Object> c = new Completer<Object>(); | |
| 19 futures.add(c.future); | |
| 20 Future future = Future.wait(futures); | |
| 21 c.complete(null); | |
| 22 return future; | |
| 23 } | |
| 24 | |
| 25 Future testCompleteBeforeWait() { | |
| 26 List<Future> futures = new List<Future>(); | |
| 27 Completer c = new Completer(); | |
| 28 futures.add(c.future); | |
| 29 c.complete(null); | |
| 30 return Future.wait(futures); | |
| 31 } | |
| 32 | |
| 33 Future testWaitWithMultipleValues() { | |
| 34 List<Future> futures = new List<Future>(); | |
| 35 Completer c1 = new Completer(); | |
| 36 Completer c2 = new Completer(); | |
| 37 futures.add(c1.future); | |
| 38 futures.add(c2.future); | |
| 39 c1.complete(1); | |
| 40 c2.complete(2); | |
| 41 return Future.wait(futures).then((values) { | |
| 42 Expect.listEquals([1, 2], values); | |
| 43 }); | |
| 44 } | |
| 45 | |
| 46 Future testWaitWithSingleError() { | |
| 47 List<Future> futures = new List<Future>(); | |
| 48 Completer c1 = new Completer(); | |
| 49 Completer c2 = new Completer(); | |
| 50 futures.add(c1.future); | |
| 51 futures.add(c2.future); | |
| 52 c1.complete(); | |
| 53 c2.completeError('correct error'); | |
| 54 | |
| 55 return Future.wait(futures).then((_) { | |
| 56 throw 'incorrect error'; | |
| 57 }).catchError((error, stackTrace) { | |
| 58 Expect.equals('correct error', error); | |
| 59 Expect.isNull(stackTrace); | |
| 60 }); | |
| 61 } | |
| 62 | |
| 63 Future testWaitWithMultipleErrors() { | |
| 64 List<Future> futures = new List<Future>(); | |
| 65 Completer c1 = new Completer(); | |
| 66 Completer c2 = new Completer(); | |
| 67 futures.add(c1.future); | |
| 68 futures.add(c2.future); | |
| 69 c1.completeError('correct error'); | |
| 70 c2.completeError('incorrect error 1'); | |
| 71 | |
| 72 return Future.wait(futures).then((_) { | |
| 73 throw 'incorrect error 2'; | |
| 74 }).catchError((error, stackTrace) { | |
| 75 Expect.equals('correct error', error); | |
| 76 Expect.isNull(stackTrace); | |
| 77 }); | |
| 78 } | |
| 79 | |
| 80 Future testWaitWithMultipleErrorsEager() { | |
| 81 List<Future> futures = new List<Future>(); | |
| 82 Completer c1 = new Completer(); | |
| 83 Completer c2 = new Completer(); | |
| 84 futures.add(c1.future); | |
| 85 futures.add(c2.future); | |
| 86 c1.completeError('correct error'); | |
| 87 c2.completeError('incorrect error 1'); | |
| 88 | |
| 89 return Future.wait(futures, eagerError: true).then((_) { | |
| 90 throw 'incorrect error 2'; | |
| 91 }).catchError((error, stackTrace) { | |
| 92 Expect.equals('correct error', error); | |
| 93 Expect.isNull(stackTrace); | |
| 94 }); | |
| 95 } | |
| 96 | |
| 97 StackTrace get currentStackTrace { | |
| 98 try { | |
| 99 throw 0; | |
| 100 } catch (e, st) { | |
| 101 return st; | |
| 102 } | |
| 103 return null; | |
| 104 } | |
| 105 | |
| 106 Future testWaitWithSingleErrorWithStackTrace() { | |
| 107 List<Future> futures = new List<Future>(); | |
| 108 Completer c1 = new Completer(); | |
| 109 Completer c2 = new Completer(); | |
| 110 futures.add(c1.future); | |
| 111 futures.add(c2.future); | |
| 112 c1.complete(); | |
| 113 c2.completeError('correct error', currentStackTrace); | |
| 114 | |
| 115 return Future.wait(futures).then((_) { | |
| 116 throw 'incorrect error'; | |
| 117 }).catchError((error, stackTrace) { | |
| 118 Expect.equals('correct error', error); | |
| 119 Expect.isNotNull(stackTrace); | |
| 120 }); | |
| 121 } | |
| 122 | |
| 123 Future testWaitWithMultipleErrorsWithStackTrace() { | |
| 124 List<Future> futures = new List<Future>(); | |
| 125 Completer c1 = new Completer(); | |
| 126 Completer c2 = new Completer(); | |
| 127 futures.add(c1.future); | |
| 128 futures.add(c2.future); | |
| 129 c1.completeError('correct error', currentStackTrace); | |
| 130 c2.completeError('incorrect error 1'); | |
| 131 | |
| 132 return Future.wait(futures).then((_) { | |
| 133 throw 'incorrect error 2'; | |
| 134 }).catchError((error, stackTrace) { | |
| 135 Expect.equals('correct error', error); | |
| 136 Expect.isNotNull(stackTrace); | |
| 137 }); | |
| 138 } | |
| 139 | |
| 140 Future testWaitWithMultipleErrorsWithStackTraceEager() { | |
| 141 List<Future> futures = new List<Future>(); | |
| 142 Completer c1 = new Completer(); | |
| 143 Completer c2 = new Completer(); | |
| 144 futures.add(c1.future); | |
| 145 futures.add(c2.future); | |
| 146 c1.completeError('correct error', currentStackTrace); | |
| 147 c2.completeError('incorrect error 1'); | |
| 148 | |
| 149 return Future.wait(futures, eagerError: true).then((_) { | |
| 150 throw 'incorrect error 2'; | |
| 151 }).catchError((error, stackTrace) { | |
| 152 Expect.equals('correct error', error); | |
| 153 Expect.isNotNull(stackTrace); | |
| 154 }); | |
| 155 } | |
| 156 | |
| 157 Future testEagerWait() { | |
| 158 var st; | |
| 159 try { | |
| 160 throw 0; | |
| 161 } catch (e, s) { | |
| 162 st = s; | |
| 163 } | |
| 164 Completer c1 = new Completer(); | |
| 165 Completer c2 = new Completer(); | |
| 166 List<Future> futures = <Future>[c1.future, c2.future]; | |
| 167 Future waited = Future.wait(futures, eagerError: true); | |
| 168 var result = waited.then((v) { | |
| 169 throw "should not be called"; | |
| 170 }, onError: (e, s) { | |
| 171 Expect.equals(e, 42); | |
| 172 Expect.identical(st, s); | |
| 173 return true; | |
| 174 }); | |
| 175 c1.completeError(42, st); | |
| 176 return result; | |
| 177 } | |
| 178 | |
| 179 Future testForEachEmpty() { | |
| 180 return Future.forEach([], (_) { | |
| 181 throw 'should not be called'; | |
| 182 }); | |
| 183 } | |
| 184 | |
| 185 Future testForEach() { | |
| 186 var seen = <int>[]; | |
| 187 return Future.forEach([1, 2, 3, 4, 5], (n) { | |
| 188 seen.add(n); | |
| 189 return new Future.value(); | |
| 190 }).then((_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); | |
| 191 } | |
| 192 | |
| 193 Future testForEachSync() { | |
| 194 var seen = <int>[]; | |
| 195 return Future.forEach([1, 2, 3, 4, 5], seen.add).then( | |
| 196 (_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); | |
| 197 } | |
| 198 | |
| 199 Future testForEachWithException() { | |
| 200 var seen = <int>[]; | |
| 201 return Future.forEach([1, 2, 3, 4, 5], (n) { | |
| 202 if (n == 4) throw 'correct exception'; | |
| 203 seen.add(n); | |
| 204 return new Future.value(); | |
| 205 }).then((_) { | |
| 206 throw 'incorrect exception'; | |
| 207 }).catchError((error) { | |
| 208 Expect.equals('correct exception', error); | |
| 209 }); | |
| 210 } | |
| 211 | |
| 212 Future testDoWhile() { | |
| 213 var count = 0; | |
| 214 return Future.doWhile(() { | |
| 215 count++; | |
| 216 return new Future(() => count < 10); | |
| 217 }).then((_) => Expect.equals(10, count)); | |
| 218 } | |
| 219 | |
| 220 Future testDoWhileSync() { | |
| 221 var count = 0; | |
| 222 return Future.doWhile(() { | |
| 223 count++; | |
| 224 return count < 10; | |
| 225 }).then((_) => Expect.equals(10, count)); | |
| 226 } | |
| 227 | |
| 228 Future testDoWhileWithException() { | |
| 229 var count = 0; | |
| 230 return Future.doWhile(() { | |
| 231 count++; | |
| 232 if (count == 4) throw 'correct exception'; | |
| 233 return new Future(() => true); | |
| 234 }).then((_) { | |
| 235 throw 'incorrect exception'; | |
| 236 }).catchError((error) { | |
| 237 Expect.equals('correct exception', error); | |
| 238 }); | |
| 239 } | |
| 240 | |
| 241 main() { | |
| 242 List<Future> futures = new List<Future>(); | |
| 243 | |
| 244 futures.add(testWaitEmpty()); | |
| 245 futures.add(testCompleteAfterWait()); | |
| 246 futures.add(testCompleteBeforeWait()); | |
| 247 futures.add(testWaitWithMultipleValues()); | |
| 248 futures.add(testWaitWithSingleError()); | |
| 249 futures.add(testWaitWithMultipleErrors()); | |
| 250 futures.add(testWaitWithMultipleErrorsEager()); | |
| 251 futures.add(testWaitWithSingleErrorWithStackTrace()); | |
| 252 futures.add(testWaitWithMultipleErrorsWithStackTrace()); | |
| 253 futures.add(testWaitWithMultipleErrorsWithStackTraceEager()); | |
| 254 futures.add(testEagerWait()); | |
| 255 futures.add(testForEachEmpty()); | |
| 256 futures.add(testForEach()); | |
| 257 futures.add(testForEachSync()); | |
| 258 futures.add(testForEachWithException()); | |
| 259 futures.add(testDoWhile()); | |
| 260 futures.add(testDoWhileSync()); | |
| 261 futures.add(testDoWhileWithException()); | |
| 262 | |
| 263 asyncStart(); | |
| 264 Future.wait(futures).then((List list) { | |
| 265 Expect.equals(18, list.length); | |
| 266 asyncEnd(); | |
| 267 }); | |
| 268 } | |
| OLD | NEW |