Skip to content

Commit be1a8c6

Browse files
committed
Fail test when queue is not ready on time
1 parent b7c2796 commit be1a8c6

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/main/java/com/iexec/blockchain/tool/QueueService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ protected <T> RunnableFuture<T> newTaskFor(@NotNull Runnable runnable, T value)
3737
*
3838
* @param runnable {@link Runnable} to submit to the queue.
3939
* @param priority Whether this {@link Runnable} has a high ({@literal true}) or low ({@literal false}) priority.
40+
* @return A Future representing pending completion of the runnable.
4041
*/
41-
public void addExecutionToQueue(Runnable runnable, boolean priority) {
42-
executorService.submit(new BlockchainAction(runnable, priority));
42+
public Future<Void> addExecutionToQueue(Runnable runnable, boolean priority) {
43+
return executorService.submit(new BlockchainAction(runnable, priority), null);
4344
}
4445

4546
/**

src/test/java/com/iexec/blockchain/tool/QueueServiceTests.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.iexec.blockchain.tool;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import org.awaitility.Awaitility;
5+
import org.junit.jupiter.api.Assertions;
46
import org.junit.jupiter.api.Test;
57

68
import java.lang.reflect.Field;
79
import java.util.ArrayList;
810
import java.util.List;
911
import java.util.Queue;
12+
import java.util.concurrent.Future;
1013
import java.util.concurrent.TimeUnit;
1114
import java.util.concurrent.atomic.AtomicBoolean;
1215
import java.util.function.Function;
@@ -16,8 +19,10 @@
1619
import static org.assertj.core.api.Assertions.assertThat;
1720
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1821

22+
@Slf4j
1923
class 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

Comments
 (0)