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 import 'dart:convert'; |
| 7 import 'dart:isolate'; |
| 8 |
| 9 import 'package:async/async.dart'; |
| 10 import 'package:stream_channel/stream_channel.dart'; |
| 11 import 'package:test/test.dart'; |
| 12 |
| 13 import 'utils.dart'; |
| 14 |
| 15 void main() { |
| 16 var streamController; |
| 17 var sinkController; |
| 18 var disconnector; |
| 19 var channel; |
| 20 setUp(() { |
| 21 streamController = new StreamController(); |
| 22 sinkController = new StreamController(); |
| 23 disconnector = new Disconnector(); |
| 24 channel = new StreamChannel.withGuarantees( |
| 25 streamController.stream, sinkController.sink) |
| 26 .transform(disconnector); |
| 27 }); |
| 28 |
| 29 group("before disconnection", () { |
| 30 test("forwards events from the sink as normal", () { |
| 31 channel.sink.add(1); |
| 32 channel.sink.add(2); |
| 33 channel.sink.add(3); |
| 34 channel.sink.close(); |
| 35 |
| 36 expect(sinkController.stream.toList(), completion(equals([1, 2, 3]))); |
| 37 }); |
| 38 |
| 39 test("forwards events to the stream as normal", () { |
| 40 streamController.add(1); |
| 41 streamController.add(2); |
| 42 streamController.add(3); |
| 43 streamController.close(); |
| 44 |
| 45 expect(channel.stream.toList(), completion(equals([1, 2, 3]))); |
| 46 }); |
| 47 |
| 48 test("events can't be added when the sink is explicitly closed", () { |
| 49 sinkController.stream.listen(null); // Work around sdk#19095. |
| 50 |
| 51 expect(channel.sink.close(), completes); |
| 52 expect(() => channel.sink.add(1), throwsStateError); |
| 53 expect(() => channel.sink.addError("oh no"), throwsStateError); |
| 54 expect(() => channel.sink.addStream(new Stream.fromIterable([])), |
| 55 throwsStateError); |
| 56 }); |
| 57 |
| 58 test("events can't be added while a stream is being added", () { |
| 59 var controller = new StreamController(); |
| 60 channel.sink.addStream(controller.stream); |
| 61 |
| 62 expect(() => channel.sink.add(1), throwsStateError); |
| 63 expect(() => channel.sink.addError("oh no"), throwsStateError); |
| 64 expect(() => channel.sink.addStream(new Stream.fromIterable([])), |
| 65 throwsStateError); |
| 66 expect(() => channel.sink.close(), throwsStateError); |
| 67 |
| 68 controller.close(); |
| 69 }); |
| 70 }); |
| 71 |
| 72 test("cancels addStream when disconnected", () async { |
| 73 var canceled = false; |
| 74 var controller = new StreamController(onCancel: () { |
| 75 canceled = true; |
| 76 }); |
| 77 expect(channel.sink.addStream(controller.stream), completes); |
| 78 disconnector.disconnect(); |
| 79 |
| 80 await pumpEventQueue(); |
| 81 expect(canceled, isTrue); |
| 82 }); |
| 83 |
| 84 group("after disconnection", () { |
| 85 setUp(() => disconnector.disconnect()); |
| 86 |
| 87 test("closes the inner sink and ignores events to the outer sink", () { |
| 88 channel.sink.add(1); |
| 89 channel.sink.add(2); |
| 90 channel.sink.add(3); |
| 91 channel.sink.close(); |
| 92 |
| 93 expect(sinkController.stream.toList(), completion(isEmpty)); |
| 94 }); |
| 95 |
| 96 test("closes the stream", () { |
| 97 expect(channel.stream.toList(), completion(isEmpty)); |
| 98 }); |
| 99 |
| 100 test("completes done", () { |
| 101 sinkController.stream.listen(null); // Work around sdk#19095. |
| 102 expect(channel.sink.done, completes); |
| 103 }); |
| 104 |
| 105 test("still emits state errors after explicit close", () { |
| 106 sinkController.stream.listen(null); // Work around sdk#19095. |
| 107 expect(channel.sink.close(), completes); |
| 108 |
| 109 expect(() => channel.sink.add(1), throwsStateError); |
| 110 expect(() => channel.sink.addError("oh no"), throwsStateError); |
| 111 }); |
| 112 }); |
| 113 } |
OLD | NEW |