diff --git a/core/api/pom.xml b/core/api/pom.xml index 3fd8a01020..4229da77de 100644 --- a/core/api/pom.xml +++ b/core/api/pom.xml @@ -21,10 +21,6 @@ com.google.guava guava-testlib - - org.onlab.onos - onlab-misc - diff --git a/core/api/src/test/java/org/onlab/onos/event/AbstractEventAccumulatorTest.java b/core/api/src/test/java/org/onlab/onos/event/AbstractEventAccumulatorTest.java new file mode 100644 index 0000000000..c0cce929a3 --- /dev/null +++ b/core/api/src/test/java/org/onlab/onos/event/AbstractEventAccumulatorTest.java @@ -0,0 +1,83 @@ +package org.onlab.onos.event; + +import org.junit.Test; + +import java.util.List; +import java.util.Timer; + +import static org.junit.Assert.*; +import static org.onlab.junit.TestTools.delay; +import static org.onlab.onos.event.TestEvent.Type.FOO; + +/** + * Tests the operation of the accumulator. + */ +public class AbstractEventAccumulatorTest { + + private final Timer timer = new Timer(); + + @Test + public void basics() throws Exception { + TestAccumulator accumulator = new TestAccumulator(); + assertEquals("incorrect timer", timer, accumulator.timer()); + assertEquals("incorrect max events", 5, accumulator.maxEvents()); + assertEquals("incorrect max ms", 100, accumulator.maxBatchMillis()); + assertEquals("incorrect idle ms", 50, accumulator.maxIdleMillis()); + } + + @Test + public void eventTrigger() { + TestAccumulator accumulator = new TestAccumulator(); + accumulator.add(new TestEvent(FOO, "a")); + accumulator.add(new TestEvent(FOO, "b")); + accumulator.add(new TestEvent(FOO, "c")); + accumulator.add(new TestEvent(FOO, "d")); + assertTrue("should not have fired yet", accumulator.batch.isEmpty()); + accumulator.add(new TestEvent(FOO, "e")); + delay(10); + assertFalse("should have fired", accumulator.batch.isEmpty()); + assertEquals("incorrect batch", "abcde", accumulator.batch); + } + + @Test + public void timeTrigger() { + TestAccumulator accumulator = new TestAccumulator(); + accumulator.add(new TestEvent(FOO, "a")); + delay(40); + assertTrue("should not have fired yet", accumulator.batch.isEmpty()); + accumulator.add(new TestEvent(FOO, "b")); + delay(40); + assertTrue("should not have fired yet", accumulator.batch.isEmpty()); + accumulator.add(new TestEvent(FOO, "c")); + delay(40); + assertFalse("should have fired", accumulator.batch.isEmpty()); + assertEquals("incorrect batch", "abc", accumulator.batch); + } + + @Test + public void idleTrigger() { + TestAccumulator accumulator = new TestAccumulator(); + accumulator.add(new TestEvent(FOO, "a")); + assertTrue("should not have fired yet", accumulator.batch.isEmpty()); + accumulator.add(new TestEvent(FOO, "b")); + delay(80); + assertFalse("should have fired", accumulator.batch.isEmpty()); + assertEquals("incorrect batch", "ab", accumulator.batch); + } + + private class TestAccumulator extends AbstractEventAccumulator { + + String batch = ""; + + protected TestAccumulator() { + super(timer, 5, 100, 50); + } + + @Override + public void processEvents(List events) { + for (Event event : events) { + batch += event.subject(); + } + } + } +} diff --git a/core/pom.xml b/core/pom.xml index 13deec40ca..272b76b3bf 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -26,11 +26,14 @@ com.google.guava guava - org.onlab.onos onlab-misc + + org.onlab.onos + onlab-junit + diff --git a/pom.xml b/pom.xml index 73739a99a9..51f1a5ad91 100644 --- a/pom.xml +++ b/pom.xml @@ -130,6 +130,12 @@ onlab-misc ${project.version} + + org.onlab.onos + onlab-junit + 1.0.0-SNAPSHOT + test + org.onlab.onos diff --git a/utils/junit/pom.xml b/utils/junit/pom.xml new file mode 100644 index 0000000000..d994a0716b --- /dev/null +++ b/utils/junit/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + + org.onlab.onos + onlab-utils + 1.0.0-SNAPSHOT + ../pom.xml + + + onlab-junit + bundle + + ON.Lab JUnit test utilities + + + + junit + junit + compile + + + com.google.guava + guava-testlib + compile + + + + diff --git a/utils/junit/src/main/java/org/onlab/junit/TestTools.java b/utils/junit/src/main/java/org/onlab/junit/TestTools.java new file mode 100644 index 0000000000..959d5d998a --- /dev/null +++ b/utils/junit/src/main/java/org/onlab/junit/TestTools.java @@ -0,0 +1,93 @@ +package org.onlab.junit; + +import static com.google.common.base.Preconditions.checkArgument; +import static org.junit.Assert.fail; + +/** + * Utilities to aid in producing JUnit tests. + */ +public final class TestTools { + + // Prohibit construction + private TestTools() { + } + + /** + * Suspends the current thread for a specified number of millis. + * + * @param ms number of millis + */ + public static void delay(int ms) { + try { + Thread.sleep(ms); + } catch (InterruptedException e) { + fail("test interrupted"); + } + } + + /** + * Returns the current time in millis since epoch. + * + * @return current time + */ + public static long now() { + return System.currentTimeMillis(); + } + + /** + * Runs the specified runnable until it completes successfully or until the + * specified time expires. If the latter occurs, the first encountered + * assertion on the last attempt will be re-thrown. Errors other than + * assertion errors will be propagated immediately. + *

+ * Assertions attempts will not be closer than 10 millis apart and no + * further than 50 millis. + *

+ * + * @param delay number of millis to delay before the first attempt + * @param duration number of milliseconds beyond the current time + * @param assertions test assertions runnable + */ + public static void assertAfter(int delay, int duration, Runnable assertions) { + checkArgument(delay < duration, "delay >= duration"); + long start = now(); + int step = Math.max(Math.min((duration - delay) / 100, 50), 10); + + // Is there an initial delay? + if (delay > 0) { + delay(delay); + } + + // Keep going until the assertions succeed or until time runs-out. + while (true) { + try { + assertions.run(); + break; + } catch (AssertionError e) { + // If there was an error and time ran out, re-throw it. + if (now() - start > duration) { + throw e; + } + } + delay(step); + } + } + + /** + * Runs the specified runnable until it completes successfully or until the + * specified time expires. If the latter occurs, the first encountered + * assertion on the last attempt will be re-thrown. Errors other than + * assertion errors will be propagated immediately. + *

+ * Assertions attempts will not be closer than 10 millis apart and no + * further than 50 millis. + *

+ * + * @param duration number of milliseconds beyond the current time + * @param assertions test assertions runnable + */ + public static void assertAfter(int duration, Runnable assertions) { + assertAfter(0, duration, assertions); + } + +} diff --git a/utils/junit/src/main/javadoc/org/onlab/junit/package.html b/utils/junit/src/main/javadoc/org/onlab/junit/package.html new file mode 100644 index 0000000000..4e84cb9e8c --- /dev/null +++ b/utils/junit/src/main/javadoc/org/onlab/junit/package.html @@ -0,0 +1,3 @@ + +Utilities to assist in developing JUnit tests. + \ No newline at end of file diff --git a/utils/junit/src/test/java/org/onlab/junit/TestToolsTest.java b/utils/junit/src/test/java/org/onlab/junit/TestToolsTest.java new file mode 100644 index 0000000000..9afc9139ef --- /dev/null +++ b/utils/junit/src/test/java/org/onlab/junit/TestToolsTest.java @@ -0,0 +1,32 @@ +package org.onlab.junit; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.onlab.junit.TestTools.assertAfter; + +public class TestToolsTest { + + @Test + public void testSuccess() { + assertAfter(10, 100, new Runnable() { + int count = 0; + @Override + public void run() { + if (count++ < 3) { + assertTrue(false); + } + } + }); + } + + @Test(expected = AssertionError.class) + public void testFailure() { + assertAfter(100, new Runnable() { + @Override + public void run() { + assertTrue(false); + } + }); + } +} diff --git a/utils/pom.xml b/utils/pom.xml index c956abed80..d9e013c923 100644 --- a/utils/pom.xml +++ b/utils/pom.xml @@ -17,6 +17,7 @@ Domain agnostic ON.Lab utilities + junit misc osgi rest