Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ protected synchronized void complete() {
if (tearingDown) {
throw new IllegalStateException("testComplete called after test has completed");
}
checkThread();
if (testCompleteCalled) {
throw new IllegalStateException("already complete");
}
Expand All @@ -109,7 +108,6 @@ protected void testComplete() {
if (tearingDown) {
throw new IllegalStateException("testComplete called after test has completed");
}
checkThread();
if (testCompleteCalled) {
throw new IllegalStateException("testComplete() already called");
}
Expand Down
41 changes: 40 additions & 1 deletion vertx-core/src/test/java/io/vertx/test/core/VertxTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,52 @@ public io.vertx.core.spi.transport.Transport implementation() {
protected Vertx vertx;
protected Vertx[] vertices;
private List<Vertx> created;
private Thread junitThread;
private final Handler<Throwable> failureBridgeHandler;

public VertxTestBase(boolean stateless) {
this.stateless = stateless;
this.failureBridgeHandler = this::handleFailure;
}

public VertxTestBase() {
this(false);
}

@Override
protected void disableThreadChecks() {
if (stateless) {
// Do nothing as we actually want to ensure that this is either
// - the JUnit main thread
// - a vertx context thread that we can fail to
} else {
super.disableThreadChecks();
}
}

@Override
protected void checkThread() {
if (stateless) {
if (Thread.currentThread() == junitThread) {
// Ok
} else {
Context current = Vertx.currentContext();
if (current == null) {
System.out.println("Running test assertion from un-associated thread: " + Thread.currentThread());
new Exception().printStackTrace(System.out);
} else {
Handler<Throwable> handler = current.owner().exceptionHandler();
if (handler != failureBridgeHandler) {
System.out.println("Asserting from a vertx thread that is not relaying failures to the failure handler: " + Thread.currentThread());
new Exception().printStackTrace(System.out);
}
}
}
} else {
super.checkThread();
}
}

@Override
void handleThrowable(Throwable t) {
if (stateless) {
Expand All @@ -158,6 +195,7 @@ protected void vinit() {
}

public void setUp() throws Exception {
junitThread = Thread.currentThread();
super.setUp();
vinit();
VertxOptions options = getOptions();
Expand Down Expand Up @@ -189,6 +227,7 @@ protected VertxOptions getOptions() {
}

protected void tearDown() throws Exception {
junitThread = null;
if (created != null) {
close(created);
}
Expand Down Expand Up @@ -269,7 +308,7 @@ protected Vertx vertx(Supplier<Vertx> supplier) {

private void add(Vertx vertx) {
if (stateless) {
vertx.exceptionHandler(this::handleFailure);
vertx.exceptionHandler(failureBridgeHandler);
}
created.add(vertx);
}
Expand Down
Loading