|
| 1 | +package org.tron.core.db; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.doThrow; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.spy; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.lang.reflect.Field; |
| 10 | +import org.junit.AfterClass; |
| 11 | +import org.junit.Assert; |
| 12 | +import org.junit.BeforeClass; |
| 13 | +import org.junit.ClassRule; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.rules.TemporaryFolder; |
| 16 | +import org.rocksdb.RocksDB; |
| 17 | +import org.tron.common.TestConstants; |
| 18 | +import org.tron.common.storage.WriteOptionsWrapper; |
| 19 | +import org.tron.core.config.args.Args; |
| 20 | +import org.tron.core.db.common.DbSourceInter; |
| 21 | +import org.tron.core.store.CheckPointV2Store; |
| 22 | + |
| 23 | +public class CheckPointV2StoreTest { |
| 24 | + |
| 25 | + @ClassRule |
| 26 | + public static final TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 27 | + |
| 28 | + static { |
| 29 | + RocksDB.loadLibrary(); |
| 30 | + } |
| 31 | + |
| 32 | + @BeforeClass |
| 33 | + public static void initArgs() throws IOException { |
| 34 | + Args.setParam( |
| 35 | + new String[]{"-d", temporaryFolder.newFolder().toString()}, |
| 36 | + TestConstants.TEST_CONF |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + @AfterClass |
| 41 | + public static void destroy() { |
| 42 | + Args.clearParam(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testStubMethods() throws Exception { |
| 47 | + CheckPointV2Store store = new CheckPointV2Store("test-stubs"); |
| 48 | + try { |
| 49 | + byte[] key = "key".getBytes(); |
| 50 | + |
| 51 | + store.put(key, new byte[]{}); |
| 52 | + Assert.assertNull(store.get(key)); |
| 53 | + Assert.assertFalse(store.has(key)); |
| 54 | + store.forEach(item -> { |
| 55 | + }); |
| 56 | + Assert.assertNull(store.spliterator()); |
| 57 | + |
| 58 | + Field dbSourceField = TronDatabase.class.getDeclaredField("dbSource"); |
| 59 | + dbSourceField.setAccessible(true); |
| 60 | + DbSourceInter<byte[]> originalDbSource = |
| 61 | + (DbSourceInter<byte[]>) dbSourceField.get(store); |
| 62 | + DbSourceInter<byte[]> mockDbSource = mock(DbSourceInter.class); |
| 63 | + dbSourceField.set(store, mockDbSource); |
| 64 | + store.delete(key); |
| 65 | + dbSourceField.set(store, originalDbSource); |
| 66 | + |
| 67 | + java.lang.reflect.Method initMethod = |
| 68 | + CheckPointV2Store.class.getDeclaredMethod("init"); |
| 69 | + initMethod.setAccessible(true); |
| 70 | + initMethod.invoke(store); |
| 71 | + } finally { |
| 72 | + store.close(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testCloseWithRealResources() { |
| 78 | + CheckPointV2Store store = new CheckPointV2Store("test-close-real"); |
| 79 | + // Exercises the real writeOptions.close() and dbSource.closeDB() code paths |
| 80 | + store.close(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testCloseReleasesAllResources() throws Exception { |
| 85 | + CheckPointV2Store store = new CheckPointV2Store("test-close"); |
| 86 | + |
| 87 | + // Replace dbSource with a mock so we can verify closeDB() |
| 88 | + Field dbSourceField = TronDatabase.class.getDeclaredField("dbSource"); |
| 89 | + dbSourceField.setAccessible(true); |
| 90 | + DbSourceInter<byte[]> originalDbSource = (DbSourceInter<byte[]>) dbSourceField.get(store); |
| 91 | + DbSourceInter<byte[]> mockDbSource = mock(DbSourceInter.class); |
| 92 | + dbSourceField.set(store, mockDbSource); |
| 93 | + |
| 94 | + try { |
| 95 | + store.close(); |
| 96 | + |
| 97 | + verify(mockDbSource).closeDB(); |
| 98 | + } finally { |
| 99 | + originalDbSource.closeDB(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testCloseWhenDbSourceThrows() throws Exception { |
| 105 | + CheckPointV2Store store = new CheckPointV2Store("test-close-dbsource-throws"); |
| 106 | + |
| 107 | + Field dbSourceField = TronDatabase.class.getDeclaredField("dbSource"); |
| 108 | + dbSourceField.setAccessible(true); |
| 109 | + DbSourceInter<byte[]> originalDbSource = (DbSourceInter<byte[]>) dbSourceField.get(store); |
| 110 | + DbSourceInter<byte[]> mockDbSource = mock(DbSourceInter.class); |
| 111 | + doThrow(new RuntimeException("simulated dbSource failure")).when(mockDbSource).closeDB(); |
| 112 | + dbSourceField.set(store, mockDbSource); |
| 113 | + |
| 114 | + try { |
| 115 | + store.close(); |
| 116 | + } finally { |
| 117 | + originalDbSource.closeDB(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testCloseDbSourceWhenWriteOptionsThrows() throws Exception { |
| 123 | + CheckPointV2Store store = new CheckPointV2Store("test-close-exception"); |
| 124 | + |
| 125 | + // Replace child writeOptions with a spy that throws on close |
| 126 | + Field childWriteOptionsField = CheckPointV2Store.class.getDeclaredField("writeOptions"); |
| 127 | + childWriteOptionsField.setAccessible(true); |
| 128 | + WriteOptionsWrapper childWriteOptions = |
| 129 | + (WriteOptionsWrapper) childWriteOptionsField.get(store); |
| 130 | + WriteOptionsWrapper spyChildWriteOptions = spy(childWriteOptions); |
| 131 | + doThrow(new RuntimeException("simulated writeOptions failure")) |
| 132 | + .when(spyChildWriteOptions).close(); |
| 133 | + childWriteOptionsField.set(store, spyChildWriteOptions); |
| 134 | + |
| 135 | + // Replace parent writeOptions with a spy that throws on close |
| 136 | + Field parentWriteOptionsField = TronDatabase.class.getDeclaredField("writeOptions"); |
| 137 | + parentWriteOptionsField.setAccessible(true); |
| 138 | + WriteOptionsWrapper parentWriteOptions = |
| 139 | + (WriteOptionsWrapper) parentWriteOptionsField.get(store); |
| 140 | + WriteOptionsWrapper spyParentWriteOptions = spy(parentWriteOptions); |
| 141 | + doThrow(new RuntimeException("simulated parent writeOptions failure")) |
| 142 | + .when(spyParentWriteOptions).close(); |
| 143 | + parentWriteOptionsField.set(store, spyParentWriteOptions); |
| 144 | + |
| 145 | + // Replace dbSource with a mock |
| 146 | + Field dbSourceField = TronDatabase.class.getDeclaredField("dbSource"); |
| 147 | + dbSourceField.setAccessible(true); |
| 148 | + DbSourceInter<byte[]> originalDbSource = (DbSourceInter<byte[]>) dbSourceField.get(store); |
| 149 | + DbSourceInter<byte[]> mockDbSource = mock(DbSourceInter.class); |
| 150 | + dbSourceField.set(store, mockDbSource); |
| 151 | + |
| 152 | + try { |
| 153 | + store.close(); |
| 154 | + |
| 155 | + // dbSource.closeDB() must be called even though both writeOptions threw |
| 156 | + verify(mockDbSource).closeDB(); |
| 157 | + } finally { |
| 158 | + originalDbSource.closeDB(); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments