| 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:async'; |
| 7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
| 8 | 8 |
| 9 Future testWaitEmpty() { | 9 Future testWaitEmpty() { |
| 10 List<Future> futures = new List<Future>(); | 10 List<Future> futures = new List<Future>(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 } | 21 } |
| 22 | 22 |
| 23 Future testCompleteBeforeWait() { | 23 Future testCompleteBeforeWait() { |
| 24 List<Future> futures = new List<Future>(); | 24 List<Future> futures = new List<Future>(); |
| 25 Completer c = new Completer(); | 25 Completer c = new Completer(); |
| 26 futures.add(c.future); | 26 futures.add(c.future); |
| 27 c.complete(null); | 27 c.complete(null); |
| 28 return Futures.wait(futures); | 28 return Futures.wait(futures); |
| 29 } | 29 } |
| 30 | 30 |
| 31 Future testWaitWithMultipleValues() { |
| 32 List<Future> futures = new List<Future>(); |
| 33 Completer c1 = new Completer(); |
| 34 Completer c2 = new Completer(); |
| 35 futures.add(c1.future); |
| 36 futures.add(c2.future); |
| 37 c1.complete(1); |
| 38 c2.complete(2); |
| 39 return Futures.wait(futures).then((values) { |
| 40 Expect.listEquals([1, 2], values); |
| 41 }); |
| 42 } |
| 43 |
| 44 Future testWaitWithSingleError() { |
| 45 List<Future> futures = new List<Future>(); |
| 46 Completer c1 = new Completer(); |
| 47 Completer c2 = new Completer(); |
| 48 futures.add(c1.future); |
| 49 futures.add(c2.future); |
| 50 c1.complete(); |
| 51 c2.completeError('correct error'); |
| 52 |
| 53 return Futures.wait(futures).then((_) { |
| 54 throw 'incorrect error'; |
| 55 }).catchError((e) { |
| 56 Expect.equals('correct error', e.error); |
| 57 }); |
| 58 } |
| 59 |
| 60 Future testWaitWithMultipleErrors() { |
| 61 List<Future> futures = new List<Future>(); |
| 62 Completer c1 = new Completer(); |
| 63 Completer c2 = new Completer(); |
| 64 futures.add(c1.future); |
| 65 futures.add(c2.future); |
| 66 c1.completeError('correct error'); |
| 67 c2.completeError('incorrect error 1'); |
| 68 |
| 69 return Futures.wait(futures).then((_) { |
| 70 throw 'incorrect error 2'; |
| 71 }).catchError((e) { |
| 72 Expect.equals('correct error', e.error); |
| 73 }); |
| 74 } |
| 75 |
| 31 Future testForEachEmpty() { | 76 Future testForEachEmpty() { |
| 32 return Futures.forEach([], (_) { | 77 return Futures.forEach([], (_) { |
| 33 throw 'should not be called'; | 78 throw 'should not be called'; |
| 34 }); | 79 }); |
| 35 } | 80 } |
| 36 | 81 |
| 37 Future testForEach() { | 82 Future testForEach() { |
| 38 var seen = <int>[]; | 83 var seen = <int>[]; |
| 39 return Futures.forEach([1, 2, 3, 4, 5], (n) { | 84 return Futures.forEach([1, 2, 3, 4, 5], (n) { |
| 40 seen.add(n); | 85 seen.add(n); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 54 Expect.equals('correct exception', e.error); | 99 Expect.equals('correct exception', e.error); |
| 55 }); | 100 }); |
| 56 } | 101 } |
| 57 | 102 |
| 58 main() { | 103 main() { |
| 59 List<Future> futures = new List<Future>(); | 104 List<Future> futures = new List<Future>(); |
| 60 | 105 |
| 61 futures.add(testWaitEmpty()); | 106 futures.add(testWaitEmpty()); |
| 62 futures.add(testCompleteAfterWait()); | 107 futures.add(testCompleteAfterWait()); |
| 63 futures.add(testCompleteBeforeWait()); | 108 futures.add(testCompleteBeforeWait()); |
| 109 futures.add(testWaitWithMultipleValues()); |
| 110 futures.add(testWaitWithSingleError()); |
| 111 futures.add(testWaitWithMultipleErrors()); |
| 64 futures.add(testForEachEmpty()); | 112 futures.add(testForEachEmpty()); |
| 65 futures.add(testForEach()); | 113 futures.add(testForEach()); |
| 66 | 114 |
| 67 // Use a receive port for blocking the test. | 115 // Use a receive port for blocking the test. |
| 68 // Note that if the test fails, the program will not end. | 116 // Note that if the test fails, the program will not end. |
| 69 ReceivePort port = new ReceivePort(); | 117 ReceivePort port = new ReceivePort(); |
| 70 Futures.wait(futures).then((List list) { | 118 Futures.wait(futures).then((List list) { |
| 71 Expect.equals(5, list.length); | 119 Expect.equals(8, list.length); |
| 72 port.close(); | 120 port.close(); |
| 73 }); | 121 }); |
| 74 } | 122 } |
| OLD | NEW |