Ejemplo n.º 1
0
  @Test
  public void shouldAddRateToFirstWindowSlot() {

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

    // When
    rateWindow.incrementForTimestamp(timestamp);

    // Then
    assertEquals(rateWindow.getAverageRate(), 1D);
  }
Ejemplo n.º 2
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);
  }
Ejemplo n.º 3
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);
  }
Ejemplo n.º 4
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);
  }
Ejemplo n.º 5
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);
  }