@@ -26,49 +26,31 @@ public class AutoArenaTest {
2626 @ Test
2727 @ SuppressWarnings ("removal" ) // System.runFinalization() will be removed
2828 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 ();
3630
31+ // This object is registered to the arena.
32+ var object = new FakeSwiftInstance (arena );
3733 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- });
4734
4835 // Release the object and hope it gets GC-ed soon
4936
5037 // noinspection UnusedAssignment
5138 object = null ;
5239
5340 var i = 1_000 ;
54- while (cleanupLatch . getCount () != 0 ) {
41+ while (! statusDestroyedFlag . get () ) {
5542 System .runFinalization ();
5643 System .gc ();
5744
5845 if (i -- < 1 ) {
5946 throw new RuntimeException ("Reference was not cleaned up! Did Cleaner not pick up the release?" );
6047 }
6148 }
62-
6349 }
6450
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 );
7254 }
7355
7456 @ Override
@@ -80,10 +62,5 @@ public FakeSwiftHeapObject() {
8062 public SwiftAnyType $swiftType () {
8163 return null ;
8264 }
83-
84- @ Override
85- public AtomicBoolean $statusDestroyedFlag () {
86- return new AtomicBoolean ();
87- }
8865 }
8966}
0 commit comments