Skip to content

Commit

Permalink
test: 동기 / 비동기 분리 및 테스트 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
youngsu5582 committed Dec 2, 2024
1 parent caa996f commit c8865c1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 32 deletions.
4 changes: 2 additions & 2 deletions backend/src/test/java/config/TestAsyncConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;

Expand All @@ -12,6 +12,6 @@ public class TestAsyncConfig {

@Bean
public TaskExecutor taskExecutor() {
return new SyncTaskExecutor();
return new SimpleAsyncTaskExecutor();
}
}
17 changes: 17 additions & 0 deletions backend/src/test/java/config/TestSyncConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config;

import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;

@TestConfiguration
@EnableAsync
public class TestSyncConfig {

@Bean
public TaskExecutor taskExecutor() {
return new SyncTaskExecutor();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.context.annotation.Import;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;

@ServiceTest
Expand All @@ -59,16 +60,19 @@ class AutomaticMatchingExecutorTest {
@MockBean
private PullRequestProvider pullRequestProvider;

@Autowired
private RoomMatchInfoRepository roomMatchInfoRepository;

@SpyBean
MatchingExecutor matchingExecutor;

private Room room;
private Room emptyParticipantRoom;
private Member pororo;
private Member ash;
private Member joysun;
private Member movin;
private Member ten;
private Member cho;
@Autowired
private RoomMatchInfoRepository roomMatchInfoRepository;

@BeforeEach
void setUp() {
Expand All @@ -79,9 +83,9 @@ void setUp() {
ten = memberRepository.save(MemberFixture.MEMBER_TENTEN());
cho = memberRepository.save(MemberFixture.MEMBER_CHOCO());

room = roomRepository.save(RoomFixture.ROOM_DOMAIN(pororo, LocalDateTime.now().plusSeconds(3)));
roomMatchInfoRepository.save(new RoomMatchInfo(room.getId(),true));
emptyParticipantRoom = roomRepository.save(RoomFixture.ROOM_DOMAIN(ash, LocalDateTime.now().plusSeconds(3)));
room = roomRepository.save(RoomFixture.ROOM_DOMAIN(pororo, LocalDateTime.now()
.plusSeconds(3)));
roomMatchInfoRepository.save(new RoomMatchInfo(room.getId(), true));

participationRepository.save(new Participation(room, pororo, MemberRole.BOTH, room.getMatchingSize()));
participationRepository.save(new Participation(room, ash, MemberRole.BOTH, room.getMatchingSize()));
Expand Down Expand Up @@ -121,17 +125,14 @@ private PullRequestInfo getPullRequestInfo(Member pororo, Member ash, Member joy
@Test
@DisplayName("동시에 10개의 자동 매칭을 실행해도 PESSIMISTIC_WRITE 락을 통해 동시성을 제어할 수 있다.")
void startMatchingWithLock() throws InterruptedException {
AutomaticMatching automaticMatching = automaticMatchingRepository.save(new AutomaticMatching(room.getId(), LocalDateTime.now().plusDays(1)));
AutomaticMatching automaticMatching = automaticMatchingRepository.save(new AutomaticMatching(room.getId(), LocalDateTime.now()
.plusDays(1)));

int threadCount = 10;
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
CountDownLatch latch = new CountDownLatch(threadCount);
AtomicInteger successCount = new AtomicInteger(0);

when(pullRequestProvider.getUntilDeadline(any(), any())).thenAnswer(ignore -> {
successCount.incrementAndGet();
return getPullRequestInfo(pororo, ash, joysun, movin, ten, cho);
});
when(pullRequestProvider.getUntilDeadline(any(), any())).thenReturn(getPullRequestInfo(pororo, ash, joysun, movin, ten, cho));

for (int i = 0; i < threadCount; i++) {
executorService.execute(() -> {
Expand All @@ -144,7 +145,6 @@ void startMatchingWithLock() throws InterruptedException {
}

latch.await();

assertThat(successCount.get()).isEqualTo(1);
Mockito.verify(matchingExecutor,Mockito.times(1)).match(anyLong());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.context.annotation.Import;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
Expand All @@ -52,6 +52,9 @@ class AutomaticUpdateExecutorTest {
@MockBean
private MatchResultRepository matchResultRepository;

@SpyBean
UpdateExecutor updateExecutor;

private Room room;

@BeforeEach
Expand All @@ -65,17 +68,15 @@ void setUp() {
@Test
@DisplayName("동시에 10개의 자동 업데이트를 실행해도 PESSIMISTIC_WRITE 락을 통해 동시성을 제어할 수 있다.")
void startMatchingWithLock() throws InterruptedException {
AutomaticUpdate automaticUpdate = automaticUpdateRepository.save(new AutomaticUpdate(room.getId(), LocalDateTime.now().plusDays(1)));
AutomaticUpdate automaticUpdate = automaticUpdateRepository.save(new AutomaticUpdate(room.getId(), LocalDateTime.now()
.plusDays(1)));

int threadCount = 10;
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
CountDownLatch latch = new CountDownLatch(threadCount);
AtomicInteger successCount = new AtomicInteger(0);

when(matchResultRepository.findAllByRoomIdAndReviewStatus(anyLong(), any(ReviewStatus.class))).thenAnswer(ignore -> {
successCount.incrementAndGet();
return Collections.singletonList(new MatchResult(room.getId(), MemberFixture.MEMBER_PORORO(), MemberFixture.MEMBER_MOVIN(), ""));
});
when(matchResultRepository.findAllByRoomIdAndReviewStatus(anyLong(), any(ReviewStatus.class)))
.thenReturn(Collections.singletonList(new MatchResult(room.getId(), MemberFixture.MEMBER_PORORO(), MemberFixture.MEMBER_MOVIN(), "")));

for (int i = 0; i < threadCount; i++) {
executorService.execute(() -> {
Expand All @@ -88,7 +89,6 @@ void startMatchingWithLock() throws InterruptedException {
}

latch.await();

assertThat(successCount.get()).isEqualTo(1);
Mockito.verify(updateExecutor,Mockito.times(1)).update(anyLong());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package corea.scheduler.service;

import config.ServiceTest;
import config.TestAsyncConfig;
import config.TestSyncConfig;
import corea.alarm.domain.AlarmActionType;
import corea.alarm.domain.ServerToUserAlarm;
import corea.alarm.repository.ServerToUserAlarmRepository;
Expand Down Expand Up @@ -42,7 +42,7 @@
import static org.mockito.Mockito.when;

@ServiceTest
@Import(TestAsyncConfig.class)
@Import(TestSyncConfig.class)
class MatchingExecutorTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package corea.scheduler.service;

import config.ServiceTest;
import config.TestAsyncConfig;
import config.TestSyncConfig;
import corea.fixture.MemberFixture;
import corea.fixture.RoomFixture;
import corea.member.domain.Member;
Expand All @@ -22,7 +22,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@ServiceTest
@Import(TestAsyncConfig.class)
@Import(TestSyncConfig.class)
class UpdateExecutorTest {

@Autowired
Expand Down

0 comments on commit c8865c1

Please sign in to comment.