Skip to content

Commit

Permalink
fix: 좋아요 실행 함수를 기점으로 새로운 트랜젝션 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
sukjuhong committed Jan 9, 2025
1 parent 46a3cee commit 1a18c10
Showing 1 changed file with 33 additions and 34 deletions.
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
Expand All @@ -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
Expand All @@ -48,9 +48,6 @@ class LikeControllerTest
SpringExtension,
)

val objectMapper =
jacksonObjectMapper()

lateinit var user: User
lateinit var project: Project
lateinit var accessToken: String
Expand All @@ -59,7 +56,6 @@ class LikeControllerTest
user = userRepository.save(UserFixture.createUser())
project =
projectRepository.save(ProjectFixture.createProject())

accessToken =
jwtService.generateAccessToken(
JwtTokenData(
Expand Down Expand Up @@ -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
Expand All @@ -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,
)
}

// 스레드가 모두 끝날때 까지 대기
Expand All @@ -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
}
}
Expand Down

0 comments on commit 1a18c10

Please sign in to comment.