Skip to content

Commit

Permalink
Switch to Isolate.run (#1662)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe authored Oct 18, 2024
1 parent 6a6079a commit cc34333
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 38 deletions.
2 changes: 1 addition & 1 deletion pkgs/ffigen/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies:

dev_dependencies:
async: ^2.11.0
coverage: ^1.8.0
coverage: ^1.10.0
dart_flutter_team_lints: ^2.0.0
json_schema: ^5.1.1
leak_tracker: ^10.0.7
Expand Down
42 changes: 6 additions & 36 deletions pkgs/ffigen/test/native_objc_test/isolate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,6 @@ import '../test_utils.dart';
import 'isolate_bindings.dart';
import 'util.dart';

// TODO(https://github.com/dart-lang/coverage/issues/472): Delete this and use
// Isolate.run once the coverage bug is fixed.
Future<R> isolateRun<R>(FutureOr<R> computation()) async {
FutureOr<R> Function()? comp = computation;
Future<void> run(SendPort sendPort) async {
sendPort.send(await comp!());
comp = null;
sendPort.send(null);
Isolate.current.kill();
}

final port = ReceivePort();
final queue = StreamQueue(port);
final isolate = await Isolate.spawn(run, port.sendPort);
final result = await queue.next as R;
await queue.next; // Wait for isolate to release its reference to comp.
port.close();
return result;
}

void main() {
group('isolate', () {
setUpAll(() {
Expand All @@ -60,10 +40,6 @@ void main() {
sendable.value = 456;
sendPort.send(oldValue);
port.close();

// TODO(https://github.com/dart-lang/coverage/issues/472): Delete this.
sendPort.send(null);
Isolate.current.kill();
}

test('Sending object through a port', () async {
Expand All @@ -90,15 +66,14 @@ void main() {

sendable = null;
doGC();
// TODO(https://github.com/dart-lang/coverage/issues/472): Re-enable.
// expect(objectRetainCount(pointer), 0);
expect(objectRetainCount(pointer), 0);
}, skip: !canDoGC);

test('Capturing object in closure', () async {
Sendable? sendable = Sendable.new1();
sendable.value = 123;

final oldValue = await isolateRun(() {
final oldValue = await Isolate.run(() {
final oldValue = sendable!.value;
sendable!.value = 456;
return oldValue;
Expand All @@ -124,10 +99,6 @@ void main() {
final block = await queue.next as ObjCBlock<Void Function(Int32)>;
block(123);
port.close();

// TODO(https://github.com/dart-lang/coverage/issues/472): Delete this.
sendPort.send(null);
Isolate.current.kill();
}

test('Sending block through a port', () async {
Expand Down Expand Up @@ -156,8 +127,7 @@ void main() {

block = null;
doGC();
// TODO(https://github.com/dart-lang/coverage/issues/472): Re-enable.
// expect(blockRetainCount(pointer), 0);
expect(blockRetainCount(pointer), 0);
}, skip: !canDoGC);

ObjCBlock<Void Function(Int32)> makeBlock(Completer<int> completer) {
Expand All @@ -172,7 +142,7 @@ void main() {
final completer = Completer<int>();
ObjCBlock<Void Function(Int32)>? block = makeBlock(completer);

await isolateRun(() {
await Isolate.run(() {
block!(123);
});
final value = await completer.future;
Expand All @@ -193,7 +163,7 @@ void main() {
expect(objectRetainCount(pointer), 1);
expect(sendable.ref.isReleased, isFalse);

final (oldIsReleased, newIsReleased) = await isolateRun(() {
final (oldIsReleased, newIsReleased) = await Isolate.run(() {
final oldIsReleased = sendable.ref.isReleased;
sendable!.ref.release();
return (oldIsReleased, sendable.ref.isReleased);
Expand All @@ -213,7 +183,7 @@ void main() {

expect(sendable.ref.isReleased, isFalse);

await isolateRun(() {
await Isolate.run(() {
sendable!.ref.release();
});

Expand Down
2 changes: 1 addition & 1 deletion pkgs/objective_c/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies:

dev_dependencies:
args: ^2.0.0
coverage: ^1.8.0
coverage: ^1.10.0
dart_flutter_team_lints: ^2.0.0
ffigen: ^14.0.0
flutter_lints: ^3.0.0
Expand Down

0 comments on commit cc34333

Please sign in to comment.