dart-sdk/tests/language_2/await_for_cancel_test.dart
Jaime Wren 33ee69b3e4 Migrated test block 42 to Dart 2.0
The test change of '.nooooo()' -> '.field' mitigates a bunch of status file changes that would be required.  Since this is an await, and not a strong mode/ ddc/ resolution test, this test was modified in this way.

Change-Id: I54bb9ff51c2e86dd61eb41fdfc19c93bbdf22f7a
Reviewed-on: https://dart-review.googlesource.com/4142
Commit-Queue: Jaime Wren <jwren@google.com>
Reviewed-by: Janice Collins <jcollins@google.com>
2017-09-12 21:14:41 +00:00

94 lines
1.8 KiB
Dart

// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "dart:async";
import "package:expect/expect.dart";
import "package:async_helper/async_helper.dart";
bool canceled;
test1() async {
canceled = false;
try {
StreamController controller = infiniteStreamController();
outer:
while (true) {
await for (var x in controller.stream) {
for (int j = 0; j < 10; j++) {
if (j == 5) break outer;
}
}
}
} finally {
Expect.isTrue(canceled);
}
}
test2() async {
canceled = false;
try {
StreamController controller = infiniteStreamController();
bool first = true;
outer:
while (true) {
if (first) {
first = false;
} else {
break;
}
await for (var x in controller.stream) {
for (int j = 0; j < 10; j++) {
if (j == 5) continue outer;
}
}
}
} finally {
Expect.isTrue(canceled);
}
}
test() async {
await test1();
await test2();
}
main() {
asyncStart();
test().then((_) {
asyncEnd();
});
}
// Create a stream that produces numbers [1, 2, ... ]
StreamController infiniteStreamController() {
StreamController controller;
Timer timer;
int counter = 0;
void tick() {
if (controller.isPaused) {
return;
}
if (canceled) {
return;
}
counter++;
controller.add(counter); // Ask stream to send counter values as event.
Timer.run(tick);
}
void startTimer() {
Timer.run(tick);
}
controller = new StreamController(
onListen: startTimer,
onResume: startTimer,
onCancel: () {
canceled = true;
});
return controller;
}