Reputation: 1792
I am playing with Vertx.io (version 3.4.2). Put down some code and now I want to test it, so I write a couple of unit test. When I run each test separately they both complete well but when I do a mvn clean test they both fail with this message:
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.Net.bind(Unknown Source)
at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source)
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:128)
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:554)
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1258)
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:502)
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:487)
at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:980)
at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:250)
at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:365)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasksFrom(SingleThreadEventExecutor.java:379)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:354)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:436)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
at java.lang.Thread.run(Unknown Source)
It seems that verticle does not shutdown after test completition. Here are the tests:
@RunWith(VertxUnitRunner.class)
public class SomeApiTest {
private ObjectMapper objectMapper;
private Vertx vertx;
private Integer port;
private static Logger log;
@BeforeClass
public static void initTests() {
log = Logger.getLogger("SomeApiTest");
}
@Before
public void setUp(TestContext context) {
log.info("start up test.");
vertx = Vertx.vertx();
objectMapper = new ObjectMapper();
port = 8080;
DeploymentOptions options = new DeploymentOptions();
options.setConfig(new JsonObject());
options.getConfig().put("api.port", port);
vertx.deployVerticle("SomeApi", options, context.asyncAssertSuccess());
}
@After
public void tearDown(TestContext context) {
log.info("shutting down test.");
vertx.close(h -> {
if (h.failed()) {
log.error(h.cause());
}
});
}
@Test
public void whenRequestHitsVerticleItReturnsRecipes(TestContext context) throws Exception {
final Async async = context.async();
List<Integer> votes = new ArrayList<>();
votes.add(new Integer(6));
votes.add(new Integer(4));
votes.add(new Integer(8));
vertx.eventBus().consumer("persistence", h -> {
h.reply(Json.encode(votes));
});
vertx.createHttpClient().getNow(port, "localhost", "/votes?qty=3", response -> {
response.handler(body -> {
Integer[] xvotes;
try {
xvotes = objectMapper.readValue(body.toString(), Integer[].class);
int expected = 3;
int actual = xvotes.length;
Assert.assertEquals(expected, actual);
async.complete();
} catch (Exception e) {
log.error(e);
Assert.fail(e.getMessage());
}
});
});
}
@Test
public void fiveDishesRequestAgainsTenRecipesApi(TestContext context) throws Exception {
final Async async = context.async();
List<Integer> votes = new ArrayList<>();
votes.add(new Integer(3));
votes.add(new Integer(4));
votes.add(new Integer(7));
votes.add(new Integer(7));
votes.add(new Integer(6));
vertx.eventBus().consumer("persistence", h -> {
h.reply(Json.encode(votes));
});
vertx.createHttpClient().getNow(port, "localhost", "/votes?qty=5", response -> {
response.handler(body -> {
Integer[] xvotes;
try {
xvotes = objectMapper.readValue(body.toString(), Integer[].class);
int expected = 5;
int actual = xvotes.length;
Assert.assertEquals(expected, actual);
async.complete();
} catch (Exception e) {
log.error(e);
Assert.fail(e.getMessage());
}
});
});
}
}
Tests are simple, their purpose is to get how to unit test with Vertx. I am expecting that each verticle will be undeployed after test ends. What am I missing?
Upvotes: 1
Views: 2054
Reputation: 4245
Try to wait until vertx
is closed. Here is the example:
@After
public void tearDown(TestContext context) {
log.info("shutting down test.");
Async async = context.async();
vertx.close(h -> {
if (h.failed()) {
log.error(h.cause());
}
async.success();
});
}
Upvotes: 3