Skip to content

Commit

Permalink
test: thread sleep 대신 future get 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
This2sho committed Jun 9, 2024
1 parent 59b7bfd commit 90d5d67
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.parkingcomestrue.external.api;

import com.parkingcomestrue.fake.CircuitBreakerTestService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -17,7 +22,6 @@ class CircuitBreakerAspectTest {
private CircuitBreakerTestService service;

private boolean[] isExecuted = {false, false};
private final long SECOND = 1000;

@Test
void 서비스에_에러가_특정_지수를_넘으면_요청이_잠긴다() {
Expand All @@ -30,25 +34,27 @@ class CircuitBreakerAspectTest {
}

//when
service.call(() -> {isExecuted[0] = true;});
service.call(() -> isExecuted[0] = true);

//then
Assertions.assertThat(isExecuted[0]).isFalse();
}

@Test
void 서비스가_잠긴후_특정시간이_지나면_다시_요청을_보낼수있다() throws InterruptedException {
void 서비스가_잠긴후_특정시간이_지나면_다시_요청을_보낼수있다() throws ExecutionException, InterruptedException {
//given
for (int i = 0; i < 8; i++) {
service.call(() -> {});
}
for (int i = 0; i < 2; i++) {
service.call(() -> {throw new RuntimeException();});
}
Thread.sleep(2 * SECOND);
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

//when
service.call(() -> {isExecuted[1] = true;});
ScheduledFuture<?> future = scheduler.schedule(() -> service.call(() -> isExecuted[1] = true), 2,
TimeUnit.SECONDS);
future.get();

//then
Assertions.assertThat(isExecuted[1]).isTrue();
Expand Down

0 comments on commit 90d5d67

Please sign in to comment.