-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): improve unit test coverage
- Loading branch information
Showing
48 changed files
with
3,850 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.tron.common; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.beust.jcommander.internal.Lists; | ||
import java.util.HashMap; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.tron.common.entity.NodeInfo; | ||
import org.tron.common.entity.NodeInfo.MachineInfo; | ||
import org.tron.common.entity.NodeInfo.MachineInfo.DeadLockThreadInfo; | ||
|
||
public class EntityTest { | ||
|
||
private final MachineInfo machineInfo = new MachineInfo(); | ||
private final DeadLockThreadInfo deadLockThreadInfo = new DeadLockThreadInfo(); | ||
|
||
@Before | ||
public void setup() { | ||
deadLockThreadInfo.setName("name"); | ||
deadLockThreadInfo.setLockName("lockName"); | ||
deadLockThreadInfo.setLockOwner("lockOwner"); | ||
deadLockThreadInfo.setState("state"); | ||
deadLockThreadInfo.setStackTrace("stackTrace"); | ||
deadLockThreadInfo.setWaitTime(0L); | ||
deadLockThreadInfo.setBlockTime(0L); | ||
machineInfo.setDeadLockThreadInfoList(Lists.newArrayList(deadLockThreadInfo)); | ||
machineInfo.setJavaVersion("1.8"); | ||
machineInfo.setOsName("linux"); | ||
} | ||
|
||
@Test | ||
public void testMachineInfo() { | ||
machineInfo.setDeadLockThreadCount(3); | ||
assertTrue(CollectionUtils.isNotEmpty(machineInfo.getDeadLockThreadInfoList())); | ||
assertEquals(3, machineInfo.getDeadLockThreadCount()); | ||
|
||
} | ||
|
||
@Test | ||
public void testDeadLockThreadInfo() { | ||
assertEquals("name", deadLockThreadInfo.getName()); | ||
assertEquals("lockName", deadLockThreadInfo.getLockName()); | ||
assertEquals("lockOwner", deadLockThreadInfo.getLockOwner()); | ||
assertEquals("state", deadLockThreadInfo.getState()); | ||
assertEquals("stackTrace", deadLockThreadInfo.getStackTrace()); | ||
assertEquals(0, deadLockThreadInfo.getBlockTime()); | ||
assertEquals(0, deadLockThreadInfo.getWaitTime()); | ||
|
||
} | ||
|
||
@Test | ||
public void testNodeInfo() { | ||
NodeInfo nodeInfo = new NodeInfo(); | ||
nodeInfo.setTotalFlow(1L); | ||
nodeInfo.setCheatWitnessInfoMap(new HashMap<>()); | ||
assertEquals(1, nodeInfo.getTotalFlow()); | ||
assertNotNull(nodeInfo.getCheatWitnessInfoMap()); | ||
nodeInfo.setMachineInfo(machineInfo); | ||
nodeInfo.setBlock("block"); | ||
nodeInfo.setSolidityBlock("solidityBlock"); | ||
nodeInfo.transferToProtoEntity(); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
framework/src/test/java/org/tron/common/MultiLayoutPatternTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.tron.common; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.classic.spi.LoggingEvent; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.slf4j.LoggerFactory; | ||
import org.tron.common.log.layout.MultiLayoutPattern; | ||
|
||
public class MultiLayoutPatternTest { | ||
|
||
private MultiLayoutPattern multiLayoutPattern; | ||
private LoggerContext context; | ||
|
||
@Before | ||
public void setUp() { | ||
context = new LoggerContext(); | ||
multiLayoutPattern = new MultiLayoutPattern(); | ||
multiLayoutPattern.setContext(context); | ||
|
||
MultiLayoutPattern.Rule rule1 = new MultiLayoutPattern.Rule(); | ||
rule1.setLogger("com.example.app1"); | ||
assertNotNull(rule1.getLogger()); | ||
rule1.setPattern("%date [%thread] %-5level %logger{36} - %msg%n"); | ||
assertNotNull(rule1.getPattern()); | ||
rule1.setOutputPatternAsHeader(true); | ||
assertTrue(rule1.isOutputPatternAsHeader()); | ||
multiLayoutPattern.addRule(rule1); | ||
|
||
MultiLayoutPattern.Rule rule2 = new MultiLayoutPattern.Rule(); | ||
rule2.setLogger("com.example.app2"); | ||
rule2.setPattern("%msg%n"); | ||
multiLayoutPattern.addRule(rule2); | ||
|
||
multiLayoutPattern.start(); | ||
} | ||
|
||
@Test | ||
public void testEncodeForSpecificLogger() { | ||
ILoggingEvent event1 = createLoggingEvent("com.example.app1", "Test message 1"); | ||
byte[] encoded1 = multiLayoutPattern.encode(event1); | ||
String result1 = new String(encoded1); | ||
assertTrue(result1.contains("Test message 1")); | ||
|
||
ILoggingEvent event2 = createLoggingEvent("com.example.app2", "Test message 2"); | ||
byte[] encoded2 = multiLayoutPattern.encode(event2); | ||
String result2 = new String(encoded2); | ||
assertEquals("Test message 2\n", result2); | ||
} | ||
|
||
@Test | ||
public void testEncodeForRootLogger() { | ||
ILoggingEvent event = createLoggingEvent(Logger.ROOT_LOGGER_NAME, "Root logger message"); | ||
byte[] encoded = multiLayoutPattern.encode(event); | ||
String result = new String(encoded); | ||
assertFalse(result.contains("Root logger message")); | ||
} | ||
|
||
private ILoggingEvent createLoggingEvent(String loggerName, String message) { | ||
Logger logger = (Logger) LoggerFactory.getLogger(loggerName); | ||
return new LoggingEvent(loggerName, logger, Level.INFO, message, null, null); | ||
} | ||
|
||
} |
224 changes: 224 additions & 0 deletions
224
framework/src/test/java/org/tron/common/ParameterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
package org.tron.common; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.tron.common.parameter.RateLimiterInitialization.createHttpItem; | ||
import static org.tron.common.parameter.RateLimiterInitialization.createRpcItem; | ||
import static org.tron.core.Constant.ECKey_ENGINE; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.typesafe.config.ConfigFactory; | ||
import com.typesafe.config.ConfigObject; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.Test; | ||
import org.tron.common.parameter.CommonParameter; | ||
import org.tron.common.parameter.RateLimiterInitialization; | ||
import org.tron.common.parameter.RateLimiterInitialization.HttpRateLimiterItem; | ||
import org.tron.common.parameter.RateLimiterInitialization.RpcRateLimiterItem; | ||
import org.tron.p2p.dns.update.PublishConfig; | ||
|
||
public class ParameterTest { | ||
@Test | ||
public void testConstructor_ValidConfig() { | ||
String configStr = "{\"component\":\"testComponent\",\"strategy\":\"testStrategy\"," | ||
+ "\"paramString\":\"testParams\"}"; | ||
ConfigObject config = ConfigFactory.parseString(configStr).root().toConfig().root(); | ||
RpcRateLimiterItem item = new RpcRateLimiterItem(config); | ||
HttpRateLimiterItem item1 = new HttpRateLimiterItem(config); | ||
|
||
assertEquals("testComponent", item.getComponent()); | ||
assertEquals("testComponent", item1.getComponent()); | ||
assertEquals("testStrategy", item.getStrategy()); | ||
assertEquals("testParams", item.getParams()); | ||
assertNull(createRpcItem(null)); | ||
assertNull(createHttpItem(null)); | ||
assertNotNull(createRpcItem(config)); | ||
RateLimiterInitialization rateLimiterInitialization = new RateLimiterInitialization(); | ||
rateLimiterInitialization.setRpcMap(Lists.newArrayList(item)); | ||
assertFalse(rateLimiterInitialization.isHttpFlag()); | ||
assertTrue(rateLimiterInitialization.isRpcFlag()); | ||
} | ||
|
||
@Test | ||
public void testCommonParameter() { | ||
CommonParameter parameter = new CommonParameter(); | ||
parameter.setWitness(false); | ||
parameter.setSupportConstant(false); | ||
parameter.setMaxEnergyLimitForConstant(1000000L); | ||
parameter.setLruCacheSize(5); | ||
parameter.setMinTimeRatio(0); | ||
parameter.setMaxTimeRatio(20); | ||
parameter.setSaveInternalTx(false); | ||
parameter.setSaveFeaturedInternalTx(false); | ||
parameter.setLongRunningTime(60); | ||
parameter.setMaxHttpConnectNumber(5); | ||
|
||
assertEquals(StringUtils.EMPTY, parameter.getLogbackPath()); | ||
assertEquals(1000000L, parameter.getMaxEnergyLimitForConstant()); | ||
assertEquals(5, parameter.getLruCacheSize()); | ||
assertEquals(60, parameter.getLongRunningTime()); | ||
assertFalse(parameter.isHelp()); | ||
assertFalse(parameter.isSaveFeaturedInternalTx()); | ||
assertFalse(parameter.isSaveInternalTx()); | ||
CollectionUtils.isEmpty(parameter.getSeedNodes()); | ||
parameter.setChainId("123"); | ||
assertEquals("123", parameter.getChainId()); | ||
parameter.setNeedSyncCheck(false); | ||
assertFalse(parameter.isNeedSyncCheck()); | ||
parameter.setNodeDiscoveryEnable(false); | ||
assertFalse(parameter.isNodeDiscoveryEnable()); | ||
parameter.setNodeDiscoveryPersist(false); | ||
assertFalse(parameter.isNodeDiscoveryPersist()); | ||
parameter.setNodeEffectiveCheckEnable(false); | ||
assertFalse(parameter.isNodeEffectiveCheckEnable()); | ||
parameter.setNodeConnectionTimeout(500); | ||
assertEquals(500, parameter.getNodeConnectionTimeout()); | ||
parameter.setFetchBlockTimeout(500); | ||
assertEquals(500, parameter.getFetchBlockTimeout()); | ||
parameter.setNodeChannelReadTimeout(500); | ||
assertEquals(500, parameter.getNodeChannelReadTimeout()); | ||
parameter.setMaxConnections(500); | ||
assertEquals(500, parameter.getMaxConnections()); | ||
parameter.setMinConnections(500); | ||
assertEquals(500, parameter.getMinConnections()); | ||
parameter.setMinActiveConnections(500); | ||
assertEquals(500, parameter.getMinActiveConnections()); | ||
parameter.setMaxConnectionsWithSameIp(500); | ||
assertEquals(500, parameter.getMaxConnectionsWithSameIp()); | ||
parameter.setMaxTps(500); | ||
assertEquals(500, parameter.getMaxTps()); | ||
parameter.setMinParticipationRate(500); | ||
assertEquals(500, parameter.getMinParticipationRate()); | ||
parameter.setMaxConnectionsWithSameIp(500); | ||
assertEquals(500, parameter.getMaxConnectionsWithSameIp()); | ||
parameter.getP2pConfig(); | ||
parameter.setNodeLanIp("500"); | ||
assertEquals("500", parameter.getNodeLanIp()); | ||
parameter.setNodeP2pVersion(5); | ||
assertEquals(5, parameter.getNodeP2pVersion()); | ||
parameter.setNodeEnableIpv6(false); | ||
assertFalse(parameter.isNodeEnableIpv6()); | ||
parameter.setDnsTreeUrls(new ArrayList<>()); | ||
assertTrue(CollectionUtils.isEmpty(parameter.getDnsTreeUrls())); | ||
parameter.setDnsPublishConfig(new PublishConfig()); | ||
parameter.setSyncFetchBatchNum(500); | ||
assertEquals(500, parameter.getSyncFetchBatchNum()); | ||
parameter.setDebug(false); | ||
assertFalse(parameter.isDebug()); | ||
parameter.setFullNodeHttpPort(80); | ||
parameter.setSolidityHttpPort(80); | ||
parameter.setJsonRpcHttpFullNodePort(80); | ||
parameter.setJsonRpcHttpSolidityPort(80); | ||
parameter.setJsonRpcHttpPBFTPort(80); | ||
parameter.setRpcThreadNum(10); | ||
parameter.setSolidityThreads(5); | ||
parameter.setMaxConcurrentCallsPerConnection(10); | ||
parameter.setFlowControlWindow(20); | ||
parameter.setMaxConnectionIdleInMillis(1000); | ||
parameter.setBlockProducedTimeOut(500); | ||
parameter.setNetMaxTrxPerSecond(15); | ||
assertEquals(15, parameter.getNetMaxTrxPerSecond()); | ||
parameter.setMaxConnectionAgeInMillis(1500); | ||
parameter.setMaxMessageSize(200); | ||
parameter.setMaxHeaderListSize(100); | ||
parameter.setRpcReflectionServiceEnable(false); | ||
parameter.setValidateSignThreadNum(5); | ||
parameter.setMaintenanceTimeInterval(200); | ||
parameter.setProposalExpireTime(1000); | ||
parameter.setAllowCreationOfContracts(1); | ||
parameter.setAllowAdaptiveEnergy(1); | ||
parameter.setAllowDelegateResource(1); | ||
parameter.setAllowSameTokenName(1); | ||
parameter.setAllowTvmTransferTrc10(1); | ||
parameter.setAllowTvmConstantinople(1); | ||
parameter.setAllowTvmSolidity059(1); | ||
parameter.setForbidTransferToContract(1); | ||
parameter.setTcpNettyWorkThreadNum(5); | ||
parameter.setUdpNettyWorkThreadNum(5); | ||
parameter.setTrustNodeAddr("address"); | ||
parameter.setWalletExtensionApi(false); | ||
parameter.setEstimateEnergy(false); | ||
parameter.setEstimateEnergyMaxRetry(2); | ||
assertEquals(2, parameter.getEstimateEnergyMaxRetry()); | ||
parameter.setKeepAliveInterval(1000); | ||
parameter.setReceiveTcpMinDataLength(10); | ||
assertEquals(10, parameter.getReceiveTcpMinDataLength()); | ||
parameter.setOpenFullTcpDisconnect(false); | ||
parameter.setNodeDetectEnable(false); | ||
parameter.setAllowMultiSign(1); | ||
parameter.setVmTrace(false); | ||
parameter.setNeedToUpdateAsset(false); | ||
parameter.setTrxReferenceBlock("test"); | ||
parameter.setTrxCacheEnable(false); | ||
parameter.setAllowMarketTransaction(1); | ||
parameter.setAllowTransactionFeePool(1); | ||
parameter.setAllowBlackHoleOptimization(1); | ||
parameter.setAllowNewResourceModel(1); | ||
parameter.setEventSubscribe(false); | ||
parameter.setTrxExpirationTimeInMilliseconds(100); | ||
parameter.setAllowProtoFilterNum(10); | ||
parameter.setShieldedTransInPendingMaxCounts(1); | ||
parameter.setChangedDelegation(1); | ||
parameter.setActuatorSet(new HashSet<>()); | ||
parameter.setRateLimiterInitialization(new RateLimiterInitialization()); | ||
parameter.setRateLimiterGlobalQps(1000); | ||
parameter.setRateLimiterGlobalIpQps(100); | ||
parameter.getOverlay(); | ||
parameter.getEventPluginConfig(); | ||
parameter.getEventFilter(); | ||
parameter.setCryptoEngine(ECKey_ENGINE); | ||
parameter.setFullNodeHttpEnable(false); | ||
parameter.setSolidityNodeHttpEnable(false); | ||
parameter.setMaxTransactionPendingSize(500); | ||
parameter.setPendingTransactionTimeout(500); | ||
parameter.setNodeMetricsEnable(false); | ||
parameter.setMetricsStorageEnable(false); | ||
parameter.setInfluxDbIp("127.0.0.1"); | ||
parameter.setInfluxDbPort(90); | ||
parameter.setInfluxDbDatabase("InfluxDb"); | ||
assertEquals("InfluxDb", parameter.getInfluxDbDatabase()); | ||
parameter.setMetricsReportInterval(100); | ||
parameter.setMetricsPrometheusPort(3000); | ||
parameter.setAgreeNodeCount(10); | ||
parameter.setAllowPBFT(1); | ||
parameter.setPBFTHttpPort(70); | ||
parameter.setPBFTExpireNum(100); | ||
parameter.setAllowShieldedTRC20Transaction(10); | ||
parameter.setAllowTvmIstanbul(1); | ||
parameter.setAllowTvmFreeze(1); | ||
parameter.setAllowTvmVote(1); | ||
parameter.setAllowTvmLondon(1); | ||
parameter.setAllowTvmCompatibleEvm(1); | ||
parameter.setAllowHigherLimitForMaxCpuTimeOfOneTx(1); | ||
parameter.setHistoryBalanceLookup(false); | ||
parameter.setOpenPrintLog(false); | ||
parameter.setOpenTransactionSort(false); | ||
parameter.setAllowAssetOptimization(1); | ||
parameter.setAllowAccountAssetOptimization(1); | ||
parameter.setBlockCacheTimeout(60); | ||
parameter.setAllowNewReward(1); | ||
parameter.setAllowNewRewardAlgorithm(1); | ||
parameter.setMemoFee(100); | ||
parameter.setAllowDelegateOptimization(1); | ||
parameter.setUnfreezeDelayDays(10); | ||
parameter.setAllowOptimizedReturnValueOfChainId(1); | ||
parameter.setAllowDynamicEnergy(1); | ||
parameter.setDynamicEnergyThreshold(1); | ||
parameter.setDynamicEnergyIncreaseFactor(1); | ||
parameter.setDynamicEnergyMaxFactor(1); | ||
parameter.setDynamicConfigEnable(false); | ||
parameter.setDynamicConfigCheckInterval(10); | ||
parameter.setAllowTvmShangHai(1); | ||
parameter.setAllowCancelAllUnfreezeV2(1); | ||
parameter.setMaxUnsolidifiedBlocks(100); | ||
parameter.setAllowOldRewardOpt(1); | ||
parameter.setAllowEnergyAdjustment(1); | ||
parameter.setMaxCreateAccountTxSize(1000); | ||
} | ||
} |
Oops, something went wrong.