From 09573b7c921de045a9ce8b54787fddb3505284ab Mon Sep 17 00:00:00 2001 From: jiangyuanshu <317787106@qq.com> Date: Thu, 22 Feb 2024 17:27:17 +0800 Subject: [PATCH 1/2] resolve the coverage changes that backupserver may start in testcase or not --- .../tron/common/backup/BackupManagerTest.java | 88 +++++++++++++++---- 1 file changed, 70 insertions(+), 18 deletions(-) diff --git a/framework/src/test/java/org/tron/common/backup/BackupManagerTest.java b/framework/src/test/java/org/tron/common/backup/BackupManagerTest.java index 8a5ff4f4bc2..9ace8f5b601 100644 --- a/framework/src/test/java/org/tron/common/backup/BackupManagerTest.java +++ b/framework/src/test/java/org/tron/common/backup/BackupManagerTest.java @@ -4,41 +4,62 @@ import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ExecutorService; import java.util.concurrent.ScheduledExecutorService; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.tron.common.backup.BackupManager.BackupStatusEnum; import org.tron.common.backup.message.KeepAliveMessage; +import org.tron.common.backup.socket.BackupServer; import org.tron.common.backup.socket.UdpEvent; import org.tron.common.parameter.CommonParameter; +import org.tron.common.utils.PublicMethod; import org.tron.core.Constant; import org.tron.core.config.args.Args; public class BackupManagerTest { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + private BackupManager manager; + private BackupServer backupServer; + + @Before + public void setUp() throws Exception { + Args.setParam(new String[] {"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); + CommonParameter.getInstance().setBackupPort(PublicMethod.chooseRandomPort()); + manager = new BackupManager(); + backupServer = new BackupServer(manager); + } + + @After + public void tearDown() { + Args.clearParam(); + } + @Test public void test() throws Exception { - String[] a = new String[0]; - Args.setParam(a, Constant.TESTNET_CONF); - CommonParameter parameter = CommonParameter.getInstance(); - parameter.setBackupPriority(8); + CommonParameter.getInstance().setBackupPriority(8); List members = new ArrayList<>(); members.add("127.0.0.2"); - parameter.setBackupMembers(members); + CommonParameter.getInstance().setBackupMembers(members); - BackupManager manager = new BackupManager(); - - Field field = manager.getClass().getDeclaredField("localIp"); + Field field = manager.getClass().getDeclaredField("localIp"); field.setAccessible(true); field.set(manager, "127.0.0.1"); - Assert.assertEquals(manager.getStatus(), BackupManager.BackupStatusEnum.MASTER); + Assert.assertEquals(BackupManager.BackupStatusEnum.MASTER, manager.getStatus()); - field = manager.getClass().getDeclaredField("executorService"); + field = manager.getClass().getDeclaredField("executorService"); field.setAccessible(true); ScheduledExecutorService executorService = (ScheduledExecutorService) field.get(manager); manager.init(); executorService.shutdown(); - Assert.assertEquals(manager.getStatus(), BackupManager.BackupStatusEnum.INIT); + Assert.assertEquals(BackupManager.BackupStatusEnum.INIT, manager.getStatus()); /* ip not in the members */ manager.setStatus(BackupManager.BackupStatusEnum.INIT); @@ -46,45 +67,76 @@ public void test() throws Exception { InetSocketAddress address = new InetSocketAddress("127.0.0.3", 1000); UdpEvent event = new UdpEvent(message, address); manager.handleEvent(event); - Assert.assertEquals(manager.getStatus(), BackupManager.BackupStatusEnum.INIT); + Assert.assertEquals(BackupManager.BackupStatusEnum.INIT, manager.getStatus()); /* ip not the member */ address = new InetSocketAddress("127.0.0.3", 1000); message = new KeepAliveMessage(false, 6); event = new UdpEvent(message, address); manager.handleEvent(event); - Assert.assertEquals(manager.getStatus(), BackupManager.BackupStatusEnum.INIT); + Assert.assertEquals(BackupManager.BackupStatusEnum.INIT, manager.getStatus()); /* keepAliveMessage.getFlag() || peerPriority > priority */ address = new InetSocketAddress("127.0.0.2", 1000); message = new KeepAliveMessage(false, 6); event = new UdpEvent(message, address); manager.handleEvent(event); - Assert.assertEquals(manager.getStatus(), BackupManager.BackupStatusEnum.INIT); + Assert.assertEquals(BackupStatusEnum.SLAVER, manager.getStatus()); /* keepAliveMessage.getFlag() || peerPriority > priority */ message = new KeepAliveMessage(false, 10); event = new UdpEvent(message, address); manager.handleEvent(event); - Assert.assertEquals(manager.getStatus(), BackupManager.BackupStatusEnum.SLAVER); + Assert.assertEquals(BackupManager.BackupStatusEnum.SLAVER, manager.getStatus()); /* keepAliveMessage.getFlag() || peerPriority > priority */ manager.setStatus(BackupManager.BackupStatusEnum.INIT); message = new KeepAliveMessage(true, 6); event = new UdpEvent(message, address); manager.handleEvent(event); - Assert.assertEquals(manager.getStatus(), BackupManager.BackupStatusEnum.SLAVER); + Assert.assertEquals(BackupManager.BackupStatusEnum.SLAVER, manager.getStatus()); manager.setStatus(BackupManager.BackupStatusEnum.MASTER); message = new KeepAliveMessage(false, 10); event = new UdpEvent(message, address); manager.handleEvent(event); - Assert.assertEquals(manager.getStatus(), BackupManager.BackupStatusEnum.MASTER); + Assert.assertEquals(BackupManager.BackupStatusEnum.MASTER, manager.getStatus()); message = new KeepAliveMessage(true, 10); event = new UdpEvent(message, address); manager.handleEvent(event); - Assert.assertEquals(manager.getStatus(), BackupManager.BackupStatusEnum.SLAVER); + Assert.assertEquals(BackupManager.BackupStatusEnum.SLAVER, manager.getStatus()); + + } + + @Test + public void testSendKeepAliveMessage() throws Exception { + CommonParameter parameter = CommonParameter.getInstance(); + parameter.setBackupPriority(8); + List members = new ArrayList<>(); + members.add("127.0.0.2"); + parameter.setBackupMembers(members); + + Field field = manager.getClass().getDeclaredField("localIp"); + field.setAccessible(true); + field.set(manager, "127.0.0.1"); + + Assert.assertEquals(manager.getStatus(), BackupManager.BackupStatusEnum.MASTER); + backupServer.initServer(); + manager.init(); + + Thread.sleep(parameter.getKeepAliveInterval() + 1000);//test send KeepAliveMessage + + field = manager.getClass().getDeclaredField("executorService"); + field.setAccessible(true); + ScheduledExecutorService executorService = (ScheduledExecutorService) field.get(manager); + executorService.shutdown(); + + Field field2 = backupServer.getClass().getDeclaredField("executor"); + field2.setAccessible(true); + ExecutorService executorService2 = (ExecutorService) field2.get(backupServer); + executorService2.shutdown(); + Assert.assertEquals(BackupManager.BackupStatusEnum.INIT, manager.getStatus()); } } From 9bb0ea36b2334404d14cb39289d9d8fe9890cead Mon Sep 17 00:00:00 2001 From: jiangyuanshu <317787106@qq.com> Date: Wed, 6 Mar 2024 18:06:04 +0800 Subject: [PATCH 2/2] feat(test):fix the coverage change of org.tron.core.capsule.utils.FastByteComparisons --- framework/src/test/java/org/tron/core/tire/TrieTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/framework/src/test/java/org/tron/core/tire/TrieTest.java b/framework/src/test/java/org/tron/core/tire/TrieTest.java index ba5c536c987..7005198ad8b 100644 --- a/framework/src/test/java/org/tron/core/tire/TrieTest.java +++ b/framework/src/test/java/org/tron/core/tire/TrieTest.java @@ -26,6 +26,7 @@ import org.bouncycastle.util.Arrays; import org.junit.Assert; import org.junit.Test; +import org.tron.core.capsule.utils.FastByteComparisons; import org.tron.core.capsule.utils.RLP; import org.tron.core.trie.TrieImpl; import org.tron.core.trie.TrieImpl.Node; @@ -151,4 +152,11 @@ private void assertFalse(byte[] key1, byte[] key2, TrieImpl trieCopy) { Assert.assertFalse(trieCopy.verifyProof(trieCopy.getRootHash(), key2, trieCopy.prove(key1))); } + @Test + public void testFastByteComparisons() { + byte[] test1 = new byte[] {0x00, 0x00, 0x01, 0x02, 0x03, 0x04}; + byte[] test2 = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04}; + Assert.assertEquals(0, FastByteComparisons.compareTo(test1, 1, 5, test2, 0, 5)); + } + }