BUG 3125 : Set Rate Limit just before acquiring a permit to avoid contention
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / TransactionRateLimitingCommitCallbackTest.java
index 69a023ad3158ebbbe0e1fd04c924d0358851a0d9..de3a56d0782dafb6ee377a5284b4e35ecbb77811 100644 (file)
@@ -9,26 +9,18 @@
 package org.opendaylight.controller.cluster.datastore;
 
 import static org.junit.Assert.fail;
-import static org.mockito.Matchers.anyDouble;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 import com.codahale.metrics.Snapshot;
 import com.codahale.metrics.Timer;
-import java.util.concurrent.TimeUnit;
-import org.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
-import org.hamcrest.Matcher;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.Matchers;
 import org.mockito.Mock;
-import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
 
 public class TransactionRateLimitingCommitCallbackTest {
-
     @Mock
     public ActorContext actorContext;
 
@@ -56,95 +48,13 @@ public class TransactionRateLimitingCommitCallbackTest {
 
     @Test
     public void testSuccess(){
-
-        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);
-        }
-
-
-        TransactionRateLimitingCallback commitCallback = new TransactionRateLimitingCallback(actorContext);
-        commitCallback.run();
-        commitCallback.success();
-
-        verify(actorContext).setTxCreationLimit(Matchers.doubleThat(approximately(292)));
-    }
-
-    @Test
-    public void testSuccessPercentileValueZero(){
-
-        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);
-        }
-
-        doReturn(TimeUnit.MILLISECONDS.toNanos(0) * 1D).when(commitSnapshot).getValue(0.1);
-
-        TransactionRateLimitingCallback commitCallback = new TransactionRateLimitingCallback(actorContext);
-        commitCallback.run();
-        commitCallback.success();
-
-        verify(actorContext).setTxCreationLimit(Matchers.doubleThat(approximately(192)));
-    }
-
-    @Test
-    public void testSuccessOnePercentileValueVeryHigh(){
-
-        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);
-        }
-
-        // ten seconds
-        doReturn(TimeUnit.MILLISECONDS.toNanos(10000) * 1D).when(commitSnapshot).getValue(1.0);
-
-        TransactionRateLimitingCallback commitCallback = new TransactionRateLimitingCallback(actorContext);
-        commitCallback.run();
-        commitCallback.success();
-
-        verify(actorContext).setTxCreationLimit(Matchers.doubleThat(approximately(282)));
-    }
-
-    @Test
-    public void testSuccessWithAllPercentileValueVeryHigh(){
-
-        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);
-        }
-
-        TransactionRateLimitingCallback commitCallback = new TransactionRateLimitingCallback(actorContext);
-        commitCallback.run();
-        commitCallback.success();
-
-        verify(actorContext).setTxCreationLimit(Matchers.doubleThat(approximately(0)));
-    }
-
-    @Test
-    public void testSuccessWithRealPercentileValues(){
-
-        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(20) * 1D).when(commitSnapshot).getValue( 0.7);
-        doReturn(TimeUnit.MILLISECONDS.toNanos(100) * 1D).when(commitSnapshot).getValue( 0.9);
-        doReturn(TimeUnit.MILLISECONDS.toNanos(200) * 1D).when(commitSnapshot).getValue( 1.0);
-
         TransactionRateLimitingCallback commitCallback = new TransactionRateLimitingCallback(actorContext);
         commitCallback.run();
         commitCallback.success();
 
-        verify(actorContext).setTxCreationLimit(Matchers.doubleThat(approximately(101)));
+        verify(commitTimerContext).stop();
     }
 
-
     @Test
     public void testSuccessWithoutRun(){
         TransactionRateLimitingCallback commitCallback = new TransactionRateLimitingCallback(actorContext);
@@ -155,9 +65,7 @@ public class TransactionRateLimitingCommitCallbackTest {
         } catch(IllegalStateException e){
 
         }
-
-        verify(actorContext, never()).setTxCreationLimit(anyDouble());
-
+        verify(commitTimerContext, never()).stop();
     }
 
 
@@ -167,60 +75,7 @@ public class TransactionRateLimitingCommitCallbackTest {
         commitCallback.run();
         commitCallback.failure();
 
-        verify(actorContext, never()).setTxCreationLimit(anyDouble());
-
-    }
-
-    @Test
-    public void testAdjustRateLimitForUnusedTransaction() {
-        doReturn(commitTimer).when(actorContext).getOperationTimer("one", "commit");
-        doReturn("one").when(actorContext).getDataStoreType();
-
-        Timer commitTimer2 = Mockito.mock(Timer.class);
-        Snapshot commitSnapshot2 = Mockito.mock(Snapshot.class);
-
-        doReturn(commitSnapshot2).when(commitTimer2).getSnapshot();
-
-        doReturn(commitTimer2).when(actorContext).getOperationTimer("two", "commit");
-
-        DatastoreContext.newBuilder().dataStoreType("one").build();
-        DatastoreContext.newBuilder().dataStoreType("two").build();
-
-        doReturn(TimeUnit.MICROSECONDS.toNanos(500) * 1D).when(commitSnapshot).getValue(1 * 0.1);
-
-        TransactionRateLimitingCallback.adjustRateLimitForUnusedTransaction(actorContext);
-
-        verify(actorContext, never()).setTxCreationLimit(anyDouble());
-
-        Mockito.reset(commitSnapshot);
-
-        TransactionRateLimitingCallback.adjustRateLimitForUnusedTransaction(actorContext);
-
-        verify(actorContext, never()).setTxCreationLimit(anyDouble());
-
-        System.out.println(""+TimeUnit.SECONDS.toNanos(30)/TimeUnit.MICROSECONDS.toNanos(100));
-
-        doReturn(TimeUnit.MICROSECONDS.toNanos(100) * 1D).when(commitSnapshot2).getValue(1 * 0.1);
-
-        TransactionRateLimitingCallback.adjustRateLimitForUnusedTransaction(actorContext);
-
-        verify(actorContext).setTxCreationLimit(Matchers.doubleThat(approximately(1000)));
-    }
-
-    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;
-            }
-
-            @Override
-            public void describeTo(Description description) {
-                description.appendText("> " + val +" < " + (val+1));
-            }
-        };
+        verify(commitTimerContext, never()).stop();
     }
 
-
 }
\ No newline at end of file