Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test): optimize NodeInfoServiceTest for testcase coverage #5333

Merged
merged 2 commits into from
Jul 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.tron.core.services;

import static org.mockito.Mockito.mock;

import com.alibaba.fastjson.JSON;
import com.google.protobuf.ByteString;
import io.grpc.ManagedChannelBuilder;
import java.net.InetSocketAddress;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.mockito.Mockito;
import org.tron.api.GrpcAPI.EmptyMessage;
import org.tron.api.WalletGrpc;
import org.tron.api.WalletGrpc.WalletBlockingStub;
Expand All @@ -13,6 +17,9 @@
import org.tron.common.utils.Sha256Hash;
import org.tron.common.utils.client.Configuration;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.net.P2pEventHandlerImpl;
import org.tron.core.net.peer.PeerManager;
import org.tron.p2p.connection.Channel;
import org.tron.program.Version;


Expand All @@ -21,12 +28,14 @@ public class NodeInfoServiceTest {

private NodeInfoService nodeInfoService;
private WitnessProductBlockService witnessProductBlockService;
private P2pEventHandlerImpl p2pEventHandler;
private String fullnode = Configuration.getByPath("testng.conf").getStringList("fullnode.ip.list")
.get(0);

public NodeInfoServiceTest(TronApplicationContext context) {
nodeInfoService = context.getBean("nodeInfoService", NodeInfoService.class);
witnessProductBlockService = context.getBean(WitnessProductBlockService.class);
p2pEventHandler = context.getBean(P2pEventHandlerImpl.class);
}

public void test() {
Expand All @@ -36,6 +45,15 @@ public void test() {
200, ByteString.EMPTY);
witnessProductBlockService.validWitnessProductTwoBlock(blockCapsule1);
witnessProductBlockService.validWitnessProductTwoBlock(blockCapsule2);

//add peer
InetSocketAddress a1 = new InetSocketAddress("127.0.0.1", 10001);
Channel c1 = mock(Channel.class);
Mockito.when(c1.getInetSocketAddress()).thenReturn(a1);
Mockito.when(c1.getInetAddress()).thenReturn(a1.getAddress());
p2pEventHandler.onConnect(c1);

//test setConnectInfo
NodeInfo nodeInfo = nodeInfoService.getNodeInfo();
Assert.assertEquals(nodeInfo.getConfigNodeInfo().getCodeVersion(), Version.getVersion());
Assert.assertEquals(nodeInfo.getCheatWitnessInfoMap().size(), 1);
Expand Down