Beispiel #1
0
  @Test
  public void shouldGetMinRateEvenWhenWindowHasMovedTwiceUnderIt() {

    // 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.getMinRate();
    rateWindow.incrementForTimestamp(timestamp4);

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

    // Then
    assertEquals(rate, 0L);
  }
Beispiel #2
0
  @Test
  public void shouldGetMinRateWhenNoRateSet() {

    // Given
    RateWindow rateWindow = createRateWindow(2);

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

    // Then
    assertEquals(rate, 0L);
  }
Beispiel #3
0
  @Test
  public void shouldGetMinRateWhenTimeHasPassedCurrentIndex() {

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

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

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

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

    // Then
    assertEquals(rate, 1L);
  }
Beispiel #4
0
  @Test
  public void shouldGetMinRateWhenWindowMoves() {

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

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

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

    // Then
    assertEquals(rate, 2L);
  }