-
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.
- Loading branch information
Showing
1 changed file
with
33 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
package org.pofo.api.domain.like | ||
|
||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.extensions.spring.SpringExtension | ||
import io.kotest.matchers.nulls.shouldNotBeNull | ||
import io.kotest.matchers.shouldBe | ||
import org.pofo.api.domain.security.jwt.JwtService | ||
import org.pofo.api.domain.security.jwt.JwtTokenData | ||
import org.pofo.api.fixture.ProjectFixture | ||
import org.pofo.api.fixture.UserFixture | ||
import org.pofo.common.exception.CustomException | ||
import org.pofo.common.exception.ErrorCode | ||
import org.pofo.common.response.Version | ||
import org.pofo.domain.rds.domain.like.Like | ||
|
@@ -26,12 +25,13 @@ import org.springframework.test.context.ActiveProfiles | |
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.delete | ||
import org.springframework.test.web.servlet.post | ||
import org.springframework.transaction.annotation.Propagation | ||
import org.springframework.transaction.annotation.Transactional | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.ExecutorService | ||
import java.util.concurrent.Executors | ||
|
||
@SpringBootTest | ||
@Transactional | ||
@AutoConfigureMockMvc | ||
@ActiveProfiles("test") | ||
class LikeControllerTest | ||
|
@@ -48,9 +48,6 @@ class LikeControllerTest | |
SpringExtension, | ||
) | ||
|
||
val objectMapper = | ||
jacksonObjectMapper() | ||
|
||
lateinit var user: User | ||
lateinit var project: Project | ||
lateinit var accessToken: String | ||
|
@@ -59,7 +56,6 @@ class LikeControllerTest | |
user = userRepository.save(UserFixture.createUser()) | ||
project = | ||
projectRepository.save(ProjectFixture.createProject()) | ||
|
||
accessToken = | ||
jwtService.generateAccessToken( | ||
JwtTokenData( | ||
|
@@ -143,6 +139,27 @@ class LikeControllerTest | |
} | ||
} | ||
|
||
@Transactional(propagation = Propagation.REQUIRES_NEW) | ||
fun submitLikeRequest( | ||
executor: ExecutorService, | ||
latch: CountDownLatch, | ||
likeService: LikeService, | ||
userId: Long, | ||
projectId: Long, | ||
) { | ||
executor.submit { | ||
try { | ||
likeService.likeProject(userId, projectId) | ||
} catch (ex: Exception) { | ||
println( | ||
"User ID: $userId, Project ID: $projectId - Exception in thread ${Thread.currentThread().name}: ${ex.message}", | ||
) | ||
} finally { | ||
latch.countDown() | ||
} | ||
} | ||
} | ||
|
||
describe("좋아요 동시성 테스트") { | ||
it("동시 요청에서도 좋아요 수가 정확히 관리된다") { | ||
val likeCount = 1 | ||
|
@@ -151,32 +168,14 @@ class LikeControllerTest | |
val latch = CountDownLatch(likeCount) | ||
val executor = Executors.newFixedThreadPool(likeCount) | ||
|
||
val newUser = | ||
userRepository.saveAndFlush( | ||
User | ||
.builder() | ||
.email("[email protected]") | ||
.password("123gjs21@d") | ||
.username("testnamerr") | ||
.build(), | ||
) | ||
val newProject = | ||
projectRepository.save(ProjectFixture.createProject()) | ||
|
||
val test = userRepository.findById(newUser.id) | ||
|
||
repeat(likeCount) { | ||
executor.submit { | ||
try { | ||
likeService.likeProject(newUser.id, newProject.id) | ||
} catch (ex: Exception) { | ||
println( | ||
"User ID: ${test.id}, Project ID: ${newProject.id} - Exception in thread ${Thread.currentThread().name}: ${ex.message}", | ||
) | ||
} finally { | ||
latch.countDown() | ||
} | ||
} | ||
submitLikeRequest( | ||
executor = executor, | ||
latch = latch, | ||
likeService = likeService, | ||
userId = user.id, | ||
projectId = project.id, | ||
) | ||
} | ||
|
||
// 스레드가 모두 끝날때 까지 대기 | ||
|
@@ -185,8 +184,8 @@ class LikeControllerTest | |
|
||
// DB에 조회 한번 해봐서 제대로 값이 나왔는지 확인 | ||
val updatedProject = | ||
projectRepository.findById(newProject.id!!) | ||
?: throw CustomException(ErrorCode.PROJECT_NOT_FOUND) | ||
projectRepository.findById(project.id) | ||
updatedProject.shouldNotBeNull() | ||
updatedProject.likes shouldBe likeCount | ||
} | ||
} | ||
|