Skip to content

Commit

Permalink
Fix #1389
Browse files Browse the repository at this point in the history
Close vert.x instances in the LauncherTest and StarterTest

Signed-off-by: Clement Escoffier <clement.escoffier@gmail.com>
  • Loading branch information
cescoffier committed Apr 25, 2016
1 parent 5bfc9d6 commit a6a4301
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
Expand Up @@ -363,4 +363,11 @@ protected String getDefaultAddress() {
public void setExecutionContext(ExecutionContext context) { public void setExecutionContext(ExecutionContext context) {
this.executionContext = context; this.executionContext = context;
} }

/**
* @return the vert.x instance if created.
*/
public synchronized Vertx vertx() {
return vertx;
}
} }
Expand Up @@ -56,12 +56,12 @@ public void setUp() throws IOException {
} }


@After @After
public void tearDown() { public void tearDown() throws InterruptedException {
if (run != null) { if (run != null) {
run.vertx.close(); close(run.vertx);
} }
if (bare != null) { if (bare != null) {
bare.vertx.close(); close(bare.vertx);
} }
} }


Expand Down
34 changes: 30 additions & 4 deletions src/test/java/io/vertx/test/core/LauncherTest.java
Expand Up @@ -24,7 +24,9 @@
import io.vertx.core.metrics.MetricsOptions; import io.vertx.core.metrics.MetricsOptions;
import io.vertx.core.metrics.impl.DummyVertxMetrics; import io.vertx.core.metrics.impl.DummyVertxMetrics;
import io.vertx.core.spi.VertxMetricsFactory; import io.vertx.core.spi.VertxMetricsFactory;
import io.vertx.core.spi.launcher.Command;
import io.vertx.core.spi.metrics.VertxMetrics; import io.vertx.core.spi.metrics.VertxMetrics;
import org.junit.AfterClass;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
Expand All @@ -44,6 +46,7 @@ public class LauncherTest extends VertxTestBase {
private String expectedVersion; private String expectedVersion;
private ByteArrayOutputStream out; private ByteArrayOutputStream out;
private PrintStream stream; private PrintStream stream;
private Vertx vertx;


@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
Expand Down Expand Up @@ -76,6 +79,10 @@ public void tearDown() throws Exception {


out.close(); out.close();
stream.close(); stream.close();

if (vertx != null) {
vertx.close();
}
} }




Expand Down Expand Up @@ -184,6 +191,18 @@ public void testRunVerticleWithMainVerticleInManifestNoArgs() throws Exception {
launcher.dispatch(args); launcher.dispatch(args);
waitUntil(() -> TestVerticle.instanceCount.get() == 1); waitUntil(() -> TestVerticle.instanceCount.get() == 1);
assertEquals(Arrays.asList(args), TestVerticle.processArgs); assertEquals(Arrays.asList(args), TestVerticle.processArgs);

cleanup(launcher);
}

private void cleanup(Launcher launcher) {
RunCommand run = (RunCommand) launcher.getExistingCommandInstance("run");
if (run != null) {
Vertx v = run.vertx();
if (v != null) {
v.close();
}
}
} }


@Test @Test
Expand All @@ -201,6 +220,8 @@ public void testRunVerticleWithMainVerticleInManifestWithArgs() throws Exception
launcher.dispatch(args); launcher.dispatch(args);
waitUntil(() -> TestVerticle.instanceCount.get() == 10); waitUntil(() -> TestVerticle.instanceCount.get() == 10);
assertEquals(Arrays.asList(args), TestVerticle.processArgs); assertEquals(Arrays.asList(args), TestVerticle.processArgs);

cleanup(launcher);
} }


@Test @Test
Expand Down Expand Up @@ -249,8 +270,11 @@ public void testRunWithOverriddenDefaultCommand() throws Exception {


HelloCommand.called = false; HelloCommand.called = false;
String[] args = {"run", TestVerticle.class.getName(), "--name=vert.x"}; String[] args = {"run", TestVerticle.class.getName(), "--name=vert.x"};
Launcher.main(args); Launcher launcher = new Launcher();
launcher.dispatch(args);
waitUntil(() -> TestVerticle.instanceCount.get() == 1); waitUntil(() -> TestVerticle.instanceCount.get() == 1);

cleanup(launcher);
} }


@Test @Test
Expand All @@ -264,8 +288,11 @@ public void testRunWithOverriddenDefaultCommandRequiringArgs() throws Exception
Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); Files.copy(manifest.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);


String[] args = {TestVerticle.class.getName()}; String[] args = {TestVerticle.class.getName()};
Launcher.main(args); Launcher launcher = new Launcher();
launcher.dispatch(args);
waitUntil(() -> TestVerticle.instanceCount.get() == 1); waitUntil(() -> TestVerticle.instanceCount.get() == 1);

cleanup(launcher);
} }




