示例#1
0
  @Test
  public void shouldGetMaxRateWhenNoRateSet() {

    // Given
    RateWindow rateWindow = createRateWindow(2);

    // When
    long rate = rateWindow.getMaxRate();

    // Then
    assertEquals(rate, 0L);
  }
示例#2
0
  @Test
  public void shouldAddRateToFirstWindowSlot() {

    // Given
    RateWindow rateWindow = createRateWindow(1);
    long timestamp = getNowTimestamp(SAMPLE_RATE);

    // When
    rateWindow.incrementForTimestamp(timestamp);

    // Then
    assertEquals(rateWindow.getAverageRate(), 1D);
  }
示例#3
0
  @Test
  public void shouldGetMaxRateEvenWhenWindowHasMovedTwiceUnderIt() {

    // Given
    RateWindow rateWindow = createRateWindow(4);
    long timestamp1 = getNowTimestamp(SAMPLE_RATE);
    long timestamp2 = timestamp1 + SAMPLE_RATE;
    long timestamp3 = timestamp2 + SAMPLE_RATE;
    long timestamp4 = timestamp3 + (SAMPLE_RATE * 2);

    given(timer.now()).willReturn(timestamp3 + 1);

    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp2);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.getMaxRate();
    rateWindow.incrementForTimestamp(timestamp4);

    // When
    long rate = rateWindow.getMaxRate();

    // Then
    assertEquals(rate, 3L);
  }
示例#4
0
  @Test
  public void shouldGetMaxRateWhenTimeHasPassedCurrentIndex() {

    // Given
    RateWindow rateWindow = createRateWindow(3);
    long timestamp1 = getNowTimestamp(SAMPLE_RATE);
    long timestamp2 = timestamp1 + SAMPLE_RATE;
    long timestamp3 = timestamp2 + SAMPLE_RATE;

    given(timer.now()).willReturn(timestamp3 + SAMPLE_RATE - 1);

    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp2);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.incrementForTimestamp(timestamp3);

    // When
    long rate = rateWindow.getMaxRate();

    // Then
    assertEquals(rate, 6L);
  }
示例#5
0
  @Test
  public void shouldGetMaxRate() {

    // Given
    RateWindow rateWindow = createRateWindow(3);
    long timestamp1 = getNowTimestamp(SAMPLE_RATE);
    long timestamp2 = timestamp1 + SAMPLE_RATE;
    long timestamp3 = timestamp2 + SAMPLE_RATE;

    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp2);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.incrementForTimestamp(timestamp3);
    rateWindow.incrementForTimestamp(timestamp3);

    // When
    long rate = rateWindow.getMaxRate();

    // Then
    assertEquals(rate, 6L);
  }
示例#6
0
  @Test
  public void shouldAddRatesToSameWindowSlotWithSameTimestamp() {

    // Given
    RateWindow rateWindow = createRateWindow(1);
    long timestamp1 = getNowTimestamp(SAMPLE_RATE);

    rateWindow.incrementForTimestamp(timestamp1);

    // When
    rateWindow.incrementForTimestamp(timestamp1);

    // Then
    assertEquals(rateWindow.getAverageRate(), 2D);
  }
示例#7
0
  @Test
  public void shouldAddRatesToDifferentWindowSlotsWithAnEmptySlotBetween() {

    // Given
    RateWindow rateWindow = createRateWindow(2);
    long timestamp1 = getNowTimestamp(SAMPLE_RATE);
    long timestamp2 = timestamp1 + (SAMPLE_RATE * 2);

    rateWindow.incrementForTimestamp(timestamp1);

    // When
    rateWindow.incrementForTimestamp(timestamp2);

    // Then
    assertEquals(rateWindow.getAverageRate(), 0.5);
  }
示例#8
0
  @Test
  public void shouldAddRatesToDifferentWindowSlots() {

    // Given
    RateWindow rateWindow = createRateWindow(1);
    long timestamp1 = getNowTimestamp(SAMPLE_RATE);
    long timestamp2 = timestamp1 + SAMPLE_RATE;

    rateWindow.incrementForTimestamp(timestamp1);

    // When
    rateWindow.incrementForTimestamp(timestamp2);

    // Then
    assertEquals(rateWindow.getAverageRate(), 1D);
  }
示例#9
0
  @Test
  public void shouldUpdatePreviousRate() {

    // Given
    RateWindow rateWindow = createRateWindow(4);
    long timestamp1 = getNowTimestamp(SAMPLE_RATE);
    long timestamp2 = timestamp1 + SAMPLE_RATE;

    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp2);

    // When
    rateWindow.incrementForTimestamp(timestamp1);

    // Then
    assertEquals(rateWindow.getMaxRate(), 2L);
  }
示例#10
0
  @Test
  public void shouldNotUpdatePastRateIfBeforeWindowStart() {

    // Given
    RateWindow rateWindow = createRateWindow(1);
    long timestamp1 = getNowTimestamp(SAMPLE_RATE);
    long timestamp2 = timestamp1 - SAMPLE_RATE;

    given(timer.now()).willReturn(timestamp1 + SAMPLE_RATE);

    rateWindow.incrementForTimestamp(timestamp1);

    // When
    rateWindow.incrementForTimestamp(timestamp2);

    // Then
    assertEquals(rateWindow.getMaxRate(), 0L);
  }
示例#11
0
  @Test
  public void shouldGetAverageRateWhenTimeIsJustInLatestIndex() {

    // Given
    RateWindow rateWindow = createRateWindow(2);
    long timestamp1 = getNowTimestamp(SAMPLE_RATE);
    long timestamp2 = timestamp1 + SAMPLE_RATE;

    given(timer.now()).willReturn(timestamp2 + SAMPLE_RATE - 1);

    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp2);

    // When
    double rate = rateWindow.getAverageRate();

    // Then
    assertEquals(rate, 0.5D);
  }
示例#12
0
  @Test
  public void windowShouldBeExtendedWhenCalculatingMaxRate() {
    RateWindow rateWindow = createRateWindow(2);
    long timestamp1 = getNowTimestamp(SAMPLE_RATE);
    long timestamp2 = timestamp1 + SAMPLE_RATE;
    long timestamp3 = timestamp2 + SAMPLE_RATE;
    long timestamp4 = timestamp3 + (SAMPLE_RATE * 2);

    given(timer.now()).willReturn(timestamp1);
    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp1);
    rateWindow.incrementForTimestamp(timestamp1);

    given(timer.now()).willReturn(timestamp2);
    rateWindow.incrementForTimestamp(timestamp2);
    assertThat(rateWindow.getMaxRate()).isEqualTo(3);

    given(timer.now()).willReturn(timestamp3);
    assertThat(rateWindow.getMaxRate()).isEqualTo(1);

    given(timer.now()).willReturn(timestamp4);
    assertThat(rateWindow.getMaxRate()).isEqualTo(0);
  }