| 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>(); |
| 11 return Futures.wait(futures); | 11 return Future.wait(futures); |
| 12 } | 12 } |
| 13 | 13 |
| 14 Future testCompleteAfterWait() { | 14 Future testCompleteAfterWait() { |
| 15 List<Future> futures = new List<Future>(); | 15 List<Future> futures = new List<Future>(); |
| 16 Completer<Object> c = new Completer<Object>(); | 16 Completer<Object> c = new Completer<Object>(); |
| 17 futures.add(c.future); | 17 futures.add(c.future); |
| 18 Future future = Futures.wait(futures); | 18 Future future = Future.wait(futures); |
| 19 c.complete(null); | 19 c.complete(null); |
| 20 return future; | 20 return future; |
| 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 Future.wait(futures); |
| 29 } | 29 } |
| 30 | 30 |
| 31 Future testWaitWithMultipleValues() { | 31 Future testWaitWithMultipleValues() { |
| 32 List<Future> futures = new List<Future>(); | 32 List<Future> futures = new List<Future>(); |
| 33 Completer c1 = new Completer(); | 33 Completer c1 = new Completer(); |
| 34 Completer c2 = new Completer(); | 34 Completer c2 = new Completer(); |
| 35 futures.add(c1.future); | 35 futures.add(c1.future); |
| 36 futures.add(c2.future); | 36 futures.add(c2.future); |
| 37 c1.complete(1); | 37 c1.complete(1); |
| 38 c2.complete(2); | 38 c2.complete(2); |
| 39 return Futures.wait(futures).then((values) { | 39 return Future.wait(futures).then((values) { |
| 40 Expect.listEquals([1, 2], values); | 40 Expect.listEquals([1, 2], values); |
| 41 }); | 41 }); |
| 42 } | 42 } |
| 43 | 43 |
| 44 Future testWaitWithSingleError() { | 44 Future testWaitWithSingleError() { |
| 45 List<Future> futures = new List<Future>(); | 45 List<Future> futures = new List<Future>(); |
| 46 Completer c1 = new Completer(); | 46 Completer c1 = new Completer(); |
| 47 Completer c2 = new Completer(); | 47 Completer c2 = new Completer(); |
| 48 futures.add(c1.future); | 48 futures.add(c1.future); |
| 49 futures.add(c2.future); | 49 futures.add(c2.future); |
| 50 c1.complete(); | 50 c1.complete(); |
| 51 c2.completeError('correct error'); | 51 c2.completeError('correct error'); |
| 52 | 52 |
| 53 return Futures.wait(futures).then((_) { | 53 return Future.wait(futures).then((_) { |
| 54 throw 'incorrect error'; | 54 throw 'incorrect error'; |
| 55 }).catchError((e) { | 55 }).catchError((e) { |
| 56 Expect.equals('correct error', e.error); | 56 Expect.equals('correct error', e.error); |
| 57 }); | 57 }); |
| 58 } | 58 } |
| 59 | 59 |
| 60 Future testWaitWithMultipleErrors() { | 60 Future testWaitWithMultipleErrors() { |
| 61 List<Future> futures = new List<Future>(); | 61 List<Future> futures = new List<Future>(); |
| 62 Completer c1 = new Completer(); | 62 Completer c1 = new Completer(); |
| 63 Completer c2 = new Completer(); | 63 Completer c2 = new Completer(); |
| 64 futures.add(c1.future); | 64 futures.add(c1.future); |
| 65 futures.add(c2.future); | 65 futures.add(c2.future); |
| 66 c1.completeError('correct error'); | 66 c1.completeError('correct error'); |
| 67 c2.completeError('incorrect error 1'); | 67 c2.completeError('incorrect error 1'); |
| 68 | 68 |
| 69 return Futures.wait(futures).then((_) { | 69 return Future.wait(futures).then((_) { |
| 70 throw 'incorrect error 2'; | 70 throw 'incorrect error 2'; |
| 71 }).catchError((e) { | 71 }).catchError((e) { |
| 72 Expect.equals('correct error', e.error); | 72 Expect.equals('correct error', e.error); |
| 73 }); | 73 }); |
| 74 } | 74 } |
| 75 | 75 |
| 76 Future testForEachEmpty() { | 76 Future testForEachEmpty() { |
| 77 return Futures.forEach([], (_) { | 77 return Future.forEach([], (_) { |
| 78 throw 'should not be called'; | 78 throw 'should not be called'; |
| 79 }); | 79 }); |
| 80 } | 80 } |
| 81 | 81 |
| 82 Future testForEach() { | 82 Future testForEach() { |
| 83 var seen = <int>[]; | 83 var seen = <int>[]; |
| 84 return Futures.forEach([1, 2, 3, 4, 5], (n) { | 84 return Future.forEach([1, 2, 3, 4, 5], (n) { |
| 85 seen.add(n); | 85 seen.add(n); |
| 86 return new Future.immediate(null); | 86 return new Future.immediate(null); |
| 87 }).then((_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); | 87 }).then((_) => Expect.listEquals([1, 2, 3, 4, 5], seen)); |
| 88 } | 88 } |
| 89 | 89 |
| 90 Future testForEachWithException() { | 90 Future testForEachWithException() { |
| 91 var seen = <int>[]; | 91 var seen = <int>[]; |
| 92 return Futures.forEach([1, 2, 3, 4, 5], (n) { | 92 return Future.forEach([1, 2, 3, 4, 5], (n) { |
| 93 if (n == 4) throw 'correct exception'; | 93 if (n == 4) throw 'correct exception'; |
| 94 seen.add(n); | 94 seen.add(n); |
| 95 return new Future.immediate(null); | 95 return new Future.immediate(null); |
| 96 }).then((_) { | 96 }).then((_) { |
| 97 throw 'incorrect exception'; | 97 throw 'incorrect exception'; |
| 98 }).catchError((e) { | 98 }).catchError((e) { |
| 99 Expect.equals('correct exception', e.error); | 99 Expect.equals('correct exception', e.error); |
| 100 }); | 100 }); |
| 101 } | 101 } |
| 102 | 102 |
| 103 main() { | 103 main() { |
| 104 List<Future> futures = new List<Future>(); | 104 List<Future> futures = new List<Future>(); |
| 105 | 105 |
| 106 futures.add(testWaitEmpty()); | 106 futures.add(testWaitEmpty()); |
| 107 futures.add(testCompleteAfterWait()); | 107 futures.add(testCompleteAfterWait()); |
| 108 futures.add(testCompleteBeforeWait()); | 108 futures.add(testCompleteBeforeWait()); |
| 109 futures.add(testWaitWithMultipleValues()); | 109 futures.add(testWaitWithMultipleValues()); |
| 110 futures.add(testWaitWithSingleError()); | 110 futures.add(testWaitWithSingleError()); |
| 111 futures.add(testWaitWithMultipleErrors()); | 111 futures.add(testWaitWithMultipleErrors()); |
| 112 futures.add(testForEachEmpty()); | 112 futures.add(testForEachEmpty()); |
| 113 futures.add(testForEach()); | 113 futures.add(testForEach()); |
| 114 | 114 |
| 115 // Use a receive port for blocking the test. | 115 // Use a receive port for blocking the test. |
| 116 // Note that if the test fails, the program will not end. | 116 // Note that if the test fails, the program will not end. |
| 117 ReceivePort port = new ReceivePort(); | 117 ReceivePort port = new ReceivePort(); |
| 118 Futures.wait(futures).then((List list) { | 118 Future.wait(futures).then((List list) { |
| 119 Expect.equals(8, list.length); | 119 Expect.equals(8, list.length); |
| 120 port.close(); | 120 port.close(); |
| 121 }); | 121 }); |
| 122 } | 122 } |
| OLD | NEW |