Skip to content

Commit 722e4f2

Browse files
lrhnCommit Queue
authored andcommitted
Update isolate tests.
Remove uses of async_minitest and multitests. Removed two files that were checking dynamic behavior that no longer exists (compile-time errors reported at runtime). Rewrote and/or added to enum_const_test.dart and _deferred_in_isoltae_test.dart. Otherwise mainly removing multitest `//# ok:...` comments, or `//# 01: ...error...` that only existed because the test failed in dart2js, along with anything else that tried to special-case dart2js (which no longer supports isolates at all). Some sporadic general clean-up like removing library names. Change-Id: I99b55f05710347343286e86b37ce02f1006868dc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402700 Reviewed-by: Nate Bosch <[email protected]> Commit-Queue: Lasse Nielsen <[email protected]>
1 parent 3f4bf45 commit 722e4f2

19 files changed

+276
-312
lines changed

tests/lib/isolate/compile_time_error_test.dart

Lines changed: 0 additions & 39 deletions
This file was deleted.

tests/lib/isolate/deferred_in_isolate2_lib.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
// Used by deferred_in_isolate2_test.
6-
library deferred_in_isolate2_lib;
7-
86
String f() => "hi";

tests/lib/isolate/deferred_in_isolate2_test.dart

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,37 @@
55
library deferred_in_isolate2_test;
66

77
import 'dart:isolate';
8-
import 'package:expect/legacy/async_minitest.dart'; // ignore: deprecated_member_use
8+
9+
import 'package:expect/async_helper.dart';
10+
import 'package:expect/expect.dart';
911

1012
import 'deferred_in_isolate2_lib.dart' deferred as lib;
1113

