| 50 | */ |
| 51 | //@SuppressWarnings("unused") |
| 52 | public class CallbacksTest extends TestCase implements Paths { |
| 53 | |
| 54 | // On OSX, on Oracle JVM 1.8+, pthread cleanup thinks the native thread is |
| 55 | // not attached, and the JVM never unmaps the defunct native thread. In |
| 56 | // order to avoid this situation causing tests to time out, we need to |
| 57 | // explicitly detach the native thread after our Java code is done with it. |
| 58 | // Also reproducible on Ubuntu 6 (x86-64), Java 6 |
| 59 | private static final boolean THREAD_DETACH_BUG = Platform.isMac() || (Platform.isLinux() && Platform.is64Bit()); |
| 60 | |
| 61 | private static final String UNICODE = "[\u0444]"; |
| 62 | |
| 63 | private static final double DOUBLE_MAGIC = -118.625d; |
| 64 | private static final float FLOAT_MAGIC = -118.625f; |
| 65 | |
| 66 | private static final int THREAD_TIMEOUT = 5000; |
| 67 | |
| 68 | protected void waitFor(Thread thread) { |
| 69 | long start = System.currentTimeMillis(); |
| 70 | while (thread.isAlive()) { |
| 71 | try { |
| 72 | Thread.sleep(10); |
| 73 | } catch (InterruptedException e) { |
| 74 | } |
| 75 | if (System.currentTimeMillis() - start > THREAD_TIMEOUT) { |
| 76 | fail("Timed out waiting for native thread " + thread |
| 77 | + " to detach and terminate"); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public static class SmallTestStructure extends Structure { |
| 83 | public static final List<String> FIELDS = createFieldsOrder("value"); |
| 84 | public double value; |
| 85 | public static int allocations = 0; |
| 86 | @Override |
| 87 | protected void allocateMemory(int size) { |
| 88 | super.allocateMemory(size); |
| 89 | ++allocations; |
| 90 | } |
| 91 | public SmallTestStructure() { } |
| 92 | public SmallTestStructure(Pointer p) { super(p); read(); } |
| 93 | @Override |
| 94 | protected List<String> getFieldOrder() { |
| 95 | return FIELDS; |
| 96 | } |
| 97 | } |
| 98 | public static class TestStructure extends Structure { |
| 99 | public static class ByValue extends TestStructure implements Structure.ByValue { } |
| 100 | public static interface TestCallback extends Callback { |
| 101 | TestStructure.ByValue callback(TestStructure.ByValue s); |
| 102 | } |
| 103 | |
| 104 | public static final List<String> FIELDS = createFieldsOrder("c", "s", "i", "j", "inner"); |
| 105 | public byte c; |
| 106 | public short s; |
| 107 | public int i; |
| 108 | public long j; |
| 109 | public SmallTestStructure inner; |