sopt-makers / sopt-auth-backend

3 stars 0 forks source link

[SETTING] Spring Security & Cloud Open Feign Client #8

Closed yummygyudon closed 6 days ago

yummygyudon commented 2 weeks ago

About Issue ๐Ÿš€

์ „์—ญ์ ์ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ธ์ฆ/์ธ๊ฐ€ Filter๋ฅผ ๊ตฌ์ถ•ํ•˜๊ณ  ๊ธฐ๋ณธ์ ์ธ ์„œ๋ฒ„๊ฐ„ ํ†ต์‹ ์— ์‚ฌ์šฉํ•  client๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

Progress โœ”๏ธ

yummygyudon commented 1 week ago

[์‹œ๋„ํ–ˆ์ง€๋งŒ ์‹คํŒจํ•œ ๊ฒƒ]

OpenFeign - Test

Trade-Off

Ref.

yummygyudon commented 1 week ago

์‹œ๋„ํ•˜๊ณ ์ž ํ–ˆ๋˜ ๋ฐฉ์‹์€ ๋ฐ”๋กœ ์•„๋ž˜์˜ค ๊ฐ™์€ ์ฝ”๋“œ์ด๋‚˜ ํ•ด๋‹น ์ฝ”๋“œ๋Š” Spring ์—์„œ์˜ @Retryable๊ณผ @Recover ๋ฅผ ํ†ตํ•œ ๊ตฌํ˜„์— ์ ํ•ฉํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ณด์—ฌ์ง„๋‹ค. ์ด์œ ๋Š” ์‹ค์ œ ํƒ€ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ์˜ ๊ฒฐ๊ณผ์— ๋”ฐ๋ฅธ ๋™์  ์ฒ˜๋ฆฌ์— ๋Œ€ํ•ด์„œ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๋‹จ์ˆœ ํ–‰์œ„๋งŒ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๊ธฐ์— ์˜๋ฏธ๊ฐ€ ์—†๋Š” ํ…Œ์ŠคํŠธ๊ฐ€ ๋  ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.

...
BDDMockito
        .willThrow(new RuntimeException("", new Exception()))
        .willThrow(new RuntimeException("", new Exception()))
        .willReturn(expected)
        .given(playgroundClient).getHealthStatus();

// when
Assertions.assertThatThrownBy(() -> playgroundClient.getHealthStatus())
                    .isInstanceOf(RetryableException.class);

// then 
Mockito.verify(playgroundClient, Mockito.times(3)).getHealthStatus();

์ถ”ํ›„ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ฒ€์ฆํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 // ์ด์™€ ๊ฐ™์€ RetryableException์„ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  ์‹ค์ œ MockServer์—์„œ์˜ ํ…Œ์ŠคํŠธ๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒ๋ฉ๋‹ˆ๋‹ค.
/*
WireMock(Open Feign) / MockRestServiceServer(RestTemplate) ์„ ํ™œ์šฉํ•œ ํ…Œ์ŠคํŠธ
*/

// when - RetryableException์„ ์žก๊ธฐ์œ„ํ•ด์„  `thenThrow()`Mocking์ด ์•„๋‹Œ ์‹ค์ œ Feign ํ˜ธ์ถœ์„ ํ†ตํ•ด์„œ Retry์„ ์œ ๋„ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
Assertions.assertThatThrownBy(() -> playgroundClient.getHealthStatus())
                    .isInstanceOf(RetryableException.class);

// then 
Mockito.verify(playgroundClient, Mockito.times(3)).getHealthStatus();