|
| 1 | +package com.sun.jna; |
| 2 | + |
| 3 | +import com.sun.jna.internal.MasterAccessor; |
| 4 | +import junit.framework.TestCase; |
| 5 | + |
| 6 | +import java.util.function.Supplier; |
| 7 | + |
| 8 | +public class MasterCleanerTest extends TestCase { |
| 9 | + private CallbacksTest.TestLibrary lib; |
| 10 | + |
| 11 | + public static boolean waitFor(Supplier<Boolean> cond, long maxWaitMs) { |
| 12 | + long start = System.currentTimeMillis(); |
| 13 | + while (System.currentTimeMillis() <= start + maxWaitMs && !cond.get()) { |
| 14 | + try { |
| 15 | + Thread.sleep(10); |
| 16 | + } catch (InterruptedException ignore) {} |
| 17 | + } |
| 18 | + return cond.get(); |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + protected void setUp() { |
| 23 | + lib = Native.load("testlib", CallbacksTest.TestLibrary.class); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + protected void tearDown() { |
| 28 | + lib = null; |
| 29 | + } |
| 30 | + |
| 31 | + public void testGCCallbackOnFinalize() throws Exception { |
| 32 | + final boolean[] latch = { false }; |
| 33 | + Thread thread = new Thread(() -> { |
| 34 | + CallbacksTest.TestLibrary.VoidCallback cb = new CallbacksTest.TestLibrary.VoidCallback() { |
| 35 | + @Override |
| 36 | + public void callback() { |
| 37 | + latch[0] = true; |
| 38 | + } |
| 39 | + }; |
| 40 | + synchronized (latch) { |
| 41 | + lib.callVoidCallback(cb); |
| 42 | + latch.notifyAll(); |
| 43 | + try { |
| 44 | + latch.wait(); |
| 45 | + } catch (InterruptedException ignore) {} |
| 46 | + } |
| 47 | + }); |
| 48 | + thread.start(); |
| 49 | + |
| 50 | + CallbackReference ref; |
| 51 | + synchronized (latch) { |
| 52 | + if (!latch[0]) { |
| 53 | + latch.wait(); |
| 54 | + } |
| 55 | + assertTrue(latch[0]); |
| 56 | + // Assert thread is running and has registered a callback and master is running too |
| 57 | + assertTrue(thread.isAlive()); |
| 58 | + assertTrue(MasterAccessor.masterIsRunning()); |
| 59 | + assertFalse(MasterAccessor.getCleanerImpls().isEmpty()); |
| 60 | + assertEquals(1, CallbackReference.callbackMap.size()); |
| 61 | + ref = CallbackReference.callbackMap.values().iterator().next(); |
| 62 | + CallbacksTest.waitForGc(new CallbacksTest.Condition<CallbackReference>(ref) { |
| 63 | + public boolean evaluate(CallbackReference _ref) { |
| 64 | + return _ref.get() != null || CallbackReference.callbackMap.containsValue(_ref); |
| 65 | + } |
| 66 | + }); |
| 67 | + assertNotNull("Callback GC'd prematurely", ref.get()); |
| 68 | + assertTrue("Callback no longer in map", CallbackReference.callbackMap.containsValue(ref)); |
| 69 | + latch.notifyAll(); |
| 70 | + } |
| 71 | + thread.join(); |
| 72 | + |
| 73 | + // thread is no longer running, dummy is collectable, master is still running |
| 74 | + final Pointer cbstruct = ref.cbstruct; |
| 75 | + assertTrue(MasterAccessor.masterIsRunning()); |
| 76 | + CallbacksTest.waitForGc(new CallbacksTest.Condition<CallbackReference>(ref) { |
| 77 | + public boolean evaluate(CallbackReference _ref) { |
| 78 | + return _ref.get() != null || CallbackReference.callbackMap.containsValue(_ref); |
| 79 | + } |
| 80 | + }); |
| 81 | + assertNull("Callback not GC'd", ref.get()); |
| 82 | + assertFalse("Callback still in map", CallbackReference.callbackMap.containsValue(ref)); |
| 83 | + |
| 84 | + assertTrue("CleanerImpl still exists", |
| 85 | + waitFor(() -> MasterAccessor.getCleanerImpls().isEmpty(), 60_000)); // 1 minute |
| 86 | + |
| 87 | + thread = null; |
| 88 | + // thread is collectable -> wait until master terminates |
| 89 | + assertTrue("Master still running", |
| 90 | + waitFor(() -> !MasterAccessor.masterIsRunning(), 60_000)); // 1 minute |
| 91 | + } |
| 92 | +} |
0 commit comments