use of com.github.ambry.commons.HostLevelThrottler in project ambry by linkedin.
the class AmbrySecurityServiceTest method postProcessQuotaManagerTest.
/**
* {@link AmbrySecurityService#postProcessRequest(RestRequest, Callback)})} should throw RestServiceException if rate
* is more than expected. RestServiceErrorCode.TooManyRequests is expected in this case.
*/
@Test
public void postProcessQuotaManagerTest() throws Exception {
HostLevelThrottler quotaManager = Mockito.mock(HostLevelThrottler.class);
AmbrySecurityService ambrySecurityService = new AmbrySecurityService(new FrontendConfig(new VerifiableProperties(new Properties())), new FrontendMetrics(new MetricRegistry()), URL_SIGNING_SERVICE_FACTORY.getUrlSigningService(), quotaManager, QUOTA_MANAGER);
// Everything should be good.
Mockito.when(quotaManager.shouldThrottle(any())).thenReturn(false);
for (int i = 0; i < 100; i++) {
for (RestMethod restMethod : RestMethod.values()) {
RestRequest restRequest = createRestRequest(restMethod, "/", null);
ambrySecurityService.postProcessRequest(restRequest).get();
}
}
// Requests should be denied.
Mockito.when(quotaManager.shouldThrottle(any())).thenReturn(true);
for (RestMethod restMethod : RestMethod.values()) {
RestRequest restRequest = createRestRequest(restMethod, "/", null);
try {
ambrySecurityService.postProcessRequest(restRequest).get();
Assert.fail("Should have failed.");
} catch (Exception e) {
Assert.assertEquals("Exception should be TooManyRequests", RestServiceErrorCode.TooManyRequests, ((RestServiceException) e.getCause()).getErrorCode());
}
}
}
Aggregations