Expand Down Expand Up @@ -530,7 +557,6 @@ class MyLauncher extends Launcher {
boolean afterStartingVertxInvoked = false; boolean afterStartingVertxInvoked = false;
boolean beforeDeployingVerticle = false; boolean beforeDeployingVerticle = false;


Vertx vertx;
VertxOptions options; VertxOptions options;
DeploymentOptions deploymentOptions; DeploymentOptions deploymentOptions;
JsonObject config; JsonObject config;
Expand Down Expand Up @@ -569,7 +595,7 @@ public void beforeStartingVertx(VertxOptions options) {
@Override @Override
public void afterStartingVertx(Vertx vertx) { public void afterStartingVertx(Vertx vertx) {
afterStartingVertxInvoked = true; afterStartingVertxInvoked = true;
this.vertx = vertx; LauncherTest.this.vertx = vertx;
} }


@Override @Override
Expand Down
33 changes: 31 additions & 2 deletions src/test/java/io/vertx/test/core/StarterTest.java
Expand Up @@ -26,6 +26,7 @@
import io.vertx.core.metrics.impl.DummyVertxMetrics; import io.vertx.core.metrics.impl.DummyVertxMetrics;
import io.vertx.core.spi.VertxMetricsFactory; import io.vertx.core.spi.VertxMetricsFactory;
import io.vertx.core.spi.metrics.VertxMetrics; import io.vertx.core.spi.metrics.VertxMetrics;
import org.junit.AfterClass;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
Expand All @@ -44,6 +45,8 @@
*/ */
public class StarterTest extends VertxTestBase { public class StarterTest extends VertxTestBase {


Vertx vertx;

@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
Expand All @@ -65,6 +68,10 @@ public void setUp() throws Exception {
public void tearDown() throws Exception { public void tearDown() throws Exception {
clearProperties(); clearProperties();
super.tearDown(); super.tearDown();

if (vertx != null) {
vertx.close();
}
} }


@Test @Test
Expand All @@ -74,6 +81,7 @@ public void testVersion() throws Exception {
starter.run(args); starter.run(args);
// TODO some way of getting this from the version in pom.xml // TODO some way of getting this from the version in pom.xml
assertEquals(System.getProperty("vertxVersion"), starter.getVersion()); assertEquals(System.getProperty("vertxVersion"), starter.getVersion());
cleanup(starter);
} }


@Test @Test
Expand All @@ -93,6 +101,7 @@ public void testRunVerticleMultiple(int instances) throws Exception {
waitUntil(() -> TestVerticle.instanceCount.get() == instances); waitUntil(() -> TestVerticle.instanceCount.get() == instances);
assertEquals(Arrays.asList(args), TestVerticle.processArgs); assertEquals(Arrays.asList(args), TestVerticle.processArgs);
starter.assertHooksInvoked(); starter.assertHooksInvoked();
cleanup(starter);
} }


@Test @Test
Expand All @@ -103,6 +112,7 @@ public void testRunVerticleClustered() throws Exception {
waitUntil(() -> TestVerticle.instanceCount.get() == 1); waitUntil(() -> TestVerticle.instanceCount.get() == 1);
assertEquals(Arrays.asList(args), TestVerticle.processArgs); assertEquals(Arrays.asList(args), TestVerticle.processArgs);
starter.assertHooksInvoked(); starter.assertHooksInvoked();
cleanup(starter);
} }


@Test @Test
Expand All @@ -113,6 +123,7 @@ public void testRunVerticleHA() throws Exception {
waitUntil(() -> TestVerticle.instanceCount.get() == 1); waitUntil(() -> TestVerticle.instanceCount.get() == 1);
assertEquals(Arrays.asList(args), TestVerticle.processArgs); assertEquals(Arrays.asList(args), TestVerticle.processArgs);
starter.assertHooksInvoked(); starter.assertHooksInvoked();
cleanup(starter);
} }




Expand All @@ -123,6 +134,13 @@ public void testRunVerticleWithMainVerticleInManifestNoArgs() throws Exception {
starter.run(args); starter.run(args);
waitUntil(() -> TestVerticle.instanceCount.get() == 1); waitUntil(() -> TestVerticle.instanceCount.get() == 1);
assertEquals(Arrays.asList(args), TestVerticle.processArgs); assertEquals(Arrays.asList(args), TestVerticle.processArgs);
cleanup(starter);
}

private void cleanup(MyStarter starter) {
if (starter != null && starter.getVertx() != null) {
starter.getVertx().close();
}
} }


@Test @Test
Expand All @@ -132,6 +150,7 @@ public void testRunVerticleWithMainVerticleInManifestWithHA() throws Exception {
starter.run(args); starter.run(args);
waitUntil(() -> TestVerticle.instanceCount.get() == 1); waitUntil(() -> TestVerticle.instanceCount.get() == 1);
assertEquals(Arrays.asList(args), TestVerticle.processArgs); assertEquals(Arrays.asList(args), TestVerticle.processArgs);
cleanup(starter);
} }


