OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 library async_start_test; |
| 6 |
| 7 import "package:unittest/unittest.dart"; |
| 8 import "dart:async"; |
| 9 |
| 10 main() { |
| 11 group("basic", () { |
| 12 test("empty", () { |
| 13 f() async* {} |
| 14 return f().toList().then((v) { |
| 15 expect(v, equals([])); |
| 16 }); |
| 17 }); |
| 18 |
| 19 test("single", () { |
| 20 f() async* { yield 42; } |
| 21 return f().toList().then((v) { |
| 22 expect(v, equals([42])); |
| 23 }); |
| 24 }); |
| 25 |
| 26 test("call delays", () { |
| 27 var list = []; |
| 28 f() async* { list.add(1); yield 2; } |
| 29 // TODO(jmesserly): use tear off. For now this is a workaround for: |
| 30 // https://github.com/dart-lang/dev_compiler/issues/269 |
| 31 var res = f().forEach((x) => list.add(x)); |
| 32 list.add(0); |
| 33 return res.whenComplete(() { |
| 34 expect(list, equals([0, 1, 2])); |
| 35 }); |
| 36 }); |
| 37 |
| 38 test("throws", () { |
| 39 f() async* { yield 1; throw 2; } |
| 40 var completer = new Completer(); |
| 41 var list = []; |
| 42 // TODO(jmesserly): use tear off. For now this is a workaround for: |
| 43 // https://github.com/dart-lang/dev_compiler/issues/269 |
| 44 f().listen((x) => list.add(x), |
| 45 onError: (v) => list.add("$v"), |
| 46 onDone: completer.complete); |
| 47 return completer.future.whenComplete(() { |
| 48 expect(list, equals([1, "2"])); |
| 49 }); |
| 50 }); |
| 51 |
| 52 test("multiple", () { |
| 53 f() async* { |
| 54 for (int i = 0; i < 10; i++) { |
| 55 yield i; |
| 56 } |
| 57 } |
| 58 return expectList(f(), new List.generate(10, id)); |
| 59 }); |
| 60 |
| 61 test("allows await", () { |
| 62 f() async* { |
| 63 var x = await new Future.value(42); |
| 64 yield x; |
| 65 x = await new Future.value(42); |
| 66 } |
| 67 return expectList(f(), [42]); |
| 68 }); |
| 69 |
| 70 test("allows await in loop", () { |
| 71 f() async* { |
| 72 for (int i = 0; i < 10; i++) { |
| 73 yield await i; |
| 74 } |
| 75 } |
| 76 return expectList(f(), new List.generate(10, id)); |
| 77 }); |
| 78 |
| 79 test("allows yield*", () { |
| 80 f() async* { |
| 81 yield* new Stream.fromIterable([1, 2, 3]); |
| 82 } |
| 83 return expectList(f(), [1, 2, 3]); |
| 84 }); |
| 85 |
| 86 test("allows yield* of async*", () { |
| 87 f(n) async* { |
| 88 yield n; |
| 89 if (n == 0) return; |
| 90 yield* f(n - 1); |
| 91 yield n; |
| 92 } |
| 93 return expectList(f(3), [3, 2, 1, 0, 1, 2, 3]); |
| 94 }); |
| 95 |
| 96 test("Cannot yield* non-stream", () { |
| 97 f(s) async* { |
| 98 yield* s; |
| 99 } |
| 100 return f(42).transform(getErrors).single.then((v) { |
| 101 // Not implementing Stream. |
| 102 expect(v is Error, isTrue); |
| 103 }); |
| 104 }); |
| 105 |
| 106 test("Cannot yield* non-stream", () { |
| 107 f(s) async* { |
| 108 yield* s; |
| 109 } |
| 110 return f(new NotAStream()).transform(getErrors).single.then((v) { |
| 111 // Not implementing Stream. |
| 112 expect(v is Error, isTrue); |
| 113 }); |
| 114 }); |
| 115 }); |
| 116 |
| 117 group("yield statement context", () { |
| 118 test("plain", () { |
| 119 f() async* { |
| 120 yield 0; |
| 121 } |
| 122 return expectList(f(), [0]); |
| 123 }); |
| 124 |
| 125 test("if-then-else", () { |
| 126 f(b) async* { |
| 127 if (b) yield 0; else yield 1; |
| 128 } |
| 129 return expectList(f(true), [0]).whenComplete(() { |
| 130 expectList(f(false), [1]); |
| 131 }); |
| 132 }); |
| 133 |
| 134 test("block", () { |
| 135 f() async* { |
| 136 yield 0; |
| 137 { yield 1; } |
| 138 yield 2; |
| 139 } |
| 140 return expectList(f(), [0, 1, 2]); |
| 141 }); |
| 142 |
| 143 test("labeled", () { |
| 144 f() async* { |
| 145 label1: yield 0; |
| 146 } |
| 147 return expectList(f(), [0]); |
| 148 }); |
| 149 |
| 150 // VM issue 2238 |
| 151 test("labeled 2", () { /// 01: ok |
| 152 f() async* { /// 01: continued |
| 153 label1: label2: yield 0; /// 01: continued |
| 154 } /// 01: continued |
| 155 return expectList(f(), [0]); /// 01: continued |
| 156 }); /// 01: continued |
| 157 |
| 158 test("for-loop", () { |
| 159 f() async* { |
| 160 for (int i = 0; i < 3; i++) yield i; |
| 161 } |
| 162 return expectList(f(), [0, 1, 2]); |
| 163 }); |
| 164 |
| 165 test("for-in-loop", () { |
| 166 f() async* { |
| 167 for (var i in [0, 1, 2]) yield i; |
| 168 } |
| 169 return expectList(f(), [0, 1, 2]); |
| 170 }); |
| 171 |
| 172 test("await for-in-loop", () { |
| 173 f() async* { |
| 174 await for (var i in new Stream.fromIterable([0, 1, 2])) yield i; |
| 175 } |
| 176 return expectList(f(), [0, 1, 2]); |
| 177 }); |
| 178 |
| 179 test("while-loop", () { |
| 180 f() async* { |
| 181 int i = 0; |
| 182 while (i < 3) yield i++; |
| 183 } |
| 184 return expectList(f(), [0, 1, 2]); |
| 185 }); |
| 186 |
| 187 test("do-while-loop", () { |
| 188 f() async* { |
| 189 int i = 0; |
| 190 do yield i++; while (i < 3); |
| 191 } |
| 192 return expectList(f(), [0, 1, 2]); |
| 193 }); |
| 194 |
| 195 test("try-catch-finally", () { |
| 196 f() async* { |
| 197 try { yield 0; } catch (e) { yield 1; } finally { yield 2; } |
| 198 } |
| 199 return expectList(f(), [0, 2]); |
| 200 }); |
| 201 |
| 202 test("try-catch-finally 2", () { |
| 203 f() async* { |
| 204 try { yield throw 0; } catch (e) { yield 1; } finally { yield 2; } |
| 205 } |
| 206 return expectList(f(), [1, 2]); |
| 207 }); |
| 208 |
| 209 // TODO(jmesserly): restore this when we fix |
| 210 // https://github.com/dart-lang/dev_compiler/issues/263 |
| 211 /*test("switch-case", () { |
| 212 f(v) async* { |
| 213 switch (v) { |
| 214 case 0: |
| 215 yield 0; |
| 216 continue label1; |
| 217 label1: |
| 218 case 1: |
| 219 yield 1; |
| 220 break; |
| 221 default: |
| 222 yield 2; |
| 223 } |
| 224 } |
| 225 return expectList(f(0), [0, 1]).whenComplete(() { |
| 226 return expectList(f(1), [1]); |
| 227 }).whenComplete(() { |
| 228 return expectList(f(2), [2]); |
| 229 }); |
| 230 });*/ |
| 231 |
| 232 test("dead-code return", () { |
| 233 f() async* { |
| 234 return; |
| 235 yield 1; |
| 236 } |
| 237 return expectList(f(), []); |
| 238 }); |
| 239 |
| 240 test("dead-code throw", () { |
| 241 f() async* { |
| 242 try { |
| 243 throw 0; |
| 244 yield 1; |
| 245 } catch (_) {} |
| 246 } |
| 247 return expectList(f(), []); |
| 248 }); |
| 249 |
| 250 test("dead-code break", () { |
| 251 f() async* { |
| 252 while (true) { |
| 253 break; |
| 254 yield 1; |
| 255 } |
| 256 } |
| 257 return expectList(f(), []); |
| 258 }); |
| 259 |
| 260 test("dead-code break 2", () { |
| 261 f() async* { |
| 262 label: { |
| 263 break label; |
| 264 yield 1; |
| 265 } |
| 266 } |
| 267 return expectList(f(), []); |
| 268 }); |
| 269 |
| 270 test("dead-code continue", () { |
| 271 f() async* { |
| 272 do { |
| 273 continue; |
| 274 yield 1; |
| 275 } while (false); |
| 276 } |
| 277 return expectList(f(), []); |
| 278 }); |
| 279 }); |
| 280 |
| 281 group("yield expressions", () { |
| 282 test("local variable", () { |
| 283 f() async* { |
| 284 var x = 42; |
| 285 yield x; |
| 286 } |
| 287 return expectList(f(), [42]); |
| 288 }); |
| 289 |
| 290 test("constant variable", () { |
| 291 f() async* { |
| 292 const x = 42; |
| 293 yield x; |
| 294 } |
| 295 return expectList(f(), [42]); |
| 296 }); |
| 297 |
| 298 test("function call", () { |
| 299 g() => 42; |
| 300 f() async* { |
| 301 yield g(); |
| 302 } |
| 303 return expectList(f(), [42]); |
| 304 }); |
| 305 |
| 306 test("unary operator", () { |
| 307 f() async* { |
| 308 var x = -42; |
| 309 yield -x; |
| 310 } |
| 311 return expectList(f(), [42]); |
| 312 }); |
| 313 |
| 314 test("binary operator", () { |
| 315 f() async* { |
| 316 var x = 21; |
| 317 yield x + x; |
| 318 } |
| 319 return expectList(f(), [42]); |
| 320 }); |
| 321 |
| 322 test("ternary operator", () { |
| 323 f() async* { |
| 324 var x = 21; |
| 325 yield x == 21 ? x + x : x; |
| 326 } |
| 327 return expectList(f(), [42]); |
| 328 }); |
| 329 |
| 330 test("suffix post-increment", () { |
| 331 f() async* { |
| 332 var x = 42; |
| 333 yield x++; |
| 334 } |
| 335 return expectList(f(), [42]); |
| 336 }); |
| 337 |
| 338 test("suffix pre-increment", () { |
| 339 f() async* { |
| 340 var x = 41; |
| 341 yield ++x; |
| 342 } |
| 343 return expectList(f(), [42]); |
| 344 }); |
| 345 |
| 346 test("assignment", () { |
| 347 f() async* { |
| 348 var x = 37; |
| 349 yield x = 42; |
| 350 } |
| 351 return expectList(f(), [42]); |
| 352 }); |
| 353 |
| 354 test("assignment op", () { |
| 355 f() async* { |
| 356 var x = 41; |
| 357 yield x += 1; |
| 358 } |
| 359 return expectList(f(), [42]); |
| 360 }); |
| 361 |
| 362 test("await", () { |
| 363 f() async* { |
| 364 yield await new Future.value(42); |
| 365 } |
| 366 return expectList(f(), [42]); |
| 367 }); |
| 368 |
| 369 test("index operator", () { |
| 370 f() async* { |
| 371 var x = [42]; |
| 372 yield x[0]; |
| 373 } |
| 374 return expectList(f(), [42]); |
| 375 }); |
| 376 |
| 377 test("function expression block", () { |
| 378 var o = new Object(); |
| 379 f() async* { |
| 380 yield () { return o; }; |
| 381 } |
| 382 return f().first.then((v) { |
| 383 expect(v(), same(o)); |
| 384 }); |
| 385 }); |
| 386 |
| 387 test("function expression arrow", () { |
| 388 var o = new Object(); |
| 389 f() async* { |
| 390 yield () => o; |
| 391 } |
| 392 return f().first.then((v) { |
| 393 expect(v(), same(o)); |
| 394 }); |
| 395 }); |
| 396 |
| 397 test("function expression block async", () { |
| 398 var o = new Object(); |
| 399 f() async* { |
| 400 yield () async { return o; }; |
| 401 } |
| 402 return f().first.then((v) => v()).then((v) { |
| 403 expect(v, same(o)); |
| 404 }); |
| 405 }); |
| 406 |
| 407 test("function expression arrow async", () { |
| 408 var o = new Object(); |
| 409 f() async* { |
| 410 yield () async => o; |
| 411 } |
| 412 return f().first.then((v) => v()).then((v) { |
| 413 expect(v, same(o)); |
| 414 }); |
| 415 }); |
| 416 |
| 417 test("function expression block async*", () { |
| 418 var o = new Object(); |
| 419 f() async* { |
| 420 yield () async* { yield o; }; |
| 421 } |
| 422 return f().first.then((v) => v().first).then((v) { |
| 423 expect(v, same(o)); |
| 424 }); |
| 425 }); |
| 426 }); |
| 427 |
| 428 group("loops", () { |
| 429 test("simple yield", () { |
| 430 f() async* { |
| 431 for (int i = 0; i < 3; i++) { |
| 432 yield i; |
| 433 } |
| 434 } |
| 435 return expectList(f(), [0, 1, 2]); |
| 436 }); |
| 437 |
| 438 test("yield in double loop", () { |
| 439 f() async* { |
| 440 for (int i = 0; i < 3; i++) { |
| 441 for (int j = 0; j < 2; j++) { |
| 442 yield i * 2 + j; |
| 443 } |
| 444 } |
| 445 } |
| 446 return expectList(f(), [0, 1, 2, 3, 4, 5]); |
| 447 }); |
| 448 |
| 449 test("yield in try body", () { |
| 450 var list = []; |
| 451 f() async* { |
| 452 for (int i = 0; i < 3; i++) { |
| 453 try { |
| 454 yield i; |
| 455 } finally { |
| 456 list.add("$i"); |
| 457 } |
| 458 } |
| 459 } |
| 460 return expectList(f(), [0, 1, 2]).whenComplete(() { |
| 461 expect(list, equals(["0", "1", "2"])); |
| 462 }); |
| 463 }); |
| 464 |
| 465 test("yield in catch", () { |
| 466 var list = []; |
| 467 f() async* { |
| 468 for (int i = 0; i < 3; i++) { |
| 469 try { |
| 470 throw i; |
| 471 } catch (e) { |
| 472 yield e; |
| 473 } finally { |
| 474 list.add("$i"); |
| 475 } |
| 476 } |
| 477 } |
| 478 return expectList(f(), [0, 1, 2]).whenComplete(() { |
| 479 expect(list, equals(["0", "1", "2"])); |
| 480 }); |
| 481 }); |
| 482 |
| 483 test("yield in finally", () { |
| 484 var list = []; |
| 485 f() async* { |
| 486 for (int i = 0; i < 3; i++) { |
| 487 try { |
| 488 throw i; |
| 489 } finally { |
| 490 yield i; |
| 491 list.add("$i"); |
| 492 continue; |
| 493 } |
| 494 } |
| 495 } |
| 496 return expectList(f(), [0, 1, 2]).whenComplete(() { |
| 497 expect(list, equals(["0", "1", "2"])); |
| 498 }); |
| 499 }); |
| 500 |
| 501 test("keep yielding after cancel", () { |
| 502 f() async* { |
| 503 for (int i = 0; i < 10; i++) { |
| 504 try { |
| 505 yield i; |
| 506 } finally { |
| 507 continue; |
| 508 } |
| 509 } |
| 510 } |
| 511 return expectList(f().take(3), [0, 1, 2]); |
| 512 }); |
| 513 }); |
| 514 |
| 515 group("canceling", () { |
| 516 // Stream.take(n) automatically cancels after seeing the n'th value. |
| 517 |
| 518 test("cancels at yield", () { |
| 519 Completer exits = new Completer(); |
| 520 var list = []; |
| 521 f() async* { |
| 522 try { |
| 523 list.add(0); |
| 524 yield list.add(1); |
| 525 list.add(2); |
| 526 } finally { |
| 527 exits.complete(3); |
| 528 } |
| 529 } |
| 530 // No events must be fired synchronously in response to a listen. |
| 531 var subscription = f().listen((v) { fail("Received event $v"); }, |
| 532 onDone: () { fail("Received done"); }); |
| 533 // No events must be delivered after a cancel. |
| 534 subscription.cancel(); |
| 535 return exits.future.then((v) { |
| 536 expect(v, equals(3)); |
| 537 expect(list, equals([0, 1])); |
| 538 }); |
| 539 }); |
| 540 |
| 541 test("does cancel eventually", () { |
| 542 var exits = new Completer(); |
| 543 var list = []; |
| 544 f() async* { |
| 545 int i = 0; |
| 546 try { |
| 547 while (true) yield i++; |
| 548 } finally { |
| 549 list.add("a"); |
| 550 exits.complete(i); |
| 551 } |
| 552 } |
| 553 return expectList(f().take(5), [0, 1, 2, 3, 4]) |
| 554 .then((_) => exits.future) |
| 555 .then((v) { |
| 556 expect(v, greaterThan(4)); |
| 557 expect(list, ["a"]); |
| 558 }); |
| 559 }); |
| 560 |
| 561 group("at index", () { |
| 562 f() async* { |
| 563 try { |
| 564 yield await new Future.microtask(() => 1); |
| 565 } finally { |
| 566 try { |
| 567 yield await new Future.microtask(() => 2); |
| 568 } finally { |
| 569 yield await new Future.microtask(() => 3); |
| 570 } |
| 571 } |
| 572 } |
| 573 test("- all, sanity check", () { |
| 574 return expectList(f(), [1, 2, 3]); |
| 575 }); |
| 576 test("after end", () { |
| 577 return expectList(f().take(4), [1, 2, 3]); |
| 578 }); |
| 579 test("at end", () { |
| 580 return expectList(f().take(3), [1, 2, 3]); |
| 581 }); |
| 582 test("before end", () { |
| 583 return expectList(f().take(2), [1, 2]); |
| 584 }); |
| 585 test("early", () { |
| 586 return expectList(f().take(1), [1]); |
| 587 }); |
| 588 test("at start", () { |
| 589 return expectList(f().take(0), []); |
| 590 }); |
| 591 }); |
| 592 |
| 593 // Crashes dart2js. |
| 594 // test("regression-fugl/fisk", () { |
| 595 // var res = []; |
| 596 // fisk() async* { |
| 597 // res.add("+fisk"); |
| 598 // try { |
| 599 // for (int i = 0; i < 2; i++) { |
| 600 // yield await new Future.microtask(() => i); |
| 601 // } |
| 602 // } finally { |
| 603 // res.add("-fisk"); |
| 604 // } |
| 605 // } |
| 606 |
| 607 // fugl(int count) async { |
| 608 // res.add("fisk $count"); |
| 609 // try { |
| 610 // await for(int i in fisk().take(count)) res.add(i); |
| 611 // } finally { |
| 612 // res.add("done"); |
| 613 // } |
| 614 // } |
| 615 |
| 616 // return fugl(3).whenComplete(() => fugl(2)) |
| 617 // .whenComplete(() => fugl(1)) |
| 618 // .whenComplete(() { |
| 619 // expect(res, ["fisk 3", "+fisk", 0, 1, "-fisk", "done", |
| 620 // "fisk 2", "+fisk", 0, 1, "-fisk", "done", |
| 621 // "fisk 1", "+fisk", 0, "done", "-fisk", ]); |
| 622 // }); |
| 623 // }); |
| 624 |
| 625 }); |
| 626 |
| 627 group("pausing", () { |
| 628 test("pauses execution at yield for at least a microtask", () { |
| 629 var list = []; |
| 630 f() async* { |
| 631 list.add(1); |
| 632 yield 2; |
| 633 list.add(3); |
| 634 yield 4; |
| 635 list.add(5); |
| 636 } |
| 637 var done = new Completer(); |
| 638 var sub = f().listen((v) { |
| 639 if (v == 2) { |
| 640 expect(list, equals([1])); |
| 641 } else if (v == 4) { |
| 642 expect(list, equals([1, 3])); |
| 643 } else { |
| 644 fail("Unexpected value $v"); |
| 645 } |
| 646 }, onDone: () { |
| 647 expect(list, equals([1, 3, 5])); |
| 648 done.complete(); |
| 649 }); |
| 650 return done.future; |
| 651 }); |
| 652 |
| 653 test("pause stops execution at yield", () { |
| 654 var list = []; |
| 655 f() async* { |
| 656 list.add(1); |
| 657 yield 2; |
| 658 list.add(3); |
| 659 yield 4; |
| 660 list.add(5); |
| 661 } |
| 662 var done = new Completer(); |
| 663 var sub; |
| 664 sub = f().listen((v) { |
| 665 if (v == 2) { |
| 666 expect(list, equals([1])); |
| 667 sub.pause(); |
| 668 new Timer(MS * 300, () { |
| 669 expect(list.length, lessThan(3)); |
| 670 sub.resume(); |
| 671 }); |
| 672 } else if (v == 4) { |
| 673 expect(list, equals([1, 3])); |
| 674 } else { |
| 675 fail("Unexpected value $v"); |
| 676 } |
| 677 }, onDone: () { |
| 678 expect(list, equals([1, 3, 5])); |
| 679 done.complete(); |
| 680 }); |
| 681 return done.future; |
| 682 }); |
| 683 |
| 684 test("pause stops execution at yield 2", () { |
| 685 var list = []; |
| 686 f() async* { |
| 687 int i = 0; |
| 688 while (true) { |
| 689 yield i; |
| 690 list.add(i); |
| 691 i++; |
| 692 } |
| 693 } |
| 694 int expected = 0; |
| 695 var done = new Completer(); |
| 696 var sub; |
| 697 sub = f().listen((v) { |
| 698 expect(v, equals(expected++)); |
| 699 if (v % 5 == 0) { |
| 700 sub.pause(new Future.delayed(MS * 300)); |
| 701 } else if (v == 17) { |
| 702 sub.cancel(); |
| 703 done.complete(); |
| 704 } |
| 705 }, onDone: () { |
| 706 fail("Unexpected done!"); |
| 707 }); |
| 708 return done.future.whenComplete(() { |
| 709 expect(list.length == 18 || list.length == 19, isTrue); |
| 710 }); |
| 711 }); |
| 712 |
| 713 test("canceling while paused at yield", () { /// 02: ok |
| 714 var list = []; /// 02: contin
ued |
| 715 var sync = new Sync(); /// 02: contin
ued |
| 716 f() async* { /// 02: contin
ued |
| 717 list.add("*1"); /// 02: contin
ued |
| 718 yield 1; /// 02: contin
ued |
| 719 await sync.wait(); /// 02: contin
ued |
| 720 sync.release(); /// 02: contin
ued |
| 721 list.add("*2"); /// 02: contin
ued |
| 722 yield 2; /// 02: contin
ued |
| 723 list.add("*3"); /// 02: contin
ued |
| 724 }; /// 02: contin
ued |
| 725 var stream = f(); /// 02: contin
ued |
| 726 // TODO(jmesserly): added workaround for: |
| 727 // https://github.com/dart-lang/dev_compiler/issues/269 |
| 728 var sub = stream.listen((x) => list.add(x)); ///
02: continued |
| 729 return sync.wait().whenComplete(() { /// 02: contin
ued |
| 730 expect(list, equals(["*1", 1])); /// 02: contin
ued |
| 731 sub.pause(); /// 02: contin
ued |
| 732 return sync.wait(); /// 02: contin
ued |
| 733 }).whenComplete(() { /// 02: contin
ued |
| 734 expect(list, equals(["*1", 1, "*2"])); /// 02: contin
ued |
| 735 sub.cancel(); /// 02: contin
ued |
| 736 return new Future.delayed(MS * 200, () { /// 02: contin
ued |
| 737 // Should not have yielded 2 or added *3 while paused. /// 02: contin
ued |
| 738 expect(list, equals(["*1", 1, "*2"])); /// 02: contin
ued |
| 739 }); /// 02: contin
ued |
| 740 }); /// 02: contin
ued |
| 741 }, skip: "other impls aren't passing this test, see " |
| 742 "https://github.com/dart-lang/sdk/issues/22853");
/// 02: continued |
| 743 }); |
| 744 |
| 745 group("await for", () { |
| 746 mkStream(int n) async* { |
| 747 for (int i = 0; i < n; i++) yield i; |
| 748 } |
| 749 |
| 750 test("simple stream", () { |
| 751 f(s) async { |
| 752 var r = 0; |
| 753 await for(var v in s) r += v; |
| 754 return r; |
| 755 } |
| 756 return f(mkStream(5)).then((v) { |
| 757 expect(v, equals(10)); |
| 758 }); |
| 759 }); |
| 760 |
| 761 test("simple stream, await", () { |
| 762 f(s) async { |
| 763 var r = 0; |
| 764 await for(var v in s) r += await new Future.microtask(() => v); |
| 765 return r; |
| 766 } |
| 767 return f(mkStream(5)).then((v) { |
| 768 expect(v, equals(10)); |
| 769 }); |
| 770 }); |
| 771 |
| 772 test("simple stream - take", () { /// 03: ok |
| 773 f(s) async { /// 03: continued |
| 774 var r = 0; /// 03: continued |
| 775 await for(var v in s.take(5)) r += v; /// 03: continued |
| 776 return r; /// 03: continued |
| 777 } /// 03: continued |
| 778 return f(mkStream(10)).then((v) { /// 03: continued |
| 779 expect(v, equals(10)); /// 03: continued |
| 780 }); /// 03: continued |
| 781 }); /// 03: continued |
| 782 |
| 783 test("simple stream reyield", () { |
| 784 f(s) async* { |
| 785 var r = 0; |
| 786 await for(var v in s) yield r += v; |
| 787 } |
| 788 return expectList(f(mkStream(5)), [0, 1, 3, 6, 10]); |
| 789 }); |
| 790 |
| 791 test("simple stream, await, reyield", () { |
| 792 f(s) async* { |
| 793 var r = 0; |
| 794 await for(var v in s) yield r += await new Future.microtask(() => v); |
| 795 } |
| 796 return expectList(f(mkStream(5)), [0, 1, 3, 6, 10]); |
| 797 }); |
| 798 |
| 799 test("simple stream - take, reyield", () { /// 04: ok |
| 800 f(s) async* { /// 04: continued |
| 801 var r = 0; /// 04: continued |
| 802 await for(var v in s.take(5)) yield r += v; /// 04: continued |
| 803 } /// 04: continued |
| 804 return expectList(f(mkStream(10)), [0, 1, 3, 6, 10]); /// 04: continued |
| 805 }); /// 04: continued |
| 806 |
| 807 test("nested", () { |
| 808 f() async { |
| 809 var r = 0; |
| 810 await for (var i in mkStream(5)) { |
| 811 await for (var j in mkStream(3)) { |
| 812 r += i * j; |
| 813 } |
| 814 } |
| 815 return r; |
| 816 } |
| 817 return f().then((v) { |
| 818 expect(v, equals((1 + 2 + 3 + 4) * (1 + 2))); |
| 819 }); |
| 820 }); |
| 821 |
| 822 test("nested, await", () { |
| 823 f() async { |
| 824 var r = 0; |
| 825 await for (var i in mkStream(5)) { |
| 826 await for (var j in mkStream(3)) { |
| 827 r += await new Future.microtask(() => i * j); |
| 828 } |
| 829 } |
| 830 return r; |
| 831 } |
| 832 return f().then((v) { |
| 833 expect(v, equals((1 + 2 + 3 + 4) * (1 + 2))); |
| 834 }); |
| 835 }); |
| 836 |
| 837 test("nested, await * 2", () { |
| 838 f() async { |
| 839 var r = 0; |
| 840 await for (var i in mkStream(5)) { |
| 841 var ai = await new Future.microtask(() => i); |
| 842 await for (var j in mkStream(3)) { |
| 843 r += await new Future.microtask(() => ai * j); |
| 844 } |
| 845 } |
| 846 return r; |
| 847 } |
| 848 return f().then((v) { |
| 849 expect(v, equals((1 + 2 + 3 + 4) * (1 + 2))); |
| 850 }); |
| 851 }); |
| 852 |
| 853 test("await pauses loop", () { /// 05: ok |
| 854 var sc; /// 05: con
tinued |
| 855 var i = 0; /// 05: con
tinued |
| 856 void send() { /// 05: con
tinued |
| 857 if (i == 5) { /// 05: con
tinued |
| 858 sc.close(); /// 05: con
tinued |
| 859 } else { /// 05: con
tinued |
| 860 sc.add(i++); /// 05: con
tinued |
| 861 } /// 05: con
tinued |
| 862 } /// 05: con
tinued |
| 863 sc = new StreamController(onListen: send, onResume: send); /// 05: con
tinued |
| 864 f(s) async { /// 05: con
tinued |
| 865 var r = 0; /// 05: con
tinued |
| 866 await for (var i in s) { /// 05: con
tinued |
| 867 r += await new Future.delayed(MS * 10, () => i); /// 05: con
tinued |
| 868 } /// 05: con
tinued |
| 869 return r; /// 05: con
tinued |
| 870 } /// 05: con
tinued |
| 871 return f(sc.stream).then((v) { /// 05: con
tinued |
| 872 expect(v, equals(10)); /// 05: con
tinued |
| 873 }); /// 05: con
tinued |
| 874 }, skip: "other impls aren't passing this test, see " |
| 875 "https://github.com/dart-lang/sdk/issues/22853");
/// 05: continued |
| 876 }); |
| 877 } |
| 878 |
| 879 // Obscuring identity function. |
| 880 id(x) { |
| 881 try { |
| 882 if (x != null) throw x; |
| 883 } catch (e) { |
| 884 return e; |
| 885 } |
| 886 return null; |
| 887 } |
| 888 |
| 889 expectList(stream, list) { |
| 890 return stream.toList().then((v) { |
| 891 expect(v, equals(list)); |
| 892 }); |
| 893 } |
| 894 |
| 895 const MS = const Duration(milliseconds: 1); |
| 896 |
| 897 var getErrors = new StreamTransformer.fromHandlers( |
| 898 handleData:(data, sink) { fail("Unexpected value"); }, |
| 899 handleError: (e, s, sink) { sink.add(e); }, |
| 900 handleDone: (sink) { sink.close(); }); |
| 901 |
| 902 class NotAStream { |
| 903 listen(oData, {onError, onDone, cancelOnError}) { |
| 904 fail("Not implementing Stream."); |
| 905 } |
| 906 } |
| 907 |
| 908 /** |
| 909 * Allows two asynchronous executions to synchronize. |
| 910 * |
| 911 * Calling [wait] and waiting for the returned future to complete will |
| 912 * wait for the other executions to call [wait] again. At that point, |
| 913 * the waiting execution is allowed to continue (the returned future completes), |
| 914 * and the more resent call to [wait] is now the waiting execution. |
| 915 */ |
| 916 class Sync { |
| 917 Completer _completer = null; |
| 918 // Release whoever is currently waiting and start waiting yourself. |
| 919 Future wait([v]) { |
| 920 if (_completer != null) _completer.complete(v); |
| 921 _completer = new Completer(); |
| 922 return _completer.future; |
| 923 } |
| 924 |
| 925 // Release whoever is currently waiting. |
| 926 void release([v]) { |
| 927 if (_completer != null) { |
| 928 _completer.complete(v); |
| 929 _completer = null; |
| 930 } |
| 931 } |
| 932 } |
OLD | NEW |