Skip to content

Commit 7015fa3

Browse files
authored
fix(test): remove all junit dir and add timeout for gRPC call (#6441)
* remove all junit dir under tmp * add withDeadlineAfter for newBlockingStub * use TimeoutInterceptor for each grpc call * optimize SyncBlockChainMsgHandlerTest
1 parent 3a1a5d0 commit 7015fa3

File tree

8 files changed

+121
-50
lines changed

8 files changed

+121
-50
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.tron.common.utils;
2+
3+
import io.grpc.CallOptions;
4+
import io.grpc.Channel;
5+
import io.grpc.ClientCall;
6+
import io.grpc.ClientInterceptor;
7+
import io.grpc.MethodDescriptor;
8+
import java.util.concurrent.TimeUnit;
9+
10+
11+
public class TimeoutInterceptor implements ClientInterceptor {
12+
13+
private final long timeout;
14+
15+
/**
16+
* @param timeout ms
17+
*/
18+
public TimeoutInterceptor(long timeout) {
19+
this.timeout = timeout;
20+
}
21+
22+
@Override
23+
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
24+
MethodDescriptor<ReqT, RespT> method,
25+
CallOptions callOptions,
26+
Channel next) {
27+
return next.newCall(method,
28+
callOptions.withDeadlineAfter(timeout, TimeUnit.MILLISECONDS));
29+
}
30+
}

