|
4 | 4 | import java.util.ArrayList; |
5 | 5 | import java.util.Arrays; |
6 | 6 | import java.util.List; |
| 7 | +import java.util.Set; |
| 8 | +import java.util.stream.Collectors; |
7 | 9 | import lombok.AllArgsConstructor; |
8 | 10 | import lombok.EqualsAndHashCode; |
9 | 11 | import lombok.NoArgsConstructor; |
10 | 12 | import org.junit.After; |
11 | 13 | import org.junit.Assert; |
12 | 14 | import org.junit.Before; |
13 | 15 | import org.junit.Test; |
| 16 | +import org.springframework.util.CollectionUtils; |
| 17 | +import org.testng.collections.Sets; |
14 | 18 | import org.tron.common.application.Application; |
15 | 19 | import org.tron.common.application.ApplicationFactory; |
16 | 20 | import org.tron.common.application.TronApplicationContext; |
| 21 | +import org.tron.common.cache.CacheStrategies; |
17 | 22 | import org.tron.common.utils.FileUtil; |
18 | 23 | import org.tron.common.utils.SessionOptional; |
19 | 24 | import org.tron.core.Constant; |
|
24 | 29 | import org.tron.core.db2.core.Snapshot; |
25 | 30 | import org.tron.core.db2.core.SnapshotManager; |
26 | 31 | import org.tron.core.db2.core.SnapshotRoot; |
| 32 | +import org.tron.core.exception.ItemNotFoundException; |
27 | 33 |
|
28 | 34 | public class SnapshotRootTest { |
29 | 35 |
|
30 | 36 | private TestRevokingTronStore tronDatabase; |
31 | 37 | private TronApplicationContext context; |
32 | 38 | private Application appT; |
33 | 39 | private SnapshotManager revokingDatabase; |
| 40 | + private final Set<String> noSecondCacheDBs = Sets.newHashSet(Arrays.asList("trans-cache", |
| 41 | + "exchange-v2","nullifier","accountTrie","transactionRetStore","accountid-index", |
| 42 | + "market_account","market_pair_to_price","recent-transaction","block-index","block", |
| 43 | + "market_pair_price_to_order","proposal","tree-block-index","IncrementalMerkleTree", |
| 44 | + "asset-issue","balance-trace","transactionHistoryStore","account-index","section-bloom", |
| 45 | + "exchange","market_order","account-trace","contract-state","trans")); |
| 46 | + private Set<String> allDBNames; |
| 47 | + private Set<String> allRevokingDBNames; |
| 48 | + |
34 | 49 |
|
35 | 50 | @Before |
36 | 51 | public void init() { |
@@ -113,6 +128,52 @@ public synchronized void testMergeList() { |
113 | 128 | tronDatabase.close(); |
114 | 129 | } |
115 | 130 |
|
| 131 | + @Test |
| 132 | + public void testSecondCacheCheck() |
| 133 | + throws ItemNotFoundException { |
| 134 | + revokingDatabase = context.getBean(SnapshotManager.class); |
| 135 | + allRevokingDBNames = parseRevokingDBNames(context); |
| 136 | + allDBNames = Arrays.stream(new File("output_revokingStore_test/database").list()) |
| 137 | + .collect(Collectors.toSet()); |
| 138 | + if (CollectionUtils.isEmpty(allDBNames)) { |
| 139 | + throw new ItemNotFoundException("No DBs found"); |
| 140 | + } |
| 141 | + allDBNames.removeAll(noSecondCacheDBs); |
| 142 | + allDBNames.removeAll(CacheStrategies.CACHE_DBS); |
| 143 | + allDBNames.retainAll(allRevokingDBNames); |
| 144 | + org.junit.Assert.assertEquals(String.format("New added dbs %s " |
| 145 | + + "shall consider to add second cache or add to noNeedCheckDBs!", |
| 146 | + allDBNames.stream().collect(Collectors.joining(","))), allDBNames.size(), 0); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testSecondCacheCheckAddDb() |
| 151 | + throws ItemNotFoundException { |
| 152 | + revokingDatabase = context.getBean(SnapshotManager.class); |
| 153 | + allRevokingDBNames = parseRevokingDBNames(context); |
| 154 | + allRevokingDBNames.add("secondCheckTestDB"); |
| 155 | + FileUtil.createDirIfNotExists("output_revokingStore_test/database/secondCheckTestDB"); |
| 156 | + allDBNames = Arrays.stream(new File("output_revokingStore_test/database").list()) |
| 157 | + .collect(Collectors.toSet()); |
| 158 | + FileUtil.deleteDir(new File("output_revokingStore_test/database/secondCheckTestDB")); |
| 159 | + if (CollectionUtils.isEmpty(allDBNames)) { |
| 160 | + throw new ItemNotFoundException("No DBs found"); |
| 161 | + } |
| 162 | + allDBNames.removeAll(noSecondCacheDBs); |
| 163 | + allDBNames.removeAll(CacheStrategies.CACHE_DBS); |
| 164 | + allDBNames.retainAll(allRevokingDBNames); |
| 165 | + org.junit.Assert.assertTrue(String.format("New added dbs %s " |
| 166 | + + "check second cache failed!", |
| 167 | + allDBNames.stream().collect(Collectors.joining(","))), allDBNames.size() == 1); |
| 168 | + } |
| 169 | + |
| 170 | + private Set<String> parseRevokingDBNames(TronApplicationContext context) { |
| 171 | + SnapshotManager snapshotManager = context.getBean(SnapshotManager.class); |
| 172 | + return snapshotManager.getDbs().stream().map(chainbase -> |
| 173 | + chainbase.getDbName()).collect(Collectors.toSet()); |
| 174 | + } |
| 175 | + |
| 176 | + |
116 | 177 | @NoArgsConstructor |
117 | 178 | @AllArgsConstructor |
118 | 179 | @EqualsAndHashCode |
|
0 commit comments