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