@ -76,6 +76,7 @@ import static org.junit.Assert.assertEquals;
@@ -76,6 +76,7 @@ import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any ;
import static org.mockito.Matchers.anyBoolean ;
import static org.mockito.Matchers.anyInt ;
import static org.mockito.Matchers.anyObject ;
import static org.mockito.Matchers.eq ;
import static org.mockito.Mockito.doReturn ;
import static org.mockito.Mockito.doThrow ;
@ -241,6 +242,46 @@ public class RetryableFeignLoadBalancerTests {
@@ -241,6 +242,46 @@ public class RetryableFeignLoadBalancerTests {
assertEquals ( 1 , backOffPolicy . getCount ( ) ) ;
}
@Test
public void executeRetryOnStatusCodeWithEmptyBody ( ) throws Exception {
int retriesNextServer = 0 ;
when ( this . config . get ( MaxAutoRetriesNextServer ,
DEFAULT_MAX_AUTO_RETRIES_NEXT_SERVER ) ) . thenReturn ( retriesNextServer ) ;
doReturn ( new Server ( "foo" , 80 ) ) . when ( lb ) . chooseServer ( anyObject ( ) ) ;
RibbonLoadBalancerContext lbContext = new RibbonLoadBalancerContext ( lb , config ) ;
SpringClientFactory clientFactory = mock ( SpringClientFactory . class ) ;
IClientConfig config = mock ( IClientConfig . class ) ;
doReturn ( 1 ) . when ( config ) . get ( eq ( CommonClientConfigKey . MaxAutoRetries ) , anyInt ( ) ) ;
doReturn ( retriesNextServer ) . when ( config ) . get ( eq ( CommonClientConfigKey . MaxAutoRetriesNextServer ) , anyInt ( ) ) ;
doReturn ( true ) . when ( config ) . get ( eq ( CommonClientConfigKey . OkToRetryOnAllOperations ) , eq ( false ) ) ;
doReturn ( defaultConnectTimeout ) . when ( config ) . get ( eq ( CommonClientConfigKey . ConnectTimeout ) ) ;
doReturn ( defaultReadTimeout ) . when ( config ) . get ( eq ( CommonClientConfigKey . ReadTimeout ) ) ;
doReturn ( "404" ) . when ( config ) . getPropertyAsString ( eq ( RibbonLoadBalancedRetryPolicy . RETRYABLE_STATUS_CODES ) , eq ( "" ) ) ;
doReturn ( config ) . when ( clientFactory ) . getClientConfig ( eq ( "default" ) ) ;
doReturn ( lbContext ) . when ( clientFactory ) . getLoadBalancerContext ( any ( String . class ) ) ;
MyBackOffPolicy backOffPolicy = new MyBackOffPolicy ( ) ;
RibbonLoadBalancedRetryFactory loadBalancedRetryFactory = new RibbonLoadBalancedRetryFactory ( clientFactory ) {
@Override
public BackOffPolicy createBackOffPolicy ( String service ) {
return backOffPolicy ;
}
} ;
HttpRequest springRequest = mock ( HttpRequest . class ) ;
Request feignRequest = Request . create ( "GET" , "http://foo" , new HashMap < String , Collection < String > > ( ) ,
new byte [ ] { } , StandardCharsets . UTF_8 ) ;
Client client = mock ( Client . class ) ;
FeignLoadBalancer . RibbonRequest request = new FeignLoadBalancer . RibbonRequest ( client , feignRequest , new URI ( "http://foo" ) ) ;
Response response = Response . builder ( ) . status ( 404 ) . headers ( new HashMap < String , Collection < String > > ( ) ) . build ( ) ;
Response fourOFourResponse = Response . builder ( ) . status ( 404 ) . headers ( new HashMap < String , Collection < String > > ( ) ) . build ( ) ;
doReturn ( fourOFourResponse ) . doReturn ( response ) . when ( client ) . execute ( any ( Request . class ) , any ( Request . Options . class ) ) ;
RetryableFeignLoadBalancer feignLb = new RetryableFeignLoadBalancer ( lb , config , inspector , loadBalancedRetryFactory ) ;
FeignLoadBalancer . RibbonResponse ribbonResponse = feignLb . execute ( request , null ) ;
assertEquals ( 404 , ribbonResponse . toResponse ( ) . status ( ) ) ;
assertEquals ( new Integer ( 0 ) , ribbonResponse . toResponse ( ) . body ( ) . length ( ) ) ;
verify ( client , times ( 2 ) ) . execute ( any ( Request . class ) , any ( Request . Options . class ) ) ;
assertEquals ( 1 , backOffPolicy . getCount ( ) ) ;
}
@Test
public void getRequestSpecificRetryHandler ( ) throws Exception {
RibbonLoadBalancerContext lbContext = new RibbonLoadBalancerContext ( lb , config ) ;