Skip to content

Commit

Permalink
refactor: code smell 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
seungh1024 committed Feb 24, 2024
1 parent 893d60a commit d784f28
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public boolean charge(long pk, long money) {
}

public Long getChargeLimit(long pk) {
Long limit = (Long)redisTemplate.opsForValue().get(pk);
Long limit = redisTemplate.opsForValue().get(pk);
if (limit == null) {
limit = mainAccountRepository.findChargeLimitByPk(pk)
.orElseThrow(() -> AccountErrorCode.ACCOUNT_NOT_FOUND.accountException("pk에 해당하는 계좌 없음. PK = " + pk));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public class MainAccountService {
@Transactional(isolation = Isolation.READ_COMMITTED)
public long chargeMoney(long mainAccountPk, long money) {
if (!chargeLimitManager.charge(mainAccountPk, money)) {
throw AccountErrorCode.CHARGE_LIMIT_EXCESS.accountException(
"충전 한도 초과, money = " + money);
throw AccountErrorCode.CHARGE_LIMIT_EXCESS.accountException("충전 한도 초과, money = " + money);
}

MainAccount mainAccount = mainAccountRepository.findByPkForUpdate(mainAccountPk)
Expand All @@ -48,15 +47,15 @@ public long chargeMoney(long mainAccountPk, long money) {
@Transactional(isolation = Isolation.READ_COMMITTED)
public void sendToSavingAccount(long mainAccountPk, long savingAccountPk, long money) {
MainAccount mainAccount = mainAccountRepository.findByPkForUpdate(mainAccountPk)
.orElseThrow(() -> AccountErrorCode.ACCOUNT_NOT_FOUND.accountException());
.orElseThrow(AccountErrorCode.ACCOUNT_NOT_FOUND::accountException);

if (!isSendValid(mainAccount.getMoney(), money)) {
throw AccountErrorCode.INVALID_MONEY_SEND.accountException(
"현재 잔고 부족, mainAccount.getMoney() = " + mainAccount.getMoney() + ", send money = " + money);
}

SavingAccount savingAccount = savingAccountRepository.findByPkForUpdate(savingAccountPk)
.orElseThrow(() -> AccountErrorCode.ACCOUNT_NOT_FOUND.accountException());
.orElseThrow(AccountErrorCode.ACCOUNT_NOT_FOUND::accountException);
savingAccount.addMoney(money);
savingAccountRepository.save(savingAccount);

Expand All @@ -68,7 +67,7 @@ public MainAccountResponseDto getMainAccountInfo(long mainAccountPk) {
MainAccount mainAccount = mainAccountRepository.findById(mainAccountPk)
.orElseThrow(() -> AccountErrorCode.ACCOUNT_NOT_FOUND.accountException(
"존재하지 않는 계좌, mainAccountPk = " + mainAccountPk));

return new MainAccountResponseDto(mainAccount);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.c4marathon.assignment.member.session;

import java.io.Serializable;

public record SessionMemberInfo(
long memberPk,
String memberId,
long mainAccountPk
) {
) implements Serializable {
}
26 changes: 0 additions & 26 deletions src/test/java/org/c4marathon/assignment/SetContainerTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import org.c4marathon.assignment.SetContainerTest;
import org.c4marathon.assignment.bankaccount.entity.MainAccount;
import org.c4marathon.assignment.bankaccount.entity.SavingAccount;
import org.c4marathon.assignment.bankaccount.limit.ChargeLimitManager;
Expand All @@ -24,15 +23,35 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import com.redis.testcontainers.RedisContainer;

import static org.junit.jupiter.api.Assertions.*;

@ActiveProfiles("test")
@SpringBootTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
public class MoneySendConcurrencyTest extends SetContainerTest {
public class MoneySendConcurrencyTest {

private static final String REDIS_IMAGE = "redis:latest";
private static final int REDIS_PORT = 6379;

@Container
private static RedisContainer redis = new RedisContainer(DockerImageName.parse(REDIS_IMAGE)).withExposedPorts(
REDIS_PORT);

@DynamicPropertySource
private static void redisProperties(DynamicPropertyRegistry registry) {
redis.start();
registry.add("spring.data.redis.host", redis::getHost);
registry.add("spring.data.redis.port", redis::getFirstMappedPort);
}

@Autowired
MemberRepository memberRepository;
Expand Down

0 comments on commit d784f28

Please sign in to comment.