12-
loadDeferred(port) {
14+
void loadDeferred(SendPort port) {
1315
lib.loadLibrary().then((_) {
1416
port.send(lib.f());
1517
});
1618
}
1719

18-
main() {
19-
test("Deferred loading in isolate", () {
20-
ReceivePort port = new ReceivePort();
21-
port.first.then(expectAsync((msg) {
22-
expect(msg, equals("hi"));
23-
}));
24-
Isolate.spawn(loadDeferred, port.sendPort);
25-
});
20+
void main() {
21+
asyncStart(2);
22+
var port = RawReceivePort();
23+
port.handler = (msg) {
24+
if (msg == null) {
25+
asyncEnd();
26+
port.close();
27+
} else if (msg case [String error, String stack]) {
28+
var remoteError = RemoteError("Error in isolate: $error", stack);
29+
Error.throwWithStackTrace(remoteError, remoteError.stackTrace);
30+
} else {
31+
Expect.equals("hi", msg);
32+
asyncEnd();
33+
}
34+
};
35+
Isolate.spawn(
36+
loadDeferred,
37+
port.sendPort,
38+
onError: port.sendPort,
39+
onExit: port.sendPort,
40+
);
2641
}

tests/lib/isolate/deferred_in_isolate_app.dart

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'deferred_in_isolate_lib.dart' deferred as test;
5+
import 'dart:isolate' show SendPort;
66

7-
void main(args, msg) {
8-
assert(args != null);
9-
assert(args.length == 1);
10-
assert(msg != null);
11-
assert(msg.length == 1);
7+
import 'deferred_in_isolate_lib.dart' deferred as lib;
128

9+
void main(List<String> args, Object? msg) {
10+
assert(args.length == 1);
1311
var expectedMsg = args[0];
14-
var replyPort = msg[0];
12+
var replyPort = msg as SendPort;
1513

16-
try {
17-
print("BeforeLibraryLoading");
14+
replyPort.send(true); // Tell test that isolate has started.
1815

19-
test.loadLibrary().then((_) {
20-
var obj = new test.DeferredObj(expectedMsg);
21-
replyPort.send(obj.toString());
22-
}).catchError((error) {
23-
replyPort.send("Error from isolate:\n$error");
24-
});
25-
} catch (exception, stacktrace) {
26-
replyPort.send("Exception from isolate:\n$exception\n$stacktrace");
27-
}
16+
lib.loadLibrary().then((_) {
17+
var obj = lib.DeferredObj(expectedMsg);
18+
replyPort.send("$obj");
19+
});
2820
}

tests/lib/isolate/deferred_in_isolate_lib.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
library deferred_in_isolate_lib;
6-
75
class DeferredObj {
86
DeferredObj(this._val);
9-
toString() => "$_val";
7+
String toString() => "*$_val*";
108

11-
var _val;
9+
final Object? _val;
1210
}

tests/lib/isolate/deferred_in_isolate_test.dart

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,45 @@
77

88
import 'dart:isolate';
99

10-
main() {
11-
try {
12-
var receivePort = new RawReceivePort();
13-
var expectedMsg = "Deferred Loaded.";
10+
import 'package:expect/async_helper.dart';
11+
import 'package:expect/expect.dart';
1412

15-
receivePort.handler = (msg) {
16-
if (msg != expectedMsg) {
17-
print("Test failed.");
18-
throw msg; // Fail the test if the message is not expected.
19-
}
20-
print('Test done.');
13+
void main() {
14+
asyncStart(4);
15+
var receivePort = RawReceivePort();
16+
var nonce = "Deferred Loaded.";
17+
int state = 0; // Incremented when reaching expected steps in communication.
18+
19+
receivePort.handler = (reply) {
20+
if (reply == null) {
21+
Expect.equals(2, state); // After isolate result message.
22+
state = -1;
23+
// Isolate exit.
2124
receivePort.close();
22-
};
25+
asyncEnd();
26+
} else if (reply == true) {
27+
Expect.equals(0, state); // Initial isolate-start message.
28+
state = 1;
29+
asyncEnd();
30+
} else if (reply case [String error, String stack]) {
31+
// Isolate unhandled or handled error.
32+
var remoteError = RemoteError("@$state: $error", stack);
33+
Error.throwWithStackTrace(remoteError, remoteError.stackTrace);
34+
} else {
35+
Expect.equals("*$nonce*", reply);
36+
Expect.equals(1, state); // After isolate-start message.
37+
state = 2;
38+
asyncEnd();
39+
}
40+
};
2341

24-
var stopwatch = new Stopwatch()..start();
25-
Isolate.spawnUri(new Uri(path: 'deferred_in_isolate_app.dart'),
26-
[expectedMsg], [receivePort.sendPort]).then((isolate) {
27-
print('Isolate spawn: ${stopwatch.elapsedMilliseconds}ms');
28-
}).catchError((error) {
29-
print(error);
30-
});
31-
} catch (exception, stackTrace) {
32-
print('Test failed.');
33-
print(exception);
34-
print(stackTrace);
35-
rethrow;
36-
}
42+
Isolate.spawnUri(
43+
Uri(path: 'deferred_in_isolate_app.dart'),
44+
[nonce],
45+
receivePort.sendPort,
46+
onError: receivePort.sendPort,
47+
onExit: receivePort.sendPort,
48+
).then((_) {
49+
asyncEnd();
50+
});
3751
}

tests/lib/isolate/enum_const_test.dart

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,56 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// Formatting can break multitests, so don't format them.
6-
// dart format off
7-
85
import "dart:isolate";
6+
7+
import "package:expect/async_helper.dart";
98
import "package:expect/expect.dart";
109

11-
enum Foo { BAR, BAZ }
10+
/// Check that an enum value sent through a send/receive-port retains identity.
11+
12+
enum Foo { bar, baz }
13+
14+
void main() {
15+
asyncStart();
16+
test1();
17+
test2();
18+
test3();
19+
asyncEnd();
20+
}
1221

13-
verify(val) {
14-
Expect.identical(Foo.BAR, val);
22+
void verify(Object? val) {
23+
Expect.identical(Foo.bar, val);
1524
}
1625

17-
main() {
18-
test1(); //# 01: ok
19-
test2(); //# 02: ok
26+
void test1() {
27+
// Sanity check.
28+
verify(Foo.bar);
2029
}
2130

22-
test1() => verify(Foo.BAR);
31+
void test2() {
32+
// From same isolate.
33+
asyncStart();
34+
var rp = RawReceivePort();
35+
rp.handler = (val) {
36+
verify(val);
37+
rp.close();
38+
asyncEnd();
39+
};
40+
rp.sendPort.send(Foo.bar);
41+
}
2342

24-
test2() {
25-
var rp;
26-
rp = new RawReceivePort((val) {
43+
void test3() {
44+
// From other isolate.
45+
asyncStart();
46+
var rp = RawReceivePort();
47+
rp.handler = (val) {
2748
verify(val);
2849
rp.close();
29-
});
30-
rp.sendPort.send(Foo.BAR);
50+
asyncEnd();
51+
};
52+
Isolate.spawn(_sendFoo, rp.sendPort);
53+
}
54+
55+
void _sendFoo(SendPort sendPort) {
56+
sendPort.send(Foo.bar);
3157
}

tests/lib/isolate/isolate_import_test.dart

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/lib/isolate/issue_21398_parent_isolate2_test.dart

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,43 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// Formatting can break multitests, so don't format them.
6-
// dart format off
7-
85
import 'dart:isolate';
96
import 'package:expect/async_helper.dart';
107
import "package:expect/expect.dart";
118

129
import "deferred_loaded_lib.dart" deferred as lib;
1310

14-
// In this test case we send an object created from a deferred library
15-
// that is loaded in the child isolate but not the parent isolate. The
16-
// parent isolate does not know about the type of this object and throws
17-
// an unhandled exception.
18-
funcChild(args) {
19-
var replyPort = args[0];
11+
// Sends an object created from a deferred library that is loaded in the child
12+
// isolate but not the parent isolate.
13+
// The parent isolate successfully receives the object.
14+
15+
void funcChild(SendPort replyPort) {
2016
// Deferred load a library, create an object from that library and send
2117
// it over to the parent isolate which has not yet loaded that library.
2218
lib.loadLibrary().then((_) {
2319
replyPort.send(new lib.FromChildIsolate());
2420
});
2521
}
2622

27-
void helperFunction() {
23+
void main() {
2824
var receivePort = new ReceivePort();
2925
asyncStart();
3026

3127
// Spawn an isolate using spawnFunction.
32-
Isolate.spawn(funcChild, [receivePort.sendPort]).then((isolate) {
33-
receivePort.listen((msg) {
34-
// We don't expect to receive any valid messages.
35-
Expect.fail("We don't expect to receive any valid messages");
36-
receivePort.close();
37-
asyncEnd();
38-
}, onError: (e) {
39-
// We don't expect to receive any error messages, per spec listen
40-
// does not receive an error object.
41-
Expect.fail("We don't expect to receive any error messages");
42-
receivePort.close();
43-
asyncEnd();
44-
});
28+
Isolate.spawn(funcChild, receivePort.sendPort).then((isolate) {
29+
receivePort.listen(
30+
(dynamic msg) {
31+
Expect.equals(10, msg.fld);
32+
receivePort.close();
33+
asyncEnd();
34+
},
35+
onError: (e, s) {
36+
// We don't expect to receive any error messages, per spec listen
37+
// does not receive an error object.
38+
Expect.fail("We don't expect to receive any error messages");
39+
receivePort.close();
40+
asyncEnd();
41+
},
42+
);
4543
});
4644
}
47-
48-
main() {
49-
helperFunction(); //# 01: runtime error
50-
}

0 commit comments

Comments
 (0)