storm-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ptgo...@apache.org
Subject [40/50] [abbrv] merge storm-starter into examples
Date Thu, 20 Mar 2014 21:23:03 GMT
http://git-wip-us.apache.org/repos/asf/incubator-storm/blob/f1d7fca7/examples/storm-starter/test/jvm/storm/starter/tools/SlidingWindowCounterTest.java
----------------------------------------------------------------------
diff --cc examples/storm-starter/test/jvm/storm/starter/tools/SlidingWindowCounterTest.java
index 0000000,0000000..1c883e5
new file mode 100644
--- /dev/null
+++ b/examples/storm-starter/test/jvm/storm/starter/tools/SlidingWindowCounterTest.java
@@@ -1,0 -1,0 +1,89 @@@
++package storm.starter.tools;
++
++import org.testng.annotations.DataProvider;
++import org.testng.annotations.Test;
++
++import java.util.Map;
++
++import static org.fest.assertions.api.Assertions.assertThat;
++
++public class SlidingWindowCounterTest {
++
++  private static final int ANY_WINDOW_LENGTH_IN_SLOTS = 2;
++  private static final Object ANY_OBJECT = "ANY_OBJECT";
++
++  @DataProvider
++  public Object[][] illegalWindowLengths() {
++    return new Object[][]{ { -10 }, { -3 }, { -2 }, { -1 }, { 0 }, { 1 } };
++  }
++
++  @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "illegalWindowLengths")
++  public void lessThanTwoSlotsShouldThrowIAE(int windowLengthInSlots) {
++    new SlidingWindowCounter<Object>(windowLengthInSlots);
++  }
++
++  @DataProvider
++  public Object[][] legalWindowLengths() {
++    return new Object[][]{ { 2 }, { 3 }, { 20 } };
++  }
++
++  @Test(dataProvider = "legalWindowLengths")
++  public void twoOrMoreSlotsShouldBeValid(int windowLengthInSlots) {
++    new SlidingWindowCounter<Object>(windowLengthInSlots);
++  }
++
++  @Test
++  public void newInstanceShouldHaveEmptyCounts() {
++    // given
++    SlidingWindowCounter<Object> counter = new SlidingWindowCounter<Object>(ANY_WINDOW_LENGTH_IN_SLOTS);
++
++    // when
++    Map<Object, Long> counts = counter.getCountsThenAdvanceWindow();
++
++    // then
++    assertThat(counts).isEmpty();
++  }
++
++  @DataProvider
++  public Object[][] simulatedCounterIterations() {
++    return new Object[][]{ { 2, new int[]{ 3, 2, 0, 0, 1, 0, 0, 0 }, new long[]{ 3, 5, 2,
0, 1, 1, 0, 0 } },
++        { 3, new int[]{ 3, 2, 0, 0, 1, 0, 0, 0 }, new long[]{ 3, 5, 5, 2, 1, 1, 1, 0 } },
++        { 4, new int[]{ 3, 2, 0, 0, 1, 0, 0, 0 }, new long[]{ 3, 5, 5, 5, 3, 1, 1, 1 } },
++        { 5, new int[]{ 3, 2, 0, 0, 1, 0, 0, 0 }, new long[]{ 3, 5, 5, 5, 6, 3, 1, 1 } },
++        { 5, new int[]{ 3, 11, 5, 13, 7, 17, 0, 3, 50, 600, 7000 },
++            new long[]{ 3, 14, 19, 32, 39, 53, 42, 40, 77, 670, 7653 } }, };
++  }
++
++  @Test(dataProvider = "simulatedCounterIterations")
++  public void testCounterWithSimulatedRuns(int windowLengthInSlots, int[] incrementsPerIteration,
++      long[] expCountsPerIteration) {
++    // given
++    SlidingWindowCounter<Object> counter = new SlidingWindowCounter<Object>(windowLengthInSlots);
++    int numIterations = incrementsPerIteration.length;
++
++    for (int i = 0; i < numIterations; i++) {
++      int numIncrements = incrementsPerIteration[i];
++      long expCounts = expCountsPerIteration[i];
++      // Objects are absent if they were zero both this iteration
++      // and the last -- if only this one, we need to report zero.
++      boolean expAbsent = ((expCounts == 0) && ((i == 0) || (expCountsPerIteration[i
- 1] == 0)));
++
++      // given (for this iteration)
++      for (int j = 0; j < numIncrements; j++) {
++        counter.incrementCount(ANY_OBJECT);
++      }
++
++      // when (for this iteration)
++      Map<Object, Long> counts = counter.getCountsThenAdvanceWindow();
++
++      // then (for this iteration)
++      if (expAbsent) {
++        assertThat(counts).doesNotContainKey(ANY_OBJECT);
++      }
++      else {
++        assertThat(counts.get(ANY_OBJECT)).isEqualTo(expCounts);
++      }
++    }
++  }
++
++}

