Skip to content

Commit 8c81b07

Browse files
committed
Improve VertxTestBase stateless mode by logging un-associated threads during tests that will not correctly report a failure.
Motivation: The VertxTestBase stateless mode is only effective at the condition that asserting threads can report the failure correctly to the test. Changes: Add console logging to stateless mode that logs threads unable to report correctly and that should be fixed prior.
1 parent 9745ae7 commit 8c81b07

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

vertx-core/src/test/java/io/vertx/test/core/AsyncTestBase.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ protected synchronized void complete() {
9595
if (tearingDown) {
9696
throw new IllegalStateException("testComplete called after test has completed");
9797
}
98-
checkThread();
9998
if (testCompleteCalled) {
10099
throw new IllegalStateException("already complete");
101100
}
@@ -109,7 +108,6 @@ protected void testComplete() {
109108
if (tearingDown) {
110109
throw new IllegalStateException("testComplete called after test has completed");
111110
}
112-
checkThread();
113111
if (testCompleteCalled) {
114112
throw new IllegalStateException("testComplete() already called");
115113
}

vertx-core/src/test/java/io/vertx/test/core/VertxTestBase.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,52 @@ public io.vertx.core.spi.transport.Transport implementation() {
133133
protected Vertx vertx;
134134
protected Vertx[] vertices;
135135
private List<Vertx> created;
136+
private Thread junitThread;
137+
private final Handler<Throwable> failureBridgeHandler;
136138

137139
public VertxTestBase(boolean stateless) {
138140
this.stateless = stateless;
141+
this.failureBridgeHandler = this::handleFailure;
139142
}
140143

141144
public VertxTestBase() {
142145
this(false);
143146
}
144147

148+
@Override
149+
protected void disableThreadChecks() {
150+
if (stateless) {
151+
// Do nothing as we actually want to ensure that this is either
152+
// - the JUnit main thread
153+
// - a vertx context thread that we can fail to
154+
} else {
155+
super.disableThreadChecks();
156+
}
157+
}
158+
159+
@Override
160+
protected void checkThread() {
161+
if (stateless) {
162+
if (Thread.currentThread() == junitThread) {
163+
// Ok
164+
} else {
165+
Context current = Vertx.currentContext();
166+
if (current == null) {
167+
System.out.println("Running test assertion from un-associated thread: " + Thread.currentThread());
168+
new Exception().printStackTrace(System.out);
169+
} else {
170+
Handler<Throwable> handler = current.owner().exceptionHandler();
171+
if (handler != failureBridgeHandler) {
172+
System.out.println("Asserting from a vertx thread that is not relaying failures to the failure handler: " + Thread.currentThread());
173+
new Exception().printStackTrace(System.out);
174+
}
175+
}
176+
}
177+
} else {
178+
super.checkThread();
179+
}
180+
}
181+
145182
@Override
146183
void handleThrowable(Throwable t) {
147184
if (stateless) {
@@ -158,6 +195,7 @@ protected void vinit() {
158195
}
159196

160197
public void setUp() throws Exception {
198+
junitThread = Thread.currentThread();
161199
super.setUp();
162200
vinit();
163201
VertxOptions options = getOptions();
@@ -189,6 +227,7 @@ protected VertxOptions getOptions() {
189227
}
190228

191229
protected void tearDown() throws Exception {
230+
junitThread = null;
192231
if (created != null) {
193232
close(created);
194233
}
@@ -269,7 +308,7 @@ protected Vertx vertx(Supplier<Vertx> supplier) {
269308

270309
private void add(Vertx vertx) {
271310
if (stateless) {
272-
vertx.exceptionHandler(this::handleFailure);
311+
vertx.exceptionHandler(failureBridgeHandler);
273312
}
274313
created.add(vertx);
275314
}

0 commit comments

Comments
 (0)