| 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(); |
| 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.asBroadcastStream()); |
| 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(); |
| 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.asBroadcastStream(), |
| 29 unsubscribeOnError: true); |
| 29 Events sentEvents = | 30 Events sentEvents = |
| 30 new Events()..add(42)..error("error")..add("Are you there?"); | 31 new Events()..add(42)..error("error")..add("Are you there?"); |
| 31 sentEvents.replay(c); | 32 sentEvents.replay(c); |
| 32 Expect.listEquals(expectedEvents.events, actualEvents.events); | 33 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 33 | 34 |
| 34 // Test manual unsubscription. | 35 // Test manual unsubscription. |
| 35 c = new StreamController.broadcast(); | 36 c = new StreamController(); |
| 36 expectedEvents = new Events()..add(42)..error("error")..add(37); | 37 expectedEvents = new Events()..add(42)..error("error")..add(37); |
| 37 actualEvents = new Events.capture(c.stream, unsubscribeOnError: false); | 38 actualEvents = new Events.capture(c.stream.asBroadcastStream(), |
| 39 unsubscribeOnError: false); |
| 38 expectedEvents.replay(c); | 40 expectedEvents.replay(c); |
| 39 actualEvents.subscription.cancel(); | 41 actualEvents.subscription.cancel(); |
| 40 c.add("Are you there"); // Not sent to actualEvents. | 42 c.add("Are you there"); // Not sent to actualEvents. |
| 41 Expect.listEquals(expectedEvents.events, actualEvents.events); | 43 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 42 | 44 |
| 43 // Test filter. | 45 // Test filter. |
| 44 c = new StreamController.broadcast(); | 46 c = new StreamController(); |
| 45 expectedEvents = new Events() | 47 expectedEvents = new Events() |
| 46 ..add("a string")..add("another string")..close(); | 48 ..add("a string")..add("another string")..close(); |
| 47 sentEvents = new Events() | 49 sentEvents = new Events() |
| 48 ..add("a string")..add(42)..add("another string")..close(); | 50 ..add("a string")..add(42)..add("another string")..close(); |
| 49 actualEvents = new Events.capture(c.stream.where((v) => v is String)); | 51 actualEvents = new Events.capture(c.stream |
| 52 .asBroadcastStream() |
| 53 .where((v) => v is String)); |
| 50 sentEvents.replay(c); | 54 sentEvents.replay(c); |
| 51 Expect.listEquals(expectedEvents.events, actualEvents.events); | 55 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 52 | 56 |
| 53 // Test map. | 57 // Test map. |
| 54 c = new StreamController.broadcast(); | 58 c = new StreamController(); |
| 55 expectedEvents = new Events()..add("abab")..error("error")..close(); | 59 expectedEvents = new Events()..add("abab")..error("error")..close(); |
| 56 sentEvents = new Events()..add("ab")..error("error")..close(); | 60 sentEvents = new Events()..add("ab")..error("error")..close(); |
| 57 actualEvents = new Events.capture(c.stream.map((v) => "$v$v")); | 61 actualEvents = new Events.capture(c.stream |
| 62 .asBroadcastStream() |
| 63 .map((v) => "$v$v")); |
| 58 sentEvents.replay(c); | 64 sentEvents.replay(c); |
| 59 Expect.listEquals(expectedEvents.events, actualEvents.events); | 65 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 60 | 66 |
| 61 // Test handleError. | 67 // Test handleError. |
| 62 c = new StreamController.broadcast(); | 68 c = new StreamController(); |
| 63 expectedEvents = new Events()..add("ab")..error("[foo]"); | 69 expectedEvents = new Events()..add("ab")..error("[foo]"); |
| 64 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); | 70 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); |
| 65 actualEvents = new Events.capture(c.stream.handleError((v) { | 71 actualEvents = new Events.capture(c.stream |
| 72 .asBroadcastStream() |
| 73 .handleError((v) { |
| 66 if (v.error is String) { | 74 if (v.error is String) { |
| 67 throw new AsyncError("[${v.error}]", | 75 throw new AsyncError("[${v.error}]", |
| 68 "other stack"); | 76 "other stack"); |
| 69 } | 77 } |
| 70 }), unsubscribeOnError: true); | 78 }), unsubscribeOnError: true); |
| 71 sentEvents.replay(c); | 79 sentEvents.replay(c); |
| 72 Expect.listEquals(expectedEvents.events, actualEvents.events); | 80 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 73 | 81 |
| 74 // reduce is tested asynchronously and therefore not in this file. | 82 // reduce is tested asynchronously and therefore not in this file. |
| 75 | 83 |
| 76 // Test expand | 84 // Test expand |
| 77 c = new StreamController.broadcast(); | 85 c = new StreamController(); |
| 78 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); | 86 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); |
| 79 expectedEvents = new Events()..add(1)..add(2)..add(3) | 87 expectedEvents = new Events()..add(1)..add(2)..add(3) |
| 80 ..add(1)..add(2) | 88 ..add(1)..add(2) |
| 81 ..add(1)..add(2)..add(3)..add(4) | 89 ..add(1)..add(2)..add(3)..add(4) |
| 82 ..close(); | 90 ..close(); |
| 83 actualEvents = new Events.capture(c.stream.expand((v) { | 91 actualEvents = new Events.capture(c.stream.asBroadcastStream().expand((v) { |
| 84 var l = []; | 92 var l = []; |
| 85 for (int i = 0; i < v; i++) l.add(i + 1); | 93 for (int i = 0; i < v; i++) l.add(i + 1); |
| 86 return l; | 94 return l; |
| 87 })); | 95 })); |
| 88 sentEvents.replay(c); | 96 sentEvents.replay(c); |
| 89 Expect.listEquals(expectedEvents.events, actualEvents.events); | 97 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 90 | 98 |
| 91 // Test transform. | 99 // Test transform. |
| 92 c = new StreamController.broadcast(); | 100 c = new StreamController(); |
| 93 sentEvents = new Events()..add("a")..error(42)..add("b")..close(); | 101 sentEvents = new Events()..add("a")..error(42)..add("b")..close(); |
| 94 expectedEvents = | 102 expectedEvents = |
| 95 new Events()..error("a")..add(42)..error("b")..add("foo")..close(); | 103 new Events()..error("a")..add(42)..error("b")..add("foo")..close(); |
| 96 actualEvents = new Events.capture(c.stream.transform( | 104 actualEvents = new Events.capture(c.stream.asBroadcastStream().transform( |
| 97 new StreamTransformer( | 105 new StreamTransformer( |
| 98 handleData: (v, s) { s.addError(new AsyncError(v)); }, | 106 handleData: (v, s) { s.addError(new AsyncError(v)); }, |
| 99 handleError: (e, s) { s.add(e.error); }, | 107 handleError: (e, s) { s.add(e.error); }, |
| 100 handleDone: (s) { | 108 handleDone: (s) { |
| 101 | 109 |
| 102 s.add("foo"); | 110 s.add("foo"); |
| 103 | 111 |
| 104 s.close(); | 112 s.close(); |
| 105 | 113 |
| 106 }))); | 114 }))); |
| 107 sentEvents.replay(c); | 115 sentEvents.replay(c); |
| 108 Expect.listEquals(expectedEvents.events, actualEvents.events); | 116 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 109 | 117 |
| 110 // Test multiple filters. | 118 // Test multiple filters. |
| 111 c = new StreamController.broadcast(); | 119 c = new StreamController(); |
| 112 sentEvents = new Events()..add(42) | 120 sentEvents = new Events()..add(42) |
| 113 ..add("snugglefluffy") | 121 ..add("snugglefluffy") |
| 114 ..add(7) | 122 ..add(7) |
| 115 ..add("42") | 123 ..add("42") |
| 116 ..error("not FormatException") // Unsubscribes. | 124 ..error("not FormatException") // Unsubscribes. |
| 117 ..close(); | 125 ..close(); |
| 118 expectedEvents = new Events()..add(42)..error("not FormatException"); | 126 expectedEvents = new Events()..add(42)..error("not FormatException"); |
| 119 actualEvents = new Events.capture( | 127 actualEvents = new Events.capture( |
| 120 c.stream.where((v) => v is String) | 128 c.stream.asBroadcastStream().where((v) => v is String) |
| 121 .map((v) => int.parse(v)) | 129 .map((v) => int.parse(v)) |
| 122 .handleError((v) { | 130 .handleError((v) { |
| 123 if (v.error is! FormatException) throw v; | 131 if (v.error is! FormatException) throw v; |
| 124 }) | 132 }) |
| 125 .where((v) => v > 10), | 133 .where((v) => v > 10), |
| 126 unsubscribeOnError: true); | 134 unsubscribeOnError: true); |
| 127 sentEvents.replay(c); | 135 sentEvents.replay(c); |
| 128 Expect.listEquals(expectedEvents.events, actualEvents.events); | 136 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 129 | 137 |
| 130 // Test subscription changes while firing. | 138 // Test subscription changes while firing. |
| 131 c = new StreamController.broadcast(); | 139 c = new StreamController(); |
| 132 var sink = c.sink; | 140 var sink = c.sink; |
| 133 var stream = c.stream; | 141 var stream = c.stream.asBroadcastStream(); |
| 134 var counter = 0; | 142 var counter = 0; |
| 135 var subscription = stream.listen(null); | 143 var subscription = stream.listen(null); |
| 136 subscription.onData((data) { | 144 subscription.onData((data) { |
| 137 counter += data; | 145 counter += data; |
| 138 subscription.cancel(); | 146 subscription.cancel(); |
| 139 stream.listen((data) { | 147 stream.listen((data) { |
| 140 counter += 10 * data; | 148 counter += 10 * data; |
| 141 }); | 149 }); |
| 142 var subscription2 = stream.listen(null); | 150 var subscription2 = stream.listen(null); |
| 143 subscription2.onData((data) { | 151 subscription2.onData((data) { |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 ..add(5)..add(6)..add(4)..add(6)..add(8)..add(3)..add(4)..add(1)..close(); | 388 ..add(5)..add(6)..add(4)..add(6)..add(8)..add(3)..add(4)..add(1)..close(); |
| 381 expectedEvents = new Events() | 389 expectedEvents = new Events() |
| 382 ..add(5)..add(4)..add(3)..add(1)..close(); | 390 ..add(5)..add(4)..add(3)..add(1)..close(); |
| 383 // Use 'distinct' as a filter with access to the previously emitted event. | 391 // Use 'distinct' as a filter with access to the previously emitted event. |
| 384 actualEvents = new Events.capture(c.stream.distinct((a, b) => a < b)); | 392 actualEvents = new Events.capture(c.stream.distinct((a, b) => a < b)); |
| 385 sentEvents.replay(c); | 393 sentEvents.replay(c); |
| 386 Expect.listEquals(expectedEvents.events, actualEvents.events); | 394 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 387 } | 395 } |
| 388 | 396 |
| 389 testClosed() { | 397 testClosed() { |
| 390 for (StreamController c in [new StreamController(), | 398 StreamController c = new StreamController(); |
| 391 new StreamController.broadcast()]) { | 399 Expect.isFalse(c.isClosed); |
| 392 Expect.isFalse(c.isClosed); | 400 c.add(42); |
| 393 c.add(42); | 401 Expect.isFalse(c.isClosed); |
| 394 Expect.isFalse(c.isClosed); | 402 c.addError("bad"); |
| 395 c.addError("bad"); | 403 Expect.isFalse(c.isClosed); |
| 396 Expect.isFalse(c.isClosed); | 404 c.close(); |
| 397 c.close(); | 405 Expect.isTrue(c.isClosed); |
| 398 Expect.isTrue(c.isClosed); | |
| 399 } | |
| 400 } | 406 } |
| 401 | 407 |
| 402 main() { | 408 main() { |
| 403 testMultiController(); | 409 testMultiController(); |
| 404 testSingleController(); | 410 testSingleController(); |
| 405 testExtraMethods(); | 411 testExtraMethods(); |
| 406 testClosed(); | 412 testClosed(); |
| 407 } | 413 } |
| OLD | NEW |