Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Connector implements Runnable {

private static final int DEFAULT_PORT = 8080;
private static final int DEFAULT_ACCEPT_COUNT = 100;
private static final int DEFAULT_MAX_THREADS = 250;
static final int DEFAULT_MAX_THREADS = 250;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 좋네요


private final ServerSocket serverSocket;
private final ExecutorService executor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.apache.catalina.connector;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.net.ConnectException;
import java.net.Socket;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class ConnectorTest {

@DisplayName("서버를 종료하면 더 이상 클라이언트를 받을 수 없다")
@Test
void stop() {
Connector connector = new Connector();
connector.start();
connector.stop();

assertThatThrownBy(() -> {
try (Socket clientSocket = new Socket("localhost", 8080)) {}
}).isInstanceOf(ConnectException.class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 단순 예외가 발생한다 보다 의미 전달이 명확해서 시원하네요!

}

@DisplayName("스레드 풀의 크기는 250이다")
@Test
void testServerStop() {
final var executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(Connector.DEFAULT_MAX_THREADS);
for (int i = 0; i < 350; i++) {
executor.submit(threadSleep());
}
final int expectedPoolSize = Connector.DEFAULT_MAX_THREADS;
final int expectedQueueSize = 100;

assertThat(expectedPoolSize).isEqualTo(executor.getPoolSize());
assertThat(expectedQueueSize).isEqualTo(executor.getQueue().size());
}

private Runnable threadSleep() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍

return () -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
};
}
}