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:stream_channel/stream_channel.dart'; |
| 9 import 'package:test/test.dart'; |
| 10 |
| 11 import 'utils.dart'; |
| 12 |
| 13 final _delayTransformer = new StreamTransformer.fromHandlers( |
| 14 handleData: (data, sink) => new Future.microtask(() => sink.add(data)), |
| 15 handleDone: (sink) => new Future.microtask(() => sink.close())); |
| 16 |
| 17 final _delaySinkTransformer = |
| 18 new StreamSinkTransformer.fromStreamTransformer(_delayTransformer); |
| 19 |
| 20 void main() { |
| 21 var controller; |
| 22 var channel; |
| 23 setUp(() { |
| 24 controller = new StreamChannelController(); |
| 25 |
| 26 // Add a bunch of layers of asynchronous dispatch between the channel and |
| 27 // the underlying controllers. |
| 28 var stream = controller.foreign.stream; |
| 29 var sink = controller.foreign.sink; |
| 30 for (var i = 0; i < 10; i++) { |
| 31 stream = stream.transform(_delayTransformer); |
| 32 sink = _delaySinkTransformer.bind(sink); |
| 33 } |
| 34 |
| 35 channel = new StreamChannel.withCloseGuarantee(stream, sink); |
| 36 }); |
| 37 |
| 38 test("closing the event sink causes the stream to close before it emits any " |
| 39 "more events", () async { |
| 40 controller.local.sink.add(1); |
| 41 controller.local.sink.add(2); |
| 42 controller.local.sink.add(3); |
| 43 |
| 44 expect(channel.stream.listen(expectAsync((event) { |
| 45 if (event == 2) channel.sink.close(); |
| 46 }, count: 2)).asFuture(), completes); |
| 47 |
| 48 await pumpEventQueue(); |
| 49 }); |
| 50 |
| 51 test("closing the event sink before events are emitted causes the stream to " |
| 52 "close immediately", () async { |
| 53 channel.sink.close(); |
| 54 channel.stream.listen( |
| 55 expectAsync((_) {}, count: 0), |
| 56 onError: expectAsync((_, __) {}, count: 0), |
| 57 onDone: expectAsync(() {})); |
| 58 |
| 59 controller.local.sink.add(1); |
| 60 controller.local.sink.add(2); |
| 61 controller.local.sink.add(3); |
| 62 controller.local.sink.close(); |
| 63 |
| 64 await pumpEventQueue(); |
| 65 }); |
| 66 } |
OLD | NEW |