-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
171 additions
and
70 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
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
|
@@ -57,15 +56,13 @@ class SongServiceTest extends UsingJpaTest { | |
@Autowired | ||
private ArtistRepository artistRepository; | ||
|
||
@Autowired | ||
private EntityManager entityManager; | ||
|
||
private final InMemorySongs inMemorySongs = new InMemorySongs(entityManager); | ||
private InMemorySongs inMemorySongs; | ||
|
||
private SongService songService; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
inMemorySongs = new InMemorySongs(entityManager); | ||
songService = new SongService( | ||
songRepository, | ||
killingPartRepository, | ||
|
@@ -121,8 +118,8 @@ void findById_exist_login_member() { | |
//given | ||
final Member member = createAndSaveMember("[email protected]", "email"); | ||
final Song song = registerNewSong("title"); | ||
inMemorySongs.recreate(List.of(song)); | ||
addLikeToEachKillingParts(song, member); | ||
inMemorySongs.recreate(songRepository.findAllWithKillingParts()); | ||
addMemberPartToSong(10, 5, song, member); | ||
|
||
//when | ||
|
@@ -166,7 +163,7 @@ private MemberPart addMemberPartToSong(final int startSecond, final int length, | |
void findById_exist_not_login_member() { | ||
//given | ||
final Song song = registerNewSong("title"); | ||
inMemorySongs.recreate(songRepository.findAllWithKillingParts()); | ||
inMemorySongs.recreate(List.of(song)); | ||
|
||
//when인 | ||
saveAndClearEntityManager(); | ||
|
@@ -204,6 +201,7 @@ void findById_exist_not_login_member() { | |
void findById_notExist() { | ||
//given | ||
final Member member = createAndSaveMember("[email protected]", "email"); | ||
inMemorySongs.recreate(List.of()); | ||
|
||
//when | ||
//then | ||
|
@@ -231,8 +229,8 @@ void showHighLikedSongs() { | |
addLikeToEachKillingParts(thirdSong, member2); | ||
addLikeToEachKillingParts(fourthSong, member1); | ||
|
||
inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); | ||
saveAndClearEntityManager(); | ||
inMemorySongs.recreate(songRepository.findAllWithKillingParts()); | ||
|
||
// when | ||
final List<HighLikedSongResponse> result = songService.showHighLikedSongs(); | ||
|
@@ -308,7 +306,6 @@ void firstFindByMember() { | |
// 4, 3, 5, 2, 1 | ||
addLikeToEachKillingParts(thirdSong, member); | ||
addLikeToEachKillingParts(fourthSong, member); | ||
inMemorySongs.recreate(songRepository.findAllWithKillingParts()); | ||
|
||
// 1, 2, 3 노래에 memberPart 추가 | ||
addMemberPartToSong(10, 5, firstSong, member); | ||
|
@@ -317,6 +314,7 @@ void firstFindByMember() { | |
addMemberPartToSong(10, 5, fourthSong, member); | ||
|
||
saveAndClearEntityManager(); | ||
inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); | ||
|
||
// when | ||
final SongSwipeResponse result = | ||
|
@@ -355,6 +353,8 @@ void firstFindByAnonymous() { | |
final Member member = createAndSaveMember("[email protected]", "first"); | ||
final Long notExistSongId = Long.MAX_VALUE; | ||
|
||
saveAndClearEntityManager(); | ||
|
||
// when | ||
// then | ||
assertThatThrownBy( | ||
|
@@ -389,8 +389,6 @@ void findSongByIdForBeforeSwipe() { | |
addLikeToEachKillingParts(fourthSong, member2); | ||
addLikeToEachKillingParts(firstSong, member2); | ||
|
||
inMemorySongs.recreate(songRepository.findAllWithKillingParts()); | ||
|
||
addMemberPartToSong(10, 5, firstSong, member); | ||
addMemberPartToSong(10, 5, secondSong, member); | ||
addMemberPartToSong(10, 5, standardSong, member); | ||
|
@@ -399,6 +397,7 @@ void findSongByIdForBeforeSwipe() { | |
|
||
// 정렬 순서: 2L, 4L, 1L, 5L, 3L | ||
saveAndClearEntityManager(); | ||
inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); | ||
|
||
// when | ||
final List<SongResponse> beforeResponses = | ||
|
@@ -432,7 +431,6 @@ void findSongByIdForAfterSwipe() { | |
addLikeToEachKillingParts(secondSong, member2); | ||
addLikeToEachKillingParts(standardSong, member2); | ||
addLikeToEachKillingParts(firstSong, member2); | ||
inMemorySongs.recreate(songRepository.findAllWithKillingParts()); | ||
|
||
addMemberPartToSong(10, 5, firstSong, member); | ||
addMemberPartToSong(10, 5, secondSong, member); | ||
|
@@ -441,8 +439,8 @@ void findSongByIdForAfterSwipe() { | |
addMemberPartToSong(10, 5, fifthSong, member); | ||
|
||
// 정렬 순서: 2L, 4L, 1L, 5L, 3L | ||
|
||
saveAndClearEntityManager(); | ||
inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); | ||
|
||
// when | ||
final List<SongResponse> afterResponses = | ||
|
@@ -483,9 +481,9 @@ void findSongsByGenre() { | |
addLikeToEachKillingParts(song1, member); | ||
addLikeToEachKillingParts(song1, secondMember); | ||
addLikeToEachKillingParts(song3, member); | ||
inMemorySongs.recreate(songRepository.findAllWithKillingParts()); | ||
|
||
// 정렬 순서: 2L, 1L, 3L, 5L, 4L | ||
inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); | ||
saveAndClearEntityManager(); | ||
|
||
// when | ||
|
@@ -508,9 +506,9 @@ void findSongById() { | |
final Song song = registerNewSong("title"); | ||
final Member member = createAndSaveMember("[email protected]", "nickname"); | ||
addLikeToEachKillingParts(song, member); | ||
inMemorySongs.recreate(songRepository.findAllWithKillingParts()); | ||
addMemberPartToSong(10, 5, song, member); | ||
saveAndClearEntityManager(); | ||
inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); | ||
|
||
// when | ||
final SongResponse response = songService.findSongById(song.getId(), | ||
|
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 |
---|---|---|
|
@@ -19,16 +19,19 @@ | |
import shook.shook.member.domain.repository.MemberRepository; | ||
import shook.shook.song.application.killingpart.dto.KillingPartLikeRequest; | ||
import shook.shook.song.domain.InMemorySongs; | ||
import shook.shook.song.domain.Song; | ||
import shook.shook.song.domain.killingpart.KillingPart; | ||
import shook.shook.song.domain.killingpart.repository.KillingPartLikeRepository; | ||
import shook.shook.song.domain.killingpart.repository.KillingPartRepository; | ||
import shook.shook.song.domain.repository.SongRepository; | ||
|
||
@Sql("classpath:/killingpart/initialize_killing_part_song.sql") | ||
@SpringBootTest | ||
class KillingPartLikeConcurrencyTest { | ||
|
||
private static KillingPart SAVED_KILLING_PART; | ||
private static Member SAVED_MEMBER; | ||
private static Song SAVED_SONG; | ||
|
||
@Autowired | ||
private KillingPartRepository killingPartRepository; | ||
|
@@ -45,11 +48,15 @@ class KillingPartLikeConcurrencyTest { | |
@Autowired | ||
private InMemorySongs inMemorySongs; | ||
|
||
@Autowired | ||
private SongRepository songRepository; | ||
|
||
private KillingPartLikeService likeService; | ||
private TransactionTemplate transactionTemplate; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
SAVED_SONG = songRepository.findById(1L).get(); | ||
SAVED_KILLING_PART = killingPartRepository.findById(1L).get(); | ||
SAVED_MEMBER = memberRepository.findById(1L).get(); | ||
likeService = new KillingPartLikeService(killingPartRepository, memberRepository, killingPartLikeRepository, | ||
|
@@ -64,6 +71,7 @@ void likeByMultiplePeople() throws InterruptedException { | |
// given | ||
final Member first = SAVED_MEMBER; | ||
final Member second = memberRepository.save(new Member("[email protected]", "second")); | ||
inMemorySongs.recreate(songRepository.findAllWithKillingPartsAndLikes()); | ||
|
||
// when | ||
ExecutorService executorService = Executors.newFixedThreadPool(2); | ||
|
@@ -91,8 +99,10 @@ void likeByMultiplePeople() throws InterruptedException { | |
Thread.sleep(1000); | ||
|
||
// then | ||
final KillingPart killingPart = killingPartRepository.findById(SAVED_KILLING_PART.getId()).get(); | ||
assertThat(killingPart.getLikeCount()).isEqualTo(2); | ||
final KillingPart killingPart = inMemorySongs.getSongById(SAVED_SONG.getId()).getKillingParts().stream() | ||
.filter(kp -> kp.getId().equals(SAVED_KILLING_PART.getId())) | ||
.findAny().get(); | ||
assertThat(killingPart.getAtomicLikeCount().get()).isEqualTo(2); | ||
} | ||
|
||
@Disabled("UPDATE + 1 사용 시 한 사용자의 동시에 도착하는 좋아요 요청 동시성 문제 발생") | ||
|
Oops, something went wrong.