Skip to content

Commit f0dfdea

Browse files
Little-Peonyclaude
andcommitted
test(framework,plugins): fix flaky tests in bandwidth, stats and db-move
- BandWidthRuntimeTest: deploy contract before setDebug(true) to avoid CPU timeout triggered by debug mode during contract deployment - TronStatsManagerTest: capture P2pStats snapshot before invoking work() and compare against snapshot instead of hardcoded 0, preventing failures when another test starts the real P2pService with non-zero stats - DbMoveTest: add @after cleanup of output-directory-toolkit so LevelDB and RocksDB tests don't share the same relative destination path - PeerStatusCheckMockTest: replace 5-second sleep with mock executor; verify scheduleWithFixedDelay args and run the captured Runnable directly Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a8d650f commit f0dfdea

4 files changed

Lines changed: 65 additions & 11 deletions

File tree

framework/src/test/java/org/tron/common/runtime/vm/BandWidthRuntimeTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.BeforeClass;
2525
import org.junit.Test;
2626
import org.tron.common.BaseTest;
27+
import org.tron.common.parameter.CommonParameter;
2728
import org.tron.common.runtime.RuntimeImpl;
2829
import org.tron.common.runtime.TvmTestUtils;
2930
import org.tron.common.utils.Commons;
@@ -153,8 +154,10 @@ public void testSuccess() {
153154

154155
@Test
155156
public void testSuccessNoBandd() {
157+
boolean originalDebug = CommonParameter.getInstance().isDebug();
156158
try {
157159
byte[] contractAddress = createContract();
160+
CommonParameter.getInstance().setDebug(true);
158161
TriggerSmartContract triggerContract = TvmTestUtils.createTriggerContract(contractAddress,
159162
"setCoin(uint256)", "50", false,
160163
0, Commons.decodeFromBase58Check(TriggerOwnerTwoAddress));
@@ -185,6 +188,8 @@ public void testSuccessNoBandd() {
185188
balance);
186189
} catch (TronException e) {
187190
Assert.assertNotNull(e);
191+
} finally {
192+
CommonParameter.getInstance().setDebug(originalDebug);
188193
}
189194
}
190195

@@ -254,4 +259,4 @@ public void testMaxContractResultSize() {
254259
}
255260
Assert.assertEquals(2, maxSize);
256261
}
257-
}
262+
}
Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package org.tron.core.net.peer;
22

3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.eq;
35
import static org.mockito.Mockito.doThrow;
6+
import static org.mockito.Mockito.mock;
47
import static org.mockito.Mockito.spy;
58

9+
import java.util.concurrent.ScheduledExecutorService;
10+
import java.util.concurrent.TimeUnit;
611
import org.junit.After;
712
import org.junit.Assert;
813
import org.junit.Test;
914
import org.mockito.Mockito;
15+
import org.tron.common.utils.ReflectUtils;
1016

1117
public class PeerStatusCheckMockTest {
1218
@After
@@ -15,17 +21,26 @@ public void clearMocks() {
1521
}
1622

1723
@Test
18-
public void testInitException() throws InterruptedException {
24+
public void testInitException() {
1925
PeerStatusCheck peerStatusCheck = spy(new PeerStatusCheck());
26+
ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
27+
ReflectUtils.setFieldValue(peerStatusCheck, "peerStatusCheckExecutor", executor);
2028
doThrow(new RuntimeException("test exception")).when(peerStatusCheck).statusCheck();
29+
2130
peerStatusCheck.init();
2231

23-
// the initialDelay of scheduleWithFixedDelay is 5s; wait for at least one execution
24-
Thread.sleep(5000L);
32+
Mockito.verify(executor).scheduleWithFixedDelay(any(Runnable.class), eq(5L), eq(2L),
33+
eq(TimeUnit.SECONDS));
34+
Runnable scheduledTask = Mockito.mockingDetails(executor).getInvocations().stream()
35+
.filter(invocation -> invocation.getMethod().getName().equals("scheduleWithFixedDelay"))
36+
.map(invocation -> (Runnable) invocation.getArgument(0))
37+
.findFirst()
38+
.orElseThrow(() -> new AssertionError("scheduled task was not registered"));
39+
40+
scheduledTask.run();
2541

26-
// Verify statusCheck() was invoked by the scheduler and the exception was handled gracefully
27-
Mockito.verify(peerStatusCheck, Mockito.atLeastOnce()).statusCheck();
42+
Mockito.verify(peerStatusCheck).statusCheck();
2843
Assert.assertNotNull(peerStatusCheck);
2944
}
3045

31-
}
46+
}