http://git-wip-us.apache.org/repos/asf/incubator-storm/blob/f1d7fca7/examples/storm-starter/test/jvm/storm/starter/tools/SlotBasedCounterTest.java
----------------------------------------------------------------------
diff --cc examples/storm-starter/test/jvm/storm/starter/tools/SlotBasedCounterTest.java
index 0000000,0000000..aeeec63
new file mode 100644
--- /dev/null
+++ b/examples/storm-starter/test/jvm/storm/starter/tools/SlotBasedCounterTest.java
@@@ -1,0 -1,0 +1,164 @@@
++package storm.starter.tools;
++
++import org.testng.annotations.DataProvider;
++import org.testng.annotations.Test;
++
++import java.util.Map;
++
++import static org.fest.assertions.api.Assertions.assertThat;
++
++public class SlotBasedCounterTest {
++
++  private static final int ANY_NUM_SLOTS = 1;
++  private static final int ANY_SLOT = 0;
++  private static final Object ANY_OBJECT = "ANY_OBJECT";
++
++  @DataProvider
++  public Object[][] illegalNumSlotsData() {
++    return new Object[][]{ { -10 }, { -3 }, { -2 }, { -1 }, { 0 } };
++  }
++
++  @Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "illegalNumSlotsData")
++  public void negativeOrZeroNumSlotsShouldThrowIAE(int numSlots) {
++    new SlotBasedCounter<Object>(numSlots);
++  }
++
++  @DataProvider
++  public Object[][] legalNumSlotsData() {
++    return new Object[][]{ { 1 }, { 2 }, { 3 }, { 20 } };
++  }
++
++  @Test(dataProvider = "legalNumSlotsData")
++  public void positiveNumSlotsShouldBeOk(int numSlots) {
++    new SlotBasedCounter<Object>(numSlots);
++  }
++
++  @Test
++  public void newInstanceShouldHaveEmptyCounts() {
++    // given
++    SlotBasedCounter<Object> counter = new SlotBasedCounter<Object>(ANY_NUM_SLOTS);
++
++    // when
++    Map<Object, Long> counts = counter.getCounts();
++
++    // then
++    assertThat(counts).isEmpty();
++  }
++
++  @Test
++  public void shouldReturnNonEmptyCountsWhenAtLeastOneObjectWasCounted() {
++    // given
++    SlotBasedCounter<Object> counter = new SlotBasedCounter<Object>(ANY_NUM_SLOTS);
++    counter.incrementCount(ANY_OBJECT, ANY_SLOT);
++
++    // when
++    Map<Object, Long> counts = counter.getCounts();
++
++    // then
++    assertThat(counts).isNotEmpty();
++
++    // additional tests that go beyond what this test is primarily about
++    assertThat(counts.size()).isEqualTo(1);
++    assertThat(counts.get(ANY_OBJECT)).isEqualTo(1);
++  }
++
++  @DataProvider
++  public Object[][] incrementCountData() {
++    return new Object[][]{ { new String[]{ "foo", "bar" }, new int[]{ 3, 2 } } };
++  }
++
++  @Test(dataProvider = "incrementCountData")
++  public void shouldIncrementCount(Object[] objects, int[] expCounts) {
++    // given
++    SlotBasedCounter<Object> counter = new SlotBasedCounter<Object>(ANY_NUM_SLOTS);
++
++    // when
++    for (int i = 0; i < objects.length; i++) {
++      Object obj = objects[i];
++      int numIncrements = expCounts[i];
++      for (int j = 0; j < numIncrements; j++) {
++        counter.incrementCount(obj, ANY_SLOT);
++      }
++    }
++
++    // then
++    for (int i = 0; i < objects.length; i++) {
++      assertThat(counter.getCount(objects[i], ANY_SLOT)).isEqualTo(expCounts[i]);
++    }
++    assertThat(counter.getCount("nonexistentObject", ANY_SLOT)).isEqualTo(0);
++  }
++
++  @Test
++  public void shouldReturnZeroForNonexistentObject() {
++    // given
++    SlotBasedCounter<Object> counter = new SlotBasedCounter<Object>(ANY_NUM_SLOTS);
++
++    // when
++    counter.incrementCount("somethingElse", ANY_SLOT);
++
++    // then
++    assertThat(counter.getCount("nonexistentObject", ANY_SLOT)).isEqualTo(0);
++  }
++
++  @Test
++  public void shouldIncrementCountOnlyOneSlotAtATime() {
++    // given
++    int numSlots = 3;
++    Object obj = Long.valueOf(10);
++    SlotBasedCounter<Object> counter = new SlotBasedCounter<Object>(numSlots);
++
++    // when (empty)
++    // then
++    assertThat(counter.getCount(obj, 0)).isEqualTo(0);
++    assertThat(counter.getCount(obj, 1)).isEqualTo(0);
++    assertThat(counter.getCount(obj, 2)).isEqualTo(0);
++
++    // when
++    counter.incrementCount(obj, 1);
++
++    // then
++    assertThat(counter.getCount(obj, 0)).isEqualTo(0);
++    assertThat(counter.getCount(obj, 1)).isEqualTo(1);
++    assertThat(counter.getCount(obj, 2)).isEqualTo(0);
++  }
++
++  @Test
++  public void wipeSlotShouldSetAllCountsInSlotToZero() {
++    // given
++    SlotBasedCounter<Object> counter = new SlotBasedCounter<Object>(ANY_NUM_SLOTS);
++    Object countWasOne = "countWasOne";
++    Object countWasThree = "countWasThree";
++    counter.incrementCount(countWasOne, ANY_SLOT);
++    counter.incrementCount(countWasThree, ANY_SLOT);
++    counter.incrementCount(countWasThree, ANY_SLOT);
++    counter.incrementCount(countWasThree, ANY_SLOT);
++
++    // when
++    counter.wipeSlot(ANY_SLOT);
++
++    // then
++    assertThat(counter.getCount(countWasOne, ANY_SLOT)).isEqualTo(0);
++    assertThat(counter.getCount(countWasThree, ANY_SLOT)).isEqualTo(0);
++  }
++
++  @Test
++  public void wipeZerosShouldRemoveAnyObjectsWithZeroTotalCount() {
++    // given
++    SlotBasedCounter<Object> counter = new SlotBasedCounter<Object>(2);
++    int wipeSlot = 0;
++    int otherSlot = 1;
++    Object willBeRemoved = "willBeRemoved";
++    Object willContinueToBeTracked = "willContinueToBeTracked";
++    counter.incrementCount(willBeRemoved, wipeSlot);
++    counter.incrementCount(willContinueToBeTracked, wipeSlot);
++    counter.incrementCount(willContinueToBeTracked, otherSlot);
++
++    // when
++    counter.wipeSlot(wipeSlot);
++    counter.wipeZeros();
++
++    // then
++    assertThat(counter.getCounts()).doesNotContainKey(willBeRemoved);
++    assertThat(counter.getCounts()).containsKey(willContinueToBeTracked);
++  }
++}


Mime
View raw message