Skip to content

Commit

Permalink
Merge pull request tronprotocol#5747 from 317787106/hotfix/coverage_c…
Browse files Browse the repository at this point in the history
…hange

feat(test): resolve the test coverage changes of some classes
  • Loading branch information
lvs007 authored May 31, 2024
2 parents 3a3a37e + 9bb0ea3 commit d383177
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,139 @@
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<String> 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);
KeepAliveMessage message = new KeepAliveMessage(false, 6);
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<String> 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());
}
}
8 changes: 8 additions & 0 deletions framework/src/test/java/org/tron/core/tire/TrieTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

}

0 comments on commit d383177

Please sign in to comment.