| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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:async/async.dart'; | 7 import 'package:async/async.dart'; |
| 8 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
| 9 | 9 |
| 10 main() { | 10 main() { |
| 11 var controller; | 11 StreamController<int> controller; |
| 12 var splitter; | 12 var splitter; |
| 13 setUp(() { | 13 setUp(() { |
| 14 controller = new StreamController<int>(); | 14 controller = new StreamController<int>(); |
| 15 splitter = new StreamSplitter<int>(controller.stream); | 15 splitter = new StreamSplitter<int>(controller.stream); |
| 16 }); | 16 }); |
| 17 | 17 |
| 18 test("a branch that's created before the stream starts to replay it", | 18 test("a branch that's created before the stream starts to replay it", |
| 19 () async { | 19 () async { |
| 20 var events = []; | 20 var events = []; |
| 21 var branch = splitter.split(); | 21 var branch = splitter.split(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 40 test("a branch replays error events as well as data events", () { | 40 test("a branch replays error events as well as data events", () { |
| 41 var branch = splitter.split(); | 41 var branch = splitter.split(); |
| 42 splitter.close(); | 42 splitter.close(); |
| 43 | 43 |
| 44 controller.add(1); | 44 controller.add(1); |
| 45 controller.addError("error"); | 45 controller.addError("error"); |
| 46 controller.add(3); | 46 controller.add(3); |
| 47 controller.close(); | 47 controller.close(); |
| 48 | 48 |
| 49 var count = 0; | 49 var count = 0; |
| 50 branch.listen(expectAsync((value) { | 50 branch.listen( |
| 51 expect(count, anyOf(0, 2)); | 51 expectAsync1((value) { |
| 52 expect(value, equals(count + 1)); | 52 expect(count, anyOf(0, 2)); |
| 53 count++; | 53 expect(value, equals(count + 1)); |
| 54 }, count: 2), onError: expectAsync((error) { | 54 count++; |
| 55 }, count: 2), onError: expectAsync1((error) { |
| 55 expect(count, equals(1)); | 56 expect(count, equals(1)); |
| 56 expect(error, equals("error")); | 57 expect(error, equals("error")); |
| 57 count++; | 58 count++; |
| 58 }), onDone: expectAsync(() { | 59 }), onDone: expectAsync0(() { |
| 59 expect(count, equals(3)); | 60 expect(count, equals(3)); |
| 60 })); | 61 })); |
| 61 }); | 62 }); |
| 62 | 63 |
| 63 test("a branch that's created in the middle of a stream replays it", () async
{ | 64 test("a branch that's created in the middle of a stream replays it", |
| 65 () async { |
| 64 controller.add(1); | 66 controller.add(1); |
| 65 controller.add(2); | 67 controller.add(2); |
| 66 await flushMicrotasks(); | 68 await flushMicrotasks(); |
| 67 | 69 |
| 68 var branch = splitter.split(); | 70 var branch = splitter.split(); |
| 69 splitter.close(); | 71 splitter.close(); |
| 70 | 72 |
| 71 controller.add(3); | 73 controller.add(3); |
| 72 controller.add(4); | 74 controller.add(4); |
| 73 controller.close(); | 75 controller.close(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 97 | 99 |
| 98 // TODO(nweiz): Test that branches have the correct reified type once Dart | 100 // TODO(nweiz): Test that branches have the correct reified type once Dart |
| 99 // 1.11 is released. In 1.10, the stream exposed by a StreamController didn't | 101 // 1.11 is released. In 1.10, the stream exposed by a StreamController didn't |
| 100 // have a reified type. | 102 // have a reified type. |
| 101 | 103 |
| 102 test("multiple branches each replay the stream", () async { | 104 test("multiple branches each replay the stream", () async { |
| 103 var branch1 = splitter.split(); | 105 var branch1 = splitter.split(); |
| 104 controller.add(1); | 106 controller.add(1); |
| 105 controller.add(2); | 107 controller.add(2); |
| 106 await flushMicrotasks(); | 108 await flushMicrotasks(); |
| 107 | 109 |
| 108 var branch2 = splitter.split(); | 110 var branch2 = splitter.split(); |
| 109 controller.add(3); | 111 controller.add(3); |
| 110 controller.close(); | 112 controller.close(); |
| 111 await flushMicrotasks(); | 113 await flushMicrotasks(); |
| 112 | 114 |
| 113 var branch3 = splitter.split(); | 115 var branch3 = splitter.split(); |
| 114 splitter.close(); | 116 splitter.close(); |
| 115 | 117 |
| 116 expect(branch1.toList(), completion(equals([1, 2, 3]))); | 118 expect(branch1.toList(), completion(equals([1, 2, 3]))); |
| 117 expect(branch2.toList(), completion(equals([1, 2, 3]))); | 119 expect(branch2.toList(), completion(equals([1, 2, 3]))); |
| 118 expect(branch3.toList(), completion(equals([1, 2, 3]))); | 120 expect(branch3.toList(), completion(equals([1, 2, 3]))); |
| 119 }); | 121 }); |
| 120 | 122 |
| 121 test("a branch doesn't close until the source stream closes", () async { | 123 test("a branch doesn't close until the source stream closes", () async { |
| 122 var branch = splitter.split(); | 124 var branch = splitter.split(); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 var branch4 = splitter.split(); | 202 var branch4 = splitter.split(); |
| 201 splitter.close(); | 203 splitter.close(); |
| 202 await flushMicrotasks(); | 204 await flushMicrotasks(); |
| 203 expect(controller.isPaused, isTrue); | 205 expect(controller.isPaused, isTrue); |
| 204 | 206 |
| 205 branch4.listen(null); | 207 branch4.listen(null); |
| 206 await flushMicrotasks(); | 208 await flushMicrotasks(); |
| 207 expect(controller.isPaused, isFalse); | 209 expect(controller.isPaused, isFalse); |
| 208 }); | 210 }); |
| 209 | 211 |
| 210 test("the source stream is canceled when it's closed after all branches have " | 212 test( |
| 213 "the source stream is canceled when it's closed after all branches have " |
| 211 "been canceled", () async { | 214 "been canceled", () async { |
| 212 var branch1 = splitter.split(); | 215 var branch1 = splitter.split(); |
| 213 var branch2 = splitter.split(); | 216 var branch2 = splitter.split(); |
| 214 var branch3 = splitter.split(); | 217 var branch3 = splitter.split(); |
| 215 | 218 |
| 216 var subscription1 = branch1.listen(null); | 219 var subscription1 = branch1.listen(null); |
| 217 var subscription2 = branch2.listen(null); | 220 var subscription2 = branch2.listen(null); |
| 218 var subscription3 = branch3.listen(null); | 221 var subscription3 = branch3.listen(null); |
| 219 | 222 |
| 220 subscription1.cancel(); | 223 subscription1.cancel(); |
| 221 await flushMicrotasks(); | 224 await flushMicrotasks(); |
| 222 expect(controller.hasListener, isTrue); | 225 expect(controller.hasListener, isTrue); |
| 223 | 226 |
| 224 subscription2.cancel(); | 227 subscription2.cancel(); |
| 225 await flushMicrotasks(); | 228 await flushMicrotasks(); |
| 226 expect(controller.hasListener, isTrue); | 229 expect(controller.hasListener, isTrue); |
| 227 | 230 |
| 228 subscription3.cancel(); | 231 subscription3.cancel(); |
| 229 await flushMicrotasks(); | 232 await flushMicrotasks(); |
| 230 expect(controller.hasListener, isTrue); | 233 expect(controller.hasListener, isTrue); |
| 231 | 234 |
| 232 splitter.close(); | 235 splitter.close(); |
| 233 expect(controller.hasListener, isFalse); | 236 expect(controller.hasListener, isFalse); |
| 234 }); | 237 }); |
| 235 | 238 |
| 236 test("the source stream is canceled when all branches are canceled after it " | 239 test( |
| 240 "the source stream is canceled when all branches are canceled after it " |
| 237 "has been closed", () async { | 241 "has been closed", () async { |
| 238 var branch1 = splitter.split(); | 242 var branch1 = splitter.split(); |
| 239 var branch2 = splitter.split(); | 243 var branch2 = splitter.split(); |
| 240 var branch3 = splitter.split(); | 244 var branch3 = splitter.split(); |
| 241 splitter.close(); | 245 splitter.close(); |
| 242 | 246 |
| 243 var subscription1 = branch1.listen(null); | 247 var subscription1 = branch1.listen(null); |
| 244 var subscription2 = branch2.listen(null); | 248 var subscription2 = branch2.listen(null); |
| 245 var subscription3 = branch3.listen(null); | 249 var subscription3 = branch3.listen(null); |
| 246 | 250 |
| 247 subscription1.cancel(); | 251 subscription1.cancel(); |
| 248 await flushMicrotasks(); | 252 await flushMicrotasks(); |
| 249 expect(controller.hasListener, isTrue); | 253 expect(controller.hasListener, isTrue); |
| 250 | 254 |
| 251 subscription2.cancel(); | 255 subscription2.cancel(); |
| 252 await flushMicrotasks(); | 256 await flushMicrotasks(); |
| 253 expect(controller.hasListener, isTrue); | 257 expect(controller.hasListener, isTrue); |
| 254 | 258 |
| 255 subscription3.cancel(); | 259 subscription3.cancel(); |
| 256 await flushMicrotasks(); | 260 await flushMicrotasks(); |
| 257 expect(controller.hasListener, isFalse); | 261 expect(controller.hasListener, isFalse); |
| 258 }); | 262 }); |
| 259 | 263 |
| 260 test("a splitter that's closed before any branches are added never listens " | 264 test( |
| 265 "a splitter that's closed before any branches are added never listens " |
| 261 "to the source stream", () { | 266 "to the source stream", () { |
| 262 splitter.close(); | 267 splitter.close(); |
| 263 | 268 |
| 264 // This would throw an error if the stream had already been listened to. | 269 // This would throw an error if the stream had already been listened to. |
| 265 controller.stream.listen(null); | 270 controller.stream.listen(null); |
| 266 }); | 271 }); |
| 267 | 272 |
| 268 test("splitFrom splits a source stream into the designated number of " | 273 test( |
| 274 "splitFrom splits a source stream into the designated number of " |
| 269 "branches", () { | 275 "branches", () { |
| 270 var branches = StreamSplitter.splitFrom(controller.stream, 5); | 276 var branches = StreamSplitter.splitFrom(controller.stream, 5); |
| 271 | 277 |
| 272 controller.add(1); | 278 controller.add(1); |
| 273 controller.add(2); | 279 controller.add(2); |
| 274 controller.add(3); | 280 controller.add(3); |
| 275 controller.close(); | 281 controller.close(); |
| 276 | 282 |
| 277 expect(branches[0].toList(), completion(equals([1, 2, 3]))); | 283 expect(branches[0].toList(), completion(equals([1, 2, 3]))); |
| 278 expect(branches[1].toList(), completion(equals([1, 2, 3]))); | 284 expect(branches[1].toList(), completion(equals([1, 2, 3]))); |
| 279 expect(branches[2].toList(), completion(equals([1, 2, 3]))); | 285 expect(branches[2].toList(), completion(equals([1, 2, 3]))); |
| 280 expect(branches[3].toList(), completion(equals([1, 2, 3]))); | 286 expect(branches[3].toList(), completion(equals([1, 2, 3]))); |
| 281 expect(branches[4].toList(), completion(equals([1, 2, 3]))); | 287 expect(branches[4].toList(), completion(equals([1, 2, 3]))); |
| 282 }); | 288 }); |
| 283 } | 289 } |
| 284 | 290 |
| 285 /// Wait for all microtasks to complete. | 291 /// Wait for all microtasks to complete. |
| 286 Future flushMicrotasks() => new Future.delayed(Duration.ZERO); | 292 Future flushMicrotasks() => new Future.delayed(Duration.ZERO); |
| OLD | NEW |