OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Test the basic StreamController and StreamController.singleSubscription. |
| 6 import 'dart:async'; |
| 7 import 'dart:isolate'; |
| 8 import '../../../pkg/unittest/lib/unittest.dart'; |
| 9 import 'event_helper.dart'; |
| 10 |
| 11 testController() { |
| 12 // Test reduce |
| 13 test("StreamController.reduce", () { |
| 14 StreamController c = new StreamController(); |
| 15 c.reduce(0, (a,b) => a + b) |
| 16 .then(expectAsync1((int v) { |
| 17 Expect.equals(42, v); |
| 18 })); |
| 19 c.add(10); |
| 20 c.add(32); |
| 21 c.close(); |
| 22 }); |
| 23 |
| 24 test("StreamController.reduce throws", () { |
| 25 StreamController c = new StreamController(); |
| 26 c.reduce(0, (a,b) { throw "Fnyf!"; }) |
| 27 .catchError(expectAsync1((e) { |
| 28 Expect.equals("Fnyf!", e.error); |
| 29 })); |
| 30 c.add(42); |
| 31 }); |
| 32 |
| 33 test("StreamController.pipeInto", () { |
| 34 StreamController c = new StreamController(); |
| 35 var list = <int>[]; |
| 36 c.pipeInto(new CollectionSink<int>(list)) |
| 37 .then(expectAsync0(() { Expect.listEquals(<int>[1,2,9,3,9], list); })); |
| 38 c.add(1); |
| 39 c.add(2); |
| 40 c.add(9); |
| 41 c.add(3); |
| 42 c.add(9); |
| 43 c.close(); |
| 44 }); |
| 45 } |
| 46 |
| 47 testSingleController() { |
| 48 test("Single-subscription StreamController.reduce", () { |
| 49 StreamController c = new StreamController.singleSubscription(); |
| 50 c.reduce(0, (a,b) => a + b) |
| 51 .then(expectAsync1((int v) { Expect.equals(42, v); })); |
| 52 c.add(10); |
| 53 c.add(32); |
| 54 c.close(); |
| 55 }); |
| 56 |
| 57 test("Single-subscription StreamController.reduce throws", () { |
| 58 StreamController c = new StreamController.singleSubscription(); |
| 59 c.reduce(0, (a,b) { throw "Fnyf!"; }) |
| 60 .catchError(expectAsync1((e) { Expect.equals("Fnyf!", e.error); })); |
| 61 c.add(42); |
| 62 }); |
| 63 |
| 64 test("Single-subscription StreamController.pipeInto", () { |
| 65 StreamController c = new StreamController.singleSubscription(); |
| 66 var list = <int>[]; |
| 67 c.pipeInto(new CollectionSink<int>(list)) |
| 68 .then(expectAsync0(() { Expect.listEquals(<int>[1,2,9,3,9], list); })); |
| 69 c.add(1); |
| 70 c.add(2); |
| 71 c.add(9); |
| 72 c.add(3); |
| 73 c.add(9); |
| 74 c.close(); |
| 75 }); |
| 76 |
| 77 test("Single-subscription StreamController subscription changes", () { |
| 78 StreamController c = new StreamController.singleSubscription(); |
| 79 StreamSink sink = c.sink; |
| 80 Stream stream = c.stream; |
| 81 int counter = 0; |
| 82 var subscription; |
| 83 subscription = stream.listen((data) { |
| 84 counter += data; |
| 85 Expect.throws(() => stream.listen(null), (e) => e is StateError); |
| 86 subscription.cancel(); |
| 87 stream.listen((data) { |
| 88 counter += data * 10; |
| 89 }, |
| 90 onDone: expectAsync0(() { |
| 91 Expect.equals(1 + 20, counter); |
| 92 })); |
| 93 }); |
| 94 sink.add(1); |
| 95 sink.add(2); |
| 96 sink.close(); |
| 97 }); |
| 98 |
| 99 test("Single-subscription StreamController events are buffered when" |
| 100 " there is no subscriber", |
| 101 () { |
| 102 StreamController c = new StreamController.singleSubscription(); |
| 103 StreamSink sink = c.sink; |
| 104 Stream stream = c.stream; |
| 105 int counter = 0; |
| 106 sink.add(1); |
| 107 sink.add(2); |
| 108 sink.close(); |
| 109 stream.listen( |
| 110 (data) { |
| 111 counter += data; |
| 112 }, |
| 113 onDone: expectAsync0(() { |
| 114 Expect.equals(3, counter); |
| 115 })); |
| 116 }); |
| 117 |
| 118 // Test subscription changes while firing. |
| 119 test("Single-subscription StreamController subscription changes while firing", |
| 120 () { |
| 121 StreamController c = new StreamController.singleSubscription(); |
| 122 StreamSink sink = c.sink; |
| 123 Stream stream = c.stream; |
| 124 int counter = 0; |
| 125 var subscription = stream.listen(null); |
| 126 subscription.onData(expectAsync1((data) { |
| 127 counter += data; |
| 128 subscription.cancel(); |
| 129 stream.listen((data) { |
| 130 counter += 10 * data; |
| 131 }, |
| 132 onDone: expectAsync0(() { |
| 133 Expect.equals(1 + 20 + 30 + 40 + 50, counter); |
| 134 })); |
| 135 Expect.throws(() => stream.listen(null), (e) => e is StateError); |
| 136 })); |
| 137 sink.add(1); // seen by stream 1 |
| 138 sink.add(2); // seen by stream 10 and 100 |
| 139 sink.add(3); // -"- |
| 140 sink.add(4); // -"- |
| 141 sink.add(5); // seen by stream 10 |
| 142 sink.close(); |
| 143 }); |
| 144 } |
| 145 |
| 146 testExtraMethods() { |
| 147 Events sentEvents = new Events()..add(7)..add(9)..add(13)..add(87)..close(); |
| 148 |
| 149 test("firstMatching", () { |
| 150 StreamController c = new StreamController(); |
| 151 Future f = c.firstMatching((x) => (x % 3) == 0); |
| 152 f.then(expectAsync1((v) { Expect.equals(9, v); })); |
| 153 sentEvents.replay(c); |
| 154 }); |
| 155 |
| 156 test("firstMatching 2", () { |
| 157 StreamController c = new StreamController(); |
| 158 Future f = c.firstMatching((x) => (x % 4) == 0); |
| 159 f.catchError(expectAsync1((e) {})); |
| 160 sentEvents.replay(c); |
| 161 }); |
| 162 |
| 163 test("firstMatching 3", () { |
| 164 StreamController c = new StreamController(); |
| 165 Future f = c.firstMatching((x) => (x % 4) == 0, defaultValue: () => 999); |
| 166 f.then(expectAsync1((v) { Expect.equals(999, v); })); |
| 167 sentEvents.replay(c); |
| 168 }); |
| 169 |
| 170 |
| 171 test("lastMatching", () { |
| 172 StreamController c = new StreamController(); |
| 173 Future f = c.lastMatching((x) => (x % 3) == 0); |
| 174 f.then(expectAsync1((v) { Expect.equals(87, v); })); |
| 175 sentEvents.replay(c); |
| 176 }); |
| 177 |
| 178 test("lastMatching 2", () { |
| 179 StreamController c = new StreamController(); |
| 180 Future f = c.lastMatching((x) => (x % 4) == 0); |
| 181 f.catchError(expectAsync1((e) {})); |
| 182 sentEvents.replay(c); |
| 183 }); |
| 184 |
| 185 test("lastMatching 3", () { |
| 186 StreamController c = new StreamController(); |
| 187 Future f = c.lastMatching((x) => (x % 4) == 0, defaultValue: () => 999); |
| 188 f.then(expectAsync1((v) { Expect.equals(999, v); })); |
| 189 sentEvents.replay(c); |
| 190 }); |
| 191 |
| 192 test("singleMatching", () { |
| 193 StreamController c = new StreamController(); |
| 194 Future f = c.singleMatching((x) => (x % 9) == 0); |
| 195 f.then(expectAsync1((v) { Expect.equals(9, v); })); |
| 196 sentEvents.replay(c); |
| 197 }); |
| 198 |
| 199 test("singleMatching 2", () { |
| 200 StreamController c = new StreamController(); |
| 201 Future f = c.singleMatching((x) => (x % 3) == 0); // Matches both 9 and 87.
. |
| 202 f.catchError(expectAsync1((e) { Expect.isTrue(e.error is StateError); })); |
| 203 sentEvents.replay(c); |
| 204 }); |
| 205 |
| 206 test("first", () { |
| 207 StreamController c = new StreamController(); |
| 208 Future f = c.first; |
| 209 f.then(expectAsync1((v) { Expect.equals(7, v);})); |
| 210 sentEvents.replay(c); |
| 211 }); |
| 212 |
| 213 test("first empty", () { |
| 214 StreamController c = new StreamController(); |
| 215 Future f = c.first; |
| 216 f.catchError(expectAsync1((e) { Expect.isTrue(e.error is StateError); })); |
| 217 Events emptyEvents = new Events()..close(); |
| 218 emptyEvents.replay(c); |
| 219 }); |
| 220 |
| 221 test("first error", () { |
| 222 StreamController c = new StreamController(); |
| 223 Future f = c.first; |
| 224 f.catchError(expectAsync1((e) { Expect.equals("error", e.error); })); |
| 225 Events errorEvents = new Events()..error("error")..close(); |
| 226 errorEvents.replay(c); |
| 227 }); |
| 228 |
| 229 test("first error 2", () { |
| 230 StreamController c = new StreamController(); |
| 231 Future f = c.first; |
| 232 f.catchError(expectAsync1((e) { Expect.equals("error", e.error); })); |
| 233 Events errorEvents = new Events()..error("error")..error("error2")..close(); |
| 234 errorEvents.replay(c); |
| 235 }); |
| 236 |
| 237 test("last", () { |
| 238 StreamController c = new StreamController(); |
| 239 Future f = c.last; |
| 240 f.then(expectAsync1((v) { Expect.equals(87, v);})); |
| 241 sentEvents.replay(c); |
| 242 }); |
| 243 |
| 244 test("last empty", () { |
| 245 StreamController c = new StreamController(); |
| 246 Future f = c.last; |
| 247 f.catchError(expectAsync1((e) { Expect.isTrue(e.error is StateError); })); |
| 248 Events emptyEvents = new Events()..close(); |
| 249 emptyEvents.replay(c); |
| 250 }); |
| 251 |
| 252 test("last error", () { |
| 253 StreamController c = new StreamController(); |
| 254 Future f = c.last; |
| 255 f.catchError(expectAsync1((e) { Expect.equals("error", e.error); })); |
| 256 Events errorEvents = new Events()..error("error")..close(); |
| 257 errorEvents.replay(c); |
| 258 }); |
| 259 |
| 260 test("last error 2", () { |
| 261 StreamController c = new StreamController(); |
| 262 Future f = c.last; |
| 263 f.catchError(expectAsync1((e) { Expect.equals("error", e.error); })); |
| 264 Events errorEvents = new Events()..error("error")..error("error2")..close(); |
| 265 errorEvents.replay(c); |
| 266 }); |
| 267 |
| 268 test("elementAt", () { |
| 269 StreamController c = new StreamController(); |
| 270 Future f = c.elementAt(2); |
| 271 f.then(expectAsync1((v) { Expect.equals(13, v);})); |
| 272 sentEvents.replay(c); |
| 273 }); |
| 274 |
| 275 test("elementAt 2", () { |
| 276 StreamController c = new StreamController(); |
| 277 Future f = c.elementAt(20); |
| 278 f.catchError(expectAsync1((e) { Expect.isTrue(e.error is StateError); })); |
| 279 sentEvents.replay(c); |
| 280 }); |
| 281 } |
| 282 |
| 283 testPause() { |
| 284 test("pause event-unpause", () { |
| 285 StreamController c = new StreamController(); |
| 286 Events actualEvents = new Events.capture(c); |
| 287 Events expectedEvents = new Events(); |
| 288 expectedEvents.add(42); |
| 289 c.add(42); |
| 290 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 291 SignalCompleter completer = new SignalCompleter(); |
| 292 actualEvents.pause(completer.signal); |
| 293 c..add(43)..add(44)..close(); |
| 294 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 295 completer.complete(); |
| 296 expectedEvents..add(43)..add(44)..close(); |
| 297 actualEvents.onDone(expectAsync0(() { |
| 298 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 299 })); |
| 300 }); |
| 301 |
| 302 test("pause twice event-unpause", () { |
| 303 StreamController c = new StreamController(); |
| 304 Events actualEvents = new Events.capture(c); |
| 305 Events expectedEvents = new Events(); |
| 306 expectedEvents.add(42); |
| 307 c.add(42); |
| 308 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 309 SignalCompleter completer = new SignalCompleter(); |
| 310 SignalCompleter completer2 = new SignalCompleter(); |
| 311 actualEvents.pause(completer.signal); |
| 312 actualEvents.pause(completer2.signal); |
| 313 c..add(43)..add(44)..close(); |
| 314 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 315 completer.complete(); |
| 316 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 317 completer2.complete(); |
| 318 expectedEvents..add(43)..add(44)..close(); |
| 319 actualEvents.onDone(expectAsync0((){ |
| 320 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 321 })); |
| 322 }); |
| 323 |
| 324 test("pause twice direct-unpause", () { |
| 325 StreamController c = new StreamController(); |
| 326 Events actualEvents = new Events.capture(c); |
| 327 Events expectedEvents = new Events(); |
| 328 expectedEvents.add(42); |
| 329 c.add(42); |
| 330 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 331 actualEvents.pause(); |
| 332 actualEvents.pause(); |
| 333 c.add(43); |
| 334 c.add(44); |
| 335 c.close(); |
| 336 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 337 actualEvents.resume(); |
| 338 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 339 expectedEvents..add(43)..add(44)..close(); |
| 340 actualEvents.onDone(expectAsync0(() { |
| 341 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 342 })); |
| 343 actualEvents.resume(); |
| 344 }); |
| 345 |
| 346 test("pause twice direct-event-unpause", () { |
| 347 StreamController c = new StreamController(); |
| 348 Events actualEvents = new Events.capture(c); |
| 349 Events expectedEvents = new Events(); |
| 350 expectedEvents.add(42); |
| 351 c.add(42); |
| 352 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 353 SignalCompleter completer = new SignalCompleter(); |
| 354 actualEvents.pause(completer.signal); |
| 355 actualEvents.pause(); |
| 356 c.add(43); |
| 357 c.add(44); |
| 358 c.close(); |
| 359 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 360 actualEvents.resume(); |
| 361 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 362 expectedEvents..add(43)..add(44)..close(); |
| 363 actualEvents.onDone(expectAsync0(() { |
| 364 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 365 })); |
| 366 completer.complete(); |
| 367 }); |
| 368 |
| 369 test("pause twice direct-unpause", () { |
| 370 StreamController c = new StreamController(); |
| 371 Events actualEvents = new Events.capture(c); |
| 372 Events expectedEvents = new Events(); |
| 373 expectedEvents.add(42); |
| 374 c.add(42); |
| 375 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 376 SignalCompleter completer = new SignalCompleter(); |
| 377 actualEvents.pause(completer.signal); |
| 378 actualEvents.pause(); |
| 379 c.add(43); |
| 380 c.add(44); |
| 381 c.close(); |
| 382 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 383 completer.complete(); |
| 384 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 385 expectedEvents..add(43)..add(44)..close(); |
| 386 actualEvents.onDone(expectAsync0(() { |
| 387 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 388 })); |
| 389 actualEvents.resume(); |
| 390 }); |
| 391 } |
| 392 |
| 393 main() { |
| 394 testController(); |
| 395 testSingleController(); |
| 396 testExtraMethods(); |
| 397 testPause(); |
| 398 } |
OLD | NEW |