Skip to content

Commit c74b88b

Browse files
committed
fixat mer från coderabbit
1 parent d87171b commit c74b88b

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/main/java/org/example/FileCache.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
public class FileCache {
66
private static final ConcurrentHashMap<String, byte[]> cache = new ConcurrentHashMap<>();
77

8+
private FileCache() {}
9+
810
public static byte[] get(String key) {
911
return cache.get(key);
1012
}
@@ -16,9 +18,5 @@ public static void put(String key, byte[] content) {
1618
public static void clear() {
1719
cache.clear();
1820
}
19-
public static void clearCache() {
20-
FileCache.clear();
21-
}
22-
2321
}
2422

src/main/java/org/example/StaticFileHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.nio.file.Files;
99
import java.nio.file.Path;
1010
import java.nio.file.Paths;
11+
import java.nio.file.NoSuchFileException;
12+
1113

1214
public class StaticFileHandler {
1315
private static final String DEFAULT_WEB_ROOT = "www";
@@ -44,11 +46,14 @@ public void sendGetRequest(OutputStream outputStream, String uri) throws IOExcep
4446
outputStream.write(response.build());
4547
outputStream.flush();
4648

47-
} catch (IOException e) {
49+
} catch (NoSuchFileException e) {
4850
writeResponse(outputStream, 404, "Not Found");
51+
} catch (IOException e) {
52+
writeResponse(outputStream, 500, "Internal Server Error");
4953
}
5054
}
5155

56+
5257
private String sanitizeUri(String uri) {
5358
if (uri == null || uri.isEmpty()) return "index.html";
5459

src/test/java/org/example/StaticFileHandlerTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void testConcurrent_MultipleReads() throws InterruptedException, IOException {
8787

8888
handler.sendGetRequest(new ByteArrayOutputStream(), "shared.html");
8989

90-
// Act - 10 trådar läser samma fil 50 gånger varje
90+
// Act - 10 threads reading same file 50 times each
9191
Thread[] threads = new Thread[10];
9292
final Exception[] threadError = new Exception[1];
9393

@@ -100,7 +100,7 @@ void testConcurrent_MultipleReads() throws InterruptedException, IOException {
100100
String response = out.toString();
101101

102102
if (!response.contains("HTTP/1.1 200") || !response.contains("Data")) {
103-
throw new AssertionError("Oväntad response");
103+
throw new AssertionError("Unexpected response");
104104
}
105105
}
106106
} catch (Exception e) {
@@ -111,7 +111,15 @@ void testConcurrent_MultipleReads() throws InterruptedException, IOException {
111111
});
112112
threads[i].start();
113113
}
114+
115+
// Wait for all threads to complete
116+
for (Thread thread : threads) {
117+
thread.join();
114118
}
119+
120+
// Assert
121+
assertNull(threadError[0], "Thread threw exception: " + (threadError[0] != null ? threadError[0].getMessage() : ""));
122+
}
115123

116124
@Test
117125
void test_file_that_exists_should_return_200() throws IOException {

0 commit comments

Comments
 (0)