Skip to content

Commit

Permalink
creating flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mageddo committed Aug 6, 2024
1 parent 2b8c367 commit 1702d70
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/mageddo/dnsproxyserver/server/Starter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import javax.inject.Inject;
import javax.inject.Singleton;

import java.util.Set;

import static com.mageddo.dnsproxyserver.quarkus.Quarkus.isTest;
Expand All @@ -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<StartupEvent> 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();
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/mageddo/dnsproxyserver/AppIntTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {

Expand Down

0 comments on commit 1702d70

Please sign in to comment.