| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Test the basic StreamController and StreamController.singleSubscription. | 5 // Test the basic StreamController and StreamController.singleSubscription. |
| 6 library stream_controller_test; | 6 library stream_controller_test; |
| 7 | 7 |
| 8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 import 'event_helper.dart'; | 10 import 'event_helper.dart'; |
| 11 | 11 |
| 12 testMultiController() { | 12 testMultiController() { |
| 13 // Test normal flow. | 13 // Test normal flow. |
| 14 var c = new StreamController.broadcast(); | 14 var c = new StreamController.broadcast(); |
| 15 Events expectedEvents = new Events() | 15 Events expectedEvents = new Events() |
| 16 ..add(42) | 16 ..add(42) |
| 17 ..add("dibs") | 17 ..add("dibs") |
| 18 ..error("error!") | 18 ..error("error!") |
| 19 ..error("error too!") | 19 ..error("error too!") |
| 20 ..close(); | 20 ..close(); |
| 21 Events actualEvents = new Events.capture(c.stream); | 21 Events actualEvents = new Events.capture(c.stream); |
| 22 expectedEvents.replay(c); | 22 expectedEvents.replay(c); |
| 23 Expect.listEquals(expectedEvents.events, actualEvents.events); | 23 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 24 | 24 |
| 25 // Test automatic unsubscription on error. | 25 // Test automatic unsubscription on error. |
| 26 c = new StreamController.broadcast(); | 26 c = new StreamController.broadcast(); |
| 27 expectedEvents = new Events()..add(42)..error("error"); | 27 expectedEvents = new Events()..add(42)..error("error"); |
| 28 actualEvents = new Events.capture(c.stream, unsubscribeOnError: true); | 28 actualEvents = new Events.capture(c.stream, cancelOnError: true); |
| 29 Events sentEvents = | 29 Events sentEvents = |
| 30 new Events()..add(42)..error("error")..add("Are you there?"); | 30 new Events()..add(42)..error("error")..add("Are you there?"); |
| 31 sentEvents.replay(c); | 31 sentEvents.replay(c); |
| 32 Expect.listEquals(expectedEvents.events, actualEvents.events); | 32 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 33 | 33 |
| 34 // Test manual unsubscription. | 34 // Test manual unsubscription. |
| 35 c = new StreamController.broadcast(); | 35 c = new StreamController.broadcast(); |
| 36 expectedEvents = new Events()..add(42)..error("error")..add(37); | 36 expectedEvents = new Events()..add(42)..error("error")..add(37); |
| 37 actualEvents = new Events.capture(c.stream, unsubscribeOnError: false); | 37 actualEvents = new Events.capture(c.stream, cancelOnError: false); |
| 38 expectedEvents.replay(c); | 38 expectedEvents.replay(c); |
| 39 actualEvents.subscription.cancel(); | 39 actualEvents.subscription.cancel(); |
| 40 c.add("Are you there"); // Not sent to actualEvents. | 40 c.add("Are you there"); // Not sent to actualEvents. |
| 41 Expect.listEquals(expectedEvents.events, actualEvents.events); | 41 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 42 | 42 |
| 43 // Test filter. | 43 // Test filter. |
| 44 c = new StreamController.broadcast(); | 44 c = new StreamController.broadcast(); |
| 45 expectedEvents = new Events() | 45 expectedEvents = new Events() |
| 46 ..add("a string")..add("another string")..close(); | 46 ..add("a string")..add("another string")..close(); |
| 47 sentEvents = new Events() | 47 sentEvents = new Events() |
| (...skipping 12 matching lines...) Expand all Loading... |
| 60 | 60 |
| 61 // Test handleError. | 61 // Test handleError. |
| 62 c = new StreamController.broadcast(); | 62 c = new StreamController.broadcast(); |
| 63 expectedEvents = new Events()..add("ab")..error("[foo]"); | 63 expectedEvents = new Events()..add("ab")..error("[foo]"); |
| 64 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); | 64 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); |
| 65 actualEvents = new Events.capture(c.stream.handleError((v) { | 65 actualEvents = new Events.capture(c.stream.handleError((v) { |
| 66 if (v.error is String) { | 66 if (v.error is String) { |
| 67 throw new AsyncError("[${v.error}]", | 67 throw new AsyncError("[${v.error}]", |
| 68 "other stack"); | 68 "other stack"); |
| 69 } | 69 } |
| 70 }), unsubscribeOnError: true); | 70 }), cancelOnError: true); |
| 71 sentEvents.replay(c); | 71 sentEvents.replay(c); |
| 72 Expect.listEquals(expectedEvents.events, actualEvents.events); | 72 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 73 | 73 |
| 74 // reduce is tested asynchronously and therefore not in this file. | 74 // reduce is tested asynchronously and therefore not in this file. |
| 75 | 75 |
| 76 // Test expand | 76 // Test expand |
| 77 c = new StreamController.broadcast(); | 77 c = new StreamController.broadcast(); |
| 78 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); | 78 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); |
| 79 expectedEvents = new Events()..add(1)..add(2)..add(3) | 79 expectedEvents = new Events()..add(1)..add(2)..add(3) |
| 80 ..add(1)..add(2) | 80 ..add(1)..add(2) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 ..error("not FormatException") // Unsubscribes. | 116 ..error("not FormatException") // Unsubscribes. |
| 117 ..close(); | 117 ..close(); |
| 118 expectedEvents = new Events()..add(42)..error("not FormatException"); | 118 expectedEvents = new Events()..add(42)..error("not FormatException"); |
| 119 actualEvents = new Events.capture( | 119 actualEvents = new Events.capture( |
| 120 c.stream.where((v) => v is String) | 120 c.stream.where((v) => v is String) |
| 121 .map((v) => int.parse(v)) | 121 .map((v) => int.parse(v)) |
| 122 .handleError((v) { | 122 .handleError((v) { |
| 123 if (v.error is! FormatException) throw v; | 123 if (v.error is! FormatException) throw v; |
| 124 }) | 124 }) |
| 125 .where((v) => v > 10), | 125 .where((v) => v > 10), |
| 126 unsubscribeOnError: true); | 126 cancelOnError: true); |
| 127 sentEvents.replay(c); | 127 sentEvents.replay(c); |
| 128 Expect.listEquals(expectedEvents.events, actualEvents.events); | 128 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 129 | 129 |
| 130 // Test subscription changes while firing. | 130 // Test subscription changes while firing. |
| 131 c = new StreamController.broadcast(); | 131 c = new StreamController.broadcast(); |
| 132 var sink = c.sink; | 132 var sink = c.sink; |
| 133 var stream = c.stream; | 133 var stream = c.stream; |
| 134 var counter = 0; | 134 var counter = 0; |
| 135 var subscription = stream.listen(null); | 135 var subscription = stream.listen(null); |
| 136 subscription.onData((data) { | 136 subscription.onData((data) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 162 ..error("error!") | 162 ..error("error!") |
| 163 ..error("error too!") | 163 ..error("error too!") |
| 164 ..close(); | 164 ..close(); |
| 165 Events actualEvents = new Events.capture(c.stream); | 165 Events actualEvents = new Events.capture(c.stream); |
| 166 expectedEvents.replay(c); | 166 expectedEvents.replay(c); |
| 167 Expect.listEquals(expectedEvents.events, actualEvents.events); | 167 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 168 | 168 |
| 169 // Test automatic unsubscription on error. | 169 // Test automatic unsubscription on error. |
| 170 c = new StreamController(); | 170 c = new StreamController(); |
| 171 expectedEvents = new Events()..add(42)..error("error"); | 171 expectedEvents = new Events()..add(42)..error("error"); |
| 172 actualEvents = new Events.capture(c.stream, unsubscribeOnError: true); | 172 actualEvents = new Events.capture(c.stream, cancelOnError: true); |
| 173 Events sentEvents = | 173 Events sentEvents = |
| 174 new Events()..add(42)..error("error")..add("Are you there?"); | 174 new Events()..add(42)..error("error")..add("Are you there?"); |
| 175 sentEvents.replay(c); | 175 sentEvents.replay(c); |
| 176 Expect.listEquals(expectedEvents.events, actualEvents.events); | 176 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 177 | 177 |
| 178 // Test manual unsubscription. | 178 // Test manual unsubscription. |
| 179 c = new StreamController(); | 179 c = new StreamController(); |
| 180 expectedEvents = new Events()..add(42)..error("error")..add(37); | 180 expectedEvents = new Events()..add(42)..error("error")..add(37); |
| 181 actualEvents = new Events.capture(c.stream, unsubscribeOnError: false); | 181 actualEvents = new Events.capture(c.stream, cancelOnError: false); |
| 182 expectedEvents.replay(c); | 182 expectedEvents.replay(c); |
| 183 actualEvents.subscription.cancel(); | 183 actualEvents.subscription.cancel(); |
| 184 c.add("Are you there"); // Not sent to actualEvents. | 184 c.add("Are you there"); // Not sent to actualEvents. |
| 185 Expect.listEquals(expectedEvents.events, actualEvents.events); | 185 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 186 | 186 |
| 187 // Test filter. | 187 // Test filter. |
| 188 c = new StreamController(); | 188 c = new StreamController(); |
| 189 expectedEvents = new Events() | 189 expectedEvents = new Events() |
| 190 ..add("a string")..add("another string")..close(); | 190 ..add("a string")..add("another string")..close(); |
| 191 sentEvents = new Events() | 191 sentEvents = new Events() |
| (...skipping 12 matching lines...) Expand all Loading... |
| 204 | 204 |
| 205 // Test handleError. | 205 // Test handleError. |
| 206 c = new StreamController(); | 206 c = new StreamController(); |
| 207 expectedEvents = new Events()..add("ab")..error("[foo]"); | 207 expectedEvents = new Events()..add("ab")..error("[foo]"); |
| 208 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); | 208 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); |
| 209 actualEvents = new Events.capture(c.stream.handleError((v) { | 209 actualEvents = new Events.capture(c.stream.handleError((v) { |
| 210 if (v.error is String) { | 210 if (v.error is String) { |
| 211 throw new AsyncError("[${v.error}]", | 211 throw new AsyncError("[${v.error}]", |
| 212 "other stack"); | 212 "other stack"); |
| 213 } | 213 } |
| 214 }), unsubscribeOnError: true); | 214 }), cancelOnError: true); |
| 215 sentEvents.replay(c); | 215 sentEvents.replay(c); |
| 216 Expect.listEquals(expectedEvents.events, actualEvents.events); | 216 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 217 | 217 |
| 218 // reduce is tested asynchronously and therefore not in this file. | 218 // reduce is tested asynchronously and therefore not in this file. |
| 219 | 219 |
| 220 // Test expand | 220 // Test expand |
| 221 c = new StreamController(); | 221 c = new StreamController(); |
| 222 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); | 222 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); |
| 223 expectedEvents = new Events()..add(1)..add(2)..add(3) | 223 expectedEvents = new Events()..add(1)..add(2)..add(3) |
| 224 ..add(1)..add(2) | 224 ..add(1)..add(2) |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 ..error("not FormatException") // Unsubscribes. | 293 ..error("not FormatException") // Unsubscribes. |
| 294 ..close(); | 294 ..close(); |
| 295 expectedEvents = new Events()..add(42)..error("not FormatException"); | 295 expectedEvents = new Events()..add(42)..error("not FormatException"); |
| 296 actualEvents = new Events.capture( | 296 actualEvents = new Events.capture( |
| 297 c.stream.where((v) => v is String) | 297 c.stream.where((v) => v is String) |
| 298 .map((v) => int.parse(v)) | 298 .map((v) => int.parse(v)) |
| 299 .handleError((v) { | 299 .handleError((v) { |
| 300 if (v.error is! FormatException) throw v; | 300 if (v.error is! FormatException) throw v; |
| 301 }) | 301 }) |
| 302 .where((v) => v > 10), | 302 .where((v) => v > 10), |
| 303 unsubscribeOnError: true); | 303 cancelOnError: true); |
| 304 sentEvents.replay(c); | 304 sentEvents.replay(c); |
| 305 Expect.listEquals(expectedEvents.events, actualEvents.events); | 305 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 306 | 306 |
| 307 // Test that only one subscription is allowed. | 307 // Test that only one subscription is allowed. |
| 308 c = new StreamController(); | 308 c = new StreamController(); |
| 309 var sink = c.sink; | 309 var sink = c.sink; |
| 310 var stream = c.stream; | 310 var stream = c.stream; |
| 311 var counter = 0; | 311 var counter = 0; |
| 312 var subscription = stream.listen((data) { counter += data; }); | 312 var subscription = stream.listen((data) { counter += data; }); |
| 313 Expect.throws(() => stream.listen(null), (e) => e is StateError); | 313 Expect.throws(() => stream.listen(null), (e) => e is StateError); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 Expect.isTrue(c.isClosed); | 398 Expect.isTrue(c.isClosed); |
| 399 } | 399 } |
| 400 } | 400 } |
| 401 | 401 |
| 402 main() { | 402 main() { |
| 403 testMultiController(); | 403 testMultiController(); |
| 404 testSingleController(); | 404 testSingleController(); |
| 405 testExtraMethods(); | 405 testExtraMethods(); |
| 406 testClosed(); | 406 testClosed(); |
| 407 } | 407 } |
| OLD | NEW |