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_async_test; | 6 library stream_controller_async_test; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 import '../../../pkg/unittest/lib/unittest.dart'; | 10 import '../../../pkg/unittest/lib/unittest.dart'; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 c.add(1); | 79 c.add(1); |
80 c.add(2); | 80 c.add(2); |
81 c.add(9); | 81 c.add(9); |
82 c.add(3); | 82 c.add(3); |
83 c.add(9); | 83 c.add(9); |
84 c.close(); | 84 c.close(); |
85 }); | 85 }); |
86 | 86 |
87 test("Single-subscription StreamController subscription changes", () { | 87 test("Single-subscription StreamController subscription changes", () { |
88 StreamController c = new StreamController(); | 88 StreamController c = new StreamController(); |
89 StreamSink sink = c.sink; | 89 EventSink sink = c.sink; |
90 Stream stream = c.stream; | 90 Stream stream = c.stream; |
91 int counter = 0; | 91 int counter = 0; |
92 var subscription; | 92 var subscription; |
93 subscription = stream.listen((data) { | 93 subscription = stream.listen((data) { |
94 counter += data; | 94 counter += data; |
95 Expect.throws(() => stream.listen(null), (e) => e is StateError); | 95 Expect.throws(() => stream.listen(null), (e) => e is StateError); |
96 subscription.cancel(); | 96 subscription.cancel(); |
97 stream.listen((data) { | 97 stream.listen((data) { |
98 counter += data * 10; | 98 counter += data * 10; |
99 }, | 99 }, |
100 onDone: expectAsync0(() { | 100 onDone: expectAsync0(() { |
101 Expect.equals(1 + 20, counter); | 101 Expect.equals(1 + 20, counter); |
102 })); | 102 })); |
103 }); | 103 }); |
104 sink.add(1); | 104 sink.add(1); |
105 sink.add(2); | 105 sink.add(2); |
106 sink.close(); | 106 sink.close(); |
107 }); | 107 }); |
108 | 108 |
109 test("Single-subscription StreamController events are buffered when" | 109 test("Single-subscription StreamController events are buffered when" |
110 " there is no subscriber", | 110 " there is no subscriber", |
111 () { | 111 () { |
112 StreamController c = new StreamController(); | 112 StreamController c = new StreamController(); |
113 StreamSink sink = c.sink; | 113 EventSink sink = c.sink; |
114 Stream stream = c.stream; | 114 Stream stream = c.stream; |
115 int counter = 0; | 115 int counter = 0; |
116 sink.add(1); | 116 sink.add(1); |
117 sink.add(2); | 117 sink.add(2); |
118 sink.close(); | 118 sink.close(); |
119 stream.listen( | 119 stream.listen( |
120 (data) { | 120 (data) { |
121 counter += data; | 121 counter += data; |
122 }, | 122 }, |
123 onDone: expectAsync0(() { | 123 onDone: expectAsync0(() { |
124 Expect.equals(3, counter); | 124 Expect.equals(3, counter); |
125 })); | 125 })); |
126 }); | 126 }); |
127 | 127 |
128 // Test subscription changes while firing. | 128 // Test subscription changes while firing. |
129 test("Single-subscription StreamController subscription changes while firing", | 129 test("Single-subscription StreamController subscription changes while firing", |
130 () { | 130 () { |
131 StreamController c = new StreamController(); | 131 StreamController c = new StreamController(); |
132 StreamSink sink = c.sink; | 132 EventSink sink = c.sink; |
133 Stream stream = c.stream; | 133 Stream stream = c.stream; |
134 int counter = 0; | 134 int counter = 0; |
135 var subscription = stream.listen(null); | 135 var subscription = stream.listen(null); |
136 subscription.onData(expectAsync1((data) { | 136 subscription.onData(expectAsync1((data) { |
137 counter += data; | 137 counter += data; |
138 subscription.cancel(); | 138 subscription.cancel(); |
139 stream.listen((data) { | 139 stream.listen((data) { |
140 counter += 10 * data; | 140 counter += 10 * data; |
141 }, | 141 }, |
142 onDone: expectAsync0(() { | 142 onDone: expectAsync0(() { |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 c.close(); | 413 c.close(); |
414 }); | 414 }); |
415 } | 415 } |
416 | 416 |
417 testStreamError(name, streamErrorTransform) { | 417 testStreamError(name, streamErrorTransform) { |
418 test("rethrow-$name-error", () { | 418 test("rethrow-$name-error", () { |
419 StreamController c = new StreamController(); | 419 StreamController c = new StreamController(); |
420 Stream s = streamErrorTransform(c.stream, (e) { throw error; }); | 420 Stream s = streamErrorTransform(c.stream, (e) { throw error; }); |
421 s.listen((_) { Expect.fail("unexpected value"); }, onError: expectAsync1( | 421 s.listen((_) { Expect.fail("unexpected value"); }, onError: expectAsync1( |
422 (AsyncError e) { Expect.identical(error, e); })); | 422 (AsyncError e) { Expect.identical(error, e); })); |
423 c.signalError(null); | 423 c.addError(null); |
424 c.close(); | 424 c.close(); |
425 }); | 425 }); |
426 } | 426 } |
427 | 427 |
428 testFuture(name, streamValueTransform) { | 428 testFuture(name, streamValueTransform) { |
429 test("rethrow-$name-value", () { | 429 test("rethrow-$name-value", () { |
430 StreamController c = new StreamController(); | 430 StreamController c = new StreamController(); |
431 Future f = streamValueTransform(c.stream, (v) { throw error; }); | 431 Future f = streamValueTransform(c.stream, (v) { throw error; }); |
432 f.then((v) { Expect.fail("unreachable"); }, | 432 f.then((v) { Expect.fail("unreachable"); }, |
433 onError: expectAsync1((e) { Expect.identical(error, e); })); | 433 onError: expectAsync1((e) { Expect.identical(error, e); })); |
(...skipping 17 matching lines...) Expand all Loading... |
451 testFuture("reduce", (s, act) => s.reduce(0, (a,b) => act(b))); | 451 testFuture("reduce", (s, act) => s.reduce(0, (a,b) => act(b))); |
452 } | 452 } |
453 | 453 |
454 main() { | 454 main() { |
455 testController(); | 455 testController(); |
456 testSingleController(); | 456 testSingleController(); |
457 testExtraMethods(); | 457 testExtraMethods(); |
458 testPause(); | 458 testPause(); |
459 testRethrow(); | 459 testRethrow(); |
460 } | 460 } |
OLD | NEW |