Search in sources :

Example 1 with HostLevelThrottler

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());
        }
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) FrontendConfig(com.github.ambry.config.FrontendConfig) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) VerifiableProperties(com.github.ambry.config.VerifiableProperties) HostLevelThrottler(com.github.ambry.commons.HostLevelThrottler) MetricRegistry(com.codahale.metrics.MetricRegistry) BlobProperties(com.github.ambry.messageformat.BlobProperties) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) RestServiceException(com.github.ambry.rest.RestServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RestMethod(com.github.ambry.rest.RestMethod) Test(org.junit.Test)

Aggregations

MetricRegistry (com.codahale.metrics.MetricRegistry)1 HostLevelThrottler (com.github.ambry.commons.HostLevelThrottler)1 FrontendConfig (com.github.ambry.config.FrontendConfig)1 VerifiableProperties (com.github.ambry.config.VerifiableProperties)1 BlobProperties (com.github.ambry.messageformat.BlobProperties)1 MockRestRequest (com.github.ambry.rest.MockRestRequest)1 RestMethod (com.github.ambry.rest.RestMethod)1 RestRequest (com.github.ambry.rest.RestRequest)1 RestServiceException (com.github.ambry.rest.RestServiceException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 Properties (java.util.Properties)1 ExecutionException (java.util.concurrent.ExecutionException)1 JSONException (org.json.JSONException)1 Test (org.junit.Test)1