Skip to content

Commit

Permalink
[ffigen] Clean up global_test and comment out the flaky line. (#1674)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe authored Jan 7, 2025
1 parent 3b66738 commit d9bb34c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 87 deletions.
8 changes: 4 additions & 4 deletions pkgs/ffigen/test/native_objc_test/global_native_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ exclude-all-by-default: true
ffi-native:
globals:
include:
- globalNativeString
- globalNativeObject
- globalNativeBlock
- globalString
- globalObject
- globalBlock
headers:
entry-points:
- 'global_native_test.h'
- 'global_test.h'
preamble: |
// ignore_for_file: camel_case_types, non_constant_identifier_names, unnecessary_non_null_assertion, unused_element, unused_field
59 changes: 22 additions & 37 deletions pkgs/ffigen/test/native_objc_test/global_native_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
// Objective C support is only available on mac.
@TestOn('mac-os')

// TODO(https://github.com/dart-lang/native/issues/1435): Fix flakiness.
@Retry(3)

import 'dart:ffi';
import 'dart:io';

Expand All @@ -26,61 +23,49 @@ void main() {
final dylib = File('test/native_objc_test/objc_test.dylib');
verifySetupFile(dylib);
DynamicLibrary.open(dylib.absolute.path);
generateBindingsForCoverage('global_native');
generateBindingsForCoverage('global');
});

test('Global string', () {
expect(globalNativeString.toString(), 'Hello World');
globalNativeString = 'Something else'.toNSString();
expect(globalNativeString.toString(), 'Something else');
expect(globalString.toString(), 'Hello World');
globalString = 'Something else'.toNSString();
expect(globalString.toString(), 'Something else');
globalString = 'Hello World'.toNSString();
});

(Pointer<ObjCObject>, Pointer<ObjCObject>) globalObjectRefCountingInner() {
final obj1 = NSObject.new1();
globalNativeObject = obj1;
final obj1raw = obj1.ref.pointer;
expect(objectRetainCount(obj1raw), 2); // obj1, and the global variable.

final obj2 = NSObject.new1();
globalNativeObject = obj2;
final obj2raw = obj2.ref.pointer;
expect(objectRetainCount(obj2raw), 2); // obj2, and the global variable.
expect(objectRetainCount(obj1raw), 1); // Just obj1.
expect(obj1, isNotNull); // Force obj1 to stay in scope.
expect(obj2, isNotNull); // Force obj2 to stay in scope.

return (obj1raw, obj2raw);
Pointer<ObjCObject> globalObjectRefCountingInner() {
globalObject = NSObject.new1();
final obj1raw = globalObject!.ref.pointer;

// TODO(https://github.com/dart-lang/native/issues/1435): Fix flakiness.
// expect(objectRetainCount(obj1raw), greaterThan(0));

return obj1raw;
}

test('Global object ref counting', () {
final (obj1raw, obj2raw) = globalObjectRefCountingInner();
final obj1raw = globalObjectRefCountingInner();
globalObject = null;
doGC();

expect(objectRetainCount(obj2raw), 1); // Just the global variable.
expect(objectRetainCount(obj1raw), 0);

globalNativeObject = null;
expect(objectRetainCount(obj2raw), 0);
expect(objectRetainCount(obj1raw), 0);
}, skip: !canDoGC);

test('Global block', () {
globalNativeBlock = ObjCBlock_Int32_Int32.fromFunction((int x) => x * 10);
expect(globalNativeBlock!(123), 1230);
globalNativeBlock =
ObjCBlock_Int32_Int32.fromFunction((int x) => x + 1000);
expect(globalNativeBlock!(456), 1456);
globalBlock = ObjCBlock_Int32_Int32.fromFunction((int x) => x * 10);
expect(globalBlock!(123), 1230);
globalBlock = ObjCBlock_Int32_Int32.fromFunction((int x) => x + 1000);
expect(globalBlock!(456), 1456);
});

(Pointer<ObjCBlockImpl>, Pointer<ObjCBlockImpl>)
globalBlockRefCountingInner() {
final blk1 = ObjCBlock_Int32_Int32.fromFunction((int x) => x * 10);
globalNativeBlock = blk1;
globalBlock = blk1;
final blk1raw = blk1.ref.pointer;
expect(blockRetainCount(blk1raw), 2); // blk1, and the global variable.

final blk2 = ObjCBlock_Int32_Int32.fromFunction((int x) => x + 1000);
globalNativeBlock = blk2;
globalBlock = blk2;
final blk2raw = blk2.ref.pointer;
expect(blockRetainCount(blk2raw), 2); // blk2, and the global variable.
expect(blockRetainCount(blk1raw), 1); // Just blk1.
Expand All @@ -97,7 +82,7 @@ void main() {
expect(blockRetainCount(blk2raw), 1); // Just the global variable.
expect(blockRetainCount(blk1raw), 0);

globalNativeBlock = null;
globalBlock = null;
expect(blockRetainCount(blk2raw), 0);
expect(blockRetainCount(blk1raw), 0);
}, skip: !canDoGC);
Expand Down
10 changes: 0 additions & 10 deletions pkgs/ffigen/test/native_objc_test/global_native_test.h

This file was deleted.

11 changes: 0 additions & 11 deletions pkgs/ffigen/test/native_objc_test/global_native_test.m

This file was deleted.

36 changes: 11 additions & 25 deletions pkgs/ffigen/test/native_objc_test/global_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
// Objective C support is only available on mac.
@TestOn('mac-os')

// TODO(https://github.com/dart-lang/native/issues/1435): Fix flakiness.
@Retry(3)

import 'dart:ffi';
import 'dart:io';

Expand Down Expand Up @@ -35,34 +32,23 @@ void main() {
expect(lib.globalString.toString(), 'Hello World');
lib.globalString = 'Something else'.toNSString();
expect(lib.globalString.toString(), 'Something else');
lib.globalString = 'Hello World'.toNSString();
});

(Pointer<ObjCObject>, Pointer<ObjCObject>) globalObjectRefCountingInner() {
final obj1 = NSObject.new1();
lib.globalObject = obj1;
final obj1raw = obj1.ref.pointer;
expect(objectRetainCount(obj1raw), 2); // obj1, and the global variable.

final obj2 = NSObject.new1();
lib.globalObject = obj2;
final obj2raw = obj2.ref.pointer;
expect(objectRetainCount(obj2raw), 2); // obj2, and the global variable.
expect(objectRetainCount(obj1raw), 1); // Just obj1.
expect(obj1, isNotNull); // Force obj1 to stay in scope.
expect(obj2, isNotNull); // Force obj2 to stay in scope.

return (obj1raw, obj2raw);
}
Pointer<ObjCObject> globalObjectRefCountingInner() {
lib.globalObject = NSObject.new1();
final obj1raw = lib.globalObject!.ref.pointer;

test('Global object ref counting', () {
final (obj1raw, obj2raw) = globalObjectRefCountingInner();
doGC();
// TODO(https://github.com/dart-lang/native/issues/1435): Fix flakiness.
// expect(objectRetainCount(obj1raw), greaterThan(0));

expect(objectRetainCount(obj2raw), 1); // Just the global variable.
expect(objectRetainCount(obj1raw), 0);
return obj1raw;
}

test('Global object ref counting', () {
final obj1raw = globalObjectRefCountingInner();
lib.globalObject = null;
expect(objectRetainCount(obj2raw), 0);
doGC();
expect(objectRetainCount(obj1raw), 0);
}, skip: !canDoGC);

Expand Down

0 comments on commit d9bb34c

Please sign in to comment.