-
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.
Merge pull request #22 from KU-Taverse/test/circuitbreaker
[Test] circuitbreaker test
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ku.user.user.service; | ||
|
||
import ku.user.client.GameServiceClient; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.cloud.client.circuitbreaker.CircuitBreaker; | ||
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class TestService { | ||
private final GameServiceClient gameServiceClient; | ||
private final CircuitBreakerFactory circuitBreakerFactory; | ||
|
||
|
||
public String getResult(String memberId) { | ||
CircuitBreaker circuitBreaker = circuitBreakerFactory.create("circuitBreaker"); | ||
String result = circuitBreaker.run(()->gameServiceClient.getGameResults("1234"), throwable -> "1234"); | ||
return result; | ||
} | ||
|
||
} |
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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ku.user.client; | ||
|
||
import ku.user.user.service.TestService; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.web.client.RestClientException; | ||
|
||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
@SpringBootTest(properties = "spring.profiles.active=local") | ||
class GameServiceClientTest { | ||
|
||
@MockBean | ||
private GameServiceClient gameServiceClient; | ||
|
||
@Autowired | ||
private TestService testService; | ||
|
||
@Test | ||
void 요청_시간이_초과되면_예외를_발생시킨다() { | ||
// Given: 예외를 던지도록 Mock 설정 | ||
given(gameServiceClient.getGameResults(anyString())) | ||
.willThrow(new RestClientException("Timeout")); | ||
|
||
// When & Then | ||
try { | ||
gameServiceClient.getGameResults("1234"); | ||
} catch (RestClientException e) { | ||
assertThat(e.getMessage()).contains("Timeout"); | ||
} | ||
} | ||
|
||
@Test | ||
void CircuitBreaker가_실패_fallback이_호출된다() { | ||
// Given | ||
given(gameServiceClient.getGameResults(anyString())) | ||
.willThrow(new RuntimeException("Service unavailable")); | ||
|
||
|
||
// When | ||
String result = testService.getResult("testUser"); | ||
|
||
// Then | ||
assertThat(result).isEqualTo("1234"); // Fallback 결과를 확인 | ||
} | ||
|
||
|
||
|
||
} |