Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions core/src/main/java/io/grpc/internal/ServiceConfigUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ static Throttle getThrottlePolicy(@Nullable Map<String, ?> serviceConfig) {
return null;
}

// TODO(dapengzhang0): check if this is null.
float maxTokens = JsonUtil.getNumberAsDouble(throttling, "maxTokens").floatValue();
float tokenRatio = JsonUtil.getNumberAsDouble(throttling, "tokenRatio").floatValue();
float maxTokens = checkNotNull(
JsonUtil.getNumberAsDouble(throttling, "maxTokens"),
"maxTokens cannot be empty").floatValue();
float tokenRatio = checkNotNull(
JsonUtil.getNumberAsDouble(throttling, "tokenRatio"),
"tokenRatio cannot be empty").floatValue();
checkState(maxTokens > 0f, "maxToken should be greater than zero");
checkState(tokenRatio > 0f, "tokenRatio should be greater than zero");
return new Throttle(maxTokens, tokenRatio);
Expand Down
22 changes: 22 additions & 0 deletions core/src/test/java/io/grpc/internal/ServiceConfigUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ public void unwrapLoadBalancingConfigList_failOnMalformedConfig() throws Excepti
}
}

@Test
public void getThrottlePolicy_failOnMissingMaxTokens() throws Exception {
String serviceConfig = "{\"retryThrottling\" : {\"tokenRatio\" : 0.1}}";
try {
ServiceConfigUtil.getThrottlePolicy(checkObject(JsonParser.parse(serviceConfig)));
fail("Should throw");
} catch (Exception e) {
assertThat(e).hasMessageThat().contains("maxTokens cannot be empty");
}
}

@Test
public void getThrottlePolicy_failOnMissingTokenRatio() throws Exception {
String serviceConfig = "{\"retryThrottling\" : {\"maxTokens\" : 10}}";
try {
ServiceConfigUtil.getThrottlePolicy(checkObject(JsonParser.parse(serviceConfig)));
fail("Should throw");
} catch (Exception e) {
assertThat(e).hasMessageThat().contains("tokenRatio cannot be empty");
}
}

@SuppressWarnings("unchecked")
private static List<Map<String, ?>> checkObjectList(Object o) {
return (List<Map<String, ?>>) o;
Expand Down
Loading