Improved unit tests for AveragingProgressTracker class 45/53145/12
authorIvan Hrasko <ivan.hrasko@pantheon.tech>
Fri, 10 Mar 2017 14:45:13 +0000 (15:45 +0100)
committerTom Pantelis <tompantelis@gmail.com>
Fri, 14 Apr 2017 21:07:14 +0000 (21:07 +0000)
Change-Id: I079b45304d82bfc9022321a1648fbdba13409c90
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
opendaylight/md-sal/cds-access-client/src/test/java/org/opendaylight/controller/cluster/access/client/AveragingProgressTrackerTest.java

index fdaff6f..934e158 100644 (file)
@@ -8,52 +8,82 @@
 package org.opendaylight.controller.cluster.access.client;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
 
+import com.google.common.testing.FakeTicker;
+import java.util.concurrent.TimeUnit;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.internal.matchers.apachecommons.ReflectionEquals;
 
 public class AveragingProgressTrackerTest {
+    private static final long CHECKER = TimeUnit.MILLISECONDS.toNanos(500);
+    private static final long TICKER_STEP = 100;
 
-    private static final long NOW = 1000000000;
-    private static final long CHECKER = 500000000;
-    private ProgressTracker averagingProgressTracker;
+    private static FakeTicker ticker;
+
+    private AveragingProgressTracker averagingProgressTracker;
 
     @Before
     public void setUp() {
-        averagingProgressTracker = new AveragingProgressTracker(4);
-        long delay = averagingProgressTracker.estimateIsolatedDelay(NOW);
-        assertEquals(0, delay);
-        for (int i = 0; i < 2; i++) {
-            delay = averagingProgressTracker.openTask(NOW);
-            assertEquals(0, delay);
-        }
+        ticker = new FakeTicker();
+        ticker.setAutoIncrementStep(TICKER_STEP, TimeUnit.MILLISECONDS);
+        averagingProgressTracker = new AveragingProgressTracker(3);
     }
 
     @Test
     public void estimateIsolatedDelayTest() {
-        long delay = averagingProgressTracker.openTask(NOW);
-        assertEquals(CHECKER, delay);
+        long time = ticker.read();
+        assertEquals(0, averagingProgressTracker.estimateIsolatedDelay(time));
+
+        // less than half
+        time = ticker.read();
+        assertTrue(averagingProgressTracker.openTask(time) <= CHECKER);
+        assertEquals(0, averagingProgressTracker.estimateIsolatedDelay(time));
 
-        delay = averagingProgressTracker.openTask(NOW);
-        assertEquals(NOW, delay);
+        // more than half but less than limit
+        time = ticker.read();
+        assertTrue(averagingProgressTracker.openTask(time) <= CHECKER);
+        assertTrue(averagingProgressTracker.estimateIsolatedDelay(time) < CHECKER);
 
-        delay = averagingProgressTracker.estimateIsolatedDelay(NOW);
-        assertEquals(CHECKER, delay);
+        // limit reached
+        time = ticker.read();
+        assertTrue(averagingProgressTracker.openTask(time) >= CHECKER);
+        assertEquals(CHECKER, averagingProgressTracker.estimateIsolatedDelay(time));
 
-        averagingProgressTracker.closeTask(3000000000L, 0, 0, 0);
+        // above limit, no higher isolated delays than CHECKER
+        time = ticker.read();
+        assertTrue(averagingProgressTracker.openTask(time) >= CHECKER);
+        assertEquals(CHECKER, averagingProgressTracker.estimateIsolatedDelay(time));
 
-        delay = averagingProgressTracker.estimateIsolatedDelay(NOW);
-        assertEquals(0, delay);
+        // close tasks to get under the half
+        averagingProgressTracker.closeTask(ticker.read(), 0, 0, 0);
+        averagingProgressTracker.closeTask(ticker.read(), 0, 0, 0);
+        averagingProgressTracker.closeTask(ticker.read(), 0, 0, 0);
+
+        assertEquals(0, averagingProgressTracker.estimateIsolatedDelay(ticker.read()));
     }
 
     @Test
     public void copyObjectTest() {
-        final ProgressTracker copyAverageProgressTracker =
-                new AveragingProgressTracker((AveragingProgressTracker) averagingProgressTracker);
-        assertEquals(copyAverageProgressTracker.openTask(NOW), averagingProgressTracker.openTask(NOW));
-        assertEquals(averagingProgressTracker.tasksClosed(), copyAverageProgressTracker.tasksClosed());
-        assertEquals(averagingProgressTracker.tasksEncountered(), copyAverageProgressTracker.tasksEncountered());
-        assertEquals(averagingProgressTracker.tasksOpen(), copyAverageProgressTracker.tasksOpen());
-    }
+        final AveragingProgressTracker copyAverageProgressTracker = new AveragingProgressTracker(
+                averagingProgressTracker);
 
+        // copied object is the same as original
+        assertTrue(new ReflectionEquals(averagingProgressTracker).matches(copyAverageProgressTracker));
+
+        // afterwards work of copied tracker is independent
+        averagingProgressTracker.openTask(ticker.read());
+
+        final long time = ticker.read();
+        assertNotEquals("Trackers are expected to return different results for tracking",
+                averagingProgressTracker.openTask(time), copyAverageProgressTracker.openTask(time));
+        assertNotEquals("Trackers are expected to encounter different amount of tasks",
+                averagingProgressTracker.tasksEncountered(), copyAverageProgressTracker.tasksEncountered());
+
+        // and copied object is then no more the same as original
+        assertFalse(new ReflectionEquals(averagingProgressTracker).matches(copyAverageProgressTracker));
+    }
 }
\ No newline at end of file

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.