OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 import "dart:async"; | |
6 import "dart:math" show Random; | |
7 import "package:async/async.dart"; | |
8 import "package:test/test.dart"; | |
9 | |
10 final someStack = StackTrace.current; | |
11 Result<int> res(int n) => new Result<int>.value(n); | |
12 Result err(n) => new ErrorResult("$n", someStack); | |
13 | |
14 main() { | |
15 test("empty", () async { | |
16 var all = await Result.captureAll<int>(futures(0)); | |
17 expect(all, []); | |
18 }); | |
19 | |
20 group("futures only,", () { | |
21 test("single", () async { | |
22 var all = await Result.captureAll<int>(futures(1)); | |
23 expect(all, [res(0)]); | |
24 }); | |
25 | |
26 test("multiple", () async { | |
27 var all = await Result.captureAll<int>(futures(3)); | |
28 expect(all, [res(0), res(1), res(2)]); | |
29 }); | |
30 | |
31 test("error only", () async { | |
32 var all = await Result.captureAll<int>(futures(1, throwWhen: (_) => true)) ; | |
floitsch
2017/08/24 17:46:39
run darfmt on this file.
| |
33 expect(all, [err(0)]); | |
34 }); | |
35 | |
36 test("multiple error only", () async { | |
37 var all = await Result.captureAll<int>(futures(3, throwWhen: (_) => true)) ; | |
38 expect(all, [err(0), err(1), err(2)]); | |
39 }); | |
40 | |
41 test("mixed error and value", () async { | |
42 var all = await Result.captureAll<int>( | |
43 futures(4, throwWhen: (x) => x.isOdd)); | |
44 expect(all, [res(0), err(1), res(2), err(3)]); | |
45 }); | |
46 | |
47 test("completion permutation 1-2-3", () async { | |
48 var cs = new List.generate(3, (_) => new Completer<int>()); | |
49 var all = Result.captureAll<int>(cs.map((c) => c.future)); | |
50 expect(all, completion([res(1), res(2), err(3)])); | |
51 await 0; | |
52 cs[0].complete(1); | |
53 await 0; | |
54 cs[1].complete(2); | |
55 await 0; | |
56 cs[2].completeError("3", someStack); | |
57 }); | |
58 | |
59 test("completion permutation 1-3-2", () async { | |
60 var cs = new List.generate(3, (_) => new Completer<int>()); | |
61 var all = Result.captureAll<int>(cs.map((c) => c.future)); | |
62 expect(all, completion([res(1), res(2), err(3)])); | |
63 await 0; | |
64 cs[0].complete(1); | |
65 await 0; | |
66 cs[2].completeError("3", someStack); | |
67 await 0; | |
68 cs[1].complete(2); | |
69 }); | |
70 | |
71 test("completion permutation 2-1-3", () async { | |
72 var cs = new List.generate(3, (_) => new Completer<int>()); | |
73 var all = Result.captureAll<int>(cs.map((c) => c.future)); | |
74 expect(all, completion([res(1), res(2), err(3)])); | |
75 await 0; | |
76 cs[1].complete(2); | |
77 await 0; | |
78 cs[0].complete(1); | |
79 await 0; | |
80 cs[2].completeError("3", someStack); | |
81 }); | |
82 | |
83 test("completion permutation 2-3-1", () async { | |
84 var cs = new List.generate(3, (_) => new Completer<int>()); | |
85 var all = Result.captureAll<int>(cs.map((c) => c.future)); | |
86 expect(all, completion([res(1), res(2), err(3)])); | |
87 await 0; | |
88 cs[1].complete(2); | |
89 await 0; | |
90 cs[2].completeError("3", someStack); | |
91 await 0; | |
92 cs[0].complete(1); | |
93 }); | |
94 | |
95 test("completion permutation 3-1-2", () async { | |
96 var cs = new List.generate(3, (_) => new Completer<int>()); | |
97 var all = Result.captureAll<int>(cs.map((c) => c.future)); | |
98 expect(all, completion([res(1), res(2), err(3)])); | |
99 await 0; | |
100 cs[2].completeError("3", someStack); | |
101 await 0; | |
102 cs[0].complete(1); | |
103 await 0; | |
104 cs[1].complete(2); | |
105 }); | |
106 | |
107 test("completion permutation 3-2-1", () async { | |
108 var cs = new List.generate(3, (_) => new Completer<int>()); | |
109 var all = Result.captureAll<int>(cs.map((c) => c.future)); | |
110 expect(all, completion([res(1), res(2), err(3)])); | |
111 await 0; | |
112 cs[2].completeError("3", someStack); | |
113 await 0; | |
114 cs[1].complete(2); | |
115 await 0; | |
116 cs[0].complete(1); | |
117 }); | |
118 | |
119 var seed = new Random().nextInt(0x100000000); | |
120 int n = 25; // max 32, otherwise rnd.nextInt(1<<n) won't work. | |
121 test("randomized #$n seed:${seed.toRadixString(16)}", () async { | |
122 var cs = new List.generate(n, (_) => new Completer<int>()); | |
123 var all = Result.captureAll<int>(cs.map((c) => c.future)); | |
124 var rnd = new Random(seed); | |
125 var throwFlags = rnd.nextInt(1 << n); // Bit-flag for throwing. | |
126 bool throws(index) => (throwFlags & (1 << index)) != 0; | |
127 var expected = | |
128 new List.generate(n, (x) => throws(x) ? err(x) : res(x)); | |
129 | |
130 expect(all, completion(expected)); | |
131 | |
132 var completeFunctions = new List<Function()>.generate(n, (i) { | |
133 var c = cs[i]; | |
134 return () => throws(i) ? c.completeError("$i", someStack) : c.complete(i ); | |
135 }); | |
136 completeFunctions.shuffle(rnd); | |
137 for (int i = 0; i < n; i++) { | |
138 await 0; | |
139 completeFunctions[i](); | |
140 } | |
141 }); | |
142 }); | |
143 group("values only,", () { | |
144 test("single", () async { | |
145 var all = await Result.captureAll<int>(<int>[1]); | |
146 expect(all, [res(1)]); | |
147 }); | |
148 test("multiple", () async { | |
149 var all = await Result.captureAll<int>(<int>[1, 2, 3]); | |
150 expect(all, [res(1), res(2), res(3)]); | |
151 }); | |
152 }); | |
153 group("mixed futures and values,", () { | |
154 test("no error", () async { | |
155 var all = await Result.captureAll<int>(<FutureOr<int>>[ | |
156 1, | |
157 new Future<int>(() => 2), | |
158 3, | |
159 new Future<int>.value(4), | |
160 ]); | |
161 expect(all, [res(1), res(2), res(3), res(4)]); | |
162 }); | |
163 test("error", () async { | |
164 var all = await Result.captureAll<int>(<FutureOr<int>>[ | |
165 1, | |
166 new Future<int>(() => 2), | |
167 3, | |
168 new Future<int>(() async => await new Future.error("4", someStack)), | |
169 new Future<int>.value(5) | |
170 ]); | |
171 expect(all, [res(1), res(2), res(3), err(4), res(5)]); | |
172 }); | |
173 | |
174 }); | |
175 } | |
176 | |
177 Iterable<Future<int>> futures(int count, { bool throwWhen(int index) }) sync* { | |
floitsch
2017/08/24 17:46:39
Move this function to the top.
| |
178 for (int i = 0; i < count; i++) { | |
179 if (throwWhen != null && throwWhen(i)) { | |
180 yield new Future<int>.error("$i", someStack); | |
181 } else { | |
182 yield new Future<int>.value(i); | |
183 } | |
184 } | |
185 } | |
OLD | NEW |