@Test @Test
Expand All @@ -141,6 +160,7 @@ public void testRunVerticleWithMainVerticleInManifestWithArgs() throws Exception
starter.run(args); starter.run(args);
waitUntil(() -> TestVerticle.instanceCount.get() == 1); waitUntil(() -> TestVerticle.instanceCount.get() == 1);
assertEquals(Arrays.asList(args), TestVerticle.processArgs); assertEquals(Arrays.asList(args), TestVerticle.processArgs);
cleanup(starter);
} }


@Test @Test
Expand All @@ -151,6 +171,7 @@ public void testRunVerticleWithConfString() throws Exception {
starter.run(args); starter.run(args);
waitUntil(() -> TestVerticle.instanceCount.get() == 1); waitUntil(() -> TestVerticle.instanceCount.get() == 1);
assertEquals(conf, TestVerticle.conf); assertEquals(conf, TestVerticle.conf);
cleanup(starter);
} }


@Rule @Rule
Expand All @@ -168,6 +189,7 @@ public void testRunVerticleWithConfFile() throws Exception {
starter.run(args); starter.run(args);
waitUntil(() -> TestVerticle.instanceCount.get() == 1); waitUntil(() -> TestVerticle.instanceCount.get() == 1);
assertEquals(conf, TestVerticle.conf); assertEquals(conf, TestVerticle.conf);
cleanup(starter);
} }


@Test @Test
Expand Down Expand Up @@ -201,10 +223,11 @@ private void testConfigureFromSystemProperties(boolean clustered) throws Excepti
VertxOptions opts = starter.getVertxOptions(); VertxOptions opts = starter.getVertxOptions();


assertEquals(123, opts.getEventLoopPoolSize(), 0); assertEquals(123, opts.getEventLoopPoolSize(), 0);
assertEquals(123767667l, opts.getMaxEventLoopExecuteTime()); assertEquals(123767667L, opts.getMaxEventLoopExecuteTime());
assertEquals(true, opts.getMetricsOptions().isEnabled()); assertEquals(true, opts.getMetricsOptions().isEnabled());
assertEquals("somegroup", opts.getHAGroup()); assertEquals("somegroup", opts.getHAGroup());


cleanup(starter);
} }


private void clearProperties() { private void clearProperties() {
Expand Down Expand Up @@ -245,6 +268,8 @@ public MetricsOptions newOptions() {
VertxOptions opts = starter.getVertxOptions(); VertxOptions opts = starter.getVertxOptions();
CustomMetricsOptions custom = (CustomMetricsOptions) opts.getMetricsOptions(); CustomMetricsOptions custom = (CustomMetricsOptions) opts.getMetricsOptions();
assertEquals("customPropertyValue", custom.getCustomProperty()); assertEquals("customPropertyValue", custom.getCustomProperty());

cleanup(starter);
} finally { } finally {
ConfigurableMetricsFactory.delegate = null; ConfigurableMetricsFactory.delegate = null;
} }
Expand All @@ -268,6 +293,7 @@ public void testConfigureFromSystemPropertiesInvalidPropertyName() throws Except
def.getMetricsOptions().setEnabled(true); def.getMetricsOptions().setEnabled(true);
} }
assertEquals(def, opts); assertEquals(def, opts);
cleanup(starter);
} }


@Test @Test
Expand All @@ -287,6 +313,7 @@ public void testConfigureFromSystemPropertiesInvalidPropertyType() throws Except
def.getMetricsOptions().setEnabled(true); def.getMetricsOptions().setEnabled(true);
} }
assertEquals(def, opts); assertEquals(def, opts);
cleanup(starter);
} }


@Test @Test
Expand All @@ -296,14 +323,15 @@ public void testRunWithCommandLine() throws Exception {
String cl = "run java:" + TestVerticle.class.getCanonicalName() + " -instances " + instances; String cl = "run java:" + TestVerticle.class.getCanonicalName() + " -instances " + instances;
starter.run(cl); starter.run(cl);
waitUntil(() -> TestVerticle.instanceCount.get() == instances); waitUntil(() -> TestVerticle.instanceCount.get() == instances);
cleanup(starter);
} }


class MyStarter extends Starter { class MyStarter extends Starter {
boolean beforeStartingVertxInvoked = false; boolean beforeStartingVertxInvoked = false;
boolean afterStartingVertxInvoked = false; boolean afterStartingVertxInvoked = false;
boolean beforeDeployingVerticle = false; boolean beforeDeployingVerticle = false;


public Vertx getVert() { public Vertx getVertx() {
return vertx; return vertx;
} }


Expand Down Expand Up @@ -341,6 +369,7 @@ protected void beforeDeployingVerticle(DeploymentOptions deploymentOptions) {
} }


public void assertHooksInvoked() { public void assertHooksInvoked() {
StarterTest.this.vertx = vertx;
assertTrue(beforeStartingVertxInvoked); assertTrue(beforeStartingVertxInvoked);
assertTrue(afterStartingVertxInvoked); assertTrue(afterStartingVertxInvoked);
assertTrue(beforeDeployingVerticle); assertTrue(beforeDeployingVerticle);
Expand Down

0 comments on commit a6a4301

Please sign in to comment.