import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
+
import com.codahale.metrics.Snapshot;
import com.codahale.metrics.Timer;
import java.util.concurrent.TimeUnit;
private Snapshot commitSnapshot;
@Before
- public void setUp(){
+ public void setUp() {
MockitoAnnotations.initMocks(this);
doReturn(datastoreContext).when(actorContext).getDatastoreContext();
doReturn(30).when(datastoreContext).getShardTransactionCommitTimeoutInSeconds();
}
@Test
- public void testAcquireRateLimitChanged(){
- for(int i=1;i<11;i++){
+ public void testAcquireRateLimitChanged() {
+ for (int i = 1; i < 11; i++) {
// Keep on increasing the amount of time it takes to complete transaction for each tenth of a
// percentile. Essentially this would be 1ms for the 10th percentile, 2ms for 20th percentile and so on.
doReturn(TimeUnit.MILLISECONDS.toNanos(i) * 1D).when(commitSnapshot).getValue(i * 0.1);
@Test
- public void testAcquirePercentileValueZero(){
+ public void testAcquirePercentileValueZero() {
- for(int i=1;i<11;i++){
+ for (int i = 1; i < 11; i++) {
// Keep on increasing the amount of time it takes to complete transaction for each tenth of a
// percentile. Essentially this would be 1ms for the 10th percentile, 2ms for 20th percentile and so on.
doReturn(TimeUnit.MILLISECONDS.toNanos(i) * 1D).when(commitSnapshot).getValue(i * 0.1);
}
@Test
- public void testAcquireOnePercentileValueVeryHigh(){
+ public void testAcquireOnePercentileValueVeryHigh() {
- for(int i=1;i<11;i++){
+ for (int i = 1; i < 11; i++) {
// Keep on increasing the amount of time it takes to complete transaction for each tenth of a
// percentile. Essentially this would be 1ms for the 10th percentile, 2ms for 20th percentile and so on.
doReturn(TimeUnit.MILLISECONDS.toNanos(i) * 1D).when(commitSnapshot).getValue(i * 0.1);
}
@Test
- public void testAcquireWithAllPercentileValueVeryHigh(){
+ public void testAcquireWithAllPercentileValueVeryHigh() {
- for(int i=1;i<11;i++){
+ for (int i = 1; i < 11; i++) {
// Keep on increasing the amount of time it takes to complete transaction for each tenth of a
// percentile. Essentially this would be 1ms for the 10th percentile, 2ms for 20th percentile and so on.
doReturn(TimeUnit.MILLISECONDS.toNanos(10000) * 1D).when(commitSnapshot).getValue(i * 0.1);
}
@Test
- public void testAcquireWithRealPercentileValues(){
+ public void testAcquireWithRealPercentileValues() {
- for(int i=1;i<11;i++){
+ for (int i = 1; i < 11; i++) {
// Keep on increasing the amount of time it takes to complete transaction for each tenth of a
// percentile. Essentially this would be 1ms for the 10th percentile, 2ms for 20th percentile and so on.
doReturn(TimeUnit.MILLISECONDS.toNanos(8) * 1D).when(commitSnapshot).getValue(i * 0.1);
assertEquals(51, rateLimiter.getPollOnCount());
}
-
-
@Test
- public void testAcquireGetRateLimitFromOtherDataStores(){
- for(int i=1;i<11;i++){
+ public void testAcquireGetRateLimitFromOtherDataStores() {
+ for (int i = 1; i < 11; i++) {
// Keep on increasing the amount of time it takes to complete transaction for each tenth of a
// percentile. Essentially this would be 1ms for the 10th percentile, 2ms for 20th percentile and so on.
doReturn(0.0D).when(commitSnapshot).getValue(i * 0.1);
doReturn(operationalCommitTimerContext).when(operationalCommitTimer).time();
doReturn(operationalCommitSnapshot).when(operationalCommitTimer).getSnapshot();
- for(int i=1;i<11;i++){
+ for (int i = 1; i < 11; i++) {
// Keep on increasing the amount of time it takes to complete transaction for each tenth of a
// percentile. Essentially this would be 1ms for the 10th percentile, 2ms for 20th percentile and so on.
doReturn(TimeUnit.MILLISECONDS.toNanos(i) * 1D).when(operationalCommitSnapshot).getValue(i * 0.1);
}
- DatastoreContext.getGlobalDatastoreTypes().add("config");
- DatastoreContext.getGlobalDatastoreTypes().add("operational");
+ DatastoreContext.getGlobalDatastoreNames().add("config");
+ DatastoreContext.getGlobalDatastoreNames().add("operational");
TransactionRateLimiter rateLimiter = new TransactionRateLimiter(actorContext);
}
@Test
- public void testRateLimiting(){
+ public void testRateLimiting() {
- for(int i=1;i<11;i++){
+ for (int i = 1; i < 11; i++) {
doReturn(TimeUnit.SECONDS.toNanos(1) * 1D).when(commitSnapshot).getValue(i * 0.1);
}
}
@Test
- public void testRateLimitNotCalculatedUntilPollCountReached(){
+ public void testRateLimitNotCalculatedUntilPollCountReached() {
- for(int i=1;i<11;i++){
+ for (int i = 1; i < 11; i++) {
// Keep on increasing the amount of time it takes to complete transaction for each tenth of a
// percentile. Essentially this would be 1ms for the 10th percentile, 2ms for 20th percentile and so on.
doReturn(TimeUnit.MILLISECONDS.toNanos(8) * 1D).when(commitSnapshot).getValue(i * 0.1);
assertEquals(51, rateLimiter.getPollOnCount());
- for(int i=0;i<49;i++){
+ for (int i = 0; i < 49; i++) {
rateLimiter.acquire();
}
}
@Test
- public void testAcquireNegativeAcquireAndPollOnCount(){
+ public void testAcquireNegativeAcquireAndPollOnCount() {
- for(int i=1;i<11;i++){
+ for (int i = 1; i < 11; i++) {
// Keep on increasing the amount of time it takes to complete transaction for each tenth of a
// percentile. Essentially this would be 1ms for the 10th percentile, 2ms for 20th percentile and so on.
doReturn(TimeUnit.MILLISECONDS.toNanos(8) * 1D).when(commitSnapshot).getValue(i * 0.1);
doReturn(TimeUnit.MILLISECONDS.toNanos(200) * 1D).when(commitSnapshot).getValue(1.0);
TransactionRateLimiter rateLimiter = new TransactionRateLimiter(actorContext);
- rateLimiter.setAcquireCount(Long.MAX_VALUE-1);
+ rateLimiter.setAcquireCount(Long.MAX_VALUE - 1);
rateLimiter.setPollOnCount(Long.MAX_VALUE);
rateLimiter.acquire();
assertEquals(-9223372036854775759L, rateLimiter.getPollOnCount());
- for(int i=0;i<50;i++){
+ for (int i = 0; i < 50; i++) {
rateLimiter.acquire();
}
}
- public Matcher<Double> approximately(final double val){
+ public Matcher<Double> approximately(final double val) {
return new BaseMatcher<Double>() {
@Override
- public boolean matches(Object o) {
- Double aDouble = (Double) o;
- return aDouble >= val && aDouble <= val+1;
+ public boolean matches(Object obj) {
+ Double value = (Double) obj;
+ return value >= val && value <= val + 1;
}
@Override
public void describeTo(Description description) {
- description.appendText("> " + val +" < " + (val+1));
+ description.appendText("> " + val + " < " + (val + 1));
}
};
}
-}
\ No newline at end of file
+}