| 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 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
| 8 import '../../../pkg/unittest/lib/unittest.dart'; | 8 import '../../../pkg/unittest/lib/unittest.dart'; |
| 9 import 'event_helper.dart'; | 9 import 'event_helper.dart'; |
| 10 | 10 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 c.add(9); | 73 c.add(9); |
| 74 c.close(); | 74 c.close(); |
| 75 }); | 75 }); |
| 76 | 76 |
| 77 test("Single-subscription StreamController subscription changes", () { | 77 test("Single-subscription StreamController subscription changes", () { |
| 78 StreamController c = new StreamController.singleSubscription(); | 78 StreamController c = new StreamController.singleSubscription(); |
| 79 StreamSink sink = c.sink; | 79 StreamSink sink = c.sink; |
| 80 Stream stream = c.stream; | 80 Stream stream = c.stream; |
| 81 int counter = 0; | 81 int counter = 0; |
| 82 var subscription; | 82 var subscription; |
| 83 subscription = stream.subscribe(onData: (data) { | 83 subscription = stream.listen((data) { |
| 84 counter += data; | 84 counter += data; |
| 85 Expect.throws(() => stream.subscribe(), (e) => e is StateError); | 85 Expect.throws(() => stream.listen(null), (e) => e is StateError); |
| 86 subscription.unsubscribe(); | 86 subscription.cancel(); |
| 87 stream.subscribe(onData: (data) { | 87 stream.listen((data) { |
| 88 counter += data * 10; | 88 counter += data * 10; |
| 89 }, | 89 }, |
| 90 onDone: expectAsync0(() { | 90 onDone: expectAsync0(() { |
| 91 Expect.equals(1 + 20, counter); | 91 Expect.equals(1 + 20, counter); |
| 92 })); | 92 })); |
| 93 }); | 93 }); |
| 94 sink.add(1); | 94 sink.add(1); |
| 95 sink.add(2); | 95 sink.add(2); |
| 96 sink.close(); | 96 sink.close(); |
| 97 }); | 97 }); |
| 98 | 98 |
| 99 test("Single-subscription StreamController events are buffered when" | 99 test("Single-subscription StreamController events are buffered when" |
| 100 " there is no subscriber", | 100 " there is no subscriber", |
| 101 () { | 101 () { |
| 102 StreamController c = new StreamController.singleSubscription(); | 102 StreamController c = new StreamController.singleSubscription(); |
| 103 StreamSink sink = c.sink; | 103 StreamSink sink = c.sink; |
| 104 Stream stream = c.stream; | 104 Stream stream = c.stream; |
| 105 int counter = 0; | 105 int counter = 0; |
| 106 sink.add(1); | 106 sink.add(1); |
| 107 sink.add(2); | 107 sink.add(2); |
| 108 sink.close(); | 108 sink.close(); |
| 109 stream.subscribe( | 109 stream.listen( |
| 110 onData: (data) { | 110 (data) { |
| 111 counter += data; | 111 counter += data; |
| 112 }, | 112 }, |
| 113 onDone: expectAsync0(() { | 113 onDone: expectAsync0(() { |
| 114 Expect.equals(3, counter); | 114 Expect.equals(3, counter); |
| 115 })); | 115 })); |
| 116 }); | 116 }); |
| 117 | 117 |
| 118 // Test subscription changes while firing. | 118 // Test subscription changes while firing. |
| 119 test("Single-subscription StreamController subscription changes while firing", | 119 test("Single-subscription StreamController subscription changes while firing", |
| 120 () { | 120 () { |
| 121 StreamController c = new StreamController.singleSubscription(); | 121 StreamController c = new StreamController.singleSubscription(); |
| 122 StreamSink sink = c.sink; | 122 StreamSink sink = c.sink; |
| 123 Stream stream = c.stream; | 123 Stream stream = c.stream; |
| 124 int counter = 0; | 124 int counter = 0; |
| 125 var subscription = stream.subscribe(); | 125 var subscription = stream.listen(null); |
| 126 subscription.onData(expectAsync1((data) { | 126 subscription.onData(expectAsync1((data) { |
| 127 counter += data; | 127 counter += data; |
| 128 subscription.unsubscribe(); | 128 subscription.cancel(); |
| 129 stream.subscribe(onData: (data) { | 129 stream.listen((data) { |
| 130 counter += 10 * data; | 130 counter += 10 * data; |
| 131 }, | 131 }, |
| 132 onDone: expectAsync0(() { | 132 onDone: expectAsync0(() { |
| 133 Expect.equals(1 + 20 + 30 + 40 + 50, counter); | 133 Expect.equals(1 + 20 + 30 + 40 + 50, counter); |
| 134 })); | 134 })); |
| 135 Expect.throws(() => stream.subscribe(), (e) => e is StateError); | 135 Expect.throws(() => stream.listen(null), (e) => e is StateError); |
| 136 })); | 136 })); |
| 137 sink.add(1); // seen by stream 1 | 137 sink.add(1); // seen by stream 1 |
| 138 sink.add(2); // seen by stream 10 and 100 | 138 sink.add(2); // seen by stream 10 and 100 |
| 139 sink.add(3); // -"- | 139 sink.add(3); // -"- |
| 140 sink.add(4); // -"- | 140 sink.add(4); // -"- |
| 141 sink.add(5); // seen by stream 10 | 141 sink.add(5); // seen by stream 10 |
| 142 sink.close(); | 142 sink.close(); |
| 143 }); | 143 }); |
| 144 } | 144 } |
| 145 | 145 |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 actualEvents.resume(); | 389 actualEvents.resume(); |
| 390 }); | 390 }); |
| 391 } | 391 } |
| 392 | 392 |
| 393 main() { | 393 main() { |
| 394 testController(); | 394 testController(); |
| 395 testSingleController(); | 395 testSingleController(); |
| 396 testExtraMethods(); | 396 testExtraMethods(); |
| 397 testPause(); | 397 testPause(); |
| 398 } | 398 } |
| OLD | NEW |