Skip to content

Commit f2e064b

Browse files
author
morgan.peng
committed
feat(db): check second cache config in unit tests
1. check second cache config in unit tests
1 parent 74b34bc commit f2e064b

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

framework/src/test/java/org/tron/core/db2/SnapshotRootTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44
import java.util.ArrayList;
55
import java.util.Arrays;
66
import java.util.List;
7+
import java.util.Set;
8+
import java.util.stream.Collectors;
79
import lombok.AllArgsConstructor;
810
import lombok.EqualsAndHashCode;
911
import lombok.NoArgsConstructor;
1012
import org.junit.After;
1113
import org.junit.Assert;
1214
import org.junit.Before;
1315
import org.junit.Test;
16+
import org.springframework.util.CollectionUtils;
17+
import org.testng.collections.Sets;
1418
import org.tron.common.application.Application;
1519
import org.tron.common.application.ApplicationFactory;
1620
import org.tron.common.application.TronApplicationContext;
21+
import org.tron.common.cache.CacheStrategies;
1722
import org.tron.common.utils.FileUtil;
1823
import org.tron.common.utils.SessionOptional;
1924
import org.tron.core.Constant;
@@ -24,13 +29,23 @@
2429
import org.tron.core.db2.core.Snapshot;
2530
import org.tron.core.db2.core.SnapshotManager;
2631
import org.tron.core.db2.core.SnapshotRoot;
32+
import org.tron.core.exception.ItemNotFoundException;
2733

2834
public class SnapshotRootTest {
2935

3036
private TestRevokingTronStore tronDatabase;
3137
private TronApplicationContext context;
3238
private Application appT;
3339
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+
3449

3550
@Before
3651
public void init() {
@@ -113,6 +128,52 @@ public synchronized void testMergeList() {
113128
tronDatabase.close();
114129
}
115130

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+
116177
@NoArgsConstructor
117178
@AllArgsConstructor
118179
@EqualsAndHashCode

0 commit comments

Comments
 (0)