diff --git a/src/main/java/com/grabpt/service/ChatService/redis/UnreadCountChatService.java b/src/main/java/com/grabpt/service/ChatService/redis/UnreadCountChatService.java index ff091e35..c3db5c58 100644 --- a/src/main/java/com/grabpt/service/ChatService/redis/UnreadCountChatService.java +++ b/src/main/java/com/grabpt/service/ChatService/redis/UnreadCountChatService.java @@ -3,6 +3,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import java.util.HashMap; @@ -15,12 +16,12 @@ @RequiredArgsConstructor public class UnreadCountChatService { - private final RedisTemplate redisTemplate; + //private final RedisTemplate redisTemplate; + private final StringRedisTemplate redisTemplate; - private String generateKey(Long roomId, Long userId){ - return "chat:unread:"+roomId+":"+userId; + private String generateKey(Long roomId, Long userId) { + return "chat:room_unread:" + roomId + ":" + userId; } - public void incrementUnreadCount(Long roomId, Long userId){ String key = generateKey(roomId, userId); redisTemplate.opsForValue().increment(key); @@ -42,7 +43,7 @@ public Map getUnreadCounts(List roomIds, Long userId){ .toList(); // Redis MGET - List values = redisTemplate.opsForValue().multiGet(keys); + List values = redisTemplate.opsForValue().multiGet(keys); Map resultMap = new HashMap<>(); for(int i=0; i 성공해야 함 + unreadCountChatService.incrementUnreadCount(roomId, userId); + + // 2. 유저가 방에 들어와서 읽음 처리 (SET 0) -> 여기서 JSON으로 꼬이던 문제 발생 지점 + unreadCountChatService.resetUnreadCount(roomId, userId); + + // 3. 다시 메시지를 받음 (INCR) -> 에러가 터지지 않고 1이 되어야 함! + unreadCountChatService.incrementUnreadCount(roomId, userId); + + // then (검증) + Map result = unreadCountChatService.getUnreadCounts(List.of(roomId), userId); + + // 최종 안읽음 개수는 1개여야 한다. + assertThat(result.get(roomId)).isEqualTo(1L); + + } finally { + // 테스트 끝난 후 캐시 정리 + redisTemplate.delete(key); + } + } +}