Skip to content

Commit

Permalink
More GitHub Actions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanGiles committed Jan 23, 2024
1 parent 5216be4 commit 77854eb
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/net/jonathangiles/tools/teenyhttpd/TeenyHttpd.java
Expand Up @@ -21,6 +21,7 @@
import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
Expand Down Expand Up @@ -55,7 +56,9 @@ public class TeenyHttpd {

private ServerSocket serverSocket;

private boolean isRunning = false;
private volatile boolean isRunning = false;

private CountDownLatch startLatch;

private final Map<Method, Map<RequestPath, Function<Request, Response>>> routes = new HashMap<>();

Expand Down Expand Up @@ -135,11 +138,16 @@ private void _addRoute(final Method method, final String path, final Function<Re
public void start() {
Thread serverThread = new Thread(this::startServer);
serverThread.start();
try {
startLatch = new CountDownLatch(1);
startLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

private void startServer() {
System.out.println("TeenyHttp server started.\nListening for connections on port : " + port);
isRunning = true;
executorService = executorSupplier.get();

try {
Expand All @@ -149,6 +157,8 @@ private void startServer() {
}

try {
isRunning = true;
startLatch.countDown();
while (isRunning) {
final Socket clientSocket = serverSocket.accept();
executorService.execute(() -> handleIncomingRequest(clientSocket));
Expand Down

0 comments on commit 77854eb

Please sign in to comment.