11package com .iexec .blockchain .tool ;
22
3+ import lombok .extern .slf4j .Slf4j ;
34import org .awaitility .Awaitility ;
5+ import org .junit .jupiter .api .Assertions ;
46import org .junit .jupiter .api .Test ;
57
68import java .lang .reflect .Field ;
79import java .util .ArrayList ;
810import java .util .List ;
911import java .util .Queue ;
12+ import java .util .concurrent .Future ;
1013import java .util .concurrent .TimeUnit ;
1114import java .util .concurrent .atomic .AtomicBoolean ;
1215import java .util .function .Function ;
1619import static org .assertj .core .api .Assertions .assertThat ;
1720import static org .assertj .core .api .Assertions .assertThatThrownBy ;
1821
22+ @ Slf4j
1923class QueueServiceTests {
2024
25+ private static final int TIMEOUT_DURATION = 5 ;
2126 private final QueueService queueService = new QueueService ();
2227
2328 // region executeActions
@@ -30,7 +35,7 @@ void shouldRunHighPriorityRunnable() {
3035
3136 Awaitility
3237 .await ()
33- .atMost (5 , TimeUnit .SECONDS )
38+ .atMost (TIMEOUT_DURATION , TimeUnit .SECONDS )
3439 .until (() -> highPriorityTimestamps .size () == 1 );
3540
3641 // We simply ensure the execution has completed.
@@ -46,7 +51,7 @@ void shouldRunLowPriorityRunnable() {
4651
4752 Awaitility
4853 .await ()
49- .atMost (5 , TimeUnit .SECONDS )
54+ .atMost (TIMEOUT_DURATION , TimeUnit .SECONDS )
5055 .until (() -> lowPriorityTimestamps .size () == 1 );
5156
5257 // We simply ensure the execution has completed.
@@ -64,14 +69,20 @@ void shouldRunHighPriorityBeforeLowPriority() {
6469 // Add a low priority and a high priority tasks to queue.
6570 // The queueService should select the high priority task before the low priority.
6671 AtomicBoolean queueReady = new AtomicBoolean (false );
67- queueService .addExecutionToQueue (() -> waitQueueReady (queueReady ), false );
72+ Future < Void > future = queueService .addExecutionToQueue (() -> waitQueueReady (queueReady ), false );
6873 queueService .addExecutionToQueue (lowPriorityRunnable , false );
6974 queueService .addExecutionToQueue (highPriorityRunnable , true );
7075 queueReady .set (true );
7176
77+ try {
78+ future .get (TIMEOUT_DURATION , TimeUnit .SECONDS );
79+ } catch (Exception e ) {
80+ Assertions .fail ("Queue was not ready on time" );
81+ }
82+
7283 Awaitility
7384 .await ()
74- .atMost (5 , TimeUnit .SECONDS )
85+ .atMost (TIMEOUT_DURATION , TimeUnit .SECONDS )
7586 .until (() -> highPriorityTimestamps .size () == 1 && lowPriorityTimestamps .size () == 1 );
7687
7788 // We have executed a single task per priority so we that's what we should now have.
@@ -109,7 +120,7 @@ void shouldExecuteInOrder() throws NoSuchFieldException {
109120 };
110121
111122 AtomicBoolean queueReady = new AtomicBoolean (false );
112- queueService .addExecutionToQueue (() -> waitQueueReady (queueReady ), false );
123+ Future < Void > future = queueService .addExecutionToQueue (() -> waitQueueReady (queueReady ), false );
113124 for (int i = 0 ; i < taskNumberPerPriority ; i ++) {
114125 queueService .addExecutionToQueue (runnableCreator .apply (taskNumberPerPriority + i ), false );
115126 }
@@ -118,6 +129,12 @@ void shouldExecuteInOrder() throws NoSuchFieldException {
118129 }
119130 queueReady .set (true );
120131
132+ try {
133+ future .get (TIMEOUT_DURATION , TimeUnit .SECONDS );
134+ } catch (Exception e ) {
135+ Assertions .fail ("Queue was not ready on time" );
136+ }
137+
121138 Awaitility
122139 .await ()
123140 .atMost (5 , TimeUnit .SECONDS )
@@ -142,7 +159,7 @@ void shouldExecuteInOrder() throws NoSuchFieldException {
142159
143160 private void waitQueueReady (AtomicBoolean queueReady ) {
144161 Awaitility .await ()
145- .atMost (5 , TimeUnit .SECONDS )
162+ .atMost (TIMEOUT_DURATION , TimeUnit .SECONDS )
146163 .until (queueReady ::getPlain );
147164 }
148165 // endregion
0 commit comments