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:stream_channel/stream_channel.dart'; |
| 8 import 'package:test/test.dart'; |
| 9 |
| 10 import 'utils.dart'; |
| 11 |
| 12 void main() { |
| 13 var streamController; |
| 14 var sinkController; |
| 15 var channel; |
| 16 setUp(() { |
| 17 streamController = new StreamController(); |
| 18 sinkController = new StreamController(); |
| 19 channel = new StreamChannel.withGuarantees( |
| 20 streamController.stream, sinkController.sink); |
| 21 }); |
| 22 |
| 23 group("with a broadcast stream", () { |
| 24 setUp(() { |
| 25 streamController = new StreamController.broadcast(); |
| 26 channel = new StreamChannel.withGuarantees( |
| 27 streamController.stream, sinkController.sink); |
| 28 }); |
| 29 |
| 30 test("buffers events", () async { |
| 31 streamController.add(1); |
| 32 streamController.add(2); |
| 33 streamController.add(3); |
| 34 await pumpEventQueue(); |
| 35 |
| 36 expect(channel.stream.toList(), completion(equals([1, 2, 3]))); |
| 37 streamController.close(); |
| 38 }); |
| 39 |
| 40 test("only allows a single subscription", () { |
| 41 channel.stream.listen(null); |
| 42 expect(() => channel.stream.listen(null), throwsStateError); |
| 43 }); |
| 44 }); |
| 45 |
| 46 test("closing the event sink causes the stream to close before it emits any " |
| 47 "more events", () { |
| 48 streamController.add(1); |
| 49 streamController.add(2); |
| 50 streamController.add(3); |
| 51 |
| 52 expect(channel.stream.listen(expectAsync((event) { |
| 53 if (event == 2) channel.sink.close(); |
| 54 }, count: 2)).asFuture(), completes); |
| 55 }); |
| 56 |
| 57 test("after the stream closes, the sink ignores events", () async { |
| 58 streamController.close(); |
| 59 |
| 60 // Wait for the done event to be delivered. |
| 61 await channel.stream.toList(); |
| 62 channel.sink.add(1); |
| 63 channel.sink.add(2); |
| 64 channel.sink.add(3); |
| 65 channel.sink.close(); |
| 66 |
| 67 // None of our channel.sink additions should make it to the other endpoint. |
| 68 sinkController.stream.listen( |
| 69 expectAsync((_) {}, count: 0), |
| 70 onDone: expectAsync(() {}, count: 0)); |
| 71 await pumpEventQueue(); |
| 72 }); |
| 73 |
| 74 test("canceling the stream's subscription has no effect on the sink", |
| 75 () async { |
| 76 channel.stream.listen(null).cancel(); |
| 77 await pumpEventQueue(); |
| 78 |
| 79 channel.sink.add(1); |
| 80 channel.sink.add(2); |
| 81 channel.sink.add(3); |
| 82 channel.sink.close(); |
| 83 expect(sinkController.stream.toList(), completion(equals([1, 2, 3]))); |
| 84 }); |
| 85 |
| 86 test("canceling the stream's subscription doesn't stop a done event", |
| 87 () async { |
| 88 channel.stream.listen(null).cancel(); |
| 89 await pumpEventQueue(); |
| 90 |
| 91 streamController.close(); |
| 92 await pumpEventQueue(); |
| 93 |
| 94 channel.sink.add(1); |
| 95 channel.sink.add(2); |
| 96 channel.sink.add(3); |
| 97 channel.sink.close(); |
| 98 |
| 99 // The sink should be ignoring events because the stream closed. |
| 100 sinkController.stream.listen( |
| 101 expectAsync((_) {}, count: 0), |
| 102 onDone: expectAsync(() {}, count: 0)); |
| 103 await pumpEventQueue(); |
| 104 }); |
| 105 |
| 106 test("forwards errors to the other endpoint", () { |
| 107 channel.sink.addError("error"); |
| 108 expect(sinkController.stream.first, throwsA("error")); |
| 109 }); |
| 110 } |
OLD | NEW |