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