-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
NodeTestTask.java
188 lines (156 loc) · 5.92 KB
/
NodeTestTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright 2015-2018 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.platform.engine.support.hierarchical;
import static java.util.stream.Collectors.toCollection;
import static org.junit.platform.engine.TestExecutionResult.failed;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Future;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.engine.EngineExecutionListener;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutorService.TestTask;
import org.junit.platform.engine.support.hierarchical.Node.ExecutionMode;
import org.junit.platform.engine.support.hierarchical.Node.SkipResult;
/**
* @since 1.3
*/
class NodeTestTask<C extends EngineExecutionContext> implements TestTask {
private final TestDescriptor testDescriptor;
private final EngineExecutionListener listener;
private final HierarchicalTestExecutorService executorService;
private final ThrowableCollector.Factory throwableCollectorFactory;
private final Node<C> node;
private final ExecutionMode executionMode;
private final Set<ExclusiveResource> exclusiveResources;
private final List<NodeTestTask<C>> children;
private ResourceLock resourceLock = NopLock.INSTANCE;
private Optional<ExecutionMode> forcedExecutionMode = Optional.empty();
private C parentContext;
private C context;
private SkipResult skipResult;
private boolean started;
private ThrowableCollector throwableCollector;
NodeTestTask(TestDescriptor testDescriptor, EngineExecutionListener listener,
HierarchicalTestExecutorService executorService, ThrowableCollector.Factory throwableCollectorFactory) {
this.testDescriptor = testDescriptor;
this.listener = listener;
this.executorService = executorService;
this.throwableCollectorFactory = throwableCollectorFactory;
node = asNode(testDescriptor);
executionMode = node.getExecutionMode();
exclusiveResources = node.getExclusiveResources();
// @formatter:off
children = testDescriptor.getChildren().stream()
.map(descriptor -> new NodeTestTask<C>(descriptor, listener, executorService, throwableCollectorFactory))
.collect(toCollection(ArrayList::new));
// @formatter:on
}
public Set<ExclusiveResource> getExclusiveResources() {
return exclusiveResources;
}
public List<NodeTestTask<C>> getChildren() {
return children;
}
@Override
public ResourceLock getResourceLock() {
return resourceLock;
}
public void setResourceLock(ResourceLock resourceLock) {
this.resourceLock = resourceLock;
}
@Override
public ExecutionMode getExecutionMode() {
return forcedExecutionMode.orElse(executionMode);
}
public void setForcedExecutionMode(ExecutionMode forcedExecutionMode) {
this.forcedExecutionMode = Optional.of(forcedExecutionMode);
}
public void setParentContext(C parentContext) {
this.parentContext = parentContext;
}
@Override
public void execute() {
throwableCollector = throwableCollectorFactory.create();
prepare();
if (throwableCollector.isEmpty()) {
checkWhetherSkipped();
}
if (throwableCollector.isEmpty() && !skipResult.isSkipped()) {
executeRecursively();
}
if (context != null) {
cleanUp();
}
reportCompletion();
}
private void prepare() {
throwableCollector.execute(() -> context = node.prepare(parentContext));
}
private void checkWhetherSkipped() {
throwableCollector.execute(() -> skipResult = node.shouldBeSkipped(context));
}
private void executeRecursively() {
listener.executionStarted(testDescriptor);
started = true;
throwableCollector.execute(() -> {
context = node.before(context);
List<Future<?>> futures = new ArrayList<>();
context = node.execute(context,
dynamicTestDescriptor -> executeDynamicTest(dynamicTestDescriptor, futures));
children.forEach(child -> child.setParentContext(context));
executorService.invokeAll(children);
// using a for loop for the sake for ForkJoinPool's work stealing
for (Future<?> future : futures) {
future.get();
}
});
throwableCollector.execute(() -> node.after(context));
}
private void executeDynamicTest(TestDescriptor dynamicTestDescriptor, List<Future<?>> futures) {
listener.dynamicTestRegistered(dynamicTestDescriptor);
NodeTestTask<C> nodeTestTask = new NodeTestTask<>(dynamicTestDescriptor, listener, executorService,
throwableCollectorFactory);
Set<ExclusiveResource> exclusiveResources = nodeTestTask.getExclusiveResources();
if (!exclusiveResources.isEmpty()) {
listener.executionStarted(dynamicTestDescriptor);
String message = "Dynamic test descriptors must not declare exclusive resources: " + exclusiveResources;
listener.executionFinished(dynamicTestDescriptor, failed(new JUnitException(message)));
}
else {
nodeTestTask.setParentContext(context);
futures.add(executorService.submit(nodeTestTask));
}
}
private void cleanUp() {
throwableCollector.execute(() -> node.cleanUp(context));
}
private void reportCompletion() {
if (throwableCollector.isEmpty() && skipResult.isSkipped()) {
listener.executionSkipped(testDescriptor, skipResult.getReason().orElse("<unknown>"));
return;
}
if (!started) {
// Call executionStarted first to comply with the contract of EngineExecutionListener.
listener.executionStarted(testDescriptor);
}
listener.executionFinished(testDescriptor, throwableCollector.toTestExecutionResult());
throwableCollector = null;
}
@SuppressWarnings("unchecked")
private Node<C> asNode(TestDescriptor testDescriptor) {
return (testDescriptor instanceof Node ? (Node<C>) testDescriptor : noOpNode);
}
@SuppressWarnings("rawtypes")
private static final Node noOpNode = new Node() {
};
}