-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: DELETE/timetable 동시성 에러 #743
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.restassured.response.Response; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
@@ -20,6 +21,12 @@ | |
import io.restassured.RestAssured; | ||
import io.restassured.http.ContentType; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
class TimetableApiTest extends AcceptanceTest { | ||
|
||
|
@@ -584,4 +591,45 @@ void deleteTimetable() { | |
|
||
assertThat(timetableRepository.findById(2)).isNotPresent(); | ||
} | ||
|
||
@Test | ||
@DisplayName("시간표 삭제 동시성 예외 적절하게 처리하는지 테스트한다.") | ||
void deleteTimetableConcurrency() throws InterruptedException { | ||
User user = userFixture.준호_학생().getUser(); | ||
String token = userFixture.getToken(user); | ||
Semester semester = semesterFixture.semester("20192"); | ||
|
||
Lecture 건축구조의_이해_및_실습 = lectureFixture.건축구조의_이해_및_실습(semester.getSemester()); | ||
Lecture HRD_개론 = lectureFixture.HRD_개론(semester.getSemester()); | ||
|
||
timetableV2Fixture.시간표6(user, semester, 건축구조의_이해_및_실습, HRD_개론); | ||
|
||
ExecutorService executor = Executors.newFixedThreadPool(2); | ||
CountDownLatch latch = new CountDownLatch(2); | ||
Comment on lines
+607
to
+608
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A최근에 동시성 문제가 많이 발생하는데, 코드 리뷰하는 과정에서 참 많이 배우게 되는 것 같아요!👍 |
||
|
||
List<Response> responseList = new ArrayList<>(); | ||
Runnable deleteTask = () -> { | ||
Response response = RestAssured | ||
.given() | ||
.header("Authorization", "Bearer " + token) | ||
.when() | ||
.param("id", 2) | ||
.delete("/timetable"); | ||
responseList.add(response); | ||
latch.countDown(); | ||
}; | ||
|
||
executor.submit(deleteTask); | ||
executor.submit(deleteTask); | ||
|
||
latch.await(); | ||
|
||
boolean hasConflict = responseList.stream() | ||
.anyMatch(response -> response.getStatusCode() == 409); | ||
|
||
assertThat(hasConflict).isTrue(); | ||
assertThat(timetableRepository.findById(2)).isNotPresent(); | ||
|
||
executor.shutdown(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JPA에선 트랜잭션 충돌을 커밋 or flush할 당시에 감지합니다. 그래서 flush를 하지 않는다면, try범위 밖에서 커밋되면서 예외가 발생되어 잡지 못합니다. 그래서 try문 범위 안에서 예외를 발생시킬 수 있도록 flush 코드를 추가했습니다.