-
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.
Merge pull request #156 from PawWithU/feat/155-db-unique-constraint
[Feature] 동시성 이슈 해결을 위한 DB Unique 조건 적용
- Loading branch information
Showing
4 changed files
with
96 additions
and
16 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
74 changes: 74 additions & 0 deletions
74
src/test/java/com/pawwithu/connectdog/domain/application/service/ApplicationServiceTest.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,74 @@ | ||
package com.pawwithu.connectdog.domain.application.service; | ||
|
||
import com.pawwithu.connectdog.domain.application.dto.request.VolunteerApplyRequest; | ||
import com.pawwithu.connectdog.domain.application.repository.ApplicationRepository; | ||
import com.pawwithu.connectdog.domain.volunteer.entity.SocialType; | ||
import com.pawwithu.connectdog.domain.volunteer.entity.Volunteer; | ||
import com.pawwithu.connectdog.domain.volunteer.entity.VolunteerRole; | ||
import com.pawwithu.connectdog.domain.volunteer.repository.VolunteerRepository; | ||
import com.pawwithu.connectdog.error.exception.custom.BadRequestException; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
@SpringBootTest | ||
class ApplicationServiceTest { | ||
|
||
@Autowired | ||
private ApplicationRepository applicationRepository; | ||
@Autowired | ||
private VolunteerRepository volunteerRepository; | ||
@Autowired | ||
private ApplicationService applicationService; | ||
|
||
@Test | ||
public void test_concurrency() throws InterruptedException { | ||
// 이동봉사자 정보 설정 | ||
Volunteer volunteer1 = new Volunteer("[email protected]", VolunteerRole.AUTH_VOLUNTEER, SocialType.NAVER, "자동차"); | ||
Volunteer volunteer2 = new Volunteer("[email protected]", VolunteerRole.AUTH_VOLUNTEER, SocialType.NAVER, "12A"); | ||
|
||
volunteerRepository.save(volunteer1); | ||
volunteerRepository.save(volunteer2); | ||
|
||
int numberOfThreads = 2; | ||
ExecutorService service = Executors.newFixedThreadPool(numberOfThreads); | ||
CountDownLatch latch = new CountDownLatch(numberOfThreads); | ||
VolunteerApplyRequest requestDto1 = new VolunteerApplyRequest("하노정", | ||
"01022223333", | ||
"자동차", | ||
"이동봉사 신청하겠습니다!"); | ||
VolunteerApplyRequest requestDto2 = new VolunteerApplyRequest("민경혁", | ||
"01044445555", | ||
"자동차", | ||
"이동봉사 신청하겠습니다!"); | ||
|
||
service.execute(() -> { | ||
try { | ||
applicationService.volunteerApply("[email protected]", 1L, requestDto1); | ||
} catch (BadRequestException e) { | ||
System.out.println("e.getMessage() = " + e.getMessage()); | ||
} | ||
latch.countDown(); | ||
}); | ||
service.execute(() -> { | ||
try { | ||
applicationService.volunteerApply("[email protected]", 1L, requestDto2); | ||
} catch (BadRequestException e) { | ||
System.out.println("e.getMessage() = " + e.getMessage()); | ||
} | ||
latch.countDown(); | ||
}); | ||
latch.await(); | ||
Thread.sleep(1000); | ||
|
||
// 결과 확인 | ||
Long count = applicationRepository.countAllByPostId(1L); | ||
Assertions.assertThat(count).isEqualTo(1); | ||
} | ||
|
||
} |