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