| 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 "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
| 9 | 9 |
| 10 Future testWaitEmpty() { | 10 Future testWaitEmpty() { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 List<Future> futures = new List<Future>(); | 46 List<Future> futures = new List<Future>(); |
| 47 Completer c1 = new Completer(); | 47 Completer c1 = new Completer(); |
| 48 Completer c2 = new Completer(); | 48 Completer c2 = new Completer(); |
| 49 futures.add(c1.future); | 49 futures.add(c1.future); |
| 50 futures.add(c2.future); | 50 futures.add(c2.future); |
| 51 c1.complete(); | 51 c1.complete(); |
| 52 c2.completeError('correct error'); | 52 c2.completeError('correct error'); |
| 53 | 53 |
| 54 return Future.wait(futures).then((_) { | 54 return Future.wait(futures).then((_) { |
| 55 throw 'incorrect error'; | 55 throw 'incorrect error'; |
| 56 }).catchError((e) { | 56 }).catchError((error) { |
| 57 Expect.equals('correct error', e.error); | 57 Expect.equals('correct error', error); |
| 58 }); | 58 }); |
| 59 } | 59 } |
| 60 | 60 |
| 61 Future testWaitWithMultipleErrors() { | 61 Future testWaitWithMultipleErrors() { |
| 62 List<Future> futures = new List<Future>(); | 62 List<Future> futures = new List<Future>(); |
| 63 Completer c1 = new Completer(); | 63 Completer c1 = new Completer(); |
| 64 Completer c2 = new Completer(); | 64 Completer c2 = new Completer(); |
| 65 futures.add(c1.future); | 65 futures.add(c1.future); |
| 66 futures.add(c2.future); | 66 futures.add(c2.future); |
| 67 c1.completeError('correct error'); | 67 c1.completeError('correct error'); |
| 68 c2.completeError('incorrect error 1'); | 68 c2.completeError('incorrect error 1'); |
| 69 | 69 |
| 70 return Future.wait(futures).then((_) { | 70 return Future.wait(futures).then((_) { |
| 71 throw 'incorrect error 2'; | 71 throw 'incorrect error 2'; |
| 72 }).catchError((e) { | 72 }).catchError((error) { |
| 73 Expect.equals('correct error', e.error); | 73 Expect.equals('correct error', error); |
| 74 }); | 74 }); |
| 75 } | 75 } |
| 76 | 76 |
| 77 Future testForEachEmpty() { | 77 Future testForEachEmpty() { |
| 78 return Future.forEach([], (_) { | 78 return Future.forEach([], (_) { |
| 79 throw 'should not be called'; | 79 throw 'should not be called'; |
| 80 }); | 80 }); |
| 81 } | 81 } |
| 82 | 82 |
| 83 Future testForEach() { | 83 Future testForEach() { |
| 84 var seen = <int>[]; | 84 var seen = <int>[]; |
| 85 return Future.forEach([1, 2, 3, 4, 5], (n) { | 85 return Future.forEach([1, 2, 3, 4, 5], (n) { |
| 86 seen.add(n); | 86 seen.add(n); |
| 87 return new Future.immediate(null); | 87 return new Future.immediate(null); |
| 88 }).then((_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); | 88 }).then((_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); |
| 89 } | 89 } |
| 90 | 90 |
| 91 Future testForEachWithException() { | 91 Future testForEachWithException() { |
| 92 var seen = <int>[]; | 92 var seen = <int>[]; |
| 93 return Future.forEach([1, 2, 3, 4, 5], (n) { | 93 return Future.forEach([1, 2, 3, 4, 5], (n) { |
| 94 if (n == 4) throw 'correct exception'; | 94 if (n == 4) throw 'correct exception'; |
| 95 seen.add(n); | 95 seen.add(n); |
| 96 return new Future.immediate(null); | 96 return new Future.immediate(null); |
| 97 }).then((_) { | 97 }).then((_) { |
| 98 throw 'incorrect exception'; | 98 throw 'incorrect exception'; |
| 99 }).catchError((e) { | 99 }).catchError((error) { |
| 100 Expect.equals('correct exception', e.error); | 100 Expect.equals('correct exception', error); |
| 101 }); | 101 }); |
| 102 } | 102 } |
| 103 | 103 |
| 104 main() { | 104 main() { |
| 105 List<Future> futures = new List<Future>(); | 105 List<Future> futures = new List<Future>(); |
| 106 | 106 |
| 107 futures.add(testWaitEmpty()); | 107 futures.add(testWaitEmpty()); |
| 108 futures.add(testCompleteAfterWait()); | 108 futures.add(testCompleteAfterWait()); |
| 109 futures.add(testCompleteBeforeWait()); | 109 futures.add(testCompleteBeforeWait()); |
| 110 futures.add(testWaitWithMultipleValues()); | 110 futures.add(testWaitWithMultipleValues()); |
| 111 futures.add(testWaitWithSingleError()); | 111 futures.add(testWaitWithSingleError()); |
| 112 futures.add(testWaitWithMultipleErrors()); | 112 futures.add(testWaitWithMultipleErrors()); |
| 113 futures.add(testForEachEmpty()); | 113 futures.add(testForEachEmpty()); |
| 114 futures.add(testForEach()); | 114 futures.add(testForEach()); |
| 115 futures.add(testForEachWithException()); | 115 futures.add(testForEachWithException()); |
| 116 | 116 |
| 117 // Use a receive port for blocking the test. | 117 // Use a receive port for blocking the test. |
| 118 // Note that if the test fails, the program will not end. | 118 // Note that if the test fails, the program will not end. |
| 119 ReceivePort port = new ReceivePort(); | 119 ReceivePort port = new ReceivePort(); |
| 120 Future.wait(futures).then((List list) { | 120 Future.wait(futures).then((List list) { |
| 121 Expect.equals(9, list.length); | 121 Expect.equals(9, list.length); |
| 122 port.close(); | 122 port.close(); |
| 123 }); | 123 }); |
| 124 } | 124 } |
| OLD | NEW |