Skip to content

Commit d4314ee

Browse files
committed
- applies code review suggestions
1 parent 4097367 commit d4314ee

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

src/main/java/com/microsoft/graph/httpcore/ChaosHttpHandler.java

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,53 @@
1313
import okhttp3.Response;
1414
import okhttp3.ResponseBody;
1515

16+
/**
17+
* DO NOT USE IN PRODUCTION
18+
* interceptor that randomly fails the responses for unit testing purposes
19+
*/
1620
public class ChaosHttpHandler implements Interceptor {
17-
1821
public final MiddlewareType MIDDLEWARE_TYPE = MiddlewareType.RETRY;
19-
20-
/*
21-
* constant string being used
22-
*/
23-
private final String RETRY_AFTER = "Retry-After";
24-
25-
public static final int MSClientErrorCodeTooManyRequests = 429;
26-
22+
/*
23+
* constant string being used
24+
*/
25+
private final String RETRY_AFTER = "Retry-After";
26+
/**
27+
* Denominator for the failure rate (i.e. 1/X)
28+
*/
29+
private final Integer failureRate = 3;
30+
/**
31+
* default value to return on retry after
32+
*/
33+
private final String retryAfterValue = "10";
34+
/**
35+
* body to respond on failed requests
36+
*/
37+
private final String responseBody = "{\"error\": {\"code\": \"TooManyRequests\",\"innerError\": {\"code\": \"429\",\"date\": \"2020-08-18T12:51:51\",\"message\": \"Please retry after\",\"request-id\": \"94fb3b52-452a-4535-a601-69e0a90e3aa2\",\"status\": \"429\"},\"message\": \"Please retry again later.\"}}";
38+
public static final int MSClientErrorCodeTooManyRequests = 429;
39+
2740
@Override
2841
public Response intercept(Chain chain) throws IOException {
2942
Request request = chain.request();
3043

3144
if(request.tag(TelemetryOptions.class) == null)
3245
request = request.newBuilder().tag(TelemetryOptions.class, new TelemetryOptions()).build();
3346
request.tag(TelemetryOptions.class).setFeatureUsage(TelemetryOptions.RETRY_HANDLER_ENABLED_FLAG);
34-
35-
final Integer dice = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);
47+
48+
final Integer dice = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE);
3649

37-
if(dice % 3 == 0) {
38-
return new Response
39-
.Builder()
40-
.request(request)
41-
.protocol(Protocol.HTTP_1_1)
42-
.code(MSClientErrorCodeTooManyRequests)
43-
.message("Too Many Requests")
44-
.addHeader(RETRY_AFTER, "10")
45-
.body(ResponseBody.create(MediaType.get("application/json"), "{\"error\": {\"code\": \"TooManyRequests\",\"innerError\": {\"code\": \"429\",\"date\": \"2020-08-18T12:51:51\",\"message\": \"Please retry after\",\"request-id\": \"94fb3b52-452a-4535-a601-69e0a90e3aa2\",\"status\": \"429\"},\"message\": \"Please retry again later.\"}}"))
46-
.build();
47-
} else {
48-
return chain.proceed(request);
49-
}
50+
if(dice % failureRate == 0) {
51+
return new Response
52+
.Builder()
53+
.request(request)
54+
.protocol(Protocol.HTTP_1_1)
55+
.code(MSClientErrorCodeTooManyRequests)
56+
.message("Too Many Requests")
57+
.addHeader(RETRY_AFTER, retryAfterValue)
58+
.body(ResponseBody.create(MediaType.get("application/json"), responseBody))
59+
.build();
60+
} else {
61+
return chain.proceed(request);
62+
}
5063
}
5164

5265
}

0 commit comments

Comments
 (0)