framework/src/test/java/org/tron/core/net/services/TronStatsManagerTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
import org.junit.Assert;
99
import org.junit.Test;
10+
import org.tron.core.net.TronNetService;
1011
import org.tron.core.net.service.statistics.NodeStatistics;
1112
import org.tron.core.net.service.statistics.TronStatsManager;
13+
import org.tron.p2p.stats.P2pStats;
1214
import org.tron.protos.Protocol;
1315

1416
public class TronStatsManagerTest {
@@ -50,14 +52,20 @@ public void testWork() throws Exception {
5052
Assert.assertEquals(field3.get(manager), 1L);
5153
Assert.assertEquals(field4.get(manager), 1L);
5254

55+
P2pStats statsSnapshot = TronNetService.getP2pService().getP2pStats();
56+
long expectedTcpIn = statsSnapshot.getTcpInSize();
57+
long expectedTcpOut = statsSnapshot.getTcpOutSize();
58+
long expectedUdpIn = statsSnapshot.getUdpInSize();
59+
long expectedUdpOut = statsSnapshot.getUdpOutSize();
60+
5361
Method method = manager.getClass().getDeclaredMethod("work");
5462
method.setAccessible(true);
5563
method.invoke(manager);
5664

57-
Assert.assertEquals(field1.get(manager), 0L);
58-
Assert.assertEquals(field2.get(manager), 0L);
59-
Assert.assertEquals(field3.get(manager), 0L);
60-
Assert.assertEquals(field4.get(manager), 0L);
65+
Assert.assertEquals(expectedTcpIn, (long) field1.get(manager));
66+
Assert.assertEquals(expectedTcpOut, (long) field2.get(manager));
67+
Assert.assertEquals(expectedUdpIn, (long) field3.get(manager));
68+
Assert.assertEquals(expectedUdpOut, (long) field4.get(manager));
6169
}
6270

6371
}

plugins/src/test/java/org/tron/plugins/DbMoveTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.URL;
66
import java.nio.file.Paths;
77
import lombok.extern.slf4j.Slf4j;
8+
import org.junit.After;
89
import org.junit.Assert;
910
import org.junit.Rule;
1011
import org.junit.Test;
@@ -17,6 +18,8 @@
1718
@Slf4j
1819
public class DbMoveTest {
1920

21+
private static final String OUTPUT_DIRECTORY = "output-directory-toolkit";
22+
2023
@Rule
2124
public TemporaryFolder temporaryFolder = new TemporaryFolder();
2225

@@ -30,6 +33,29 @@ private void init(DbTool.DbType dbType, String path) throws IOException, RocksDB
3033
DbTool.getDB(path, TRANS, dbType).close();
3134
}
3235

36+
@After
37+
public void destroy() {
38+
deleteDir(new File(OUTPUT_DIRECTORY));
39+
}
40+
41+
/**
42+
* delete directory.
43+
*/
44+
private static boolean deleteDir(File dir) {
45+
if (dir.isDirectory()) {
46+
String[] children = dir.list();
47+
assert children != null;
48+
for (String child : children) {
49+
boolean success = deleteDir(new File(dir, child));
50+
if (!success) {
51+
logger.warn("can't delete dir:" + dir);
52+
return false;
53+
}
54+
}
55+
}
56+
return dir.delete();
57+
}
58+
3359
private static String getConfig(String config) {
3460
URL path = DbMoveTest.class.getClassLoader().getResource(config);
3561
return path == null ? null : path.getPath();

0 commit comments

Comments
 (0)