dart-sdk/tests/lib/async/zone_run_test.dart
Johnni Winther 62f84880ef [cfe] Report error on missing return from non-nullable function
Closes #40520
Closes #40948
Closes #40425

Change-Id: I0aa3cfa51b410c90dd0bea963846eeb6b2e73efb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140540
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
2020-03-24 08:21:36 +00:00

95 lines
2.5 KiB
Dart

// Copyright (c) 2013, 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 'package:expect/expect.dart';
import 'package:async_helper/async_helper.dart';
import 'dart:async';
main() {
Completer done = new Completer();
List events = [];
bool shouldForward = true;
Expect.identical(Zone.root, Zone.current);
Zone forked = Zone.current.fork(specification: new ZoneSpecification(
run: <R>(Zone self, ZoneDelegate parent, Zone origin, R f()) {
// The zone is still the same as when origin.run was invoked, which
// is the root zone. (The origin zone hasn't been set yet).
Expect.identical(Zone.root, Zone.current);
events.add("forked.run");
if (shouldForward) return parent.run(origin, f);
return 42 as R;
}));
events.add("zone forked");
Zone expectedZone = forked;
var result = forked.run(() {
Expect.identical(expectedZone, Zone.current);
events.add("run closure");
return 499;
});
Expect.equals(499, result);
events.add("executed run");
shouldForward = false;
result = forked.run(() {
Expect.fail("should not be invoked");
return -1;
});
Expect.equals(42, result);
events.add("executed run2");
asyncStart();
shouldForward = true;
result = forked.run(() {
Expect.identical(forked, Zone.current);
events.add("run closure 2");
forked.scheduleMicrotask(() {
events.add("run closure 3");
Expect.identical(forked, Zone.current);
done.complete(true);
});
return 1234;
});
events.add("after nested scheduleMicrotask");
Expect.equals(1234, result);
done.future.whenComplete(() {
Expect.listEquals([
"zone forked",
"forked.run",
"run closure",
"executed run",
"forked.run",
"executed run2",
"forked.run",
"run closure 2",
"after nested scheduleMicrotask",
"forked.run",
"run closure 3"
], events);
asyncEnd();
});
var zone1 = Zone.root.fork();
var zone2 = Zone.root.fork();
var zone3 = Zone.root.fork();
asyncStart();
asyncStart();
zone1.run(() {
var future = new Future.value();
zone2.run(() {
future.then((_) {
Expect.identical(zone2, Zone.current);
asyncEnd();
});
});
zone3.run(() {
future.then((_) {
Expect.identical(zone3, Zone.current);
asyncEnd();
});
});
});
}