Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 future_test; | 5 library future_test; |
| 6 | 6 |
| 7 import 'package:async_helper/async_helper.dart'; | 7 import 'package:async_helper/async_helper.dart'; |
| 8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 | 10 |
| (...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 742 final future = new Future<int>.sync(() { | 742 final future = new Future<int>.sync(() { |
| 743 return new Future<int>.value(42); | 743 return new Future<int>.value(42); |
| 744 }); | 744 }); |
| 745 | 745 |
| 746 future.then((int val) { | 746 future.then((int val) { |
| 747 Expect.equals(val, 42); | 747 Expect.equals(val, 42); |
| 748 asyncEnd(); | 748 asyncEnd(); |
| 749 }); | 749 }); |
| 750 } | 750 } |
| 751 | 751 |
| 752 void testWaitCleanUp() { | |
| 753 asyncStart(); | |
| 754 // Creates three futures with different completion times, and where some fail. | |
| 755 // The `mask` specifies which futures fail (values 1-7), | |
| 756 // and `permute` defines the order of completion. values 0-5. | |
| 757 void doTest(int mask, int permute) { | |
| 758 asyncStart(); | |
| 759 String stringId = "waitCleanup-$mask-$permute"; | |
| 760 List futures = new List(3); | |
| 761 List cleanup = new List(3); | |
| 762 int permuteTmp = permute; | |
| 763 for (int i = 0; i < 3; i++) { | |
| 764 bool throws = (mask & (1 << i)) != 0; | |
| 765 var future = new Future.delayed( | |
| 766 new Duration(milliseconds: 100 * (i + 1)), | |
| 767 () => (throws ? throw "Error $i($mask-$permute)" : i)); | |
| 768 int mod = 3 - i; | |
| 769 int position = permuteTmp % mod; | |
| 770 permuteTmp = permuteTmp ~/ mod; | |
| 771 while (futures[position] != null) position++; | |
| 772 futures[position] = future; | |
| 773 cleanup[i] = throws; | |
| 774 } | |
| 775 void cleanUp(index) { | |
|
Søren Gjesse
2015/01/09 10:04:11
Check that cleanup[index] is false.
Lasse Reichstein Nielsen
2015/01/09 10:52:09
Done.
| |
| 776 cleanup[index] = true; | |
| 777 } | |
| 778 Future.wait(futures, cleanUp: cleanUp) | |
|
Søren Gjesse
2015/01/09 10:04:11
Maybe also test with eager error.
Lasse Reichstein Nielsen
2015/01/09 10:52:09
Done.
| |
| 779 .then((_) { Expect.fail("No error: $stringId"); }, | |
| 780 onError: (e, s) { | |
|
Søren Gjesse
2015/01/09 10:04:11
Wouldnt it read better if you use catchError here?
Lasse Reichstein Nielsen
2015/01/09 10:52:09
It would be wrong, even if it might read better.
T
| |
| 781 Expect.listEquals([true, true, true], cleanup); | |
| 782 asyncEnd(); | |
| 783 }); | |
| 784 } | |
| 785 | |
| 786 for (int i = 1; i < 8; i++) { | |
| 787 for (int j = 0; j < 6; j++) { | |
| 788 doTest(i, j); | |
| 789 } | |
| 790 } | |
| 791 asyncEnd(); | |
| 792 } | |
| 793 | |
| 794 void testWaitCleanUpError() { | |
| 795 var cms = const Duration(milliseconds: 100); | |
| 796 var cleanups = new List.filled(3, false); | |
| 797 var uncaughts = new List.filled(3, false); | |
| 798 asyncStart(); | |
| 799 asyncStart(); | |
| 800 asyncStart(); | |
| 801 runZoned(() { | |
| 802 Future.wait([new Future.delayed(cms, () => 0), | |
| 803 new Future.delayed(cms * 2, ()=> throw 1), | |
| 804 new Future.delayed(cms * 3, () => 2)], | |
| 805 cleanUp: (index) { | |
| 806 Expect.isTrue(index == 0 || index == 2, "$index"); | |
| 807 Expect.isFalse(cleanups[index]); | |
| 808 cleanups[index] = true; | |
| 809 throw index; | |
| 810 }) | |
| 811 .catchError((e) { | |
| 812 Expect.equals(e, 1); | |
| 813 asyncEnd(); | |
| 814 }); | |
| 815 | |
| 816 }, onError: (int index, s) { | |
| 817 Expect.isTrue(index == 0 || index == 2, "$index"); | |
| 818 Expect.isFalse(uncaughts[index]); | |
| 819 uncaughts[index] = true; | |
| 820 asyncEnd(); | |
| 821 }); | |
| 822 } | |
| 823 | |
| 752 main() { | 824 main() { |
| 753 asyncStart(); | 825 asyncStart(); |
| 754 | 826 |
| 755 testValue(); | 827 testValue(); |
| 756 testSync(); | 828 testSync(); |
| 757 testNeverComplete(); | 829 testNeverComplete(); |
| 758 | 830 |
| 759 testComplete(); | 831 testComplete(); |
| 760 testCompleteWithSuccessHandlerBeforeComplete(); | 832 testCompleteWithSuccessHandlerBeforeComplete(); |
| 761 testCompleteWithSuccessHandlerAfterComplete(); | 833 testCompleteWithSuccessHandlerAfterComplete(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 798 testFutureCatchThrowsAsync(); | 870 testFutureCatchThrowsAsync(); |
| 799 testFutureWhenThrowsAsync(); | 871 testFutureWhenThrowsAsync(); |
| 800 testFutureCatchRethrowsAsync(); | 872 testFutureCatchRethrowsAsync(); |
| 801 | 873 |
| 802 testChainedFutureValue(); | 874 testChainedFutureValue(); |
| 803 testChainedFutureValueDelay(); | 875 testChainedFutureValueDelay(); |
| 804 testChainedFutureError(); | 876 testChainedFutureError(); |
| 805 | 877 |
| 806 testSyncFuture_i13368(); | 878 testSyncFuture_i13368(); |
| 807 | 879 |
| 880 testWaitCleanUp(); | |
| 881 testWaitCleanUpError(); | |
| 882 | |
| 808 asyncEnd(); | 883 asyncEnd(); |
| 809 } | 884 } |
| 810 | 885 |
| 811 /// A Future that isn't recognizable as a _Future. | 886 /// A Future that isn't recognizable as a _Future. |
| 812 class CustomFuture<T> implements Future<T> { | 887 class CustomFuture<T> implements Future<T> { |
| 813 Future _realFuture; | 888 Future _realFuture; |
| 814 CustomFuture(this._realFuture); | 889 CustomFuture(this._realFuture); |
| 815 Future then(action(result), {Function onError}) => | 890 Future then(action(result), {Function onError}) => |
| 816 _realFuture.then(action, onError: onError); | 891 _realFuture.then(action, onError: onError); |
| 817 Future catchError(Function onError, {bool test(e)}) => | 892 Future catchError(Function onError, {bool test(e)}) => |
| 818 _realFuture.catchError(onError, test: test); | 893 _realFuture.catchError(onError, test: test); |
| 819 Future whenComplete(action()) => _realFuture.whenComplete(action); | 894 Future whenComplete(action()) => _realFuture.whenComplete(action); |
| 820 Future timeout(Duration timeLimit, {void onTimeout()}) => | 895 Future timeout(Duration timeLimit, {void onTimeout()}) => |
| 821 _realFuture.timeout(timeLimit, onTimeout: onTimeout); | 896 _realFuture.timeout(timeLimit, onTimeout: onTimeout); |
| 822 Stream asStream() => _realFuture.asStream(); | 897 Stream asStream() => _realFuture.asStream(); |
| 823 String toString() => "CustomFuture@${_realFuture.hashCode}"; | 898 String toString() => "CustomFuture@${_realFuture.hashCode}"; |
| 824 int get hashCode => _realFuture.hashCode; | 899 int get hashCode => _realFuture.hashCode; |
| 825 } | 900 } |
| OLD | NEW |