Skip to content

Commit c329cf9

Browse files
authored
[ffigen] Fix flaky tests (#1471)
1 parent 87e8f92 commit c329cf9

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

pkgs/ffigen/test/native_objc_test/arc_test.dart

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
// Objective C support is only available on mac.
88
@TestOn('mac-os')
99

10-
// This test is slightly flaky. Some of the ref counts are occasionally lower
11-
// than expected, presumably due to the Dart wrapper objects being GC'd before
12-
// the expect call.
13-
@Retry(3)
14-
1510
import 'dart:ffi';
1611
import 'dart:io';
1712

@@ -97,6 +92,10 @@ void main() {
9792
expect(objectRetainCount(obj2raw), 3);
9893
expect(objectRetainCount(obj3raw), 2);
9994

95+
expect(obj1, isNotNull); // Force obj1 to stay in scope.
96+
expect(obj2, isNotNull); // Force obj2 to stay in scope.
97+
expect(obj3, isNotNull); // Force obj3 to stay in scope.
98+
10099
return (obj1raw, obj2raw, obj3raw);
101100
}
102101

@@ -281,21 +280,19 @@ void main() {
281280
final outerObjRaw = outerObj.pointer;
282281
expect(objectRetainCount(outerObjRaw), 1);
283282
final assignObjRaw = assignPropertiesInnerInner(counter, outerObj);
284-
expect(objectRetainCount(assignObjRaw), 2);
285283
doGC();
286284
// assignObj has been cleaned up.
287285
expect(counter.value, 1);
288286
expect(objectRetainCount(assignObjRaw), 0);
289287
expect(objectRetainCount(outerObjRaw), 1);
288+
expect(outerObj, isNotNull); // Force outerObj to stay in scope.
290289
return (outerObjRaw, assignObjRaw);
291290
}
292291

293292
test('assign properties ref count correctly', () {
294293
final counter = calloc<Int32>();
295294
counter.value = 0;
296295
final (outerObjRaw, assignObjRaw) = assignPropertiesInner(counter);
297-
expect(objectRetainCount(assignObjRaw), 0);
298-
expect(objectRetainCount(outerObjRaw), 1);
299296
doGC();
300297
expect(counter.value, 0);
301298
expect(objectRetainCount(assignObjRaw), 0);
@@ -323,12 +320,12 @@ void main() {
323320
final outerObjRaw = outerObj.pointer;
324321
expect(objectRetainCount(outerObjRaw), 1);
325322
final retainObjRaw = retainPropertiesInnerInner(counter, outerObj);
326-
expect(objectRetainCount(retainObjRaw), 4);
327323
doGC();
328324
// retainObj is still around, because outerObj retains a reference to it.
329325
expect(objectRetainCount(retainObjRaw), 2);
330326
expect(objectRetainCount(outerObjRaw), 1);
331327
expect(counter.value, 2);
328+
expect(outerObj, isNotNull); // Force outerObj to stay in scope.
332329
return (outerObjRaw, retainObjRaw);
333330
}
334331

@@ -339,8 +336,6 @@ void main() {
339336
// need an autorelease pool.
340337
final pool = lib.objc_autoreleasePoolPush();
341338
final (outerObjRaw, retainObjRaw) = retainPropertiesInner(counter);
342-
expect(objectRetainCount(retainObjRaw), 2);
343-
expect(objectRetainCount(outerObjRaw), 1);
344339
doGC();
345340
expect(objectRetainCount(retainObjRaw), 1);
346341
expect(objectRetainCount(outerObjRaw), 0);

pkgs/ffigen/test/native_objc_test/global_native_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ void main() {
4343
final obj2raw = obj2.pointer;
4444
expect(objectRetainCount(obj2raw), 2); // obj2, and the global variable.
4545
expect(objectRetainCount(obj1raw), 1); // Just obj1.
46+
expect(obj1, isNotNull); // Force obj1 to stay in scope.
47+
expect(obj2, isNotNull); // Force obj2 to stay in scope.
4648

4749
return (obj1raw, obj2raw);
4850
}
@@ -79,6 +81,8 @@ void main() {
7981
final blk2raw = blk2.pointer;
8082
expect(blockRetainCount(blk2raw), 2); // blk2, and the global variable.
8183
expect(blockRetainCount(blk1raw), 1); // Just blk1.
84+
expect(blk1, isNotNull); // Force blk1 to stay in scope.
85+
expect(blk2, isNotNull); // Force blk2 to stay in scope.
8286

8387
return (blk1raw, blk2raw);
8488
}

pkgs/ffigen/test/native_objc_test/global_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ void main() {
4545
final obj2raw = obj2.pointer;
4646
expect(objectRetainCount(obj2raw), 2); // obj2, and the global variable.
4747
expect(objectRetainCount(obj1raw), 1); // Just obj1.
48+
expect(obj1, isNotNull); // Force obj1 to stay in scope.
49+
expect(obj2, isNotNull); // Force obj2 to stay in scope.
4850

4951
return (obj1raw, obj2raw);
5052
}
@@ -80,6 +82,8 @@ void main() {
8082
final blk2raw = blk2.pointer;
8183
expect(blockRetainCount(blk2raw), 2); // blk2, and the global variable.
8284
expect(blockRetainCount(blk1raw), 1); // Just blk1.
85+
expect(blk1, isNotNull); // Force blk1 to stay in scope.
86+
expect(blk2, isNotNull); // Force blk2 to stay in scope.
8387

8488
return (blk1raw, blk2raw);
8589
}

pkgs/ffigen/test/native_objc_test/ref_count_test.dart

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
// Objective C support is only available on mac.
88
@TestOn('mac-os')
99

10-
// This test is slightly flaky. Some of the ref counts are occasionally lower
11-
// than expected, presumably due to the Dart wrapper objects being GC'd before
12-
// the expect call.
13-
@Retry(3)
14-
1510
import 'dart:ffi';
1611
import 'dart:io';
1712

@@ -98,6 +93,10 @@ void main() {
9893
expect(objectRetainCount(obj2raw), 3);
9994
expect(objectRetainCount(obj3raw), 2);
10095

96+
expect(obj1, isNotNull); // Force obj1 to stay in scope.
97+
expect(obj2, isNotNull); // Force obj2 to stay in scope.
98+
expect(obj3, isNotNull); // Force obj3 to stay in scope.
99+
101100
return (obj1raw, obj2raw, obj3raw);
102101
}
103102

@@ -282,21 +281,19 @@ void main() {
282281
final outerObjRaw = outerObj.pointer;
283282
expect(objectRetainCount(outerObjRaw), 1);
284283
final assignObjRaw = assignPropertiesInnerInner(counter, outerObj);
285-
expect(objectRetainCount(assignObjRaw), 2);
286284
doGC();
287285
// assignObj has been cleaned up.
288286
expect(counter.value, 1);
289287
expect(objectRetainCount(assignObjRaw), 0);
290288
expect(objectRetainCount(outerObjRaw), 1);
289+
expect(outerObj, isNotNull); // Force outerObj to stay in scope.
291290
return (outerObjRaw, assignObjRaw);
292291
}
293292

294293
test('assign properties ref count correctly', () {
295294
final counter = calloc<Int32>();
296295
counter.value = 0;
297296
final (outerObjRaw, assignObjRaw) = assignPropertiesInner(counter);
298-
expect(objectRetainCount(assignObjRaw), 0);
299-
expect(objectRetainCount(outerObjRaw), 1);
300297
doGC();
301298
expect(counter.value, 0);
302299
expect(objectRetainCount(assignObjRaw), 0);
@@ -324,12 +321,12 @@ void main() {
324321
final outerObjRaw = outerObj.pointer;
325322
expect(objectRetainCount(outerObjRaw), 1);
326323
final retainObjRaw = retainPropertiesInnerInner(counter, outerObj);
327-
expect(objectRetainCount(retainObjRaw), 4);
328324
doGC();
329325
// retainObj is still around, because outerObj retains a reference to it.
330326
expect(objectRetainCount(retainObjRaw), 2);
331327
expect(objectRetainCount(outerObjRaw), 1);
332328
expect(counter.value, 2);
329+
expect(outerObj, isNotNull); // Force outerObj to stay in scope.
333330
return (outerObjRaw, retainObjRaw);
334331
}
335332

@@ -340,8 +337,6 @@ void main() {
340337
// need an autorelease pool.
341338
final pool = lib.objc_autoreleasePoolPush();
342339
final (outerObjRaw, retainObjRaw) = retainPropertiesInner(counter);
343-
expect(objectRetainCount(retainObjRaw), 2);
344-
expect(objectRetainCount(outerObjRaw), 1);
345340
doGC();
346341
expect(objectRetainCount(retainObjRaw), 1);
347342
expect(objectRetainCount(outerObjRaw), 0);

0 commit comments

Comments
 (0)