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) { |
| 776 Expect.isFalse(cleanup[index]); |
| 777 cleanup[index] = true; |
| 778 } |
| 779 Future.wait(futures, cleanUp: cleanUp) |
| 780 .then((_) { Expect.fail("No error: $stringId"); }, |
| 781 onError: (e, s) { |
| 782 Expect.listEquals([true, true, true], cleanup); |
| 783 asyncEnd(); |
| 784 }); |
| 785 } |
| 786 |
| 787 for (int i = 1; i < 8; i++) { |
| 788 for (int j = 0; j < 6; j++) { |
| 789 doTest(i, j); |
| 790 } |
| 791 } |
| 792 asyncEnd(); |
| 793 } |
| 794 |
| 795 void testWaitCleanUpEager() { |
| 796 asyncStart(); |
| 797 // Creates three futures with different completion times, and where some fail. |
| 798 // The `mask` specifies which futures fail (values 1-7), |
| 799 // and `permute` defines the order of completion. values 0-5. |
| 800 void doTest(int mask, int permute) { |
| 801 asyncStart(); |
| 802 asyncStart(); |
| 803 bool done = false; |
| 804 String stringId = "waitCleanup-$mask-$permute"; |
| 805 List futures = new List(3); |
| 806 List cleanup = new List(3); |
| 807 int permuteTmp = permute; |
| 808 for (int i = 0; i < 3; i++) { |
| 809 bool throws = (mask & (1 << i)) != 0; |
| 810 var future = new Future.delayed( |
| 811 new Duration(milliseconds: 100 * (i + 1)), |
| 812 () => (throws ? throw "Error $i($mask-$permute)" : i)); |
| 813 int mod = 3 - i; |
| 814 int position = permuteTmp % mod; |
| 815 permuteTmp = permuteTmp ~/ mod; |
| 816 while (futures[position] != null) position++; |
| 817 futures[position] = future; |
| 818 cleanup[i] = throws; |
| 819 } |
| 820 void checkDone() { |
| 821 if (done) return; |
| 822 if (cleanup.every((v) => v)) { |
| 823 done = true; |
| 824 asyncEnd(); |
| 825 } |
| 826 } |
| 827 void cleanUp(index) { |
| 828 Expect.isFalse(cleanup[index]); |
| 829 cleanup[index] = true; |
| 830 // Cleanup might happen before and after the wait().then() callback. |
| 831 checkDone(); |
| 832 } |
| 833 Future.wait(futures, eagerError: true, cleanUp: cleanUp) |
| 834 .then((_) { Expect.fail("No error: $stringId"); }, |
| 835 onError: (e, s) { |
| 836 asyncEnd(); |
| 837 checkDone(); |
| 838 }); |
| 839 } |
| 840 |
| 841 for (int i = 1; i < 8; i++) { |
| 842 for (int j = 0; j < 6; j++) { |
| 843 doTest(i, j); |
| 844 } |
| 845 } |
| 846 asyncEnd(); |
| 847 } |
| 848 |
| 849 void testWaitCleanUpError() { |
| 850 var cms = const Duration(milliseconds: 100); |
| 851 var cleanups = new List.filled(3, false); |
| 852 var uncaughts = new List.filled(3, false); |
| 853 asyncStart(); |
| 854 asyncStart(); |
| 855 asyncStart(); |
| 856 runZoned(() { |
| 857 Future.wait([new Future.delayed(cms, () => 0), |
| 858 new Future.delayed(cms * 2, ()=> throw 1), |
| 859 new Future.delayed(cms * 3, () => 2)], |
| 860 cleanUp: (index) { |
| 861 Expect.isTrue(index == 0 || index == 2, "$index"); |
| 862 Expect.isFalse(cleanups[index]); |
| 863 cleanups[index] = true; |
| 864 throw index; |
| 865 }) |
| 866 .catchError((e) { |
| 867 Expect.equals(e, 1); |
| 868 asyncEnd(); |
| 869 }); |
| 870 |
| 871 }, onError: (int index, s) { |
| 872 Expect.isTrue(index == 0 || index == 2, "$index"); |
| 873 Expect.isFalse(uncaughts[index]); |
| 874 uncaughts[index] = true; |
| 875 asyncEnd(); |
| 876 }); |
| 877 } |
| 878 |
752 main() { | 879 main() { |
753 asyncStart(); | 880 asyncStart(); |
754 | 881 |
755 testValue(); | 882 testValue(); |
756 testSync(); | 883 testSync(); |
757 testNeverComplete(); | 884 testNeverComplete(); |
758 | 885 |
759 testComplete(); | 886 testComplete(); |
760 testCompleteWithSuccessHandlerBeforeComplete(); | 887 testCompleteWithSuccessHandlerBeforeComplete(); |
761 testCompleteWithSuccessHandlerAfterComplete(); | 888 testCompleteWithSuccessHandlerAfterComplete(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
798 testFutureCatchThrowsAsync(); | 925 testFutureCatchThrowsAsync(); |
799 testFutureWhenThrowsAsync(); | 926 testFutureWhenThrowsAsync(); |
800 testFutureCatchRethrowsAsync(); | 927 testFutureCatchRethrowsAsync(); |
801 | 928 |
802 testChainedFutureValue(); | 929 testChainedFutureValue(); |
803 testChainedFutureValueDelay(); | 930 testChainedFutureValueDelay(); |
804 testChainedFutureError(); | 931 testChainedFutureError(); |
805 | 932 |
806 testSyncFuture_i13368(); | 933 testSyncFuture_i13368(); |
807 | 934 |
| 935 testWaitCleanUp(); |
| 936 testWaitCleanUpError(); |
| 937 |
808 asyncEnd(); | 938 asyncEnd(); |
809 } | 939 } |
810 | 940 |
811 /// A Future that isn't recognizable as a _Future. | 941 /// A Future that isn't recognizable as a _Future. |
812 class CustomFuture<T> implements Future<T> { | 942 class CustomFuture<T> implements Future<T> { |
813 Future _realFuture; | 943 Future _realFuture; |
814 CustomFuture(this._realFuture); | 944 CustomFuture(this._realFuture); |
815 Future then(action(result), {Function onError}) => | 945 Future then(action(result), {Function onError}) => |
816 _realFuture.then(action, onError: onError); | 946 _realFuture.then(action, onError: onError); |
817 Future catchError(Function onError, {bool test(e)}) => | 947 Future catchError(Function onError, {bool test(e)}) => |
818 _realFuture.catchError(onError, test: test); | 948 _realFuture.catchError(onError, test: test); |
819 Future whenComplete(action()) => _realFuture.whenComplete(action); | 949 Future whenComplete(action()) => _realFuture.whenComplete(action); |
820 Future timeout(Duration timeLimit, {void onTimeout()}) => | 950 Future timeout(Duration timeLimit, {void onTimeout()}) => |
821 _realFuture.timeout(timeLimit, onTimeout: onTimeout); | 951 _realFuture.timeout(timeLimit, onTimeout: onTimeout); |
822 Stream asStream() => _realFuture.asStream(); | 952 Stream asStream() => _realFuture.asStream(); |
823 String toString() => "CustomFuture@${_realFuture.hashCode}"; | 953 String toString() => "CustomFuture@${_realFuture.hashCode}"; |
824 int get hashCode => _realFuture.hashCode; | 954 int get hashCode => _realFuture.hashCode; |
825 } | 955 } |
OLD | NEW |