Hi all,
Async testing is always a bit of a pain. Now, in vertx, there are some helpers to make it work properly, but somehow, I must be missing something:
public class Auth extends AbstractVerticle {
private LoginHandler loginHandler;
@Override
public void start(Future<Void> startFuture) {
info("starting module 'Auth'");
AuthProvider provider = JWTAuth.create(vertx, new JWTAuthOptions()
.addPubSecKey(new PubSecKeyOptions()
.setAlgorithm(config().getString("alg"))
.setPublicKey(config().getString("pubKey"))
.setSecretKey(config().getString("secKey"))
));
try {
loginHandler = new LoginHandler(provider);
} catch (Exception e) {
error("cannot deploy Auth module, invalid provider");
vertx.undeploy(this.deploymentID());
}
vertx.eventBus().localConsumer("auth.login", loginHandler::login);
}
}
this code does work when I add this line to the bottom:
vertx.eventBus().<JsonObject>send("auth.login", new JsonObject().put("username", "paulo").put("password", "secret"), Logger::info)
However, when I try to run tests, I get a `TimeoutException, and the tests just don't run (if I put a `Logger.info("starting test")` at the top of the test, I never see that line):
@ExtendWith(VertxExtension.class)
public class LoginHandlerTest {
private static final String AUTH_ALG = "ES256";
private static final String AUTH_PUBKEY = "...";
private static final String AUTH_SECKEY = "..." +
"...";
private static final JsonObject AUTH_CONFIG = new JsonObject()
.put("alg", AUTH_ALG)
.put("pubKey", AUTH_PUBKEY)
.put("secKey", AUTH_SECKEY);
private JWTAuth authProvider;
private EventBus eb;
@BeforeEach
void deployVerticle(Vertx vertx, VertxTestContext testContext) {
authProvider = JWTAuth.create(vertx, new JWTAuthOptions()
.addPubSecKey(new PubSecKeyOptions()
.setAlgorithm(AUTH_ALG)
.setPublicKey(AUTH_PUBKEY)
.setSecretKey(AUTH_SECKEY)));
vertx.deployVerticle(
Auth.class.getName(),
new DeploymentOptions().setConfig(AUTH_CONFIG),
testContext.succeeding(id -> testContext.completeNow()));
}
@Test
@DisplayName("check if a proper jwt is returned")
@Timeout(value = 2, timeUnit = TimeUnit.SECONDS)
void getJWTToken(Vertx vertx, VertxTestContext testContext) {
vertx.eventBus().<JsonObject>send("auth.login",
new JsonObject().put("username", "paulo").put("password", "secret"),
result -> {
info(result);
if (result.succeeded()) {
JsonObject body = result.result().body();
info(body);
authProvider.authenticate(body.getJsonObject("token"),
authResult -> {
info(authResult.result());
testContext.completeNow();
});
} else {
testContext.failNow(result.cause());
}
});
}
}
The test is just not running... What am I doing wrong?
...
2019-04-08 20:47:53 [vert.x-eventloop-thread-0]....Auth.start()
INFO: starting module 'Auth'
java.util.concurrent.TimeoutException: The test execution timed out. Make sure your asynchronous code includes calls to either VertxTestContext#completeNow(), VertxTestContext#failNow() or Checkpoint#flag()
at io.vertx.junit5.VertxExtension.joinActiveTestContexts(VertxExtension.java:239)
at io.vertx.junit5.VertxExtension.beforeTestExecution(VertxExtension.java:200)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeTestExecutionCallbacks$5(TestMethodTestDescriptor.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:169)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeTestExecutionCallbacks(TestMethodTestDescriptor.java:154)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:125)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)