@@ -26,49 +26,31 @@ public class AutoArenaTest {
26
26
@ Test
27
27
@ SuppressWarnings ("removal" ) // System.runFinalization() will be removed
28
28
public void cleaner_releases_native_resource () {
29
- SwiftHeapObject object = new FakeSwiftHeapObject ();
30
-
31
- // Latch waiting for the cleanup of the object
32
- var cleanupLatch = new CountDownLatch (1 );
33
-
34
- // we're retaining the `object`, register it with the arena:
35
- AutoSwiftMemorySession arena = (AutoSwiftMemorySession ) SwiftArena .ofAuto ();
29
+ SwiftArena arena = SwiftArena .ofAuto ();
36
30
31
+ // This object is registered to the arena.
32
+ var object = new FakeSwiftInstance (arena );
37
33
var statusDestroyedFlag = object .$statusDestroyedFlag ();
38
- Runnable markAsDestroyed = () -> statusDestroyedFlag .set (true );
39
-
40
- arena .register (object ,
41
- new SwiftHeapObjectCleanup (object .$memorySegment (), object .$swiftType (), markAsDestroyed ) {
42
- @ Override
43
- public void run () throws UnexpectedRetainCountException {
44
- cleanupLatch .countDown ();
45
- }
46
- });
47
34
48
35
// Release the object and hope it gets GC-ed soon
49
36
50
37
// noinspection UnusedAssignment
51
38
object = null ;
52
39
53
40
var i = 1_000 ;
54
- while (cleanupLatch . getCount () != 0 ) {
41
+ while (! statusDestroyedFlag . get () ) {
55
42
System .runFinalization ();
56
43
System .gc ();
57
44
58
45
if (i -- < 1 ) {
59
46
throw new RuntimeException ("Reference was not cleaned up! Did Cleaner not pick up the release?" );
60
47
}
61
48
}
62
-
63
49
}
64
50
65
- private static class FakeSwiftHeapObject implements SwiftHeapObject {
66
- public FakeSwiftHeapObject () {
67
- }
68
-
69
- @ Override
70
- public MemorySegment $memorySegment () {
71
- return MemorySegment .NULL ;
51
+ private static class FakeSwiftInstance extends SwiftInstance implements SwiftHeapObject {
52
+ public FakeSwiftInstance (SwiftArena arena ) {
53
+ super (MemorySegment .NULL , arena );
72
54
}
73
55
74
56
@ Override
@@ -80,10 +62,5 @@ public FakeSwiftHeapObject() {
80
62
public SwiftAnyType $swiftType () {
81
63
return null ;
82
64
}
83
-
84
- @ Override
85
- public AtomicBoolean $statusDestroyedFlag () {
86
- return new AtomicBoolean ();
87
- }
88
65
}
89
66
}
0 commit comments