Repository navigation
Expand file tree
/
Copy pathKhaosDatabaseTest.java
More file actions
118 lines (101 loc) · 4.49 KB
/
Copy pathKhaosDatabaseTest.java
File metadata and controls
118 lines (101 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package org.tron.core.db;
import com.google.protobuf.ByteString;
import java.io.File;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import lombok.extern.slf4j.Slf4j;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.FileUtil;
import org.tron.core.Constant;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.args.Args;
import org.tron.core.exception.BadNumberBlockException;
import org.tron.core.exception.UnLinkedBlockException;
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.BlockHeader;
import org.tron.protos.Protocol.BlockHeader.raw;
@Slf4j
public class KhaosDatabaseTest {
private static final String dbPath = "output-khaosDatabase-test";
private static KhaosDatabase khaosDatabase;
private static TronApplicationContext context;
static {
Args.setParam(new String[]{"--output-directory", dbPath},
Constant.TEST_CONF);
context = new TronApplicationContext(DefaultConfig.class);
}
@BeforeClass
public static void init() {
Args.setParam(new String[]{"-d", dbPath}, Constant.TEST_CONF);
khaosDatabase = context.getBean(KhaosDatabase.class);
}
@AfterClass
public static void destroy() {
Args.clearParam();
FileUtil.deleteDir(new File(dbPath));
context.destroy();
}
@Test
public void testStartBlock() {
BlockCapsule blockCapsule = new BlockCapsule(Block.newBuilder().setBlockHeader(
BlockHeader.newBuilder().setRawData(raw.newBuilder().setParentHash(ByteString.copyFrom(
ByteArray
.fromHexString("0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81")))
)).build());
khaosDatabase.start(blockCapsule);
Assert.assertEquals(blockCapsule, khaosDatabase.getBlock(blockCapsule.getBlockId()));
}
@Test
public void testPushGetBlock() {
BlockCapsule blockCapsule = new BlockCapsule(Block.newBuilder().setBlockHeader(
BlockHeader.newBuilder().setRawData(raw.newBuilder().setParentHash(ByteString.copyFrom(
ByteArray
.fromHexString("0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81")))
)).build());
BlockCapsule blockCapsule2 = new BlockCapsule(Block.newBuilder().setBlockHeader(
BlockHeader.newBuilder().setRawData(raw.newBuilder().setParentHash(ByteString.copyFrom(
ByteArray
.fromHexString("9938a342238077182498b464ac029222ae169360e540d1fd6aee7c2ae9575a06")))
)).build());
khaosDatabase.start(blockCapsule);
try {
khaosDatabase.push(blockCapsule2);
} catch (UnLinkedBlockException | BadNumberBlockException e) {
}
Assert.assertEquals(blockCapsule2, khaosDatabase.getBlock(blockCapsule2.getBlockId()));
Assert.assertTrue("contain is error", khaosDatabase.containBlock(blockCapsule2.getBlockId()));
khaosDatabase.removeBlk(blockCapsule2.getBlockId());
Assert.assertNull("removeBlk is error", khaosDatabase.getBlock(blockCapsule2.getBlockId()));
}
@Test
public void checkWeakReference() throws UnLinkedBlockException, BadNumberBlockException {
BlockCapsule blockCapsule = new BlockCapsule(Block.newBuilder().setBlockHeader(
BlockHeader.newBuilder().setRawData(raw.newBuilder().setParentHash(ByteString.copyFrom(
ByteArray
.fromHexString("0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b82")))
.setNumber(0)
)).build());
BlockCapsule blockCapsule2 = new BlockCapsule(Block.newBuilder().setBlockHeader(
BlockHeader.newBuilder().setRawData(raw.newBuilder().setParentHash(ByteString.copyFrom(
blockCapsule.getBlockId().getBytes())).setNumber(1))).build());
Assert.assertEquals(blockCapsule.getBlockId(), blockCapsule2.getParentHash());
khaosDatabase.start(blockCapsule);
khaosDatabase.push(blockCapsule2);
khaosDatabase.removeBlk(blockCapsule.getBlockId());
logger.info("*** " + khaosDatabase.getBlock(blockCapsule.getBlockId()));
Object object = new Object();
Reference<Object> objectReference = new WeakReference<>(object);
blockCapsule = null;
object = null;
System.gc();
logger.info("***** object ref:" + objectReference.get());
Assert.assertNull(objectReference.get());
Assert.assertNull(khaosDatabase.getParentBlock(blockCapsule2.getBlockId()));
}
}