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 main() { |
| 13 var completer; |
| 14 setUp(() { |
| 15 completer = new StreamSinkCompleter(); |
| 16 }); |
| 17 |
| 18 group("when a stream is linked before events are added", () { |
| 19 test("data events are forwarded", () { |
| 20 var sink = new TestSink(); |
| 21 completer.setDestinationSink(sink); |
| 22 completer.sink..add(1)..add(2)..add(3)..add(4); |
| 23 |
| 24 expect(sink.results[0].asValue.value, equals(1)); |
| 25 expect(sink.results[1].asValue.value, equals(2)); |
| 26 expect(sink.results[2].asValue.value, equals(3)); |
| 27 expect(sink.results[3].asValue.value, equals(4)); |
| 28 }); |
| 29 |
| 30 test("error events are forwarded", () { |
| 31 var sink = new TestSink(); |
| 32 completer.setDestinationSink(sink); |
| 33 completer.sink..addError("oh no")..addError("that's bad"); |
| 34 |
| 35 expect(sink.results[0].asError.error, equals("oh no")); |
| 36 expect(sink.results[1].asError.error, equals("that's bad")); |
| 37 }); |
| 38 |
| 39 test("addStream is forwarded", () async { |
| 40 var sink = new TestSink(); |
| 41 completer.setDestinationSink(sink); |
| 42 |
| 43 var controller = new StreamController(); |
| 44 completer.sink.addStream(controller.stream); |
| 45 |
| 46 controller.add(1); |
| 47 controller.addError("oh no"); |
| 48 controller.add(2); |
| 49 controller.addError("that's bad"); |
| 50 await flushMicrotasks(); |
| 51 |
| 52 expect(sink.results[0].asValue.value, equals(1)); |
| 53 expect(sink.results[1].asError.error, equals("oh no")); |
| 54 expect(sink.results[2].asValue.value, equals(2)); |
| 55 expect(sink.results[3].asError.error, equals("that's bad")); |
| 56 expect(sink.isClosed, isFalse); |
| 57 |
| 58 controller.close(); |
| 59 await flushMicrotasks(); |
| 60 expect(sink.isClosed, isFalse); |
| 61 }); |
| 62 |
| 63 test("close() is forwarded", () { |
| 64 var sink = new TestSink(); |
| 65 completer.setDestinationSink(sink); |
| 66 completer.sink.close(); |
| 67 expect(sink.isClosed, isTrue); |
| 68 }); |
| 69 |
| 70 test("the future from the inner close() is returned", () async { |
| 71 var closeCompleter = new Completer(); |
| 72 var sink = new TestSink(onDone: () => closeCompleter.future); |
| 73 completer.setDestinationSink(sink); |
| 74 |
| 75 var closeCompleted = false; |
| 76 completer.sink.close().then(expectAsync1((_) { |
| 77 closeCompleted = true; |
| 78 })); |
| 79 |
| 80 await flushMicrotasks(); |
| 81 expect(closeCompleted, isFalse); |
| 82 |
| 83 closeCompleter.complete(); |
| 84 await flushMicrotasks(); |
| 85 expect(closeCompleted, isTrue); |
| 86 }); |
| 87 |
| 88 test("errors are forwarded from the inner close()", () { |
| 89 var sink = new TestSink(onDone: () => throw "oh no"); |
| 90 completer.setDestinationSink(sink); |
| 91 expect(completer.sink.done, throwsA("oh no")); |
| 92 expect(completer.sink.close(), throwsA("oh no")); |
| 93 }); |
| 94 |
| 95 test("errors aren't top-leveled if only close() is listened to", () async { |
| 96 var sink = new TestSink(onDone: () => throw "oh no"); |
| 97 completer.setDestinationSink(sink); |
| 98 expect(completer.sink.close(), throwsA("oh no")); |
| 99 |
| 100 // Give the event loop a chance to top-level errors if it's going to. |
| 101 await flushMicrotasks(); |
| 102 }); |
| 103 |
| 104 test("errors aren't top-leveled if only done is listened to", () async { |
| 105 var sink = new TestSink(onDone: () => throw "oh no"); |
| 106 completer.setDestinationSink(sink); |
| 107 completer.sink.close(); |
| 108 expect(completer.sink.done, throwsA("oh no")); |
| 109 |
| 110 // Give the event loop a chance to top-level errors if it's going to. |
| 111 await flushMicrotasks(); |
| 112 }); |
| 113 }); |
| 114 |
| 115 group("when a stream is linked after events are added", () { |
| 116 test("data events are forwarded", () async { |
| 117 completer.sink..add(1)..add(2)..add(3)..add(4); |
| 118 await flushMicrotasks(); |
| 119 |
| 120 var sink = new TestSink(); |
| 121 completer.setDestinationSink(sink); |
| 122 await flushMicrotasks(); |
| 123 |
| 124 expect(sink.results[0].asValue.value, equals(1)); |
| 125 expect(sink.results[1].asValue.value, equals(2)); |
| 126 expect(sink.results[2].asValue.value, equals(3)); |
| 127 expect(sink.results[3].asValue.value, equals(4)); |
| 128 }); |
| 129 |
| 130 test("error events are forwarded", () async { |
| 131 completer.sink..addError("oh no")..addError("that's bad"); |
| 132 await flushMicrotasks(); |
| 133 |
| 134 var sink = new TestSink(); |
| 135 completer.setDestinationSink(sink); |
| 136 await flushMicrotasks(); |
| 137 |
| 138 expect(sink.results[0].asError.error, equals("oh no")); |
| 139 expect(sink.results[1].asError.error, equals("that's bad")); |
| 140 }); |
| 141 |
| 142 test("addStream is forwarded", () async { |
| 143 var controller = new StreamController(); |
| 144 completer.sink.addStream(controller.stream); |
| 145 |
| 146 controller.add(1); |
| 147 controller.addError("oh no"); |
| 148 controller.add(2); |
| 149 controller.addError("that's bad"); |
| 150 controller.close(); |
| 151 await flushMicrotasks(); |
| 152 |
| 153 var sink = new TestSink(); |
| 154 completer.setDestinationSink(sink); |
| 155 await flushMicrotasks(); |
| 156 |
| 157 expect(sink.results[0].asValue.value, equals(1)); |
| 158 expect(sink.results[1].asError.error, equals("oh no")); |
| 159 expect(sink.results[2].asValue.value, equals(2)); |
| 160 expect(sink.results[3].asError.error, equals("that's bad")); |
| 161 expect(sink.isClosed, isFalse); |
| 162 }); |
| 163 |
| 164 test("close() is forwarded", () async { |
| 165 completer.sink.close(); |
| 166 await flushMicrotasks(); |
| 167 |
| 168 var sink = new TestSink(); |
| 169 completer.setDestinationSink(sink); |
| 170 await flushMicrotasks(); |
| 171 |
| 172 expect(sink.isClosed, isTrue); |
| 173 }); |
| 174 |
| 175 test("the future from the inner close() is returned", () async { |
| 176 var closeCompleted = false; |
| 177 completer.sink.close().then(expectAsync1((_) { |
| 178 closeCompleted = true; |
| 179 })); |
| 180 await flushMicrotasks(); |
| 181 |
| 182 var closeCompleter = new Completer(); |
| 183 var sink = new TestSink(onDone: () => closeCompleter.future); |
| 184 completer.setDestinationSink(sink); |
| 185 await flushMicrotasks(); |
| 186 expect(closeCompleted, isFalse); |
| 187 |
| 188 closeCompleter.complete(); |
| 189 await flushMicrotasks(); |
| 190 expect(closeCompleted, isTrue); |
| 191 }); |
| 192 |
| 193 test("errors are forwarded from the inner close()", () async { |
| 194 expect(completer.sink.done, throwsA("oh no")); |
| 195 expect(completer.sink.close(), throwsA("oh no")); |
| 196 await flushMicrotasks(); |
| 197 |
| 198 var sink = new TestSink(onDone: () => throw "oh no"); |
| 199 completer.setDestinationSink(sink); |
| 200 }); |
| 201 |
| 202 test("errors aren't top-leveled if only close() is listened to", () async { |
| 203 expect(completer.sink.close(), throwsA("oh no")); |
| 204 await flushMicrotasks(); |
| 205 |
| 206 var sink = new TestSink(onDone: () => throw "oh no"); |
| 207 completer.setDestinationSink(sink); |
| 208 |
| 209 // Give the event loop a chance to top-level errors if it's going to. |
| 210 await flushMicrotasks(); |
| 211 }); |
| 212 |
| 213 test("errors aren't top-leveled if only done is listened to", () async { |
| 214 completer.sink.close(); |
| 215 expect(completer.sink.done, throwsA("oh no")); |
| 216 await flushMicrotasks(); |
| 217 |
| 218 var sink = new TestSink(onDone: () => throw "oh no"); |
| 219 completer.setDestinationSink(sink); |
| 220 |
| 221 // Give the event loop a chance to top-level errors if it's going to. |
| 222 await flushMicrotasks(); |
| 223 }); |
| 224 }); |
| 225 |
| 226 test("the sink is closed, the destination is set, then done is read", |
| 227 () async { |
| 228 expect(completer.sink.close(), completes); |
| 229 await flushMicrotasks(); |
| 230 |
| 231 completer.setDestinationSink(new TestSink()); |
| 232 await flushMicrotasks(); |
| 233 |
| 234 expect(completer.sink.done, completes); |
| 235 }); |
| 236 |
| 237 test("done is read, the destination is set, then the sink is closed", |
| 238 () async { |
| 239 expect(completer.sink.done, completes); |
| 240 await flushMicrotasks(); |
| 241 |
| 242 completer.setDestinationSink(new TestSink()); |
| 243 await flushMicrotasks(); |
| 244 |
| 245 expect(completer.sink.close(), completes); |
| 246 }); |
| 247 |
| 248 group("fromFuture()", () { |
| 249 test("with a successful completion", () async { |
| 250 var futureCompleter = new Completer<StreamSink>(); |
| 251 var sink = StreamSinkCompleter.fromFuture(futureCompleter.future); |
| 252 sink.add(1); |
| 253 sink.add(2); |
| 254 sink.add(3); |
| 255 sink.close(); |
| 256 |
| 257 var testSink = new TestSink(); |
| 258 futureCompleter.complete(testSink); |
| 259 await testSink.done; |
| 260 |
| 261 expect(testSink.results[0].asValue.value, equals(1)); |
| 262 expect(testSink.results[1].asValue.value, equals(2)); |
| 263 expect(testSink.results[2].asValue.value, equals(3)); |
| 264 }); |
| 265 |
| 266 test("with an error", () async { |
| 267 var futureCompleter = new Completer<StreamSink>(); |
| 268 var sink = StreamSinkCompleter.fromFuture(futureCompleter.future); |
| 269 expect(sink.done, throwsA("oh no")); |
| 270 futureCompleter.completeError("oh no"); |
| 271 }); |
| 272 }); |
| 273 |
| 274 group("setError()", () { |
| 275 test("produces a closed sink with the error", () { |
| 276 completer.setError("oh no"); |
| 277 expect(completer.sink.done, throwsA("oh no")); |
| 278 expect(completer.sink.close(), throwsA("oh no")); |
| 279 }); |
| 280 |
| 281 test("produces an error even if done was accessed earlier", () async { |
| 282 expect(completer.sink.done, throwsA("oh no")); |
| 283 expect(completer.sink.close(), throwsA("oh no")); |
| 284 await flushMicrotasks(); |
| 285 |
| 286 completer.setError("oh no"); |
| 287 }); |
| 288 }); |
| 289 |
| 290 test("doesn't allow the destination sink to be set multiple times", () { |
| 291 completer.setDestinationSink(new TestSink()); |
| 292 expect( |
| 293 () => completer.setDestinationSink(new TestSink()), throwsStateError); |
| 294 expect( |
| 295 () => completer.setDestinationSink(new TestSink()), throwsStateError); |
| 296 }); |
| 297 } |
OLD | NEW |