Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Removed timeouts from flaky tests, replacing them with mocked clock t…
Browse files Browse the repository at this point in the history
…o check invariants (#172)

Issue #170
  • Loading branch information
galkk authored Aug 29, 2019
1 parent 2ef99b3 commit d13ad57
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,25 @@ public class RollingCounter implements Counter<Long> {
private final LongAdder count;

public RollingCounter() {

this(LocalClusterState.state().getSettingValue(SqlSettings.METRICS_ROLLING_WINDOW),
LocalClusterState.state().getSettingValue(SqlSettings.METRICS_ROLLING_INTERVAL));
}

public RollingCounter(long window, long interval) {
public RollingCounter(long window, long interval, Clock clock) {
this.window = window;
this.interval = interval;
clock = Clock.systemDefaultZone();
this.clock = clock;
time2CountWin = new ConcurrentSkipListMap<>();
count = new LongAdder();
capacity = window / interval * 2;
}

public RollingCounter(long window, long interval) {
this(window, interval, Clock.systemDefaultZone());
}

@Override
public void increment() {

add(1L);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,71 @@

import com.amazon.opendistroforelasticsearch.sql.metrics.RollingCounter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.concurrent.TimeUnit;
import java.time.Clock;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class RollingCounterTest {
@Mock
Clock clock;

@Test
public void increment() throws InterruptedException {
RollingCounter counter = new RollingCounter(3, 1);
public void increment() {
RollingCounter counter = new RollingCounter(3, 1, clock);
for (int i=0; i<5; ++i) {
counter.increment();
}

assertThat(counter.getValue(), equalTo(0L));

TimeUnit.SECONDS.sleep(1L);
when(clock.millis()).thenReturn(1000L); // 1 second passed
assertThat(counter.getValue(), equalTo(5L));

counter.increment();
counter.increment();
TimeUnit.SECONDS.sleep(1L);

when(clock.millis()).thenReturn(2000L); // 1 second passed
assertThat(counter.getValue(), lessThanOrEqualTo(3L));

TimeUnit.SECONDS.sleep(1L);
when(clock.millis()).thenReturn(3000L); // 1 second passed
assertThat(counter.getValue(), equalTo(0L));

}

@Test
public void add() throws InterruptedException {
RollingCounter counter = new RollingCounter(3, 1);
public void add() {
RollingCounter counter = new RollingCounter(3, 1, clock);

counter.add(6);
assertThat(counter.getValue(), equalTo(0L));

TimeUnit.SECONDS.sleep(1L);
when(clock.millis()).thenReturn(1000L); // 1 second passed
assertThat(counter.getValue(), equalTo(6L));

counter.add(4);
TimeUnit.SECONDS.sleep(1L);
when(clock.millis()).thenReturn(2000L); // 1 second passed
assertThat(counter.getValue(), equalTo(4L));

TimeUnit.SECONDS.sleep(1L);
when(clock.millis()).thenReturn(3000L); // 1 second passed
assertThat(counter.getValue(), equalTo(0L));
}

@Test
public void trim() throws InterruptedException {
RollingCounter counter = new RollingCounter(2, 1);
public void trim() {
RollingCounter counter = new RollingCounter(2, 1, clock);

for (int i=1; i<6; ++i) {
counter.increment();
assertThat(counter.size(), equalTo(i));
TimeUnit.SECONDS.sleep(1L);
when(clock.millis()).thenReturn(i * 1000L); // i seconds passed
}
counter.increment();
assertThat(counter.size(), lessThanOrEqualTo(3));
Expand Down

0 comments on commit d13ad57

Please sign in to comment.