-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: 분산락 테스트 작성 * comment: 일반 주석 -> javadoc * test: 미션 시작 예고 시간 검증 테스트 * test: 테스트 케이스 추가 * refactor: Component 대신 TestConfiguration과 Bean 사용하도록 변경 * feat: 로그 추가 * fix: where절 오류 수정 * test: MissionRepository 단위 테스트 작성
- Loading branch information
Showing
9 changed files
with
241 additions
and
14 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
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
73 changes: 73 additions & 0 deletions
73
src/test/java/com/nexters/goalpanzi/config/redisson/RedissonLockTest.java
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.nexters.goalpanzi.config.redisson; | ||
|
||
import com.nexters.goalpanzi.config.redis.RedisInitializer; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest( | ||
classes = RedissonTestConfig.class | ||
) | ||
@ContextConfiguration( | ||
initializers = {RedisInitializer.class} | ||
) | ||
public class RedissonLockTest { | ||
|
||
private static final int THREAD_CNT = 2; | ||
|
||
@Autowired | ||
private RedissonLockTestBean redissonLockTestBean; | ||
|
||
private ExecutorService executorService; | ||
private AtomicInteger acquiredLockCnt; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
executorService = Executors.newFixedThreadPool(THREAD_CNT); | ||
acquiredLockCnt = new AtomicInteger(0); | ||
} | ||
|
||
@Test | ||
void 여러_스레드가_동시에_공유_자원에_접근할_수_없다() throws InterruptedException { | ||
for (int i = 0; i < THREAD_CNT; i++) { | ||
executorService.submit(() -> { | ||
try { | ||
redissonLockTestBean.serializeFunction("Shared Resource"); | ||
acquiredLockCnt.incrementAndGet(); | ||
} catch (InterruptedException ignored) { | ||
} | ||
}); | ||
} | ||
executorService.shutdown(); | ||
executorService.awaitTermination(5, TimeUnit.SECONDS); | ||
|
||
assertThat(acquiredLockCnt.get()).isNotEqualTo(THREAD_CNT); | ||
} | ||
|
||
@Test | ||
void 여러_스레드가_동시에_서로_다른_자원에_접근할_수_있다() throws InterruptedException { | ||
for (int i = 0; i < THREAD_CNT; i++) { | ||
String resource = "Resource" + i; | ||
executorService.submit(() -> { | ||
try { | ||
redissonLockTestBean.serializeFunction(resource); | ||
acquiredLockCnt.incrementAndGet(); | ||
} catch (InterruptedException ignored) { | ||
} | ||
}); | ||
} | ||
executorService.shutdown(); | ||
executorService.awaitTermination(5, TimeUnit.SECONDS); | ||
|
||
assertThat(acquiredLockCnt.get()).isEqualTo(THREAD_CNT); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/test/java/com/nexters/goalpanzi/config/redisson/RedissonLockTestBean.java
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.nexters.goalpanzi.config.redisson; | ||
|
||
import com.nexters.goalpanzi.common.annotation.RedissonLock; | ||
|
||
public class RedissonLockTestBean { | ||
|
||
@RedissonLock(waitTime = 1L) | ||
void serializeFunction(final Object args) throws InterruptedException { | ||
Thread.sleep(2000); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/com/nexters/goalpanzi/config/redisson/RedissonTestConfig.java
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.nexters.goalpanzi.config.redisson; | ||
|
||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
@TestConfiguration | ||
public class RedissonTestConfig { | ||
|
||
@Bean | ||
public RedissonLockTestBean redissonLockTestBean() { | ||
return new RedissonLockTestBean(); | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
src/test/java/com/nexters/goalpanzi/domain/mission/repository/MissionRepositoryTest.java
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.nexters.goalpanzi.domain.mission.repository; | ||
|
||
import com.nexters.goalpanzi.domain.mission.InvitationCode; | ||
import com.nexters.goalpanzi.domain.mission.Mission; | ||
import com.nexters.goalpanzi.domain.mission.TimeOfDay; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import static com.nexters.goalpanzi.fixture.MemberFixture.MEMBER_ID; | ||
import static com.nexters.goalpanzi.fixture.MissionFixture.*; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
@DataJpaTest | ||
class MissionRepositoryTest { | ||
|
||
@Autowired | ||
private MissionRepository missionRepository; | ||
|
||
private final Mission READY_MISSION = Mission.create( | ||
MEMBER_ID, | ||
DESCRIPTION, | ||
LocalDateTime.now().plusDays(1), | ||
LocalDateTime.now().plusDays(31), | ||
TimeOfDay.EVERYDAY, | ||
WEEK, | ||
BOARD_COUNT, | ||
InvitationCode.generate() | ||
); | ||
private final Mission IN_PROGRESS_MISSION = Mission.create( | ||
MEMBER_ID, | ||
DESCRIPTION, | ||
LocalDateTime.now().minusDays(1), | ||
LocalDateTime.now().plusDays(29), | ||
TimeOfDay.EVERYDAY, | ||
WEEK, | ||
BOARD_COUNT, | ||
InvitationCode.generate() | ||
); | ||
private final Mission COMPLETED_MISSION = Mission.create( | ||
MEMBER_ID, | ||
DESCRIPTION, | ||
LocalDateTime.now().minusDays(30), | ||
LocalDateTime.now().minusDays(1), | ||
TimeOfDay.EVERYDAY, | ||
WEEK, | ||
BOARD_COUNT, | ||
InvitationCode.generate() | ||
); | ||
|
||
@Test | ||
void 준비_상태의_미션을_조회한다() { | ||
Mission readyMission = missionRepository.save(READY_MISSION); | ||
Mission inProgressMission = missionRepository.save(IN_PROGRESS_MISSION); | ||
Mission completedMission = missionRepository.save(COMPLETED_MISSION); | ||
|
||
List<Mission> missions = missionRepository.getReadyMissions(); | ||
|
||
assertAll( | ||
() -> assertThat(missions.size()).isEqualTo(1), | ||
() -> assertThat(missions.get(0).getId()).isEqualTo(readyMission.getId()) | ||
); | ||
} | ||
|
||
@Test | ||
void 진행중인_미션을_조회한다() { | ||
Mission readyMission = missionRepository.save(READY_MISSION); | ||
Mission inProgressMission = missionRepository.save(IN_PROGRESS_MISSION); | ||
Mission completedMission = missionRepository.save(COMPLETED_MISSION); | ||
|
||
List<Mission> missions = missionRepository.getInProgressMissions(); | ||
|
||
assertAll( | ||
() -> assertThat(missions.size()).isEqualTo(1), | ||
() -> assertThat(missions.get(0).getId()).isEqualTo(inProgressMission.getId()) | ||
); | ||
} | ||
} |