Skip to content

Commit 999a763

Browse files
Merge pull request #5117 from guoquanwu/feature/unit-test-port-conflict
feature(test): use random port in unit test
2 parents 74b34bc + 722a57a commit 999a763

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

framework/src/test/java/org/tron/common/utils/PublicMethod.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package org.tron.common.utils;
22

33
import com.google.protobuf.ByteString;
4+
5+
import java.io.IOException;
46
import java.math.BigInteger;
7+
import java.net.InetAddress;
8+
import java.net.Socket;
59
import java.util.Random;
610

711
import org.tron.api.GrpcAPI;
@@ -105,11 +109,30 @@ public static GrpcAPI.Return broadcastTransaction(
105109
return response;
106110
}
107111

112+
public static int chooseRandomPort() {
113+
return chooseRandomPort(10240, 65000);
114+
}
115+
108116
public static int chooseRandomPort(int min, int max) {
109-
return new Random().nextInt(max - min + 1) + min;
117+
int port = new Random().nextInt(max - min + 1) + min;
118+
try {
119+
while (!checkPortAvailable(port)) {
120+
port = new Random().nextInt(max - min + 1) + min;
121+
}
122+
} catch (IOException e) {
123+
return new Random().nextInt(max - min + 1) + min;
124+
}
125+
return port;
110126
}
111127

112-
public static int chooseRandomPort() {
113-
return new Random().nextInt(65530 - 1024) + 1024;
128+
private static boolean checkPortAvailable(int port) throws IOException {
129+
InetAddress theAddress = InetAddress.getByName("127.0.0.1");
130+
try (Socket socket = new Socket(theAddress, port)) {
131+
// only check
132+
socket.getPort();
133+
} catch (IOException e) {
134+
return true;
135+
}
136+
return false;
114137
}
115138
}

framework/src/test/java/org/tron/core/services/filter/LiteFnQueryHttpFilterTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
import org.junit.After;
1515
import org.junit.Assert;
1616
import org.junit.Before;
17-
import org.junit.Rule;
1817
import org.junit.Test;
19-
import org.junit.rules.ExpectedException;
2018
import org.slf4j.Logger;
2119
import org.slf4j.LoggerFactory;
2220
import org.tron.common.application.Application;
2321
import org.tron.common.application.ApplicationFactory;
2422
import org.tron.common.application.TronApplicationContext;
2523
import org.tron.common.utils.FileUtil;
24+
import org.tron.common.utils.PublicMethod;
2625
import org.tron.core.ChainBaseManager;
2726
import org.tron.core.Constant;
2827
import org.tron.core.config.DefaultConfig;
@@ -50,8 +49,12 @@ public class LiteFnQueryHttpFilterTest {
5049
*/
5150
@Before
5251
public void init() {
53-
Args.setParam(new String[]{"-d", dbPath}, Constant.TEST_CONF);
54-
//Args.getInstance().setFullNodeAllowShieldedTransactionArgs(false);
52+
Args.setParam(new String[]{"-d", dbPath, "--p2p-disable", "true"}, Constant.TEST_CONF);
53+
Args.getInstance().setFullNodeAllowShieldedTransactionArgs(false);
54+
Args.getInstance().setFullNodeHttpPort(PublicMethod.chooseRandomPort());
55+
Args.getInstance().setSolidityHttpPort(PublicMethod.chooseRandomPort());
56+
Args.getInstance().setPBFTHttpPort(PublicMethod.chooseRandomPort());
57+
5558
context = new TronApplicationContext(DefaultConfig.class);
5659
appTest = ApplicationFactory.create(context);
5760
FullNodeHttpApiService httpApiService = context

0 commit comments

Comments
 (0)