From d346c2c6a8dd31dbf569d4caa5e501a4da1f0952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonard=20Br=C3=BCnings?= Date: Wed, 13 Dec 2023 16:48:24 +0100 Subject: [PATCH] Bail early if everything is as it should be --- .../spockframework/runtime/RunContext.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/spock-core/src/main/java/org/spockframework/runtime/RunContext.java b/spock-core/src/main/java/org/spockframework/runtime/RunContext.java index 3c75e70fb8..19ff54b6b2 100644 --- a/spock-core/src/main/java/org/spockframework/runtime/RunContext.java +++ b/spock-core/src/main/java/org/spockframework/runtime/RunContext.java @@ -235,17 +235,20 @@ public void ensureInstalled() { if (runContexts.isEmpty()) { // we got a new thread that didn't inherit the original context, so just add us here runContexts.add(this); + return; + } + + if (runContexts.getFirst() == this) return; // everything is well + + if (!runContexts.contains(this)) { + // something weird happened, and we got the got a separate RunContext hierarchy from what we'd expect + // maybe some thread re-use between runs + runContexts.clear(); + runContexts.add(this); } else { - if (!runContexts.contains(this)) { - // something weird happened, and we got the got a separate RunContext hierarchy from what we'd expect - // maybe some thread re-use between runs - runContexts.clear(); - runContexts.add(this); - } else { - while (runContexts.getFirst() != this) { - // we got some residual child contexts that didn't get cleared after a thread spawn - runContexts.removeFirst(); - } + while (runContexts.getFirst() != this) { + // we got some residual child contexts that didn't get cleared after a thread spawn + runContexts.removeFirst(); } } }