framework/src/test/java/org/tron/core/event/BlockEventGetTest.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
import java.util.List;
1212
import java.util.concurrent.atomic.AtomicInteger;
1313
import lombok.extern.slf4j.Slf4j;
14-
import org.junit.After;
14+
import org.junit.AfterClass;
1515
import org.junit.Assert;
1616
import org.junit.Before;
17+
import org.junit.BeforeClass;
1718
import org.junit.ClassRule;
1819
import org.junit.Test;
1920
import org.junit.rules.TemporaryFolder;
@@ -63,26 +64,33 @@ public class BlockEventGetTest extends BlockGenerate {
6364
protected Manager dbManager;
6465
long currentHeader = -1;
6566
private TronNetDelegate tronNetDelegate;
66-
private TronApplicationContext context;
67+
private static TronApplicationContext context;
6768

6869

6970
static LocalDateTime localDateTime = LocalDateTime.now();
7071
private long time = ZonedDateTime.of(localDateTime,
7172
ZoneId.systemDefault()).toInstant().toEpochMilli();
7273

73-
protected void initDbPath() throws IOException {
74-
dbPath = temporaryFolder.newFolder().toString();
74+
75+
public static String dbPath() {
76+
try {
77+
return temporaryFolder.newFolder().toString();
78+
} catch (IOException e) {
79+
Assert.fail("create temp folder failed");
80+
}
81+
return null;
82+
}
83+
84+
@BeforeClass
85+
public static void init() {
86+
Args.setParam(new String[] {"--output-directory", dbPath()}, Constant.TEST_CONF);
87+
context = new TronApplicationContext(DefaultConfig.class);
7588
}
7689

7790
@Before
7891
public void before() throws IOException {
79-
initDbPath();
80-
logger.info("Full node running.");
81-
Args.setParam(new String[] {"-d", dbPath}, Constant.TEST_CONF);
8292
Args.getInstance().setNodeListenPort(10000 + port.incrementAndGet());
8393

84-
context = new TronApplicationContext(DefaultConfig.class);
85-
8694
dbManager = context.getBean(Manager.class);
8795
setManager(dbManager);
8896

@@ -91,7 +99,7 @@ public void before() throws IOException {
9199
tronNetDelegate = context.getBean(TronNetDelegate.class);
92100
tronNetDelegate.setExit(false);
93101
currentHeader = dbManager.getDynamicPropertiesStore()
94-
.getLatestBlockHeaderNumberFromDB();
102+
.getLatestBlockHeaderNumberFromDB();
95103

96104
ByteString addressBS = ByteString.copyFrom(address);
97105
WitnessCapsule witnessCapsule = new WitnessCapsule(addressBS);
@@ -108,8 +116,10 @@ public void before() throws IOException {
108116
dps.saveAllowTvmShangHai(1);
109117
}
110118

111-
@After
112-
public void after() throws IOException {
119+
@AfterClass
120+
public static void after() throws IOException {
121+
context.destroy();
122+
Args.clearParam();
113123
}
114124

115125
@Test
@@ -132,13 +142,13 @@ public void test() throws Exception {
132142
+ "57c973388f044038eff0e6474425b38037e75e66d6b3047647290605449c7764736f6c63430008140033";
133143
Protocol.Transaction trx = TvmTestUtils.generateDeploySmartContractAndGetTransaction(
134144
"TestTRC20", address, "[{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\""
135-
+ ":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\":\"address\"}"
136-
+ ",{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\","
137-
+ "\"type\":\"event\"}]", code, 0, (long) 1e9, 100, null, 1);
145+
+ ":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\""
146+
+ ":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\""
147+
+ ":\"Transfer\",\"type\":\"event\"}]", code, 0, (long) 1e9, 100, null, 1);
138148
trx = trx.toBuilder().addRet(
139-
Protocol.Transaction.Result.newBuilder()
140-
.setContractRetValue(Protocol.Transaction.Result.contractResult.SUCCESS_VALUE)
141-
.build()).build();
149+
Protocol.Transaction.Result.newBuilder()
150+
.setContractRetValue(Protocol.Transaction.Result.contractResult.SUCCESS_VALUE)
151+
.build()).build();
142152

143153
Protocol.Block block = getSignedBlock(witnessCapsule.getAddress(), time, privateKey);
144154
BlockCapsule blockCapsule = new BlockCapsule(block.toBuilder().addTransactions(trx).build());

framework/src/test/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandlerTest.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package org.tron.core.net.messagehandler;
22

3+
import java.io.IOException;
34
import java.lang.reflect.Field;
45
import java.lang.reflect.InvocationTargetException;
56
import java.lang.reflect.Method;
67
import java.net.InetSocketAddress;
78
import java.util.ArrayList;
89
import java.util.List;
9-
import org.junit.After;
10+
import org.junit.AfterClass;
1011
import org.junit.Assert;
1112
import org.junit.Before;
12-
import org.junit.Rule;
13+
import org.junit.BeforeClass;
14+
import org.junit.ClassRule;
1315
import org.junit.Test;
1416
import org.junit.rules.TemporaryFolder;
1517
import org.tron.common.application.TronApplicationContext;
@@ -26,17 +28,29 @@
2628

2729
public class SyncBlockChainMsgHandlerTest {
2830

29-
private TronApplicationContext context;
31+
private static TronApplicationContext context;
3032
private SyncBlockChainMsgHandler handler;
3133
private PeerConnection peer;
32-
@Rule
33-
public TemporaryFolder temporaryFolder = new TemporaryFolder();
34+
@ClassRule
35+
public static final TemporaryFolder temporaryFolder = new TemporaryFolder();
36+
37+
public static String dbPath() {
38+
try {
39+
return temporaryFolder.newFolder().toString();
40+
} catch (IOException e) {
41+
Assert.fail("create temp folder failed");
42+
}
43+
return null;
44+
}
45+
46+
@BeforeClass
47+
public static void before() {
48+
Args.setParam(new String[] {"--output-directory", dbPath()}, Constant.TEST_CONF);
49+
context = new TronApplicationContext(DefaultConfig.class);
50+
}
3451

3552
@Before
3653
public void init() throws Exception {
37-
Args.setParam(new String[]{"--output-directory",
38-
temporaryFolder.newFolder().toString(), "--debug"}, Constant.TEST_CONF);
39-
context = new TronApplicationContext(DefaultConfig.class);
4054
handler = context.getBean(SyncBlockChainMsgHandler.class);
4155
peer = context.getBean(PeerConnection.class);
4256
Channel c1 = new Channel();
@@ -65,16 +79,16 @@ public void testProcessMessage() throws Exception {
6579
blockIds.add(new BlockCapsule.BlockId());
6680
SyncBlockChainMessage message = new SyncBlockChainMessage(blockIds);
6781
Method method = handler.getClass().getDeclaredMethod(
68-
"check", PeerConnection.class, SyncBlockChainMessage.class);
82+
"check", PeerConnection.class, SyncBlockChainMessage.class);
6983
method.setAccessible(true);
70-
boolean f = (boolean)method.invoke(handler, peer, message);
84+
boolean f = (boolean) method.invoke(handler, peer, message);
7185
Assert.assertNotNull(message.getAnswerMessage());
7286
Assert.assertNotNull(message.toString());
7387
Assert.assertNotNull(((BlockInventoryMessage) message).getAnswerMessage());
7488
Assert.assertFalse(f);
7589
method.invoke(handler, peer, message);
7690
method.invoke(handler, peer, message);
77-
f = (boolean)method.invoke(handler, peer, message);
91+
f = (boolean) method.invoke(handler, peer, message);
7892
Assert.assertFalse(f);
7993

8094
Method method1 = handler.getClass().getDeclaredMethod(
@@ -93,8 +107,9 @@ public void testProcessMessage() throws Exception {
93107
Assert.assertEquals(1, list.size());
94108
}
95109

96-
@After
97-
public void destroy() {
110+
@AfterClass
111+
public static void destroy() {
112+
context.destroy();
98113
Args.clearParam();
99114
}
100115

framework/src/test/java/org/tron/core/services/RpcApiServicesTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.tron.common.utils.ByteArray;
5454
import org.tron.common.utils.PublicMethod;
5555
import org.tron.common.utils.Sha256Hash;
56+
import org.tron.common.utils.TimeoutInterceptor;
5657
import org.tron.core.Constant;
5758
import org.tron.core.Wallet;
5859
import org.tron.core.capsule.AccountCapsule;
@@ -159,12 +160,15 @@ public static void init() throws IOException {
159160

160161
channelFull = ManagedChannelBuilder.forTarget(fullNode)
161162
.usePlaintext()
163+
.intercept(new TimeoutInterceptor(5000))
162164
.build();
163165
channelPBFT = ManagedChannelBuilder.forTarget(pBFTNode)
164166
.usePlaintext()
167+
.intercept(new TimeoutInterceptor(5000))
165168
.build();
166169
channelSolidity = ManagedChannelBuilder.forTarget(solidityNode)
167170
.usePlaintext()
171+
.intercept(new TimeoutInterceptor(5000))
168172
.build();
169173
context = new TronApplicationContext(DefaultConfig.class);
170174
databaseBlockingStubFull = DatabaseGrpc.newBlockingStub(channelFull);

framework/src/test/java/org/tron/core/services/WalletApiTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.grpc.ManagedChannelBuilder;
44
import java.io.IOException;
5+
import java.util.concurrent.TimeUnit;
56
import lombok.extern.slf4j.Slf4j;
67
import org.junit.AfterClass;
78
import org.junit.Assert;
@@ -15,6 +16,7 @@
1516
import org.tron.common.application.ApplicationFactory;
1617
import org.tron.common.application.TronApplicationContext;
1718
import org.tron.common.utils.PublicMethod;
19+
import org.tron.common.utils.TimeoutInterceptor;
1820
import org.tron.core.Constant;
1921
import org.tron.core.config.DefaultConfig;
2022
import org.tron.core.config.args.Args;
@@ -32,7 +34,7 @@ public class WalletApiTest {
3234

3335
@BeforeClass
3436
public static void init() throws IOException {
35-
Args.setParam(new String[]{ "-d", temporaryFolder.newFolder().toString(),
37+
Args.setParam(new String[] {"-d", temporaryFolder.newFolder().toString(),
3638
"--p2p-disable", "true"}, Constant.TEST_CONF);
3739
Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort());
3840
Args.getInstance().setRpcEnable(true);
@@ -47,6 +49,7 @@ public void listNodesTest() {
4749
Args.getInstance().getRpcPort());
4850
io.grpc.ManagedChannel channel = ManagedChannelBuilder.forTarget(fullNode)
4951
.usePlaintext()
52+
.intercept(new TimeoutInterceptor(5000))
5053
.build();
5154
try {
5255
WalletGrpc.WalletBlockingStub walletStub = WalletGrpc.newBlockingStub(channel);

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.tron.common.application.ApplicationFactory;
2222
import org.tron.common.application.TronApplicationContext;
2323
import org.tron.common.utils.PublicMethod;
24+
import org.tron.common.utils.TimeoutInterceptor;
2425
import org.tron.core.ChainBaseManager;
2526
import org.tron.core.Constant;
2627
import org.tron.core.config.DefaultConfig;
@@ -51,7 +52,7 @@ public class LiteFnQueryGrpcInterceptorTest {
5152
*/
5253
@BeforeClass
5354
public static void init() throws IOException {
54-
Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF);
55+
Args.setParam(new String[] {"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF);
5556
Args.getInstance().setRpcEnable(true);
5657
Args.getInstance().setRpcPort(PublicMethod.chooseRandomPort());
5758
Args.getInstance().setRpcSolidityEnable(true);
@@ -60,20 +61,23 @@ public static void init() throws IOException {
6061
Args.getInstance().setRpcOnPBFTPort(PublicMethod.chooseRandomPort());
6162
Args.getInstance().setP2pDisable(true);
6263
String fullnode = String.format("%s:%d", Constant.LOCAL_HOST,
63-
Args.getInstance().getRpcPort());
64+
Args.getInstance().getRpcPort());
6465
String solidityNode = String.format("%s:%d", Constant.LOCAL_HOST,
65-
Args.getInstance().getRpcOnSolidityPort());
66+
Args.getInstance().getRpcOnSolidityPort());
6667
String pBFTNode = String.format("%s:%d", Constant.LOCAL_HOST,
6768
Args.getInstance().getRpcOnPBFTPort());
6869
channelFull = ManagedChannelBuilder.forTarget(fullnode)
69-
.usePlaintext()
70-
.build();
70+
.usePlaintext()
71+
.intercept(new TimeoutInterceptor(5000))
72+
.build();
7173
channelSolidity = ManagedChannelBuilder.forTarget(solidityNode)
7274
.usePlaintext()
75+
.intercept(new TimeoutInterceptor(5000))
7376
.build();
7477
channelpBFT = ManagedChannelBuilder.forTarget(pBFTNode)
75-
.usePlaintext()
76-
.build();
78+
.usePlaintext()
79+
.intercept(new TimeoutInterceptor(5000))
80+
.build();
7781
context = new TronApplicationContext(DefaultConfig.class);
7882
blockingStubFull = WalletGrpc.newBlockingStub(channelFull);
7983
blockingStubSolidity = WalletSolidityGrpc.newBlockingStub(channelSolidity);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Collections;
1515
import java.util.List;
1616
import java.util.Objects;
17+
import java.util.concurrent.TimeUnit;
1718
import lombok.extern.slf4j.Slf4j;
1819
import org.junit.AfterClass;
1920
import org.junit.BeforeClass;
@@ -32,6 +33,7 @@
3233
import org.tron.common.application.ApplicationFactory;
3334
import org.tron.common.application.TronApplicationContext;
3435
import org.tron.common.utils.PublicMethod;
36+
import org.tron.common.utils.TimeoutInterceptor;
3537
import org.tron.core.Constant;
3638
import org.tron.core.config.DefaultConfig;
3739
import org.tron.core.config.args.Args;
@@ -73,12 +75,15 @@ public static void init() throws IOException {
7375

7476
channelFull = ManagedChannelBuilder.forTarget(fullNode)
7577
.usePlaintext()
78+
.intercept(new TimeoutInterceptor(5000))
7679
.build();
7780
channelPBFT = ManagedChannelBuilder.forTarget(pBFTNode)
7881
.usePlaintext()
82+
.intercept(new TimeoutInterceptor(5000))
7983
.build();
8084
channelSolidity = ManagedChannelBuilder.forTarget(solidityNode)
8185
.usePlaintext()
86+
.intercept(new TimeoutInterceptor(5000))
8287
.build();
8388

8489
context = new TronApplicationContext(DefaultConfig.class);

0 commit comments

Comments
 (0)