Skip to content

Commit 592a05e

Browse files
committed
Retain invoke all order
1 parent 1f9ffde commit 592a05e

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

junit-platform-engine/src/main/java/org/junit/platform/engine/support/hierarchical/WorkerThreadPoolHierarchicalTestExecutorService.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ private List<WorkQueue.Entry> forkConcurrentChildren(List<? extends TestTask> ch
345345
Consumer<TestTask> isolatedTaskCollector, List<TestTask> sameThreadTasks) {
346346

347347
List<WorkQueue.Entry> queueEntries = new ArrayList<>(children.size());
348+
int index = 0;
348349
for (TestTask child : children) {
349350
if (requiresGlobalReadWriteLock(child)) {
350351
isolatedTaskCollector.accept(child);
@@ -353,7 +354,7 @@ else if (child.getExecutionMode() == SAME_THREAD) {
353354
sameThreadTasks.add(child);
354355
}
355356
else {
356-
queueEntries.add(workQueue.createEntry(child));
357+
queueEntries.add(workQueue.createEntry(child, index++));
357358
}
358359
}
359360

@@ -645,14 +646,14 @@ private static class WorkQueue implements Iterable<WorkQueue.Entry> {
645646
private final Set<Entry> queue = new ConcurrentSkipListSet<>();
646647

647648
Entry add(TestTask task) {
648-
Entry entry = createEntry(task);
649+
Entry entry = createEntry(task, 0);
649650
LOGGER.trace(() -> "forking: " + entry.task);
650651
return doAdd(entry);
651652
}
652653

653-
Entry createEntry(TestTask task) {
654+
Entry createEntry(TestTask task, int index) {
654655
var uniqueId = task.getTestDescriptor().getUniqueId();
655-
return new Entry(uniqueId, task, new CompletableFuture<>());
656+
return new Entry(uniqueId, task, new CompletableFuture<>(), index);
656657
}
657658

658659
void addAll(Collection<Entry> entries) {
@@ -685,7 +686,7 @@ public Iterator<Entry> iterator() {
685686
return queue.iterator();
686687
}
687688

688-
private record Entry(UniqueId id, TestTask task, CompletableFuture<@Nullable Void> future)
689+
private record Entry(UniqueId id, TestTask task, CompletableFuture<@Nullable Void> future, int index)
689690
implements Comparable<Entry> {
690691

691692
@SuppressWarnings("FutureReturnValueIgnored")
@@ -710,6 +711,10 @@ public int compareTo(Entry that) {
710711
if (result != 0) {
711712
return result;
712713
}
714+
result = Integer.compare(that.index(), this.index());
715+
if (result != 0) {
716+
return result;
717+
}
713718
return compareBy(that.id(), this.id());
714719
}
715720

platform-tests/src/test/java/org/junit/platform/engine/support/hierarchical/WorkerThreadPoolHierarchicalTestExecutorServiceTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.net.URL;
3030
import java.net.URLClassLoader;
3131
import java.time.Instant;
32+
import java.util.Arrays;
33+
import java.util.Collections;
3234
import java.util.List;
3335
import java.util.concurrent.CompletableFuture;
3436
import java.util.concurrent.CountDownLatch;
@@ -473,6 +475,36 @@ void executesChildrenInOrder() throws Exception {
473475
.isSorted();
474476
}
475477

478+
@Test
479+
void executesChildrenInInvokeAllOrder() throws Exception {
480+
service = new WorkerThreadPoolHierarchicalTestExecutorService(configuration(1, 1));
481+
482+
var leaf1a = new TestTaskStub(ExecutionMode.CONCURRENT) //
483+
.withName("leaf1a").withLevel(2);
484+
var leaf1b = new TestTaskStub(ExecutionMode.CONCURRENT) //
485+
.withName("leaf1b").withLevel(2);
486+
var leaf1c = new TestTaskStub(ExecutionMode.CONCURRENT) //
487+
.withName("leaf1c").withLevel(2);
488+
var leaf1d = new TestTaskStub(ExecutionMode.CONCURRENT) //
489+
.withName("leaf1d").withLevel(2);
490+
491+
List<TestTaskStub> children = Arrays.asList(leaf1d, leaf1a, leaf1b, leaf1c);
492+
Collections.shuffle(children);
493+
494+
var root = new TestTaskStub(ExecutionMode.SAME_THREAD,
495+
() -> requiredService().invokeAll(children)) //
496+
.withName("root").withLevel(1);
497+
498+
service.submit(root).get();
499+
500+
assertThat(List.of(root, leaf1a, leaf1b, leaf1c, leaf1d)) //
501+
.allSatisfy(TestTaskStub::assertExecutedSuccessfully);
502+
503+
assertThat(children) //
504+
.extracting(TestTaskStub::startTime) //
505+
.isSorted();
506+
}
507+
476508
@Test
477509
void workIsStolenInReverseOrder() throws Exception {
478510
service = new WorkerThreadPoolHierarchicalTestExecutorService(configuration(2, 2));

0 commit comments

Comments
 (0)