Skip to content

Commit

Permalink
[BE] fix: 참여 가능 모임 조회 시 무한 루프 현상 수정 (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
J-I-H-O authored Oct 23, 2024
1 parent 20cdaa4 commit 4bdda28
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public FindClubPageByFilterResponse findByFilter(Long memberId, FindClubByFilter
addFilteredResults(request, filteredClubs, result);

while (result.size() < request.pageSize() && clubSlice.hasNext()) {
lastFoundCreatedAt = updateLastFoundCreatedAt(lastFoundCreatedAt, result);
lastFoundId = updateLastFoundId(lastFoundId, result);
lastFoundCreatedAt = updateLastFoundCreatedAt(clubSlice, result);
lastFoundId = updateLastFoundId(clubSlice, result);

clubSlice = fetchClubSlice(request, lastFoundCreatedAt, lastFoundId);
filteredClubs = filterClubs(clubSlice, filterCondition, member, pets);
Expand Down Expand Up @@ -118,16 +118,16 @@ private void addFilteredResults(FindClubByFilterRequest request, List<Club> filt
}
}

private LocalDateTime updateLastFoundCreatedAt(LocalDateTime lastFoundCreatedAt, List<Club> result) {
private LocalDateTime updateLastFoundCreatedAt(Slice<Club> clubSlice, List<Club> result) {
if (result.isEmpty()) {
return lastFoundCreatedAt;
return clubSlice.getContent().get(clubSlice.getSize() - 1).getCreatedAt();
}
return result.get(result.size() - 1).getCreatedAt();
}

private Long updateLastFoundId(Long lastFoundId, List<Club> result) {
private Long updateLastFoundId(Slice<Club> clubSlice, List<Club> result) {
if (result.isEmpty()) {
return lastFoundId;
return clubSlice.getContent().get(clubSlice.getSize() - 1).getId();
}
return result.get(result.size() - 1).getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
import com.happy.friendogly.pet.domain.Pet;
import com.happy.friendogly.pet.domain.SizeType;
import jakarta.transaction.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -139,6 +143,49 @@ void findSearching_Nothing() {
assertThat(responses.content().isEmpty()).isTrue();
}

@DisplayName("첫 페이지에 필터링 조건에 맞는 모임이 존재하지 않는 경우에도 다음 페이지를 읽어온다.")
@Test
void findSearching_NoResultOnFirstPage_MoveToNextPage() {
List<Club> clubs = new ArrayList<>();
clubs.add(createSavedClub( // member가 참여 불가능한 모임
savedMember,
List.of(savedPet),
Set.of(Gender.FEMALE),
Set.of(SizeType.SMALL)
));
clubs.add(createSavedClub( // member가 참여 불가능한 모임
savedMember,
List.of(savedPet),
Set.of(Gender.FEMALE),
Set.of(SizeType.SMALL)
));
clubs.add(createSavedClub( // member가 참여 가능한 모임
savedMember,
List.of(savedPet),
Set.of(Gender.values()),
Set.of(SizeType.values())
));

Member member = memberRepository.save(new Member("member", "sdfdfef3", "imageUrl"));
Pet pet = petRepository.save(
new Pet(member, "뚱땡이", "수컷 대형견", LocalDate.now().minusDays(1L), SizeType.LARGE, Gender.MALE, "imageUrl"));

FindClubByFilterRequest request = new FindClubByFilterRequest(
FilterCondition.ABLE_TO_JOIN.name(),
province,
city,
village,
Arrays.stream(Gender.values()).map(gender -> gender.name()).collect(Collectors.toSet()),
Arrays.stream(SizeType.values()).map(sizeType -> sizeType.name()).collect(Collectors.toSet()),
20,
LocalDateTime.of(9999, 12, 31, 23, 59, 59),
999999999L
);

FindClubPageByFilterResponse responses = clubQueryService.findByFilter(member.getId(), request);
assertThat(responses.content()).hasSize(1);
}

@DisplayName("페이지 사이즈보다 더 많은 모임이 존재하는 경우, 한번에 페이지 사이즈 만큼의 모임이 조회된다.")
@Test
void findSearching_MoreThanPageSize() {
Expand Down

0 comments on commit 4bdda28

Please sign in to comment.