| 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 'dart:async'; | |
| 7 import 'dart:isolate'; | |
| 8 | |
| 9 Future testWaitEmpty() { | |
| 10 List<Future> futures = new List<Future>(); | |
| 11 return Futures.wait(futures); | |
| 12 } | |
| 13 | |
| 14 Future testCompleteAfterWait() { | |
| 15 List<Future> futures = new List<Future>(); | |
| 16 Completer<Object> c = new Completer<Object>(); | |
| 17 futures.add(c.future); | |
| 18 Future future = Futures.wait(futures); | |
| 19 c.complete(null); | |
| 20 return future; | |
| 21 } | |
| 22 | |
| 23 Future testCompleteBeforeWait() { | |
| 24 List<Future> futures = new List<Future>(); | |
| 25 Completer c = new Completer(); | |
| 26 futures.add(c.future); | |
| 27 c.complete(null); | |
| 28 return Futures.wait(futures); | |
| 29 } | |
| 30 | |
| 31 Future testForEachEmpty() { | |
| 32 return Futures.forEach([], (_) { | |
| 33 throw 'should not be called'; | |
| 34 }); | |
| 35 } | |
| 36 | |
| 37 Future testForEach() { | |
| 38 var seen = <int>[]; | |
| 39 return Futures.forEach([1, 2, 3, 4, 5], (n) { | |
| 40 seen.add(n); | |
| 41 return new Future.immediate(null); | |
| 42 }).then((_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); | |
| 43 } | |
| 44 | |
| 45 Future testForEachWithException() { | |
| 46 var seen = <int>[]; | |
| 47 return Futures.forEach([1, 2, 3, 4, 5], (n) { | |
| 48 if (n == 4) throw 'correct exception'; | |
| 49 seen.add(n); | |
| 50 return new Future.immediate(null); | |
| 51 }).then((_) { | |
| 52 throw 'incorrect exception'; | |
| 53 }).catchError((e) { | |
| 54 Expect.equals('correct exception', e.error); | |
| 55 }); | |
| 56 } | |
| 57 | |
| 58 main() { | |
| 59 List<Future> futures = new List<Future>(); | |
| 60 | |
| 61 futures.add(testWaitEmpty()); | |
| 62 futures.add(testCompleteAfterWait()); | |
| 63 futures.add(testCompleteBeforeWait()); | |
| 64 futures.add(testForEachEmpty()); | |
| 65 futures.add(testForEach()); | |
| 66 | |
| 67 // Use a receive port for blocking the test. | |
| 68 // Note that if the test fails, the program will not end. | |
| 69 ReceivePort port = new ReceivePort(); | |
| 70 Futures.wait(futures).then((List list) { | |
| 71 Expect.equals(5, list.length); | |
| 72 port.close(); | |
| 73 }); | |
| 74 } | |
| OLD | NEW |