| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 future_test; | 5 library future_test; |
| 6 | 6 |
| 7 import 'package:async_helper/async_helper.dart'; |
| 7 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
| 8 import 'dart:async'; | 9 import 'dart:async'; |
| 9 import 'dart:isolate'; | |
| 10 | 10 |
| 11 const Duration MS = const Duration(milliseconds: 1); | 11 const Duration MS = const Duration(milliseconds: 1); |
| 12 | 12 |
| 13 void testValue() { | 13 void testValue() { |
| 14 final future = new Future<String>.value("42"); | 14 final future = new Future<String>.value("42"); |
| 15 var port = new ReceivePort(); | 15 asyncStart(); |
| 16 future.then((x) { | 16 future.then((x) { |
| 17 Expect.equals("42", x); | 17 Expect.equals("42", x); |
| 18 port.close(); | 18 asyncEnd(); |
| 19 }); | 19 }); |
| 20 } | 20 } |
| 21 | 21 |
| 22 void testSync() { | 22 void testSync() { |
| 23 compare(func) { | 23 compare(func) { |
| 24 // Compare the results of the following two futures. | 24 // Compare the results of the following two futures. |
| 25 Future f1 = new Future.sync(func); | 25 Future f1 = new Future.sync(func); |
| 26 Future f2 = new Future.value().then((_) => func()); | 26 Future f2 = new Future.value().then((_) => func()); |
| 27 f2.catchError((_){}); // I'll get the error later. | 27 f2.catchError((_){}); // I'll get the error later. |
| 28 f1.then((v1) { f2.then((v2) { Expect.equals(v1, v2); }); }, | 28 f1.then((v1) { f2.then((v2) { Expect.equals(v1, v2); }); }, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Tests for [then] | 73 // Tests for [then] |
| 74 | 74 |
| 75 void testCompleteWithSuccessHandlerBeforeComplete() { | 75 void testCompleteWithSuccessHandlerBeforeComplete() { |
| 76 final completer = new Completer<int>(); | 76 final completer = new Completer<int>(); |
| 77 final future = completer.future; | 77 final future = completer.future; |
| 78 | 78 |
| 79 int after; | 79 int after; |
| 80 | 80 |
| 81 var port = new ReceivePort(); | 81 asyncStart(); |
| 82 future.then((int v) { after = v; }) | 82 future.then((int v) { after = v; }) |
| 83 .then((_) { | 83 .then((_) { |
| 84 Expect.equals(3, after); | 84 Expect.equals(3, after); |
| 85 port.close(); | 85 asyncEnd(); |
| 86 }); | 86 }); |
| 87 | 87 |
| 88 completer.complete(3); | 88 completer.complete(3); |
| 89 Expect.isNull(after); | 89 Expect.isNull(after); |
| 90 } | 90 } |
| 91 | 91 |
| 92 void testCompleteWithSuccessHandlerAfterComplete() { | 92 void testCompleteWithSuccessHandlerAfterComplete() { |
| 93 final completer = new Completer<int>(); | 93 final completer = new Completer<int>(); |
| 94 final future = completer.future; | 94 final future = completer.future; |
| 95 | 95 |
| 96 int after; | 96 int after; |
| 97 completer.complete(3); | 97 completer.complete(3); |
| 98 Expect.isNull(after); | 98 Expect.isNull(after); |
| 99 | 99 |
| 100 var port = new ReceivePort(); | 100 asyncStart(); |
| 101 future.then((int v) { after = v; }) | 101 future.then((int v) { after = v; }) |
| 102 .then((_) { | 102 .then((_) { |
| 103 Expect.equals(3, after); | 103 Expect.equals(3, after); |
| 104 port.close(); | 104 asyncEnd(); |
| 105 }); | 105 }); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void testCompleteManySuccessHandlers() { | 108 void testCompleteManySuccessHandlers() { |
| 109 final completer = new Completer<int>(); | 109 final completer = new Completer<int>(); |
| 110 final future = completer.future; | 110 final future = completer.future; |
| 111 int before; | 111 int before; |
| 112 int after1; | 112 int after1; |
| 113 int after2; | 113 int after2; |
| 114 | 114 |
| 115 var futures = []; | 115 var futures = []; |
| 116 futures.add(future.then((int v) { before = v; })); | 116 futures.add(future.then((int v) { before = v; })); |
| 117 completer.complete(3); | 117 completer.complete(3); |
| 118 futures.add(future.then((int v) { after1 = v; })); | 118 futures.add(future.then((int v) { after1 = v; })); |
| 119 futures.add(future.then((int v) { after2 = v; })); | 119 futures.add(future.then((int v) { after2 = v; })); |
| 120 | 120 |
| 121 var port = new ReceivePort(); | 121 asyncStart(); |
| 122 Future.wait(futures).then((_) { | 122 Future.wait(futures).then((_) { |
| 123 Expect.equals(3, before); | 123 Expect.equals(3, before); |
| 124 Expect.equals(3, after1); | 124 Expect.equals(3, after1); |
| 125 Expect.equals(3, after2); | 125 Expect.equals(3, after2); |
| 126 port.close(); | 126 asyncEnd(); |
| 127 }); | 127 }); |
| 128 } | 128 } |
| 129 | 129 |
| 130 // Tests for [catchError] | 130 // Tests for [catchError] |
| 131 | 131 |
| 132 void testException() { | 132 void testException() { |
| 133 final completer = new Completer<int>(); | 133 final completer = new Completer<int>(); |
| 134 final future = completer.future; | 134 final future = completer.future; |
| 135 final ex = new Exception(); | 135 final ex = new Exception(); |
| 136 | 136 |
| 137 var port = new ReceivePort(); | 137 asyncStart(); |
| 138 future | 138 future |
| 139 .then((v) { throw "Value not expected"; }) | 139 .then((v) { throw "Value not expected"; }) |
| 140 .catchError((error) { | 140 .catchError((error) { |
| 141 Expect.equals(error, ex); | 141 Expect.equals(error, ex); |
| 142 port.close(); | 142 asyncEnd(); |
| 143 }, test: (e) => e == ex); | 143 }, test: (e) => e == ex); |
| 144 completer.completeError(ex); | 144 completer.completeError(ex); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void testExceptionHandler() { | 147 void testExceptionHandler() { |
| 148 final completer = new Completer<int>(); | 148 final completer = new Completer<int>(); |
| 149 final future = completer.future; | 149 final future = completer.future; |
| 150 final ex = new Exception(); | 150 final ex = new Exception(); |
| 151 | 151 |
| 152 var ex2; | 152 var ex2; |
| 153 var done = future.catchError((error) { ex2 = error; }); | 153 var done = future.catchError((error) { ex2 = error; }); |
| 154 | 154 |
| 155 Expect.isFalse(completer.isCompleted); | 155 Expect.isFalse(completer.isCompleted); |
| 156 completer.completeError(ex); | 156 completer.completeError(ex); |
| 157 Expect.isTrue(completer.isCompleted); | 157 Expect.isTrue(completer.isCompleted); |
| 158 | 158 |
| 159 var port = new ReceivePort(); | 159 asyncStart(); |
| 160 done.then((_) { | 160 done.then((_) { |
| 161 Expect.equals(ex, ex2); | 161 Expect.equals(ex, ex2); |
| 162 port.close(); | 162 asyncEnd(); |
| 163 }); | 163 }); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void testExceptionHandlerReturnsTrue() { | 166 void testExceptionHandlerReturnsTrue() { |
| 167 final completer = new Completer<int>(); | 167 final completer = new Completer<int>(); |
| 168 final future = completer.future; | 168 final future = completer.future; |
| 169 final ex = new Exception(); | 169 final ex = new Exception(); |
| 170 | 170 |
| 171 bool reached = false; | 171 bool reached = false; |
| 172 future.catchError((e) { }); | 172 future.catchError((e) { }); |
| 173 future.catchError((e) { reached = true; }, test: (e) => false) | 173 future.catchError((e) { reached = true; }, test: (e) => false) |
| 174 .catchError((e) {}); | 174 .catchError((e) {}); |
| 175 Expect.isFalse(completer.isCompleted); | 175 Expect.isFalse(completer.isCompleted); |
| 176 completer.completeError(ex); | 176 completer.completeError(ex); |
| 177 Expect.isTrue(completer.isCompleted); | 177 Expect.isTrue(completer.isCompleted); |
| 178 Expect.isFalse(reached); | 178 Expect.isFalse(reached); |
| 179 } | 179 } |
| 180 | 180 |
| 181 void testExceptionHandlerReturnsTrue2() { | 181 void testExceptionHandlerReturnsTrue2() { |
| 182 final completer = new Completer<int>(); | 182 final completer = new Completer<int>(); |
| 183 final future = completer.future; | 183 final future = completer.future; |
| 184 final ex = new Exception(); | 184 final ex = new Exception(); |
| 185 | 185 |
| 186 bool reached = false; | 186 bool reached = false; |
| 187 var done = future | 187 var done = future |
| 188 .catchError((e) { }, test: (e) => false) | 188 .catchError((e) { }, test: (e) => false) |
| 189 .catchError((e) { reached = true; }); | 189 .catchError((e) { reached = true; }); |
| 190 completer.completeError(ex); | 190 completer.completeError(ex); |
| 191 | 191 |
| 192 var port = new ReceivePort(); | 192 asyncStart(); |
| 193 done.then((_) { | 193 done.then((_) { |
| 194 Expect.isTrue(reached); | 194 Expect.isTrue(reached); |
| 195 port.close(); | 195 asyncEnd(); |
| 196 }); | 196 }); |
| 197 } | 197 } |
| 198 | 198 |
| 199 void testExceptionHandlerReturnsFalse() { | 199 void testExceptionHandlerReturnsFalse() { |
| 200 final completer = new Completer<int>(); | 200 final completer = new Completer<int>(); |
| 201 final future = completer.future; | 201 final future = completer.future; |
| 202 final ex = new Exception(); | 202 final ex = new Exception(); |
| 203 | 203 |
| 204 bool reached = false; | 204 bool reached = false; |
| 205 | 205 |
| 206 future.catchError((e) { }); | 206 future.catchError((e) { }); |
| 207 | 207 |
| 208 future.catchError((e) { reached = true; }, test: (e) => false) | 208 future.catchError((e) { reached = true; }, test: (e) => false) |
| 209 .catchError((e) { }); | 209 .catchError((e) { }); |
| 210 | 210 |
| 211 completer.completeError(ex); | 211 completer.completeError(ex); |
| 212 | 212 |
| 213 Expect.isFalse(reached); | 213 Expect.isFalse(reached); |
| 214 } | 214 } |
| 215 | 215 |
| 216 void testFutureAsStreamCompleteAfter() { | 216 void testFutureAsStreamCompleteAfter() { |
| 217 var completer = new Completer(); | 217 var completer = new Completer(); |
| 218 bool gotValue = false; | 218 bool gotValue = false; |
| 219 var port = new ReceivePort(); | 219 asyncStart(); |
| 220 completer.future.asStream().listen( | 220 completer.future.asStream().listen( |
| 221 (data) { | 221 (data) { |
| 222 Expect.isFalse(gotValue); | 222 Expect.isFalse(gotValue); |
| 223 gotValue = true; | 223 gotValue = true; |
| 224 Expect.equals("value", data); | 224 Expect.equals("value", data); |
| 225 }, | 225 }, |
| 226 onDone: () { | 226 onDone: () { |
| 227 Expect.isTrue(gotValue); | 227 Expect.isTrue(gotValue); |
| 228 port.close(); | 228 asyncEnd(); |
| 229 }); | 229 }); |
| 230 completer.complete("value"); | 230 completer.complete("value"); |
| 231 } | 231 } |
| 232 | 232 |
| 233 void testFutureAsStreamCompleteBefore() { | 233 void testFutureAsStreamCompleteBefore() { |
| 234 var completer = new Completer(); | 234 var completer = new Completer(); |
| 235 bool gotValue = false; | 235 bool gotValue = false; |
| 236 var port = new ReceivePort(); | 236 asyncStart(); |
| 237 completer.complete("value"); | 237 completer.complete("value"); |
| 238 completer.future.asStream().listen( | 238 completer.future.asStream().listen( |
| 239 (data) { | 239 (data) { |
| 240 Expect.isFalse(gotValue); | 240 Expect.isFalse(gotValue); |
| 241 gotValue = true; | 241 gotValue = true; |
| 242 Expect.equals("value", data); | 242 Expect.equals("value", data); |
| 243 }, | 243 }, |
| 244 onDone: () { | 244 onDone: () { |
| 245 Expect.isTrue(gotValue); | 245 Expect.isTrue(gotValue); |
| 246 port.close(); | 246 asyncEnd(); |
| 247 }); | 247 }); |
| 248 } | 248 } |
| 249 | 249 |
| 250 void testFutureAsStreamCompleteImmediate() { | 250 void testFutureAsStreamCompleteImmediate() { |
| 251 bool gotValue = false; | 251 bool gotValue = false; |
| 252 var port = new ReceivePort(); | 252 asyncStart(); |
| 253 new Future.value("value").asStream().listen( | 253 new Future.value("value").asStream().listen( |
| 254 (data) { | 254 (data) { |
| 255 Expect.isFalse(gotValue); | 255 Expect.isFalse(gotValue); |
| 256 gotValue = true; | 256 gotValue = true; |
| 257 Expect.equals("value", data); | 257 Expect.equals("value", data); |
| 258 }, | 258 }, |
| 259 onDone: () { | 259 onDone: () { |
| 260 Expect.isTrue(gotValue); | 260 Expect.isTrue(gotValue); |
| 261 port.close(); | 261 asyncEnd(); |
| 262 }); | 262 }); |
| 263 } | 263 } |
| 264 | 264 |
| 265 void testFutureAsStreamCompleteErrorAfter() { | 265 void testFutureAsStreamCompleteErrorAfter() { |
| 266 var completer = new Completer(); | 266 var completer = new Completer(); |
| 267 bool gotError = false; | 267 bool gotError = false; |
| 268 var port = new ReceivePort(); | 268 asyncStart(); |
| 269 completer.future.asStream().listen( | 269 completer.future.asStream().listen( |
| 270 (data) { | 270 (data) { |
| 271 Expect.fail("Unexpected data"); | 271 Expect.fail("Unexpected data"); |
| 272 }, | 272 }, |
| 273 onError: (error) { | 273 onError: (error) { |
| 274 Expect.isFalse(gotError); | 274 Expect.isFalse(gotError); |
| 275 gotError = true; | 275 gotError = true; |
| 276 Expect.equals("error", error); | 276 Expect.equals("error", error); |
| 277 }, | 277 }, |
| 278 onDone: () { | 278 onDone: () { |
| 279 Expect.isTrue(gotError); | 279 Expect.isTrue(gotError); |
| 280 port.close(); | 280 asyncEnd(); |
| 281 }); | 281 }); |
| 282 completer.completeError("error"); | 282 completer.completeError("error"); |
| 283 } | 283 } |
| 284 | 284 |
| 285 void testFutureAsStreamWrapper() { | 285 void testFutureAsStreamWrapper() { |
| 286 var completer = new Completer(); | 286 var completer = new Completer(); |
| 287 bool gotValue = false; | 287 bool gotValue = false; |
| 288 var port = new ReceivePort(); | 288 asyncStart(); |
| 289 completer.complete("value"); | 289 completer.complete("value"); |
| 290 completer.future | 290 completer.future |
| 291 .catchError((_) { throw "not possible"; }) // Returns a future wrapper. | 291 .catchError((_) { throw "not possible"; }) // Returns a future wrapper. |
| 292 .asStream().listen( | 292 .asStream().listen( |
| 293 (data) { | 293 (data) { |
| 294 Expect.isFalse(gotValue); | 294 Expect.isFalse(gotValue); |
| 295 gotValue = true; | 295 gotValue = true; |
| 296 Expect.equals("value", data); | 296 Expect.equals("value", data); |
| 297 }, | 297 }, |
| 298 onDone: () { | 298 onDone: () { |
| 299 Expect.isTrue(gotValue); | 299 Expect.isTrue(gotValue); |
| 300 port.close(); | 300 asyncEnd(); |
| 301 }); | 301 }); |
| 302 } | 302 } |
| 303 | 303 |
| 304 void testFutureWhenCompleteValue() { | 304 void testFutureWhenCompleteValue() { |
| 305 var port = new ReceivePort(); | 305 asyncStart(); |
| 306 int counter = 2; | 306 int counter = 2; |
| 307 countDown() { | 307 countDown() { |
| 308 if (--counter == 0) port.close(); | 308 if (--counter == 0) asyncEnd(); |
| 309 } | 309 } |
| 310 var completer = new Completer(); | 310 var completer = new Completer(); |
| 311 Future future = completer.future; | 311 Future future = completer.future; |
| 312 Future later = future.whenComplete(countDown); | 312 Future later = future.whenComplete(countDown); |
| 313 later.then((v) { | 313 later.then((v) { |
| 314 Expect.equals(42, v); | 314 Expect.equals(42, v); |
| 315 countDown(); | 315 countDown(); |
| 316 }); | 316 }); |
| 317 completer.complete(42); | 317 completer.complete(42); |
| 318 } | 318 } |
| 319 | 319 |
| 320 void testFutureWhenCompleteError() { | 320 void testFutureWhenCompleteError() { |
| 321 var port = new ReceivePort(); | 321 asyncStart(); |
| 322 int counter = 2; | 322 int counter = 2; |
| 323 countDown() { | 323 countDown() { |
| 324 if (--counter == 0) port.close(); | 324 if (--counter == 0) asyncEnd(); |
| 325 } | 325 } |
| 326 var completer = new Completer(); | 326 var completer = new Completer(); |
| 327 Future future = completer.future; | 327 Future future = completer.future; |
| 328 Future later = future.whenComplete(countDown); | 328 Future later = future.whenComplete(countDown); |
| 329 later.catchError((error) { | 329 later.catchError((error) { |
| 330 Expect.equals("error", error); | 330 Expect.equals("error", error); |
| 331 countDown(); | 331 countDown(); |
| 332 }); | 332 }); |
| 333 completer.completeError("error"); | 333 completer.completeError("error"); |
| 334 } | 334 } |
| 335 | 335 |
| 336 void testFutureWhenCompleteValueNewError() { | 336 void testFutureWhenCompleteValueNewError() { |
| 337 var port = new ReceivePort(); | 337 asyncStart(); |
| 338 int counter = 2; | 338 int counter = 2; |
| 339 countDown() { | 339 countDown() { |
| 340 if (--counter == 0) port.close(); | 340 if (--counter == 0) asyncEnd(); |
| 341 } | 341 } |
| 342 var completer = new Completer(); | 342 var completer = new Completer(); |
| 343 Future future = completer.future; | 343 Future future = completer.future; |
| 344 Future later = future.whenComplete(() { | 344 Future later = future.whenComplete(() { |
| 345 countDown(); | 345 countDown(); |
| 346 throw "new error"; | 346 throw "new error"; |
| 347 }); | 347 }); |
| 348 later.catchError((error) { | 348 later.catchError((error) { |
| 349 Expect.equals("new error", error); | 349 Expect.equals("new error", error); |
| 350 countDown(); | 350 countDown(); |
| 351 }); | 351 }); |
| 352 completer.complete(42); | 352 completer.complete(42); |
| 353 } | 353 } |
| 354 | 354 |
| 355 void testFutureWhenCompleteErrorNewError() { | 355 void testFutureWhenCompleteErrorNewError() { |
| 356 var port = new ReceivePort(); | 356 asyncStart(); |
| 357 int counter = 2; | 357 int counter = 2; |
| 358 countDown() { | 358 countDown() { |
| 359 if (--counter == 0) port.close(); | 359 if (--counter == 0) asyncEnd(); |
| 360 } | 360 } |
| 361 var completer = new Completer(); | 361 var completer = new Completer(); |
| 362 Future future = completer.future; | 362 Future future = completer.future; |
| 363 Future later = future.whenComplete(() { | 363 Future later = future.whenComplete(() { |
| 364 countDown(); | 364 countDown(); |
| 365 throw "new error"; | 365 throw "new error"; |
| 366 }); | 366 }); |
| 367 later.catchError((error) { | 367 later.catchError((error) { |
| 368 Expect.equals("new error", error); | 368 Expect.equals("new error", error); |
| 369 countDown(); | 369 countDown(); |
| 370 }); | 370 }); |
| 371 completer.completeError("error"); | 371 completer.completeError("error"); |
| 372 } | 372 } |
| 373 | 373 |
| 374 void testFutureWhenCompletePreValue() { | 374 void testFutureWhenCompletePreValue() { |
| 375 var port = new ReceivePort(); | 375 asyncStart(); |
| 376 int counter = 2; | 376 int counter = 2; |
| 377 countDown() { | 377 countDown() { |
| 378 if (--counter == 0) port.close(); | 378 if (--counter == 0) asyncEnd(); |
| 379 } | 379 } |
| 380 var completer = new Completer(); | 380 var completer = new Completer(); |
| 381 Future future = completer.future; | 381 Future future = completer.future; |
| 382 completer.complete(42); | 382 completer.complete(42); |
| 383 Timer.run(() { | 383 Timer.run(() { |
| 384 Future later = future.whenComplete(countDown); | 384 Future later = future.whenComplete(countDown); |
| 385 later.then((v) { | 385 later.then((v) { |
| 386 Expect.equals(42, v); | 386 Expect.equals(42, v); |
| 387 countDown(); | 387 countDown(); |
| 388 }); | 388 }); |
| 389 }); | 389 }); |
| 390 } | 390 } |
| 391 | 391 |
| 392 void testFutureWhenValueFutureValue() { | 392 void testFutureWhenValueFutureValue() { |
| 393 | 393 |
| 394 var port = new ReceivePort(); | 394 asyncStart(); |
| 395 int counter = 3; | 395 int counter = 3; |
| 396 countDown(int expect) { | 396 countDown(int expect) { |
| 397 Expect.equals(expect, counter); | 397 Expect.equals(expect, counter); |
| 398 if (--counter == 0) port.close(); | 398 if (--counter == 0) asyncEnd(); |
| 399 } | 399 } |
| 400 var completer = new Completer(); | 400 var completer = new Completer(); |
| 401 completer.future.whenComplete(() { | 401 completer.future.whenComplete(() { |
| 402 countDown(3); | 402 countDown(3); |
| 403 var completer2 = new Completer(); | 403 var completer2 = new Completer(); |
| 404 new Timer(MS * 10, () { | 404 new Timer(MS * 10, () { |
| 405 countDown(2); | 405 countDown(2); |
| 406 completer2.complete(37); | 406 completer2.complete(37); |
| 407 }); | 407 }); |
| 408 return completer2.future; | 408 return completer2.future; |
| 409 }).then((v) { | 409 }).then((v) { |
| 410 Expect.equals(42, v); | 410 Expect.equals(42, v); |
| 411 countDown(1); | 411 countDown(1); |
| 412 }); | 412 }); |
| 413 | 413 |
| 414 completer.complete(42); | 414 completer.complete(42); |
| 415 } | 415 } |
| 416 | 416 |
| 417 void testFutureWhenValueFutureError() { | 417 void testFutureWhenValueFutureError() { |
| 418 var port = new ReceivePort(); | 418 asyncStart(); |
| 419 int counter = 3; | 419 int counter = 3; |
| 420 countDown(int expect) { | 420 countDown(int expect) { |
| 421 Expect.equals(expect, counter); | 421 Expect.equals(expect, counter); |
| 422 if (--counter == 0) port.close(); | 422 if (--counter == 0) asyncEnd(); |
| 423 } | 423 } |
| 424 var completer = new Completer(); | 424 var completer = new Completer(); |
| 425 completer.future.whenComplete(() { | 425 completer.future.whenComplete(() { |
| 426 countDown(3); | 426 countDown(3); |
| 427 var completer2 = new Completer(); | 427 var completer2 = new Completer(); |
| 428 new Timer(MS * 10, () { | 428 new Timer(MS * 10, () { |
| 429 countDown(2); | 429 countDown(2); |
| 430 completer2.completeError("Fail"); | 430 completer2.completeError("Fail"); |
| 431 }); | 431 }); |
| 432 return completer2.future; | 432 return completer2.future; |
| 433 }).then((v) { | 433 }).then((v) { |
| 434 Expect.fail("should fail async"); | 434 Expect.fail("should fail async"); |
| 435 }, onError: (error) { | 435 }, onError: (error) { |
| 436 Expect.equals("Fail", error); | 436 Expect.equals("Fail", error); |
| 437 countDown(1); | 437 countDown(1); |
| 438 }); | 438 }); |
| 439 | 439 |
| 440 completer.complete(42); | 440 completer.complete(42); |
| 441 } | 441 } |
| 442 | 442 |
| 443 void testFutureWhenErrorFutureValue() { | 443 void testFutureWhenErrorFutureValue() { |
| 444 var port = new ReceivePort(); | 444 asyncStart(); |
| 445 int counter = 3; | 445 int counter = 3; |
| 446 countDown(int expect) { | 446 countDown(int expect) { |
| 447 Expect.equals(expect, counter); | 447 Expect.equals(expect, counter); |
| 448 if (--counter == 0) port.close(); | 448 if (--counter == 0) asyncEnd(); |
| 449 } | 449 } |
| 450 var completer = new Completer(); | 450 var completer = new Completer(); |
| 451 completer.future.whenComplete(() { | 451 completer.future.whenComplete(() { |
| 452 countDown(3); | 452 countDown(3); |
| 453 var completer2 = new Completer(); | 453 var completer2 = new Completer(); |
| 454 new Timer(MS * 10, () { | 454 new Timer(MS * 10, () { |
| 455 countDown(2); | 455 countDown(2); |
| 456 completer2.complete(37); | 456 completer2.complete(37); |
| 457 }); | 457 }); |
| 458 return completer2.future; | 458 return completer2.future; |
| 459 }).then((v) { | 459 }).then((v) { |
| 460 Expect.fail("should fail async"); | 460 Expect.fail("should fail async"); |
| 461 }, onError: (error) { | 461 }, onError: (error) { |
| 462 Expect.equals("Error", error); | 462 Expect.equals("Error", error); |
| 463 countDown(1); | 463 countDown(1); |
| 464 }); | 464 }); |
| 465 | 465 |
| 466 completer.completeError("Error"); | 466 completer.completeError("Error"); |
| 467 } | 467 } |
| 468 | 468 |
| 469 void testFutureWhenErrorFutureError() { | 469 void testFutureWhenErrorFutureError() { |
| 470 var port = new ReceivePort(); | 470 asyncStart(); |
| 471 int counter = 3; | 471 int counter = 3; |
| 472 countDown(int expect) { | 472 countDown(int expect) { |
| 473 Expect.equals(expect, counter); | 473 Expect.equals(expect, counter); |
| 474 if (--counter == 0) port.close(); | 474 if (--counter == 0) asyncEnd(); |
| 475 } | 475 } |
| 476 var completer = new Completer(); | 476 var completer = new Completer(); |
| 477 completer.future.whenComplete(() { | 477 completer.future.whenComplete(() { |
| 478 countDown(3); | 478 countDown(3); |
| 479 var completer2 = new Completer(); | 479 var completer2 = new Completer(); |
| 480 new Timer(MS * 10, () { | 480 new Timer(MS * 10, () { |
| 481 countDown(2); | 481 countDown(2); |
| 482 completer2.completeError("Fail"); | 482 completer2.completeError("Fail"); |
| 483 }); | 483 }); |
| 484 return completer2.future; | 484 return completer2.future; |
| 485 }).then((v) { | 485 }).then((v) { |
| 486 Expect.fail("should fail async"); | 486 Expect.fail("should fail async"); |
| 487 }, onError: (error) { | 487 }, onError: (error) { |
| 488 Expect.equals("Fail", error); | 488 Expect.equals("Fail", error); |
| 489 countDown(1); | 489 countDown(1); |
| 490 }); | 490 }); |
| 491 | 491 |
| 492 completer.completeError("Error"); | 492 completer.completeError("Error"); |
| 493 } | 493 } |
| 494 | 494 |
| 495 void testFutureThenThrowsAsync() { | 495 void testFutureThenThrowsAsync() { |
| 496 final completer = new Completer<int>(); | 496 final completer = new Completer<int>(); |
| 497 final future = completer.future; | 497 final future = completer.future; |
| 498 int error = 42; | 498 int error = 42; |
| 499 | 499 |
| 500 var port = new ReceivePort(); | 500 asyncStart(); |
| 501 future.then((v) { | 501 future.then((v) { |
| 502 throw error; | 502 throw error; |
| 503 }).catchError((e) { | 503 }).catchError((e) { |
| 504 Expect.identical(error, e); | 504 Expect.identical(error, e); |
| 505 port.close(); | 505 asyncEnd(); |
| 506 }); | 506 }); |
| 507 completer.complete(0); | 507 completer.complete(0); |
| 508 } | 508 } |
| 509 | 509 |
| 510 void testFutureCatchThrowsAsync() { | 510 void testFutureCatchThrowsAsync() { |
| 511 final completer = new Completer<int>(); | 511 final completer = new Completer<int>(); |
| 512 final future = completer.future; | 512 final future = completer.future; |
| 513 int error = 42; | 513 int error = 42; |
| 514 | 514 |
| 515 var port = new ReceivePort(); | 515 asyncStart(); |
| 516 future.catchError((e) { | 516 future.catchError((e) { |
| 517 throw error; | 517 throw error; |
| 518 }).catchError((e) { | 518 }).catchError((e) { |
| 519 Expect.identical(error, e); | 519 Expect.identical(error, e); |
| 520 port.close(); | 520 asyncEnd(); |
| 521 }); | 521 }); |
| 522 completer.completeError(0); | 522 completer.completeError(0); |
| 523 } | 523 } |
| 524 | 524 |
| 525 void testFutureCatchRethrowsAsync() { | 525 void testFutureCatchRethrowsAsync() { |
| 526 final completer = new Completer<int>(); | 526 final completer = new Completer<int>(); |
| 527 final future = completer.future; | 527 final future = completer.future; |
| 528 var error; | 528 var error; |
| 529 | 529 |
| 530 var port = new ReceivePort(); | 530 asyncStart(); |
| 531 future.catchError((e) { | 531 future.catchError((e) { |
| 532 error = e; | 532 error = e; |
| 533 throw e; | 533 throw e; |
| 534 }).catchError((e) { | 534 }).catchError((e) { |
| 535 Expect.identical(error, e); | 535 Expect.identical(error, e); |
| 536 port.close(); | 536 asyncEnd(); |
| 537 }); | 537 }); |
| 538 completer.completeError(0); | 538 completer.completeError(0); |
| 539 } | 539 } |
| 540 | 540 |
| 541 void testFutureWhenThrowsAsync() { | 541 void testFutureWhenThrowsAsync() { |
| 542 final completer = new Completer<int>(); | 542 final completer = new Completer<int>(); |
| 543 final future = completer.future; | 543 final future = completer.future; |
| 544 var error = 42; | 544 var error = 42; |
| 545 | 545 |
| 546 var port = new ReceivePort(); | 546 asyncStart(); |
| 547 future.whenComplete(() { | 547 future.whenComplete(() { |
| 548 throw error; | 548 throw error; |
| 549 }).catchError((e) { | 549 }).catchError((e) { |
| 550 Expect.identical(error, e); | 550 Expect.identical(error, e); |
| 551 port.close(); | 551 asyncEnd(); |
| 552 }); | 552 }); |
| 553 completer.complete(0); | 553 completer.complete(0); |
| 554 } | 554 } |
| 555 | 555 |
| 556 void testCompleteWithError() { | 556 void testCompleteWithError() { |
| 557 final completer = new Completer<int>(); | 557 final completer = new Completer<int>(); |
| 558 final future = completer.future; | 558 final future = completer.future; |
| 559 var error = 42; | 559 var error = 42; |
| 560 | 560 |
| 561 var port = new ReceivePort(); | 561 asyncStart(); |
| 562 future.catchError((e) { | 562 future.catchError((e) { |
| 563 Expect.identical(error, e); | 563 Expect.identical(error, e); |
| 564 port.close(); | 564 asyncEnd(); |
| 565 }); | 565 }); |
| 566 | 566 |
| 567 completer.completeError(error); | 567 completer.completeError(error); |
| 568 } | 568 } |
| 569 | 569 |
| 570 void testChainedFutureValue() { | 570 void testChainedFutureValue() { |
| 571 final completer = new Completer(); | 571 final completer = new Completer(); |
| 572 final future = completer.future; | 572 final future = completer.future; |
| 573 var port = new ReceivePort(); | 573 asyncStart(); |
| 574 | 574 |
| 575 future.then((v) => new Future.value(v * 2)) | 575 future.then((v) => new Future.value(v * 2)) |
| 576 .then((v) { | 576 .then((v) { |
| 577 Expect.equals(42, v); | 577 Expect.equals(42, v); |
| 578 port.close(); | 578 asyncEnd(); |
| 579 }); | 579 }); |
| 580 completer.complete(21); | 580 completer.complete(21); |
| 581 } | 581 } |
| 582 | 582 |
| 583 void testChainedFutureValueDelay() { | 583 void testChainedFutureValueDelay() { |
| 584 final completer = new Completer(); | 584 final completer = new Completer(); |
| 585 final future = completer.future; | 585 final future = completer.future; |
| 586 var port = new ReceivePort(); | 586 asyncStart(); |
| 587 | 587 |
| 588 future.then((v) => new Future.delayed(const Duration(milliseconds: 10), | 588 future.then((v) => new Future.delayed(const Duration(milliseconds: 10), |
| 589 () => v * 2)) | 589 () => v * 2)) |
| 590 .then((v) { | 590 .then((v) { |
| 591 Expect.equals(42, v); | 591 Expect.equals(42, v); |
| 592 port.close(); | 592 asyncEnd(); |
| 593 }); | 593 }); |
| 594 completer.complete(21); | 594 completer.complete(21); |
| 595 } | 595 } |
| 596 | 596 |
| 597 void testChainedFutureValue2Delay() { | 597 void testChainedFutureValue2Delay() { |
| 598 var port = new ReceivePort(); | 598 asyncStart(); |
| 599 | 599 |
| 600 new Future.delayed(const Duration(milliseconds: 10)) | 600 new Future.delayed(const Duration(milliseconds: 10)) |
| 601 .then((v) { | 601 .then((v) { |
| 602 Expect.isNull(v); | 602 Expect.isNull(v); |
| 603 port.close(); | 603 asyncEnd(); |
| 604 }); | 604 }); |
| 605 } | 605 } |
| 606 | 606 |
| 607 void testChainedFutureError() { | 607 void testChainedFutureError() { |
| 608 final completer = new Completer(); | 608 final completer = new Completer(); |
| 609 final future = completer.future; | 609 final future = completer.future; |
| 610 var port = new ReceivePort(); | 610 asyncStart(); |
| 611 | 611 |
| 612 future.then((v) => new Future.error("Fehler")) | 612 future.then((v) => new Future.error("Fehler")) |
| 613 .then((v) { Expect.fail("unreachable!"); }, onError: (error) { | 613 .then((v) { Expect.fail("unreachable!"); }, onError: (error) { |
| 614 Expect.equals("Fehler", error); | 614 Expect.equals("Fehler", error); |
| 615 port.close(); | 615 asyncEnd(); |
| 616 }); | 616 }); |
| 617 completer.complete(21); | 617 completer.complete(21); |
| 618 } | 618 } |
| 619 | 619 |
| 620 void testChainedFutureCycle() { | 620 void testChainedFutureCycle() { |
| 621 var port = new ReceivePort(); // Keep alive until port is closed. | 621 asyncStart(); |
| 622 var completer = new Completer(); | 622 var completer = new Completer(); |
| 623 var future, future2; | 623 var future, future2; |
| 624 future = completer.future.then((_) => future2); | 624 future = completer.future.then((_) => future2); |
| 625 future2 = completer.future.then((_) => future); | 625 future2 = completer.future.then((_) => future); |
| 626 completer.complete(42); | 626 completer.complete(42); |
| 627 int ctr = 2; | 627 int ctr = 2; |
| 628 void receiveError(e) { | 628 void receiveError(e) { |
| 629 Expect.isTrue(e is StateError); | 629 Expect.isTrue(e is StateError); |
| 630 if (--ctr == 0) port.close(); | 630 if (--ctr == 0) asyncEnd(); |
| 631 } | 631 } |
| 632 future.catchError(receiveError); | 632 future.catchError(receiveError); |
| 633 future.catchError(receiveError); | 633 future.catchError(receiveError); |
| 634 } | 634 } |
| 635 | 635 |
| 636 main() { | 636 main() { |
| 637 testValue(); | 637 testValue(); |
| 638 testSync(); | 638 testSync(); |
| 639 testNeverComplete(); | 639 testNeverComplete(); |
| 640 | 640 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 669 testFutureThenThrowsAsync(); | 669 testFutureThenThrowsAsync(); |
| 670 testFutureCatchThrowsAsync(); | 670 testFutureCatchThrowsAsync(); |
| 671 testFutureWhenThrowsAsync(); | 671 testFutureWhenThrowsAsync(); |
| 672 testFutureCatchRethrowsAsync(); | 672 testFutureCatchRethrowsAsync(); |
| 673 | 673 |
| 674 testChainedFutureValue(); | 674 testChainedFutureValue(); |
| 675 testChainedFutureValueDelay(); | 675 testChainedFutureValueDelay(); |
| 676 testChainedFutureError(); | 676 testChainedFutureError(); |
| 677 testChainedFutureCycle(); | 677 testChainedFutureCycle(); |
| 678 } | 678 } |
| OLD | NEW |