dart-sdk/tests/ffi_2/vmspecific_native_finalizer_isolates_test.dart
Alexander Aprelev 67683c3973 [vm/isolates] Introduce 'vm:isolate-unsendable' pragma.
Decorate Zone, Future, Completer, Timer and Stream with newly-introduced pragma to make sure that message verification stops earlier, produces shorter retaining path for the user.

BUG=https://github.com/dart-lang/sdk/issues/51722
TEST=send_unsupported_objects_test,isolate_exit_unsendable_test.dart
CoreLibraryReviewExempt: vm-specific pragmas
Change-Id: I499eea542d228ac9cf0797a682664f93f360dc80
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/289027
Commit-Queue: Alexander Aprelev <aam@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2023-03-27 20:54:11 +00:00

85 lines
2.2 KiB
Dart

// Copyright (c) 2022, 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.
//
// SharedObjects=ffi_test_functions
//
// VMOptions=--trace-finalizers
// @dart = 2.9
import 'dart:async';
import 'dart:ffi';
import 'dart:isolate';
import 'package:expect/expect.dart';
import 'package:ffi/ffi.dart';
import 'dylib_utils.dart';
import 'ffi_test_helpers.dart';
void main() async {
await testSendAndExitFinalizable();
await testSendAndExitFinalizer();
await testFinalizerRunsOnIsolateShutdown();
print('End of test, shutting down.');
}
DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");
void runIsolateAttachFinalizer(int address) {
final token = Pointer<IntPtr>.fromAddress(address);
createAndLoseFinalizable(token);
print('Isolate done.');
}
Future<void> testFinalizerRunsOnIsolateShutdown() async {
await using((Arena allocator) async {
final token = allocator<IntPtr>();
Expect.equals(0, token.value);
final portExitMessage = ReceivePort();
await Isolate.spawn(
runIsolateAttachFinalizer,
token.address,
onExit: portExitMessage.sendPort,
);
await portExitMessage.first;
doGC();
Expect.equals(42, token.value);
});
}
Future<void> testSendAndExitFinalizable() async {
final receivePort = ReceivePort();
await Isolate.spawn(
(SendPort sendPort) {
try {
Isolate.exit(sendPort, MyFinalizable());
} catch (e) {
print('Expected exception: $e.');
Isolate.exit(sendPort, e.toString());
}
},
receivePort.sendPort,
);
final result = await receivePort.first;
Expect.contains("Invalid argument: is unsendable", result);
}
Future<void> testSendAndExitFinalizer() async {
final receivePort = ReceivePort();
await Isolate.spawn(
(SendPort sendPort) {
try {
Isolate.exit(sendPort, MyFinalizable());
} catch (e) {
print('Expected exception: $e.');
Isolate.exit(sendPort, e.toString());
}
},
receivePort.sendPort,
);
final result = await receivePort.first;
Expect.contains("Invalid argument: is unsendable", result);
}