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/async.dart" show SubscriptionStream; | 7 import "package:async/async.dart" show SubscriptionStream; |
8 import "package:test/test.dart"; | 8 import "package:test/test.dart"; |
9 | 9 |
10 import "utils.dart"; | 10 import "utils.dart"; |
11 | 11 |
12 main() { | 12 main() { |
13 test("subscription stream of an entire subscription", () async { | 13 test("subscription stream of an entire subscription", () async { |
14 var stream = createStream(); | 14 var stream = createStream(); |
15 var subscription = stream.listen(null); | 15 var subscription = stream.listen(null); |
16 var subscriptionStream = new SubscriptionStream<int>(subscription); | 16 var subscriptionStream = new SubscriptionStream<int>(subscription); |
17 await flushMicrotasks(); | 17 await flushMicrotasks(); |
18 expect(subscriptionStream.toList(), completion([1, 2, 3, 4])); | 18 expect(subscriptionStream.toList(), completion([1, 2, 3, 4])); |
19 }); | 19 }); |
20 | 20 |
21 test("subscription stream after two events", () async { | 21 test("subscription stream after two events", () async { |
22 var stream = createStream(); | 22 var stream = createStream(); |
23 var skips = 0; | 23 var skips = 0; |
24 var completer = new Completer(); | 24 var completer = new Completer(); |
25 var subscription; | 25 StreamSubscription<int> subscription; |
26 subscription = stream.listen((value) { | 26 subscription = stream.listen((value) { |
27 ++skips; | 27 ++skips; |
28 expect(value, skips); | 28 expect(value, skips); |
29 if (skips == 2) { | 29 if (skips == 2) { |
30 completer.complete(new SubscriptionStream<int>(subscription)); | 30 completer.complete(new SubscriptionStream<int>(subscription)); |
31 } | 31 } |
32 }); | 32 }); |
33 var subscriptionStream = await completer.future; | 33 var subscriptionStream = await completer.future; |
34 await flushMicrotasks(); | 34 await flushMicrotasks(); |
35 expect(subscriptionStream.toList(), completion([3, 4])); | 35 expect(subscriptionStream.toList(), completion([3, 4])); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 yield 4; | 161 yield 4; |
162 } | 162 } |
163 | 163 |
164 Stream<int> createErrorStream([Completer onCancel]) async* { | 164 Stream<int> createErrorStream([Completer onCancel]) async* { |
165 bool canceled = true; | 165 bool canceled = true; |
166 try { | 166 try { |
167 yield 1; | 167 yield 1; |
168 await flushMicrotasks(); | 168 await flushMicrotasks(); |
169 yield 2; | 169 yield 2; |
170 await flushMicrotasks(); | 170 await flushMicrotasks(); |
171 yield* new Future.error("To err is divine!").asStream(); | 171 yield* new Future<int>.error("To err is divine!").asStream(); |
172 await flushMicrotasks(); | 172 await flushMicrotasks(); |
173 yield 4; | 173 yield 4; |
174 await flushMicrotasks(); | 174 await flushMicrotasks(); |
175 canceled = false; | 175 canceled = false; |
176 } finally { | 176 } finally { |
177 // Completes before the "done", but should be after all events. | 177 // Completes before the "done", but should be after all events. |
178 if (canceled && onCancel != null) { | 178 if (canceled && onCancel != null) { |
179 await flushMicrotasks(); | 179 await flushMicrotasks(); |
180 onCancel.complete(); | 180 onCancel.complete(); |
181 } | 181 } |
182 } | 182 } |
183 } | 183 } |
184 | 184 |
185 Stream<int> createLongStream() async* { | 185 Stream<int> createLongStream() async* { |
186 for (int i = 0; i < 200; i++) yield i; | 186 for (int i = 0; i < 200; i++) yield i; |
187 } | 187 } |
OLD | NEW |