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 void main() { |
| 13 test("buffers events as soon as it's bound", () async { |
| 14 var controller = new StreamController.broadcast(); |
| 15 var stream = controller.stream.transform( |
| 16 const SingleSubscriptionTransformer()); |
| 17 |
| 18 // Add events before [stream] has a listener to be sure it buffers them. |
| 19 controller.add(1); |
| 20 controller.add(2); |
| 21 await flushMicrotasks(); |
| 22 |
| 23 expect(stream.toList(), completion(equals([1, 2, 3, 4]))); |
| 24 await flushMicrotasks(); |
| 25 |
| 26 controller.add(3); |
| 27 controller.add(4); |
| 28 controller.close(); |
| 29 }); |
| 30 |
| 31 test("cancels the subscription to the broadcast stream when it's canceled", |
| 32 () async { |
| 33 var canceled = false; |
| 34 var controller = new StreamController.broadcast(onCancel: () { |
| 35 canceled = true; |
| 36 }); |
| 37 var stream = controller.stream.transform( |
| 38 const SingleSubscriptionTransformer()); |
| 39 await flushMicrotasks(); |
| 40 expect(canceled, isFalse); |
| 41 |
| 42 var subscription = stream.listen(null); |
| 43 await flushMicrotasks(); |
| 44 expect(canceled, isFalse); |
| 45 |
| 46 subscription.cancel(); |
| 47 await flushMicrotasks(); |
| 48 expect(canceled, isTrue); |
| 49 }); |
| 50 } |
OLD | NEW |