Skip to content

Commit 659978f

Browse files
lrhnCommit Queue
authored andcommitted
Convert multitests in core platform library tests.
A little extra clean-up in related files. Change-Id: I1d0d809d1ab4d756470e3a292e8e070ef31304af Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402660 Commit-Queue: Lasse Nielsen <[email protected]> Reviewed-by: Bob Nystrom <[email protected]> Reviewed-by: Nate Bosch <[email protected]>
1 parent 256df61 commit 659978f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+324
-313
lines changed

tests/lib/async/catch_errors.dart

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,36 @@ library catch_errors;
66

77
import 'dart:async';
88

9-
Stream catchErrors(dynamic body()) {
10-
late StreamController controller;
11-
12-
bool onError(e, st) {
13-
controller.add(e);
14-
return true;
15-
}
16-
17-
void onListen() {
18-
runZonedGuarded(body, onError);
19-
}
20-
21-
controller = new StreamController(onListen: onListen);
9+
/// Runs [body] inside [runZonedGuarded].
10+
///
11+
/// Runs [body] when the returned stream is listened to.
12+
/// Emits all errors, synchronous and asynchronous,
13+
/// as events on the returned stream.
14+
///
15+
/// **Notice**: The stream never closes. The caller should stop
16+
/// listening when they're convinced there will be no later error events.
17+
Stream<Object> catchErrors(void Function() body) {
18+
var controller = StreamController<Object>();
19+
controller.onListen = () {
20+
runZonedGuarded(body, (e, s) {
21+
controller.add(e);
22+
});
23+
};
2224
return controller.stream;
2325
}
2426

25-
runZonedScheduleMicrotask(
26-
body(), {
27-
void onScheduleMicrotask(void callback())?,
27+
/// Runs [body] inside [runZoneGuarded], [Zone.runGuarded] or [Zone.run].
28+
///
29+
/// If [onScheduleMicrotask] is provided, a custom zone is created
30+
/// with a [Zone.scheduleMicrotask] which calls [onScheduleMicrotask] with
31+
/// the provided callback.
32+
///
33+
/// If [onError] is provided, it's used as argument to [runZonedGuarded]
34+
/// or as error handler of the custom zone, and the `body` is then run
35+
/// using `runGuarded`.
36+
R? runZonedScheduleMicrotask<R>(
37+
R body(), {
38+
void Function(void Function() callback)? onScheduleMicrotask,
2839
Function? onError,
2940
}) {
3041
if (onScheduleMicrotask == null) {
@@ -56,18 +67,20 @@ runZonedScheduleMicrotask(
5667
}
5768
ScheduleMicrotaskHandler? asyncHandler;
5869
if (onScheduleMicrotask != null) {
59-
asyncHandler = (Zone self, ZoneDelegate parent, Zone zone, f()) {
70+
void handle(Zone self, ZoneDelegate parent, Zone zone, Function() f) {
6071
self.parent!.runUnary(onScheduleMicrotask, () => zone.runGuarded(f));
61-
};
72+
}
73+
74+
asyncHandler = handle;
6275
}
63-
ZoneSpecification specification = new ZoneSpecification(
76+
ZoneSpecification specification = ZoneSpecification(
6477
handleUncaughtError: errorHandler,
6578
scheduleMicrotask: asyncHandler,
6679
);
67-
Zone zone = Zone.current.fork(specification: specification);
6880
if (onError != null) {
69-
return zone.runGuarded(body);
81+
return runZoned<R>(body, zoneSpecification: specification);
7082
} else {
71-
return zone.run(body);
83+
Zone zone = Zone.current.fork(specification: specification);
84+
return zone.run<R>(body);
7285
}
7386
}

tests/lib/async/catch_errors11_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import 'package:expect/expect.dart';
99

1010
import 'catch_errors.dart';
1111

12-
main() {
12+
void main() {
1313
asyncStart();
14-
Completer done = new Completer();
14+
Completer done = Completer();
1515

1616
var events = [];
1717
// Test that `catchErrors` catches errors that are delayed by `Timer.run`.
1818
catchErrors(() {
1919
events.add("catch error entry");
20-
new Future.error("future error");
20+
Future.error("future error");
2121
Timer.run(() {
2222
done.complete(true);
2323
throw "timer error";

tests/lib/async/catch_errors12_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import 'package:expect/expect.dart';
99

1010
import 'catch_errors.dart';
1111

12-
main() {
12+
void main() {
1313
asyncStart();
14-
Completer done = new Completer();
14+
Completer done = Completer();
1515

1616
var events = [];
1717
// Tests that errors that have been delayed by several milliseconds with
@@ -21,11 +21,11 @@ main() {
2121
Timer.run(() {
2222
throw "timer error";
2323
});
24-
new Timer(const Duration(milliseconds: 100), () {
24+
Timer(const Duration(milliseconds: 100), () {
2525
throw "timer2 error";
2626
});
27-
new Future.value(499).then((x) {
28-
new Timer(const Duration(milliseconds: 200), () {
27+
Future.value(499).then((x) {
28+
Timer(const Duration(milliseconds: 200), () {
2929
done.complete(499);
3030
throw x;
3131
});

tests/lib/async/catch_errors13_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import 'package:expect/expect.dart';
99

1010
import 'catch_errors.dart';
1111

12-
main() {
12+
void main() {
1313
asyncStart();
14-
Completer done = new Completer();
14+
Completer done = Completer();
1515

1616
var events = [];
1717
// Work around bug that makes scheduleMicrotask use Timers. By invoking
@@ -40,7 +40,7 @@ main() {
4040
// This way we could reduce the timeout (to 10ms now), while still
4141
// allowing for more time, in case the machine is slow.
4242
void runDelayed() {
43-
new Timer(const Duration(milliseconds: 10), () {
43+
Timer(const Duration(milliseconds: 10), () {
4444
if (outerTimerRan) {
4545
scheduleMicrotask(() {
4646
throw "scheduleMicrotask";

tests/lib/async/catch_errors14_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import 'package:expect/expect.dart';
99

1010
import 'catch_errors.dart';
1111

12-
main() {
12+
void main() {
1313
asyncStart();
14-
Completer done = new Completer();
14+
Completer done = Completer();
1515

1616
var events = [];
1717
// Test that periodic Timers are handled correctly by `catchErrors`.
1818
catchErrors(() {
1919
int counter = 0;
20-
new Timer.periodic(const Duration(milliseconds: 50), (timer) {
20+
Timer.periodic(const Duration(milliseconds: 50), (timer) {
2121
if (counter++ == 5) {
2222
timer.cancel();
2323
done.complete(true);

tests/lib/async/catch_errors15_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ import 'package:expect/expect.dart';
99

1010
import 'catch_errors.dart';
1111

12-
main() {
12+
void main() {
1313
asyncStart();
14-
Completer done = new Completer();
14+
Completer done = Completer();
1515
var events = [];
1616

1717
catchErrors(() {
1818
events.add("catch error entry");
1919
catchErrors(() {
2020
events.add("catch error entry2");
21-
new Future.error("future error");
22-
new Future.error("future error2");
23-
new Future.value(499).then((x) => throw x);
24-
new Future.delayed(const Duration(milliseconds: 50), () {
21+
Future.error("future error");
22+
Future.error("future error2");
23+
Future.value(499).then((x) => throw x);
24+
Future.delayed(const Duration(milliseconds: 50), () {
2525
throw "delayed error";
2626
});
2727
throw "catch error";

tests/lib/async/catch_errors16_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import 'package:expect/expect.dart';
99

1010
import 'catch_errors.dart';
1111

12-
main() {
12+
void main() {
1313
asyncStart();
1414
var events = [];
15-
StreamController controller = new StreamController();
15+
StreamController controller = StreamController();
1616
Stream stream = controller.stream;
1717
// Test that the subscription of a stream is what counts. The error (2) runs
1818
// through the map-stream which goes through the nested `catchError` but
@@ -24,7 +24,7 @@ main() {
2424
});
2525
stream
2626
.transform(
27-
new StreamTransformer.fromHandlers(
27+
StreamTransformer.fromHandlers(
2828
handleError: (e, st, sink) {
2929
sink.add("error $e");
3030
},

tests/lib/async/catch_errors17_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ import 'package:expect/expect.dart';
99

1010
import 'catch_errors.dart';
1111

12-
main() {
12+
void main() {
1313
asyncStart();
14-
Completer done = new Completer();
14+
Completer done = Completer();
1515

1616
var events = [];
1717
late StreamController controller;
1818
// Test that errors do not cross zone boundaries.
1919
catchErrors(() {
2020
catchErrors(() {
21-
controller = new StreamController();
21+
controller = StreamController();
2222
controller.stream
2323
.map((x) {
2424
events.add("map $x");
2525
return x + 100;
2626
})
2727
.transform(
28-
new StreamTransformer.fromHandlers(
28+
StreamTransformer.fromHandlers(
2929
handleError: (e, st, sink) {
3030
sink.add("error $e");
3131
},
@@ -39,7 +39,7 @@ main() {
3939
});
4040
controller.add(1);
4141
controller.addError(2);
42-
new Future.error("outer error");
42+
Future.error("outer error");
4343
controller.close();
4444
}).listen(
4545
(x) {

tests/lib/async/catch_errors18_test.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ import 'package:expect/expect.dart';
99

1010
import 'catch_errors.dart';
1111

12-
main() {
12+
void main() {
1313
asyncStart();
14-
Completer done = new Completer();
14+
Completer done = Completer();
1515

1616
var events = [];
17-
Stream stream = new Stream.periodic(
18-
const Duration(milliseconds: 20),
19-
(x) => x,
20-
);
17+
Stream stream = Stream.periodic(const Duration(milliseconds: 20), (x) => x);
2118
// Test that errors of periodic streams are caught.
2219
catchErrors(() {
2320
var subscription;

tests/lib/async/catch_errors19_test.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ import 'package:expect/expect.dart';
99

1010
import 'catch_errors.dart';
1111

12-
main() {
12+
void main() {
1313
asyncStart();
14-
Completer done = new Completer();
14+
Completer done = Completer();
1515

1616
var events = [];
17-
Stream stream = new Stream.periodic(
18-
const Duration(milliseconds: 20),
19-
(x) => x,
20-
);
17+
Stream stream = Stream.periodic(const Duration(milliseconds: 20), (x) => x);
2118
// Test that asynchronous callbacks in the done-handler of streams (here
2219
// the `catchErrors`-stream) keep a zone alive.
2320
catchErrors(() {
@@ -29,7 +26,7 @@ main() {
2926
events.add(x);
3027
},
3128
onDone: () {
32-
new Future.delayed(const Duration(milliseconds: 30), () {
29+
Future.delayed(const Duration(milliseconds: 30), () {
3330
events.add(499);
3431
done.complete(true);
3532
});

0 commit comments

Comments
 (0)