Skip to content

Commit

Permalink
Prevent leak in StandaloneTest
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotari committed Oct 28, 2023
1 parent d7fdf4e commit b442d64
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public PulsarStandaloneStarter(String[] args) throws Exception {
jcommander.parse(args);
if (this.isHelp()) {
jcommander.usage();
System.exit(0);
exit(0);
}
if (Strings.isNullOrEmpty(this.getConfigFile())) {
String configFile = System.getProperty(PULSAR_CONFIG_FILE);
Expand All @@ -62,7 +62,7 @@ public PulsarStandaloneStarter(String[] args) throws Exception {
CmdGenerateDocs cmd = new CmdGenerateDocs("pulsar");
cmd.addCommand("standalone", this);
cmd.run(null);
System.exit(0);
exit(0);
}

if (this.isNoBroker() && this.isOnlyBroker()) {
Expand All @@ -73,7 +73,7 @@ public PulsarStandaloneStarter(String[] args) throws Exception {
} catch (Exception e) {
jcommander.usage();
log.error(e.getMessage());
System.exit(1);
exit(1);
}

try (FileInputStream inputStream = new FileInputStream(this.getConfigFile())) {
Expand Down Expand Up @@ -109,6 +109,10 @@ public PulsarStandaloneStarter(String[] args) throws Exception {
}
}

registerShutdownHook();
}

protected void registerShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
if (fnWorkerService != null) {
Expand All @@ -130,6 +134,10 @@ public PulsarStandaloneStarter(String[] args) throws Exception {
}));
}

protected void exit(int status) {
System.exit(status);
}

private static boolean argsContains(String[] args, String arg) {
return Arrays.asList(args).contains(arg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,37 @@
*/
package org.apache.pulsar.broker.service;

import org.apache.pulsar.PulsarStandaloneStarter;
import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
import org.testng.annotations.Test;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertNull;
import org.apache.pulsar.PulsarStandaloneStarter;
import org.testng.annotations.Test;

@Test(groups = "broker")
public class StandaloneTest extends MockedPulsarServiceBaseTest {

@Override
protected void setup() throws Exception {
public class StandaloneTest {

}
static class TestPulsarStandaloneStarter extends PulsarStandaloneStarter {
public TestPulsarStandaloneStarter(String[] args) throws Exception {
super(args);
}

@Override
protected void cleanup() throws Exception {
@Override
protected void registerShutdownHook() {
// ignore to prevent memory leaks
}

@Override
protected void exit(int status) {
// don't ever call System.exit in tests
throw new RuntimeException("Exited with status " + status);
}
}

@Test
public void testWithoutMetadataStoreUrlInConfFile() throws Exception {
String[] args = new String[]{"--config",
"../conf/standalone.conf"};
PulsarStandaloneStarter standalone = new PulsarStandaloneStarter(args);
PulsarStandaloneStarter standalone = new TestPulsarStandaloneStarter(args);
assertNotNull(standalone.getConfig().getProperties().getProperty("metadataStoreUrl"));
assertNotNull(standalone.getConfig().getProperties().getProperty("configurationMetadataStoreUrl"));
}
Expand All @@ -52,7 +57,7 @@ public void testWithoutMetadataStoreUrlInConfFile() throws Exception {
public void testAdvertised() throws Exception {
String[] args = new String[]{"--config",
"./src/test/resources/configurations/pulsar_broker_test_standalone.conf"};
PulsarStandaloneStarter standalone = new PulsarStandaloneStarter(args);
PulsarStandaloneStarter standalone = new TestPulsarStandaloneStarter(args);
assertNull(standalone.getConfig().getAdvertisedAddress());
assertEquals(standalone.getConfig().getAdvertisedListeners(),
"internal:pulsar://192.168.1.11:6660,internal:pulsar+ssl://192.168.1.11:6651");
Expand Down

0 comments on commit b442d64

Please sign in to comment.