Skip to content

Commit a8b6650

Browse files
author
luxl
committed
optimization unit test
1 parent c2487a1 commit a8b6650

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

Diff for: dolphinscheduler-master/src/test/java/org/apache/dolphinscheduler/server/master/runner/queue/TimeBasedTaskExecutionRunnableComparableEntryTest.java

+21-18
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717

1818
package org.apache.dolphinscheduler.server.master.runner.queue;
1919

20-
import static org.apache.dolphinscheduler.common.thread.ThreadUtils.sleep;
2120
import static org.junit.jupiter.api.Assertions.assertEquals;
2221
import static org.junit.jupiter.api.Assertions.assertTrue;
2322
import static org.junit.jupiter.api.Assertions.fail;
2423

2524
import java.util.concurrent.TimeUnit;
2625

26+
import org.awaitility.Awaitility;
2727
import org.junit.jupiter.api.BeforeEach;
2828
import org.junit.jupiter.api.Test;
2929

30-
public class TimeBasedTaskExecutionRunnableComparableEntryTest {
30+
class TimeBasedTaskExecutionRunnableComparableEntryTest {
3131

3232
private static final long TEST_DELAY_MILLS = 1000L;
3333
private final String testData = "testData";
@@ -39,7 +39,7 @@ public void setUp() {
3939
}
4040

4141
@Test
42-
public void constructor_NullData_ThrowsNullPointerException() {
42+
void constructor_NullData_ThrowsNullPointerException() {
4343
try {
4444
new TimeBasedTaskExecutionRunnableComparableEntry<>(TEST_DELAY_MILLS, null);
4545
fail("Expected NullPointerException to be thrown");
@@ -49,40 +49,43 @@ public void constructor_NullData_ThrowsNullPointerException() {
4949
}
5050

5151
@Test
52-
public void getDelay_BeforeTriggerTime_ReturnsPositive() {
52+
void getDelay_BeforeTriggerTime_ReturnsPositive() {
5353
entry = new TimeBasedTaskExecutionRunnableComparableEntry<>(TEST_DELAY_MILLS, testData);
54-
sleep(500L);
55-
long remainTime = entry.getDelay(TimeUnit.MILLISECONDS);
56-
assertTrue(remainTime > 0);
54+
Awaitility.await().atMost(500, TimeUnit.MILLISECONDS).untilAsserted(
55+
() -> assertTrue(entry.getDelay(TimeUnit.MILLISECONDS) > 0));
5756
}
5857

5958
@Test
60-
public void getDelay_AtTriggerTime_ReturnsZero() {
59+
void getDelay_AtTriggerTime_ReturnsZero() {
6160
entry = new TimeBasedTaskExecutionRunnableComparableEntry<>(TEST_DELAY_MILLS, testData);
62-
sleep(1000L);
63-
long remainTime = entry.getDelay(TimeUnit.MILLISECONDS);
64-
long tolerance = 100;
65-
assertTrue(remainTime <= tolerance);
61+
Awaitility.await().atLeast(1000, TimeUnit.MILLISECONDS)
62+
.with().pollInterval(1000, TimeUnit.MILLISECONDS)
63+
.untilAsserted(
64+
() -> {
65+
long remainTime = entry.getDelay(TimeUnit.MILLISECONDS);
66+
// The allowable error is +-200
67+
System.out.println("remainTime:" + remainTime);
68+
assertTrue(Math.abs(remainTime) <= 200);
69+
});
6670
}
6771

6872
@Test
69-
public void getDelay_AfterTriggerTime_ReturnsNegative() {
73+
void getDelay_AfterTriggerTime_ReturnsNegative() {
7074
entry = new TimeBasedTaskExecutionRunnableComparableEntry<>(TEST_DELAY_MILLS, testData);
71-
sleep(1500L);
72-
long remainTime = entry.getDelay(TimeUnit.MILLISECONDS);
73-
assertTrue(remainTime < 0);
75+
Awaitility.await().atMost(1500, TimeUnit.MILLISECONDS).untilAsserted(
76+
() -> assertTrue(entry.getDelay(TimeUnit.MILLISECONDS) < 0));
7477
}
7578

7679
@Test
77-
public void getDelay_DifferentTimeUnits_ReturnsCorrectValues() {
80+
void getDelay_DifferentTimeUnits_ReturnsCorrectValues() {
7881
long remainTimeMillis = entry.getDelay(TimeUnit.MILLISECONDS);
7982
long remainTimeSeconds = entry.getDelay(TimeUnit.SECONDS);
8083

8184
assertTrue(remainTimeSeconds <= remainTimeMillis / 1000);
8285
}
8386

8487
@Test
85-
public void compareTo_SameObject_ReturnsZero() {
88+
void compareTo_SameObject_ReturnsZero() {
8689
assertEquals(0, entry.compareTo(entry));
8790
}
8891
}

0 commit comments

Comments
 (0)