OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:async/src/future_group.dart'; | 7 import 'package:async/src/future_group.dart'; |
8 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
9 | 9 |
| 10 import 'utils.dart'; |
| 11 |
10 void main() { | 12 void main() { |
11 var futureGroup; | 13 var futureGroup; |
12 setUp(() { | 14 setUp(() { |
13 futureGroup = new FutureGroup(); | 15 futureGroup = new FutureGroup(); |
14 }); | 16 }); |
15 | 17 |
16 group("with no futures", () { | 18 group("with no futures", () { |
17 test("never completes if nothing happens", () async { | 19 test("never completes if nothing happens", () async { |
18 var completed = false; | 20 var completed = false; |
19 futureGroup.future.then((_) => completed = true); | 21 futureGroup.future.then((_) => completed = true); |
20 | 22 |
21 await new Future.delayed(Duration.ZERO); | 23 await flushMicrotasks(); |
22 expect(completed, isFalse); | 24 expect(completed, isFalse); |
23 }); | 25 }); |
24 | 26 |
25 test("completes once it's closed", () { | 27 test("completes once it's closed", () { |
26 expect(futureGroup.future, completion(isEmpty)); | 28 expect(futureGroup.future, completion(isEmpty)); |
27 futureGroup.close(); | 29 futureGroup.close(); |
28 }); | 30 }); |
29 }); | 31 }); |
30 | 32 |
31 group("with a future that already completed", () { | 33 group("with a future that already completed", () { |
32 test("never completes if nothing happens", () async { | 34 test("never completes if nothing happens", () async { |
33 futureGroup.add(new Future.value()); | 35 futureGroup.add(new Future.value()); |
34 await new Future.delayed(Duration.ZERO); | 36 await flushMicrotasks(); |
35 | 37 |
36 var completed = false; | 38 var completed = false; |
37 futureGroup.future.then((_) => completed = true); | 39 futureGroup.future.then((_) => completed = true); |
38 | 40 |
39 await new Future.delayed(Duration.ZERO); | 41 await flushMicrotasks(); |
40 expect(completed, isFalse); | 42 expect(completed, isFalse); |
41 }); | 43 }); |
42 | 44 |
43 test("completes once it's closed", () async { | 45 test("completes once it's closed", () async { |
44 futureGroup.add(new Future.value()); | 46 futureGroup.add(new Future.value()); |
45 await new Future.delayed(Duration.ZERO); | 47 await flushMicrotasks(); |
46 | 48 |
47 expect(futureGroup.future, completes); | 49 expect(futureGroup.future, completes); |
48 futureGroup.close(); | 50 futureGroup.close(); |
49 }); | 51 }); |
50 | 52 |
51 test("completes to that future's value", () { | 53 test("completes to that future's value", () { |
52 futureGroup.add(new Future.value(1)); | 54 futureGroup.add(new Future.value(1)); |
53 futureGroup.close(); | 55 futureGroup.close(); |
54 expect(futureGroup.future, completion(equals([1]))); | 56 expect(futureGroup.future, completion(equals([1]))); |
55 }); | 57 }); |
56 | 58 |
57 test("completes to that future's error, even if it's not closed", () { | 59 test("completes to that future's error, even if it's not closed", () { |
58 futureGroup.add(new Future.error("error")); | 60 futureGroup.add(new Future.error("error")); |
59 expect(futureGroup.future, throwsA("error")); | 61 expect(futureGroup.future, throwsA("error")); |
60 }); | 62 }); |
61 }); | 63 }); |
62 | 64 |
63 test("completes once all contained futures complete", () async { | 65 test("completes once all contained futures complete", () async { |
64 var futureGroup = new FutureGroup(); | |
65 var completer1 = new Completer(); | 66 var completer1 = new Completer(); |
66 var completer2 = new Completer(); | 67 var completer2 = new Completer(); |
67 var completer3 = new Completer(); | 68 var completer3 = new Completer(); |
68 | 69 |
69 futureGroup.add(completer1.future); | 70 futureGroup.add(completer1.future); |
70 futureGroup.add(completer2.future); | 71 futureGroup.add(completer2.future); |
71 futureGroup.add(completer3.future); | 72 futureGroup.add(completer3.future); |
72 futureGroup.close(); | 73 futureGroup.close(); |
73 | 74 |
74 var completed = false; | 75 var completed = false; |
75 futureGroup.future.then((_) => completed = true); | 76 futureGroup.future.then((_) => completed = true); |
76 | 77 |
77 completer1.complete(); | 78 completer1.complete(); |
78 await new Future.delayed(Duration.ZERO); | 79 await flushMicrotasks(); |
79 expect(completed, isFalse); | 80 expect(completed, isFalse); |
80 | 81 |
81 completer2.complete(); | 82 completer2.complete(); |
82 await new Future.delayed(Duration.ZERO); | 83 await flushMicrotasks(); |
83 expect(completed, isFalse); | 84 expect(completed, isFalse); |
84 | 85 |
85 completer3.complete(); | 86 completer3.complete(); |
86 await new Future.delayed(Duration.ZERO); | 87 await flushMicrotasks(); |
87 expect(completed, isTrue); | 88 expect(completed, isTrue); |
88 }); | 89 }); |
89 | 90 |
90 test("completes to the values of the futures in order of addition", () { | 91 test("completes to the values of the futures in order of addition", () { |
91 var futureGroup = new FutureGroup(); | |
92 var completer1 = new Completer(); | 92 var completer1 = new Completer(); |
93 var completer2 = new Completer(); | 93 var completer2 = new Completer(); |
94 var completer3 = new Completer(); | 94 var completer3 = new Completer(); |
95 | 95 |
96 futureGroup.add(completer1.future); | 96 futureGroup.add(completer1.future); |
97 futureGroup.add(completer2.future); | 97 futureGroup.add(completer2.future); |
98 futureGroup.add(completer3.future); | 98 futureGroup.add(completer3.future); |
99 futureGroup.close(); | 99 futureGroup.close(); |
100 | 100 |
101 // Complete the completers in reverse order to prove that that doesn't | 101 // Complete the completers in reverse order to prove that that doesn't |
102 // affect the result order. | 102 // affect the result order. |
103 completer3.complete(3); | 103 completer3.complete(3); |
104 completer2.complete(2); | 104 completer2.complete(2); |
105 completer1.complete(1); | 105 completer1.complete(1); |
106 expect(futureGroup.future, completion(equals([1, 2, 3]))); | 106 expect(futureGroup.future, completion(equals([1, 2, 3]))); |
107 }); | 107 }); |
108 | 108 |
109 test("completes to the first error to be emitted, even if it's not closed", | 109 test("completes to the first error to be emitted, even if it's not closed", |
110 () { | 110 () { |
111 var futureGroup = new FutureGroup(); | |
112 var completer1 = new Completer(); | 111 var completer1 = new Completer(); |
113 var completer2 = new Completer(); | 112 var completer2 = new Completer(); |
114 var completer3 = new Completer(); | 113 var completer3 = new Completer(); |
115 | 114 |
116 futureGroup.add(completer1.future); | 115 futureGroup.add(completer1.future); |
117 futureGroup.add(completer2.future); | 116 futureGroup.add(completer2.future); |
118 futureGroup.add(completer3.future); | 117 futureGroup.add(completer3.future); |
119 | 118 |
120 completer2.completeError("error 2"); | 119 completer2.completeError("error 2"); |
121 completer1.completeError("error 1"); | 120 completer1.completeError("error 1"); |
122 expect(futureGroup.future, throwsA("error 2")); | 121 expect(futureGroup.future, throwsA("error 2")); |
123 }); | 122 }); |
| 123 |
| 124 group("onIdle:", () { |
| 125 test("emits an event when the last pending future completes", () async { |
| 126 var idle = false; |
| 127 futureGroup.onIdle.listen((_) => idle = true); |
| 128 |
| 129 var completer1 = new Completer(); |
| 130 var completer2 = new Completer(); |
| 131 var completer3 = new Completer(); |
| 132 |
| 133 futureGroup.add(completer1.future); |
| 134 futureGroup.add(completer2.future); |
| 135 futureGroup.add(completer3.future); |
| 136 |
| 137 await flushMicrotasks(); |
| 138 expect(idle, isFalse); |
| 139 expect(futureGroup.isIdle, isFalse); |
| 140 |
| 141 completer1.complete(); |
| 142 await flushMicrotasks(); |
| 143 expect(idle, isFalse); |
| 144 expect(futureGroup.isIdle, isFalse); |
| 145 |
| 146 completer2.complete(); |
| 147 await flushMicrotasks(); |
| 148 expect(idle, isFalse); |
| 149 expect(futureGroup.isIdle, isFalse); |
| 150 |
| 151 completer3.complete(); |
| 152 await flushMicrotasks(); |
| 153 expect(idle, isTrue); |
| 154 expect(futureGroup.isIdle, isTrue); |
| 155 }); |
| 156 |
| 157 test("emits an event each time it becomes idle", () async { |
| 158 var idle = false; |
| 159 futureGroup.onIdle.listen((_) => idle = true); |
| 160 |
| 161 var completer = new Completer(); |
| 162 futureGroup.add(completer.future); |
| 163 |
| 164 completer.complete(); |
| 165 await flushMicrotasks(); |
| 166 expect(idle, isTrue); |
| 167 expect(futureGroup.isIdle, isTrue); |
| 168 |
| 169 idle = false; |
| 170 completer = new Completer(); |
| 171 futureGroup.add(completer.future); |
| 172 |
| 173 await flushMicrotasks(); |
| 174 expect(idle, isFalse); |
| 175 expect(futureGroup.isIdle, isFalse); |
| 176 |
| 177 completer.complete(); |
| 178 await flushMicrotasks(); |
| 179 expect(idle, isTrue); |
| 180 expect(futureGroup.isIdle, isTrue); |
| 181 }); |
| 182 |
| 183 test("emits an event when the group closes", () async { |
| 184 // It's important that the order of events here stays consistent over |
| 185 // time, since code may rely on it in subtle ways. |
| 186 var idle = false; |
| 187 var onIdleDone = false; |
| 188 var futureFired = false; |
| 189 |
| 190 futureGroup.onIdle.listen(expectAsync((_) { |
| 191 expect(futureFired, isFalse); |
| 192 idle = true; |
| 193 }), onDone: expectAsync(() { |
| 194 expect(idle, isTrue); |
| 195 expect(futureFired, isFalse); |
| 196 onIdleDone = true; |
| 197 })); |
| 198 |
| 199 futureGroup.future.then(expectAsync((_) { |
| 200 expect(idle, isTrue); |
| 201 expect(onIdleDone, isTrue); |
| 202 futureFired = true; |
| 203 })); |
| 204 |
| 205 var completer = new Completer(); |
| 206 futureGroup.add(completer.future); |
| 207 futureGroup.close(); |
| 208 |
| 209 await flushMicrotasks(); |
| 210 expect(idle, isFalse); |
| 211 expect(futureGroup.isIdle, isFalse); |
| 212 |
| 213 completer.complete(); |
| 214 await flushMicrotasks(); |
| 215 expect(idle, isTrue); |
| 216 expect(futureGroup.isIdle, isTrue); |
| 217 expect(futureFired, isTrue); |
| 218 }); |
| 219 }); |
124 } | 220 } |
OLD | NEW |