Search in sources :

Example 11 with QuotaException

use of com.github.ambry.quota.QuotaException in project ambry by linkedin.

the class OperationQuotaChargerTest method testCharge.

@Test
public void testCharge() throws Exception {
    // charge should return true if quotaChargeCallback is null.
    OperationQuotaCharger operationQuotaCharger = new OperationQuotaCharger(null, BLOBID, "GetOperation");
    Assert.assertTrue("charge should return true if quotaChargeCallback is null.", operationQuotaCharger.charge());
    QuotaChargeCallback quotaChargeCallback = Mockito.mock(QuotaChargeCallback.class);
    operationQuotaCharger = new OperationQuotaCharger(quotaChargeCallback, BLOBID, "GetOperation");
    // charge should return false if quotaChargeCallback throws exception and isCharged is false.
    Mockito.doThrow(new QuotaException("too many requests", new RouterException("", RouterErrorCode.TooManyRequests), true)).when(quotaChargeCallback).charge();
    Assert.assertFalse("charge should return true if quotaChargeCallback throws exception and isCharged is false.", operationQuotaCharger.charge());
    Mockito.verify(quotaChargeCallback, Mockito.times(1)).charge();
    // charge should return true if quotaChargeCallback.charge goes through.
    Mockito.doNothing().when(quotaChargeCallback).charge();
    Assert.assertTrue("charge should return true if quotaChargeCallback.charge goes through.", operationQuotaCharger.charge());
    Mockito.verify(quotaChargeCallback, Mockito.times(2)).charge();
    // Once isCharged is true, charge should never call quotaChargeCallback.charge.
    Mockito.doNothing().when(quotaChargeCallback).charge();
    Assert.assertTrue("Once isCharged is true, charge should never call quotaChargeCallback.charge.", operationQuotaCharger.charge());
    Mockito.verify(quotaChargeCallback, Mockito.times(2)).charge();
}
Also used : QuotaChargeCallback(com.github.ambry.quota.QuotaChargeCallback) QuotaException(com.github.ambry.quota.QuotaException) Test(org.junit.Test)

Aggregations

QuotaException (com.github.ambry.quota.QuotaException)11 Test (org.junit.Test)7 QuotaResource (com.github.ambry.quota.QuotaResource)5 QuotaChargeCallback (com.github.ambry.quota.QuotaChargeCallback)4 QuotaName (com.github.ambry.quota.QuotaName)3 Account (com.github.ambry.account.Account)2 QuotaMethod (com.github.ambry.quota.QuotaMethod)2 QuotaRecommendation (com.github.ambry.quota.QuotaRecommendation)2 RestRequest (com.github.ambry.rest.RestRequest)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 AccountService (com.github.ambry.account.AccountService)1 Container (com.github.ambry.account.Container)1 AccountStatsStore (com.github.ambry.accountstats.AccountStatsStore)1 ByteBufferReadableStreamChannel (com.github.ambry.commons.ByteBufferReadableStreamChannel)1 RetainingAsyncWritableChannel (com.github.ambry.commons.RetainingAsyncWritableChannel)1 QuotaConfig (com.github.ambry.config.QuotaConfig)1 VerifiableProperties (com.github.ambry.config.VerifiableProperties)1 MessageFormatException (com.github.ambry.messageformat.MessageFormatException)1 MessageFormatRecord (com.github.ambry.messageformat.MessageFormatRecord)1 GetOption (com.github.ambry.protocol.GetOption)1