17
17
18
18
package org .apache .dolphinscheduler .server .master .runner .queue ;
19
19
20
- import static org .apache .dolphinscheduler .common .thread .ThreadUtils .sleep ;
21
20
import static org .junit .jupiter .api .Assertions .assertEquals ;
22
21
import static org .junit .jupiter .api .Assertions .assertTrue ;
23
22
import static org .junit .jupiter .api .Assertions .fail ;
24
23
25
24
import java .util .concurrent .TimeUnit ;
26
25
26
+ import org .awaitility .Awaitility ;
27
27
import org .junit .jupiter .api .BeforeEach ;
28
28
import org .junit .jupiter .api .Test ;
29
29
30
- public class TimeBasedTaskExecutionRunnableComparableEntryTest {
30
+ class TimeBasedTaskExecutionRunnableComparableEntryTest {
31
31
32
32
private static final long TEST_DELAY_MILLS = 1000L ;
33
33
private final String testData = "testData" ;
@@ -39,7 +39,7 @@ public void setUp() {
39
39
}
40
40
41
41
@ Test
42
- public void constructor_NullData_ThrowsNullPointerException () {
42
+ void constructor_NullData_ThrowsNullPointerException () {
43
43
try {
44
44
new TimeBasedTaskExecutionRunnableComparableEntry <>(TEST_DELAY_MILLS , null );
45
45
fail ("Expected NullPointerException to be thrown" );
@@ -49,40 +49,43 @@ public void constructor_NullData_ThrowsNullPointerException() {
49
49
}
50
50
51
51
@ Test
52
- public void getDelay_BeforeTriggerTime_ReturnsPositive () {
52
+ void getDelay_BeforeTriggerTime_ReturnsPositive () {
53
53
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 ));
57
56
}
58
57
59
58
@ Test
60
- public void getDelay_AtTriggerTime_ReturnsZero () {
59
+ void getDelay_AtTriggerTime_ReturnsZero () {
61
60
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
+ });
66
70
}
67
71
68
72
@ Test
69
- public void getDelay_AfterTriggerTime_ReturnsNegative () {
73
+ void getDelay_AfterTriggerTime_ReturnsNegative () {
70
74
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 ));
74
77
}
75
78
76
79
@ Test
77
- public void getDelay_DifferentTimeUnits_ReturnsCorrectValues () {
80
+ void getDelay_DifferentTimeUnits_ReturnsCorrectValues () {
78
81
long remainTimeMillis = entry .getDelay (TimeUnit .MILLISECONDS );
79
82
long remainTimeSeconds = entry .getDelay (TimeUnit .SECONDS );
80
83
81
84
assertTrue (remainTimeSeconds <= remainTimeMillis / 1000 );
82
85
}
83
86
84
87
@ Test
85
- public void compareTo_SameObject_ReturnsZero () {
88
+ void compareTo_SameObject_ReturnsZero () {
86
89
assertEquals (0 , entry .compareTo (entry ));
87
90
}
88
91
}
0 commit comments