Skip to content

Commit 6556794

Browse files
authored
Make RunContext use InheritableThreadLocal (#1758)
Prior to this commit, `RunContext` used a plain `ThreadLocal`, which caused a new `RunContext` being created for every thread. With this change, child threads will inherit the `RunContext` stack of their parent thread and fork it for their use.
1 parent 164f1eb commit 6556794

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

spock-core/src/main/java/org/spockframework/runtime/RunContext.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,22 @@
3030
import static java.util.Collections.emptyList;
3131

3232
public class RunContext implements EngineExecutionContext {
33-
private static final ThreadLocal<LinkedList<RunContext>> contextStacks = ThreadLocal.withInitial(LinkedList::new);
33+
private static final InheritableThreadLocal<LinkedList<RunContext>> contextStacks = new InheritableThreadLocal<LinkedList<RunContext>>() {
34+
35+
@Override
36+
protected LinkedList<RunContext> initialValue() {
37+
return new LinkedList<>();
38+
}
39+
40+
@Override
41+
protected LinkedList<RunContext> childValue(LinkedList<RunContext> parentValue) {
42+
if (parentValue == null) {
43+
return new LinkedList<>();
44+
}
45+
// fork the parent list
46+
return new LinkedList<>(parentValue);
47+
}
48+
};
3449

3550
private final String name;
3651
private final File spockUserHome;

0 commit comments

Comments
 (0)