OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016, 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 | |
7 import "package:async/async.dart"; | |
8 import "package:test/test.dart"; | |
9 | |
10 import "utils.dart"; | |
11 | |
12 main() { | |
13 var completer; | |
14 setUp(() { | |
15 completer = new StreamSinkCompleter(); | |
16 }); | |
17 | |
18 group("when a stream is linked before events are added", () { | |
19 test("data events are forwarded", () { | |
20 var sink = new TestSink(); | |
21 completer.setDestinationSink(sink); | |
22 completer.sink..add(1)..add(2)..add(3)..add(4); | |
23 | |
24 expect(sink.results[0].asValue.value, equals(1)); | |
25 expect(sink.results[1].asValue.value, equals(2)); | |
26 expect(sink.results[2].asValue.value, equals(3)); | |
27 expect(sink.results[3].asValue.value, equals(4)); | |
28 }); | |
29 | |
30 test("error events are forwarded", () { | |
31 var sink = new TestSink(); | |
32 completer.setDestinationSink(sink); | |
33 completer.sink..addError("oh no")..addError("that's bad"); | |
34 | |
35 expect(sink.results[0].asError.error, equals("oh no")); | |
36 expect(sink.results[1].asError.error, equals("that's bad")); | |
37 }); | |
38 | |
39 test("addStream is forwarded", () async { | |
40 var sink = new TestSink(); | |
41 completer.setDestinationSink(sink); | |
42 | |
43 var controller = new StreamController(); | |
44 completer.sink.addStream(controller.stream); | |
45 | |
46 controller.add(1); | |
47 controller.addError("oh no"); | |
48 controller.add(2); | |
49 controller.addError("that's bad"); | |
50 await flushMicrotasks(); | |
51 | |
52 expect(sink.results[0].asValue.value, equals(1)); | |
53 expect(sink.results[1].asError.error, equals("oh no")); | |
54 expect(sink.results[2].asValue.value, equals(2)); | |
55 expect(sink.results[3].asError.error, equals("that's bad")); | |
56 expect(sink.isClosed, isFalse); | |
57 | |
58 controller.close(); | |
59 await flushMicrotasks(); | |
60 expect(sink.isClosed, isFalse); | |
61 }); | |
62 | |
63 test("close() is forwarded", () { | |
64 var sink = new TestSink(); | |
65 completer.setDestinationSink(sink); | |
66 completer.sink.close(); | |
67 expect(sink.isClosed, isTrue); | |
68 }); | |
69 | |
70 test("the future from the inner close() is returned", () async { | |
71 var closeCompleter = new Completer(); | |
72 var sink = new TestSink(onDone: () => closeCompleter.future); | |
73 completer.setDestinationSink(sink); | |
74 | |
75 var closeCompleted = false; | |
76 completer.sink.close().then(expectAsync((_) { | |
77 closeCompleted = true; | |
78 })); | |
79 | |
80 await flushMicrotasks(); | |
81 expect(closeCompleted, isFalse); | |
82 | |
83 closeCompleter.complete(); | |
84 await flushMicrotasks(); | |
85 expect(closeCompleted, isTrue); | |
86 }); | |
87 | |
88 test("errors are forwarded from the inner close()", () { | |
89 var sink = new TestSink(onDone: () => throw "oh no"); | |
90 completer.setDestinationSink(sink); | |
91 expect(completer.sink.done, throwsA("oh no")); | |
92 expect(completer.sink.close(), throwsA("oh no")); | |
93 }); | |
94 | |
95 test("errors aren't top-leveled if only close() is listened to", () async { | |
96 var sink = new TestSink(onDone: () => throw "oh no"); | |
97 completer.setDestinationSink(sink); | |
98 expect(completer.sink.close(), throwsA("oh no")); | |
99 | |
100 // Give the event loop a chance to top-level errors if it's going to. | |
101 await flushMicrotasks(); | |
Lasse Reichstein Nielsen
2016/01/21 07:28:47
I'd wait for a timer event here, to be sure all mi
nweiz
2016/01/21 20:59:05
That's exactly what flushMicrotasks() does, actual
| |
102 }); | |
103 | |
104 test("errors aren't top-leveled if only done is listened to", () async { | |
105 var sink = new TestSink(onDone: () => throw "oh no"); | |
106 completer.setDestinationSink(sink); | |
107 completer.sink.close(); | |
108 expect(completer.sink.done, throwsA("oh no")); | |
109 | |
110 // Give the event loop a chance to top-level errors if it's going to. | |
111 await flushMicrotasks(); | |
112 }); | |
113 }); | |
114 | |
115 group("when a stream is linked after events are added", () { | |
116 test("data events are forwarded", () async { | |
117 completer.sink..add(1)..add(2)..add(3)..add(4); | |
118 await flushMicrotasks(); | |
119 | |
120 var sink = new TestSink(); | |
121 completer.setDestinationSink(sink); | |
122 await flushMicrotasks(); | |
123 | |
124 expect(sink.results[0].asValue.value, equals(1)); | |
125 expect(sink.results[1].asValue.value, equals(2)); | |
126 expect(sink.results[2].asValue.value, equals(3)); | |
127 expect(sink.results[3].asValue.value, equals(4)); | |
128 }); | |
129 | |
130 test("error events are forwarded", () async { | |
131 completer.sink..addError("oh no")..addError("that's bad"); | |
132 await flushMicrotasks(); | |
133 | |
134 var sink = new TestSink(); | |
135 completer.setDestinationSink(sink); | |
136 await flushMicrotasks(); | |
137 | |
138 expect(sink.results[0].asError.error, equals("oh no")); | |
139 expect(sink.results[1].asError.error, equals("that's bad")); | |
140 }); | |
141 | |
142 test("addStream is forwarded", () async { | |
143 var controller = new StreamController(); | |
144 completer.sink.addStream(controller.stream); | |
145 | |
146 controller.add(1); | |
147 controller.addError("oh no"); | |
148 controller.add(2); | |
149 controller.addError("that's bad"); | |
150 controller.close(); | |
151 await flushMicrotasks(); | |
152 | |
153 var sink = new TestSink(); | |
154 completer.setDestinationSink(sink); | |
155 await flushMicrotasks(); | |
156 | |
157 expect(sink.results[0].asValue.value, equals(1)); | |
158 expect(sink.results[1].asError.error, equals("oh no")); | |
159 expect(sink.results[2].asValue.value, equals(2)); | |
160 expect(sink.results[3].asError.error, equals("that's bad")); | |
161 expect(sink.isClosed, isFalse); | |
162 }); | |
163 | |
164 test("close() is forwarded", () async { | |
165 completer.sink.close(); | |
166 await flushMicrotasks(); | |
167 | |
168 var sink = new TestSink(); | |
169 completer.setDestinationSink(sink); | |
170 await flushMicrotasks(); | |
171 | |
172 expect(sink.isClosed, isTrue); | |
173 }); | |
174 | |
175 test("the future from the inner close() is returned", () async { | |
176 var closeCompleted = false; | |
177 completer.sink.close().then(expectAsync((_) { | |
178 closeCompleted = true; | |
179 })); | |
180 await flushMicrotasks(); | |
181 | |
182 var closeCompleter = new Completer(); | |
183 var sink = new TestSink(onDone: () => closeCompleter.future); | |
184 completer.setDestinationSink(sink); | |
185 await flushMicrotasks(); | |
186 expect(closeCompleted, isFalse); | |
187 | |
188 closeCompleter.complete(); | |
189 await flushMicrotasks(); | |
190 expect(closeCompleted, isTrue); | |
191 }); | |
192 | |
193 test("errors are forwarded from the inner close()", () async { | |
194 expect(completer.sink.done, throwsA("oh no")); | |
195 expect(completer.sink.close(), throwsA("oh no")); | |
196 await flushMicrotasks(); | |
197 | |
198 var sink = new TestSink(onDone: () => throw "oh no"); | |
199 completer.setDestinationSink(sink); | |
200 }); | |
201 | |
202 test("errors aren't top-leveled if only close() is listened to", () async { | |
203 expect(completer.sink.close(), throwsA("oh no")); | |
204 await flushMicrotasks(); | |
205 | |
206 var sink = new TestSink(onDone: () => throw "oh no"); | |
207 completer.setDestinationSink(sink); | |
208 | |
209 // Give the event loop a chance to top-level errors if it's going to. | |
210 await flushMicrotasks(); | |
211 }); | |
212 | |
213 test("errors aren't top-leveled if only done is listened to", () async { | |
214 completer.sink.close(); | |
215 expect(completer.sink.done, throwsA("oh no")); | |
216 await flushMicrotasks(); | |
217 | |
218 var sink = new TestSink(onDone: () => throw "oh no"); | |
219 completer.setDestinationSink(sink); | |
220 | |
221 // Give the event loop a chance to top-level errors if it's going to. | |
222 await flushMicrotasks(); | |
223 }); | |
224 }); | |
225 | |
226 test("doesn't allow the destination sink to be set multiple times", () { | |
227 completer.setDestinationSink(new TestSink()); | |
228 expect(() => completer.setDestinationSink(new TestSink()), | |
229 throwsStateError); | |
230 expect(() => completer.setDestinationSink(new TestSink()), | |
231 throwsStateError); | |
232 }); | |
Lasse Reichstein Nielsen
2016/01/21 07:28:47
Could you make more tests with the "done" getter:
nweiz
2016/01/21 20:59:05
Done. Four of these were already being tested, so
| |
233 } | |
OLD | NEW |