|
16 | 16 |
|
17 | 17 | package io.objectbox.exception;
|
18 | 18 |
|
19 |
| -import io.objectbox.AbstractObjectBoxTest; |
20 | 19 | import org.junit.Test;
|
21 | 20 |
|
| 21 | +import java.lang.ref.WeakReference; |
22 | 22 | import java.util.ArrayList;
|
23 | 23 | import java.util.List;
|
| 24 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; |
| 26 | + |
| 27 | +import io.objectbox.AbstractObjectBoxTest; |
| 28 | + |
24 | 29 |
|
25 | 30 | import static org.junit.Assert.assertEquals;
|
| 31 | +import static org.junit.Assert.assertFalse; |
| 32 | +import static org.junit.Assert.assertNotNull; |
| 33 | +import static org.junit.Assert.assertThrows; |
| 34 | +import static org.junit.Assert.assertTrue; |
26 | 35 |
|
| 36 | +/** |
| 37 | + * Tests related to {@link DbExceptionListener}. |
| 38 | + * |
| 39 | + * Note: this test has an equivalent in test-java-android integration tests. |
| 40 | + */ |
27 | 41 | public class ExceptionTest extends AbstractObjectBoxTest {
|
28 | 42 |
|
| 43 | + @Test |
| 44 | + public void exceptionListener_null_works() { |
| 45 | + store.setDbExceptionListener(null); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void exceptionListener_closedStore_works() { |
| 50 | + store.close(); |
| 51 | + store.setDbExceptionListener(e -> System.out.println("This is never called")); |
| 52 | + } |
| 53 | + |
| 54 | + private static AtomicInteger weakRefListenerCalled = new AtomicInteger(0); |
| 55 | + |
| 56 | + @Test |
| 57 | + public void exceptionListener_noLocalRef_works() throws InterruptedException { |
| 58 | + weakRefListenerCalled.set(0); |
| 59 | + // Note: do not use lambda, it would keep a reference to this class |
| 60 | + // and prevent garbage collection of the listener. |
| 61 | + //noinspection Convert2Lambda |
| 62 | + DbExceptionListener listenerNoRef = new DbExceptionListener() { |
| 63 | + @Override |
| 64 | + public void onDbException(Exception e) { |
| 65 | + System.out.println("Listener without strong reference is called"); |
| 66 | + weakRefListenerCalled.incrementAndGet(); |
| 67 | + } |
| 68 | + }; |
| 69 | + WeakReference<DbExceptionListener> weakReference = new WeakReference<>(listenerNoRef); |
| 70 | + |
| 71 | + // Set and clear local reference. |
| 72 | + store.setDbExceptionListener(listenerNoRef); |
| 73 | + //noinspection UnusedAssignment |
| 74 | + listenerNoRef = null; |
| 75 | + |
| 76 | + // Ensure weak reference is kept as JNI is holding on to listener using a "global ref". |
| 77 | + int triesClearWeakRef = 5; |
| 78 | + while (weakReference.get() != null) { |
| 79 | + if (--triesClearWeakRef == 0) break; |
| 80 | + System.out.println("Suggesting GC"); |
| 81 | + System.gc(); |
| 82 | + System.runFinalization(); |
| 83 | + //noinspection BusyWait |
| 84 | + Thread.sleep(300); |
| 85 | + } |
| 86 | + assertEquals("Failed to keep weak reference to listener", 0, triesClearWeakRef); |
| 87 | + assertNotNull(weakReference.get()); |
| 88 | + |
| 89 | + // Throw, listener should be called. |
| 90 | + assertThrows( |
| 91 | + DbException.class, |
| 92 | + () -> DbExceptionListenerJni.nativeThrowException(store.getNativeStore(), 0) |
| 93 | + ); |
| 94 | + assertEquals(1, weakRefListenerCalled.get()); |
| 95 | + |
| 96 | + // Remove reference from native side. |
| 97 | + store.setDbExceptionListener(null); |
| 98 | + |
| 99 | + // Try and succeed to release weak reference. |
| 100 | + triesClearWeakRef = 5; |
| 101 | + while (weakReference.get() != null) { |
| 102 | + if (--triesClearWeakRef == 0) break; |
| 103 | + System.out.println("Suggesting GC"); |
| 104 | + System.gc(); |
| 105 | + System.runFinalization(); |
| 106 | + //noinspection BusyWait |
| 107 | + Thread.sleep(300); |
| 108 | + } |
| 109 | + assertTrue("Failed to release weak reference to listener", triesClearWeakRef > 0); |
| 110 | + |
| 111 | + // Throw, listener should not be called. |
| 112 | + assertThrows( |
| 113 | + DbException.class, |
| 114 | + () -> DbExceptionListenerJni.nativeThrowException(store.getNativeStore(), 0) |
| 115 | + ); |
| 116 | + assertEquals(1, weakRefListenerCalled.get()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void exceptionListener_noref() throws InterruptedException { |
| 121 | + weakRefListenerCalled.set(0); |
| 122 | + |
| 123 | + //noinspection Convert2Lambda |
| 124 | + store.setDbExceptionListener(new DbExceptionListener() { |
| 125 | + @Override |
| 126 | + public void onDbException(Exception e) { |
| 127 | + System.out.println("Listener without reference is called"); |
| 128 | + weakRefListenerCalled.incrementAndGet(); |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + for (int i = 0; i < 5; i++) { |
| 133 | + System.gc(); |
| 134 | + System.runFinalization(); |
| 135 | + Thread.sleep(100); |
| 136 | + } |
| 137 | + |
| 138 | + // Throw, listener should be called. |
| 139 | + assertThrows( |
| 140 | + DbException.class, |
| 141 | + () -> DbExceptionListenerJni.nativeThrowException(store.getNativeStore(), 0) |
| 142 | + ); |
| 143 | + assertEquals(1, weakRefListenerCalled.get()); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void exceptionListener_removing_works() { |
| 148 | + AtomicBoolean replacedListenerCalled = new AtomicBoolean(false); |
| 149 | + DbExceptionListener listenerRemoved = e -> replacedListenerCalled.set(true); |
| 150 | + |
| 151 | + store.setDbExceptionListener(listenerRemoved); |
| 152 | + store.setDbExceptionListener(null); |
| 153 | + |
| 154 | + assertThrows( |
| 155 | + DbException.class, |
| 156 | + () -> DbExceptionListenerJni.nativeThrowException(store.getNativeStore(), 0) |
| 157 | + ); |
| 158 | + assertFalse("Should not have called removed DbExceptionListener.", replacedListenerCalled.get()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void exceptionListener_replacing_works() { |
| 163 | + AtomicBoolean replacedListenerCalled = new AtomicBoolean(false); |
| 164 | + DbExceptionListener listenerReplaced = e -> replacedListenerCalled.set(true); |
| 165 | + |
| 166 | + AtomicBoolean newListenerCalled = new AtomicBoolean(false); |
| 167 | + DbExceptionListener listenerNew = e -> newListenerCalled.set(true); |
| 168 | + |
| 169 | + store.setDbExceptionListener(listenerReplaced); |
| 170 | + store.setDbExceptionListener(listenerNew); |
| 171 | + |
| 172 | + assertThrows( |
| 173 | + DbException.class, |
| 174 | + () -> DbExceptionListenerJni.nativeThrowException(store.getNativeStore(), 0) |
| 175 | + ); |
| 176 | + assertFalse("Should not have called replaced DbExceptionListener.", replacedListenerCalled.get()); |
| 177 | + assertTrue("Failed to call new DbExceptionListener.", newListenerCalled.get()); |
| 178 | + } |
| 179 | + |
29 | 180 | @Test
|
30 | 181 | public void testThrowExceptions() {
|
31 | 182 | final List<Exception> exs = new ArrayList<>();
|
|
0 commit comments