Skip to content

Commit

Permalink
test/chat-stomp-2: STOMP 세션 CONNECT 테스트 코드 작성
Browse files Browse the repository at this point in the history
- StompHandlerAdapter 클래스의 메서드 이용해
연결 정보 표시

- Connect 구현 -> 예시 : Connected: {version=[1.2], heart-beat=[0,0]}
- Timeout은 60Sec
- session의 Connect 성공 후에는 disconnect

- build.gradle 에서는 JVM 메모리 부족 이슈로 MemberControllerTest와 동일하게 제외
  • Loading branch information
KooSuYeon committed Sep 26, 2024
1 parent bf985c2 commit 79ab7db
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
exclude '**/MemberControllerTest.*'
exclude '**/WebSocketConnectTest.*'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.dife.api.controller.chat;

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

import com.dife.api.config.LocalRedisConfig;
import com.dife.api.config.TestConfig;
import java.lang.reflect.Type;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.context.annotation.Import;
import org.springframework.messaging.converter.StringMessageConverter;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import org.testcontainers.junit.jupiter.Testcontainers;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@Import(TestConfig.class)
@ExtendWith(LocalRedisConfig.class)
@Testcontainers
public class WebSocketConnectTest {

@LocalServerPort private int port;

@Test
public void testWebSocketConnect() throws Exception {
String url = "ws://localhost:" + port + "/ws";

WebSocketClient webSocketClient = new StandardWebSocketClient();
WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);
stompClient.setMessageConverter(new StringMessageConverter());

StompSession session =
stompClient
.connect(
url,
new StompSessionHandlerAdapter() {
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
System.out.println("Connected: " + connectedHeaders);
}

@Override
public void handleFrame(StompHeaders headers, Object payload) {
System.out.println("Received: " + payload);
}

@Override
public Type getPayloadType(StompHeaders headers) {
return String.class;
}
})
.get(60, TimeUnit.SECONDS);

assertThat(session.isConnected()).isTrue();

session.disconnect();
}
}

0 comments on commit 79ab7db

Please sign in to comment.