Files
sdk/tests/language/asyncstar_yieldstar_test.dart
T
Jacob Richman 2dcd56ef43 Format all tests.
There are far too many files here to review everyone carefully.
Spot checking most of the diffs look good as test code is generally written
with less care than application code so lots of ugly formatting get through.
If people notice files where the automated formatting bothers them feel free
to comment indicating file names and I'll move spaces within comments to make
the formatting cleaner and use comments to force block formatting as I have
done for other case where formatting looked bad.

BUG=
R=efortuna@google.com

Review-Url: https://codereview.chromium.org/2771453003 .
2017-04-17 14:53:02 -07:00

100 lines
2.6 KiB
Dart

// Copyright (c) 2015, 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";
Stream<int> subStream(p) async* {
yield p;
yield p + 1;
}
Stream foo(Completer<bool> finalized) async* {
int i = 0;
try {
while (true) {
yield "outer";
yield* subStream(i);
i++;
}
} finally {
// See that we did not run too many iterations.
Expect.isTrue(i < 10);
// Canceling the stream-subscription should run the finalizer.
finalized.complete(true);
}
}
foo2(Stream subStream) async* {
yield* subStream;
}
test() async {
Expect.listEquals([0, 1], await (subStream(0).toList()));
Completer<bool> finalized = new Completer<bool>();
Expect.listEquals(["outer", 0, 1, "outer", 1, 2, "outer", 2],
await (foo(finalized).take(8).toList()));
Expect.isTrue(await (finalized.future));
finalized = new Completer<bool>();
// Canceling the stream while it is yield*-ing from the sub-stream.
Expect.listEquals(["outer", 0, 1, "outer", 1, 2, "outer"],
await (foo(finalized).take(7).toList()));
Expect.isTrue(await (finalized.future));
finalized = new Completer<bool>();
Completer<bool> pausedCompleter = new Completer<bool>();
Completer<bool> resumedCompleter = new Completer<bool>();
Completer<bool> canceledCompleter = new Completer<bool>();
StreamController controller;
int i = 0;
addNext() {
if (i >= 10) return;
controller.add(i);
i++;
if (!controller.isPaused) {
scheduleMicrotask(addNext);
}
}
controller = new StreamController(onListen: () {
scheduleMicrotask(addNext);
}, onPause: () {
pausedCompleter.complete(true);
}, onResume: () {
resumedCompleter.complete(true);
scheduleMicrotask(addNext);
}, onCancel: () {
canceledCompleter.complete(true);
});
StreamSubscription subscription;
// Test that the yield*'ed stream is paused and resumed.
subscription = foo2(controller.stream).listen((event) {
if (event == 2) {
subscription.pause();
scheduleMicrotask(() {
subscription.resume();
});
}
if (event == 5) {
subscription.cancel();
}
});
// Test that the yield*'ed streamSubscription is paused, resumed and canceled
// by the async* stream.
Expect.isTrue(await pausedCompleter.future);
Expect.isTrue(await resumedCompleter.future);
Expect.isTrue(await canceledCompleter.future);
}
main() {
asyncStart();
test().then((_) {
asyncEnd();
});
}