dart-sdk/tests/language/async_star/cancel_while_paused_test.dart
Robert Nystrom b81f12a549 Migrate language_2/async_star to NNBD.
Change-Id: I0fa2ee2561dd0bc399d88868d790d56e6df3c94d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134940
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
2020-02-21 00:22:06 +00:00

67 lines
1.7 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.
// This is a regression test for issue 22853.
import "dart:async";
import "package:expect/expect.dart";
import "package:async_helper/async_helper.dart";
main() {
var list = [];
var sync = new Sync();
f() async* {
list.add("*1");
yield 1;
await sync.wait();
sync.release();
list.add("*2");
yield 2;
list.add("*3");
}
;
var stream = f();
var sub = stream.listen(list.add);
asyncStart();
return sync.wait().whenComplete(() {
Expect.listEquals(["*1", 1], list);
sub.pause();
return sync.wait();
}).whenComplete(() {
Expect.listEquals(["*1", 1, "*2"], list);
sub.cancel();
new Future.delayed(new Duration(milliseconds: 200), () {
// Should not have yielded 2 or added *3 while paused.
Expect.listEquals(["*1", 1, "*2"], list);
asyncEnd();
});
});
}
/**
* Allows two asynchronous executions to synchronize.
*
* Calling [wait] and waiting for the returned future to complete will
* wait for the other executions to call [wait] again. At that point,
* the waiting execution is allowed to continue (the returned future completes),
* and the more recent call to [wait] is now the waiting execution.
*/
class Sync {
Completer? _completer = null;
// Release whoever is currently waiting and start waiting yourself.
Future wait([v]) {
_completer?.complete(v);
_completer = new Completer();
return _completer!.future;
}
// Release whoever is currently waiting.
void release([v]) {
_completer?.complete(v);
_completer = null;
}
}