diff --git a/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJson.java b/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJson.java index 164981ee0..3170a9ba7 100644 --- a/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJson.java +++ b/src/main/java/com/mageddo/dnsproxyserver/config/dataprovider/ConfigDAOJson.java @@ -38,7 +38,9 @@ public Config find(Path configPath) { public static Path buildConfigPath(Path workDir, String configPath) { if (runningInTestsAndNoCustomConfigPath()) { - return Files.createTempFileDeleteOnExit("dns-proxy-server-junit", ".json"); + final var file = Files.createTempFileDeleteOnExit("dns-proxy-server-junit", ".json"); + log.trace("status=runningInTests, usingEmptyFile={}", file); + return file; } if (workDir != null) { return workDir diff --git a/src/main/java/com/mageddo/dnsproxyserver/server/Starter.java b/src/main/java/com/mageddo/dnsproxyserver/server/Starter.java index c6eedc807..fd7ace968 100644 --- a/src/main/java/com/mageddo/dnsproxyserver/server/Starter.java +++ b/src/main/java/com/mageddo/dnsproxyserver/server/Starter.java @@ -9,7 +9,6 @@ import javax.inject.Inject; import javax.inject.Singleton; - import java.util.Set; import static com.mageddo.dnsproxyserver.quarkus.Quarkus.isTest; @@ -19,21 +18,36 @@ @RequiredArgsConstructor(onConstructor = @__({@Inject})) public class Starter { + public static final String DNS_SERVER_MUST_START_FLAG = "mg.server.server.must-start"; private final ServerStarter dnsServerStarter; private final WebServer webServer; private final Set startupEvents; public void start() { - if(isTest()){ - log.warn("status=onTest, disabled=[dnsServer, startupEvents]"); + if (isTest()) { + log.warn("status=onTest, disabled=[startupEvents]"); } else { this.startupEvents.forEach(StartupEvent::onStart); + } + if (shouldStartDnsServer()) { this.dnsServerStarter.start(); } this.webServer.start(Configs.getInstance().getWebServerPort()); } - public void stop(){ + private static boolean shouldStartDnsServer() { + return !isTest() || isMustStartFlagActive(); + } + + private static boolean isMustStartFlagActive() { + return Boolean.getBoolean(DNS_SERVER_MUST_START_FLAG); + } + + public static void setMustStartFlagActive(boolean b) { + System.setProperty(DNS_SERVER_MUST_START_FLAG, String.valueOf(b)); + } + + public void stop() { this.dnsServerStarter.stop(); this.webServer.stop(); } diff --git a/src/test/java/com/mageddo/dnsproxyserver/AppIntTest.java b/src/test/java/com/mageddo/dnsproxyserver/AppIntTest.java index 8d4938895..50b8708bc 100644 --- a/src/test/java/com/mageddo/dnsproxyserver/AppIntTest.java +++ b/src/test/java/com/mageddo/dnsproxyserver/AppIntTest.java @@ -2,11 +2,14 @@ import com.mageddo.commons.concurrent.Threads; import com.mageddo.dns.utils.Messages; +import com.mageddo.dnsproxyserver.server.Starter; import com.mageddo.dnsproxyserver.solver.SimpleResolver; import com.mageddo.dnsproxyserver.utils.Ips; import com.mageddo.utils.Executors; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.xbill.DNS.Message; @@ -20,6 +23,16 @@ @Slf4j public class AppIntTest { + @BeforeEach + void beforeEach() { + Starter.setMustStartFlagActive(true); + } + + @AfterAll + static void afterAll(){ + Starter.setMustStartFlagActive(false); + } + @Test void appMustStartAndQuerySampleWithSuccess(@TempDir Path tempDir) {