Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

클라이언트의 SSE 연결 종료 상황을 테스트한다. #21

Open
3Juhwan opened this issue Jan 28, 2025 · 0 comments
Open

클라이언트의 SSE 연결 종료 상황을 테스트한다. #21

3Juhwan opened this issue Jan 28, 2025 · 0 comments
Labels
테스트 테스트에 수정이 필요해요.

Comments

@3Juhwan
Copy link
Member

3Juhwan commented Jan 28, 2025

작업할 내용

클라이언트가 SSE 연결을 끊었을 때를 테스트해야 한다. 아래 코드에 걸려서 에러 핸들링이 제대로 되는지 검증한다.

@Slf4j
public class SseErrorHandler {

    public static void handle(IOException e) {
        if (isClientAbortException(e)) {
            log.info("클라이언트가 연결을 종료했습니다.");
        } else {
            log.error("SSE 연결 중 오류 발생: {}", e.getMessage());
            throw new RuntimeException(e);
        }
    }

    private static boolean isClientAbortException(Throwable throwable) {
        while (throwable != null) {
            if (throwable instanceof org.apache.catalina.connector.ClientAbortException) {
                return true;
            }
            throwable = throwable.getCause();
        }
        return false;
    }
}

시도한 방법

테스트용 웹 컨테이너에 요청을 보내고 커넥션을 끊었을 때, 클라이언트가 연결을 종료했습니다.라는 로그가 찍혀야 한다. 다음은 시도했지만 실패한 방법이다.

RestAssured

    @DisplayName("SSE를 연결하고 클라이언트가 연결을 끊으면, 에러가 찍히지 않는다.")
    @Test
    void sseConnectionCloseTest() throws IOException {
        // when
        Response response = RestAssured.given()
            .accept("text/event-stream")
            .when()
            .get("/api/connect")
            .then()
            .statusCode(200)
            .extract()
            .response();

        response.body().asInputStream().close();

        // then

        // no exception
    }

Java Connection 사용

    @DisplayName("SSE를 연결하고 클라이언트가 연결을 끊으면, 에러가 찍히지 않는다.")
    @Test
    void sseConnectionCloseTest() throws IOException, InterruptedException {
        // when
        String sseUrl = "http://localhost:" + RestAssured.port + "/api/connect";

        // Open connection
        HttpURLConnection connection = (HttpURLConnection) new URL(sseUrl).openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "text/event-stream");
        connection.setDoInput(true);

        // Assert the connection was successful
        int responseCode = connection.getResponseCode();
        assertThat(responseCode).isEqualTo(200);
        System.out.println("SSE 연결 성공: " + responseCode);

        // Read a portion of the stream
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line = reader.readLine(); // SSE 메시지의 첫 번째 줄
        System.out.println("수신된 메시지: " + line);

        // Assert that the connection is active and receiving data
        assertThat(line).contains("success");

        // Disconnect the connection
        connection.disconnect();
        System.out.println("SSE 연결 종료");

        Thread.sleep(2000);
    }
@3Juhwan 3Juhwan added the 테스트 테스트에 수정이 필요해요. label Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
테스트 테스트에 수정이 필요해요.
Projects
None yet
Development

No branches or pull requests

1 participant