Skip to content

Commit

Permalink
refactor: synchronized 로 동시성 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyma-s committed Dec 15, 2023
1 parent 03654ec commit dc73563
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ public void updateCachedSong() {
.flatMap(song -> song.getKillingParts().stream())
.toList();
killingParts.forEach(entityManager::merge);
recreateCachedSong();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private void create(final KillingPart killingPart, final Member member) {
.orElseGet(() -> createNewLike(killingPart, member));
if (likeOnKillingPart.isDeleted()) {
inMemorySongs.like(killingPart, likeOnKillingPart);
likeRepository.pressLike(likeOnKillingPart.getId());
}
}

Expand Down
36 changes: 19 additions & 17 deletions backend/src/main/java/shook/shook/song/domain/InMemorySongs.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class InMemorySongs {
private Map<Long, Song> songs = new HashMap<>();
private List<Long> sortedSongIds = new ArrayList<>();

public void refreshSongs(final List<Song> songs) {
public synchronized void refreshSongs(final List<Song> songs) {
this.songs = songs.stream()
.collect(Collectors.toMap(Song::getId, song -> song, (prev, update) -> update, HashMap::new));
this.sortedSongIds = new ArrayList<>(this.songs.keySet().stream()
Expand Down Expand Up @@ -116,7 +116,16 @@ public void like(final KillingPart killingPart, final KillingPartLike likeOnKill
}
}

private static KillingPart findKillingPart(final KillingPart killingPart, final Song song) {
public void unlike(final KillingPart killingPart, final KillingPartLike unlikeOnKillingPart) {
final Song song = songs.get(killingPart.getSong().getId());
final KillingPart killingPartById = findKillingPart(killingPart, song);
final boolean updated = killingPartById.unlike(unlikeOnKillingPart);
if (updated) {
reorder(song);
}
}

private KillingPart findKillingPart(final KillingPart killingPart, final Song song) {
return song.getKillingParts().stream()
.filter(kp -> kp.equals(killingPart))
.findAny()
Expand All @@ -126,14 +135,16 @@ private static KillingPart findKillingPart(final KillingPart killingPart, final
}

private void reorder(final Song updatedSong) {
int currentSongIndex = sortedSongIds.indexOf(updatedSong.getId());
synchronized (sortedSongIds) {
int currentSongIndex = sortedSongIds.indexOf(updatedSong.getId());

if (currentSongIndex == -1) {
return;
}
if (currentSongIndex == -1) {
return;
}

moveForward(updatedSong, currentSongIndex);
moveBackward(updatedSong, currentSongIndex);
moveForward(updatedSong, currentSongIndex);
moveBackward(updatedSong, currentSongIndex);
}
}

private void moveForward(final Song changedSong, final int songIndex) {
Expand Down Expand Up @@ -186,13 +197,4 @@ private boolean shouldSwapWithNext(final Song song, final Song nextSong) {

return hasSmallerTotalLikeCountThanNextSong || hasSameTotalLikeCountAndSmallerIdThanNextSong;
}

public void unlike(final KillingPart killingPart, final KillingPartLike unlikeOnKillingPart) {
final Song song = songs.get(killingPart.getSong().getId());
final KillingPart killingPartById = findKillingPart(killingPart, song);
final boolean updated = killingPartById.unlike(unlikeOnKillingPart);
if (updated) {
reorder(song);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -47,6 +46,7 @@ class KillingPartLikeServiceTest extends UsingJpaTest {
private SongRepository songRepository;

private KillingPartLikeService likeService;

private InMemorySongs inMemorySongs;

@BeforeEach
Expand Down Expand Up @@ -221,19 +221,15 @@ void delete_alreadyDeleted_noAction() {
// then
final Optional<KillingPartLike> savedLike = killingPartLikeRepository.
findByKillingPartAndMember(SAVED_KILLING_PART, SAVED_MEMBER);
final Optional<KillingPart> updatedKillingPart = killingPartRepository.findById(
SAVED_KILLING_PART.getId());
final Song savedSong = inMemorySongs.getSongById(SAVED_SONG.getId());

assertThat(savedLike).isPresent()
.get()
.hasFieldOrPropertyWithValue("isDeleted", true);

assertThat(updatedKillingPart).isPresent()
.get()
.hasFieldOrPropertyWithValue("likeCount", 0);
assertThat(savedSong.getTotalLikeCount()).isZero();
}

@Disabled()
@DisplayName("좋아요 데이터가 존재하는 경우, 상태가 변경된다.")
@Test
void create_noAction() {
Expand All @@ -251,16 +247,13 @@ void create_noAction() {
// then
final Optional<KillingPartLike> savedLike = killingPartLikeRepository.
findByKillingPartAndMember(SAVED_KILLING_PART, SAVED_MEMBER);
final Optional<KillingPart> updatedKillingPart = killingPartRepository.findById(
SAVED_KILLING_PART.getId());
final Song savedSong = inMemorySongs.getSongById(SAVED_SONG.getId());

assertThat(savedLike).isPresent()
.get()
.hasFieldOrPropertyWithValue("isDeleted", true);

assertThat(updatedKillingPart).isPresent()
.get()
.hasFieldOrPropertyWithValue("likeCount", 0);
assertThat(savedSong.getTotalLikeCount()).isZero();
}

@DisplayName("존재하지 않는 킬링파트면 예외가 발생한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.assertj.core.api.Assertions.assertThat;
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;
Expand All @@ -30,9 +29,6 @@ class InMemorySongsTest extends UsingJpaTest {
@Autowired
private MemberRepository memberRepository;

@Autowired
private EntityManager entityManager;

@BeforeEach
void setUp() {
MEMBER = memberRepository.findById(1L).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import shook.shook.song.application.dto.MyPartsResponse;
import shook.shook.song.application.killingpart.KillingPartLikeService;
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.KillingPartRepository;
Expand All @@ -46,6 +47,9 @@ void setUp() {
private static final long SAVED_MEMBER_ID = 1L;
private static final String SAVED_MEMBER_NICKNAME = "nickname";

@Autowired
private InMemorySongs inMemorySongs;

@Autowired
private TokenProvider tokenProvider;

Expand Down Expand Up @@ -73,6 +77,8 @@ class GetLikedKillingParts {
@Test
void likedKillingPartExistWithOneDeletedLikeExist() {
//given
inMemorySongs.refreshSongs(songRepository.findAllWithKillingPartsAndLikes());

final String accessToken = tokenProvider.createAccessToken(SAVED_MEMBER_ID,
SAVED_MEMBER_NICKNAME);

Expand Down

0 comments on commit dc73563

Please sign in to comment.