| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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:stream_channel/stream_channel.dart'; | 7 import 'package:stream_channel/stream_channel.dart'; |
| 8 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
| 9 | 9 |
| 10 import 'utils.dart'; | 10 import 'utils.dart'; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 channel.sink.addError("error"); | 107 channel.sink.addError("error"); |
| 108 expect(sinkController.stream.first, throwsA("error")); | 108 expect(sinkController.stream.first, throwsA("error")); |
| 109 }); | 109 }); |
| 110 | 110 |
| 111 test("Sink.done completes once the stream is done", () { | 111 test("Sink.done completes once the stream is done", () { |
| 112 channel.stream.listen(null); | 112 channel.stream.listen(null); |
| 113 expect(channel.sink.done, completes); | 113 expect(channel.sink.done, completes); |
| 114 streamController.close(); | 114 streamController.close(); |
| 115 }); | 115 }); |
| 116 | 116 |
| 117 test("events can't be added to an explicitly-closed sink", () { |
| 118 sinkController.stream.listen(null); // Work around sdk#19095. |
| 119 |
| 120 expect(channel.sink.close(), completes); |
| 121 expect(() => channel.sink.add(1), throwsStateError); |
| 122 expect(() => channel.sink.addError("oh no"), throwsStateError); |
| 123 expect(() => channel.sink.addStream(new Stream.fromIterable([])), |
| 124 throwsStateError); |
| 125 }); |
| 126 |
| 127 test("events can't be added while a stream is being added", () { |
| 128 var controller = new StreamController(); |
| 129 channel.sink.addStream(controller.stream); |
| 130 |
| 131 expect(() => channel.sink.add(1), throwsStateError); |
| 132 expect(() => channel.sink.addError("oh no"), throwsStateError); |
| 133 expect(() => channel.sink.addStream(new Stream.fromIterable([])), |
| 134 throwsStateError); |
| 135 expect(() => channel.sink.close(), throwsStateError); |
| 136 |
| 137 controller.close(); |
| 138 }); |
| 139 |
| 117 group("with allowSinkErrors: false", () { | 140 group("with allowSinkErrors: false", () { |
| 118 setUp(() { | 141 setUp(() { |
| 119 streamController = new StreamController(); | 142 streamController = new StreamController(); |
| 120 sinkController = new StreamController(); | 143 sinkController = new StreamController(); |
| 121 channel = new StreamChannel.withGuarantees( | 144 channel = new StreamChannel.withGuarantees( |
| 122 streamController.stream, sinkController.sink, allowSinkErrors: false); | 145 streamController.stream, sinkController.sink, allowSinkErrors: false); |
| 123 }); | 146 }); |
| 124 | 147 |
| 125 test("forwards errors to Sink.done but not the stream", () { | 148 test("forwards errors to Sink.done but not the stream", () { |
| 126 channel.sink.addError("oh no"); | 149 channel.sink.addError("oh no"); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 139 expect(channel.stream.listen(expectAsync((event) { | 162 expect(channel.stream.listen(expectAsync((event) { |
| 140 if (event == 2) channel.sink.addError("oh no"); | 163 if (event == 2) channel.sink.addError("oh no"); |
| 141 }, count: 2)).asFuture(), completes); | 164 }, count: 2)).asFuture(), completes); |
| 142 }); | 165 }); |
| 143 | 166 |
| 144 test("adding an error closes the inner sink", () { | 167 test("adding an error closes the inner sink", () { |
| 145 channel.sink.addError("oh no"); | 168 channel.sink.addError("oh no"); |
| 146 expect(channel.sink.done, throwsA("oh no")); | 169 expect(channel.sink.done, throwsA("oh no")); |
| 147 expect(sinkController.stream.toList(), completion(isEmpty)); | 170 expect(sinkController.stream.toList(), completion(isEmpty)); |
| 148 }); | 171 }); |
| 172 |
| 173 test("adding an error via via addStream causes the stream to emit a done " |
| 174 "event", () async { |
| 175 var canceled = false; |
| 176 var controller = new StreamController(onCancel: () { |
| 177 canceled = true; |
| 178 }); |
| 179 |
| 180 // This future shouldn't get the error, because it's sent to [Sink.done]. |
| 181 expect(channel.sink.addStream(controller.stream), completes); |
| 182 |
| 183 controller.addError("oh no"); |
| 184 expect(channel.sink.done, throwsA("oh no")); |
| 185 await pumpEventQueue(); |
| 186 expect(canceled, isTrue); |
| 187 |
| 188 // Even though the sink is closed, this shouldn't throw an error because |
| 189 // the user didn't explicitly close it. |
| 190 channel.sink.add(1); |
| 191 }); |
| 149 }); | 192 }); |
| 150 } | 193 } |
| OLD | NEW |