-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test/chat-stomp-2: STOMP 세션 CONNECT 테스트 코드 작성
- StompHandlerAdapter 클래스의 메서드 이용해 연결 정보 표시 - Connect 구현 -> 예시 : Connected: {version=[1.2], heart-beat=[0,0]} - Timeout은 60Sec - session의 Connect 성공 후에는 disconnect - build.gradle 에서는 JVM 메모리 부족 이슈로 MemberControllerTest와 동일하게 제외
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/test/java/com/dife/api/controller/chat/WebSocketConnectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |