Skip to content

Commit

Permalink
#129 test: 히카리 연결 풀 테스트 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yonghwankim-dev committed Jan 12, 2024
1 parent 75f5d5d commit b9b07a0
Showing 1 changed file with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
import static org.springframework.web.servlet.mvc.method.annotation.SseEmitter.*;

import java.io.IOException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -38,7 +44,9 @@
import codesquad.fineants.spring.api.kis.response.CurrentPriceResponse;
import codesquad.fineants.spring.api.portfolio_stock.manager.SseEmitterManager;
import codesquad.fineants.spring.api.portfolio_stock.service.PortfolioStockService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ActiveProfiles("test")
@SpringBootTest
class PortfolioEventPublisherTest {
Expand Down Expand Up @@ -141,6 +149,42 @@ void sendEventToPortfolioWithException() throws IOException {
Assertions.assertThat(manager.size()).isZero();
}

@DisplayName("포트폴리오 이벤트 전송시 hikari connection pool이 고갈되어 전송할 수 없다")
@Test
void publishPortfolioEvent_whenConnectionPoolExhaustionOnPortfolioEventTransmission_thenError() throws
InterruptedException {
// given
Stock stock = stockRepository.save(createStock());
stockDividendRepository.saveAll(createStockDividendWith(stock));
Member member = memberRepository.save(createMember());
List<Portfolio> portfolios = new ArrayList<>();
IntStream.rangeClosed(1, 15).forEach(i -> {
Portfolio portfolio = portfolioRepository.save(createPortfolio(member, "제목1"));
portfolios.add(portfolio);
PortfolioHolding portfolioHolding = portfolioHoldingRepository.save(
createPortfolioHolding(portfolio, stock));
purchaseHistoryRepository.save(createPurchaseHistory(portfolioHolding));
currentPriceManager.addCurrentPrice(new CurrentPriceResponse("005930", 60000L));
lastDayClosingPriceManager.addPrice("005930", 50000);
});

portfolios.stream()
.map(Portfolio::getId)
.forEach(portfolioId -> {
SseEmitter sseEmitter = createSseEmitter(portfolioId);
manager.add(portfolioId, sseEmitter);
});

ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
// when
for (int i = 0; i < 10; i++) {
executor.schedule(() -> publisher.sendEventToPortfolio(LocalDateTime.of(2023, 12, 22, 10, 0, 0)), 0L,
TimeUnit.SECONDS);
}
// then
Thread.sleep(30000L);
}

private Member createMember() {
return Member.builder()
.nickname("일개미1234")
Expand All @@ -151,8 +195,12 @@ private Member createMember() {
}

private Portfolio createPortfolio(Member member) {
return createPortfolio(member, "내꿈은 워렌버핏");
}

private Portfolio createPortfolio(Member member, String name) {
return Portfolio.builder()
.name("내꿈은 워렌버핏")
.name(name)
.securitiesFirm("토스")
.budget(1000000L)
.targetGain(1500000L)
Expand Down Expand Up @@ -226,4 +274,21 @@ private List<StockDividend> createStockDividendWith(Stock stock) {
stock)
);
}

private SseEmitter createSseEmitter(Long portfolioId) {
SseEmitter emitter = new SseEmitter(Duration.ofSeconds(30).toMillis());
emitter.onTimeout(() -> {
log.info("emitter{} timeout으로 인한 제거", portfolioId);
emitter.complete();
});
emitter.onCompletion(() -> {
log.info("emitter{} completion으로 인한 제거", portfolioId);
manager.remove(portfolioId);
});
emitter.onError(throwable -> {
log.error(throwable.getMessage());
emitter.complete();
});
return emitter;
}
}

0 comments on commit b9b07a0

Please sign in to comment.