On Nov 21, 2016, at 2:02 PM, Psycho Punch <rdg...@gmail.com> wrote:
Base on unit testing documentation (JUnit), during tear down, the Vertx instance needs to be closed. Before each test, it needs to be instantiated. Is there a way to have just one single instance of Vertx and then use it for every test method in the class? When I close the Vertx instance only after all tests have run (e.g. @AfterClass), I get errors involving async being called more than once.
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/ca16e70e-619a-4399-b916-6019423084ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
@RunWith(VertxUnitRunner.class)
public class SampleTest {
//shared instance; no @Before, no @After
private Vertx vertx;
@Test
public void testOne(TestContext context) {
//invoke async.complete() here
}
@Test
public void testTwo(TestContext context) {
//invoke async.complete() here
}
}--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/dce6edb0-480f-4f3b-a8eb-ac23a701dded%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/dce6edb0-480f-4f3b-a8eb-ac23a701dded%40googlegroups.com.
@RunWith(VertxUnitRunner.class)
public class DemoTest {
private static Vertx vertx = Vertx.vertx();
@Test
public void testOne(TestContext context) {
Async async = context.async();
vertx.eventBus().consumer("sample", event -> {
async.complete();
});
vertx.eventBus().send("sample", "{}");
async.awaitSuccess(TimeUnit.SECONDS.toMillis(1));
}
@Test
public void testTwo(TestContext context) {
Async async = context.async();
vertx.eventBus().consumer("sample", event -> {
async.complete();
});
vertx.eventBus().send("sample", "{}");
async.awaitSuccess(TimeUnit.SECONDS.toMillis(1));
}
}--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/251e0557-4717-457b-b2d1-96b95996ad2a%40googlegroups.com.
On Nov 23, 2016, at 5:02 PM, Psycho Punch <rdg...@gmail.com> wrote:
In the more detailed demo code "sample" is not the name of the consumer but the address that the consumers listen for. However, explicitly unregistering the consumers between tests, solve the problem I was having. Perhaps the reason is that the code I'm testing using the consumers invoke send() instead of publish(). The pitfall was that, in between tests, new consumers are registered to the same event bus under the same Vertx instance. So, I think what I learned from here is that, while it's not necessary to close the Vertx instance in between tests, we should make sure that only the relevant Consumers and Verticles are present for every running test.
As for the async.awaitSuccess, it's necessary for my tests because without it, my test code proceeds to the post-condition checks before the asynchronous Vert.x code is completed. Perhaps if I use TestContext.assert* methods it might work, but I use either JUnit's or AssertJ's assertions most of the time.
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/290ac08b-e478-4208-bb08-5368027d1730%40googlegroups.com.
@Test
public void test() throws Exception {
Vertx vertx = Vertx.vertx();
CountDownLatch latch = new CountDownLatch(1);
log.info("timer start");
vertx.setTimer(1000, t -> {
log.info("timer finished");
latch.countDown();
});
latch.await();
}