Skip to content

Commit

Permalink
VOLDEMORT-313, cont. Update StatsTest.statsExpireOnTime() to use mocked
Browse files Browse the repository at this point in the history
time rather than sleeping.
  • Loading branch information
jghoman committed Jan 7, 2011
1 parent 550e432 commit 83e2957
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
23 changes: 17 additions & 6 deletions src/java/voldemort/store/stats/RequestCounter.java
Expand Up @@ -2,6 +2,7 @@

import java.util.concurrent.atomic.AtomicReference;

import voldemort.utils.SystemTime;
import voldemort.utils.Time;

/**
Expand All @@ -14,12 +15,21 @@ public class RequestCounter {

private final AtomicReference<Accumulator> values;
private final int durationMS;
private final Time time;

/**
* @param durationMS specifies for how long you want to maintain this
* counter (in milliseconds).
*/
public RequestCounter(int durationMS) {
this(durationMS, SystemTime.INSTANCE);
}

/**
* For testing request expiration via an injected time provider
*/
RequestCounter(int durationMS, Time time) {
this.time = time;
this.values = new AtomicReference<Accumulator>(new Accumulator());
this.durationMS = durationMS;
}
Expand All @@ -34,7 +44,7 @@ public long getTotalCount() {

public float getThroughput() {
Accumulator oldv = getValidAccumulator();
double elapsed = (System.currentTimeMillis() - oldv.startTimeMS)
double elapsed = (time.getMilliseconds() - oldv.startTimeMS)
/ (double) Time.MS_PER_SECOND;
if(elapsed > 0f) {
return (float) (oldv.count / elapsed);
Expand Down Expand Up @@ -66,7 +76,7 @@ public long getMaxLatencyInMs() {
private Accumulator getValidAccumulator() {

Accumulator accum = values.get();
long now = System.currentTimeMillis();
long now = time.getMilliseconds();

/*
* if still in the window, just return it
Expand Down Expand Up @@ -112,7 +122,8 @@ public void addRequest(long timeNS) {
public void addRequest(long timeNS, long numEmptyResponses, long bytes, long getAllAggregatedCount) {
for(int i = 0; i < 3; i++) {
Accumulator oldv = getValidAccumulator();
Accumulator newv = new Accumulator(oldv.startTimeMS, oldv.count + 1,
Accumulator newv = new Accumulator(oldv.startTimeMS,
oldv.count + 1,
oldv.totalTimeNS + timeNS,
oldv.total + 1,
oldv.numEmptyResponses + numEmptyResponses,
Expand Down Expand Up @@ -153,7 +164,7 @@ public long getGetAllAggregatedCount() {
return getValidAccumulator().getAllAggregatedCount;
}

private static class Accumulator {
private class Accumulator {

final long startTimeMS;
final long count;
Expand All @@ -166,11 +177,11 @@ private static class Accumulator {
final long totalBytes; // Sum of all the values

public Accumulator() {
this(System.currentTimeMillis(), 0, 0, 0, 0, 0, 0, 0, 0);
this(RequestCounter.this.time.getMilliseconds(), 0, 0, 0, 0, 0, 0, 0, 0);
}

public Accumulator newWithTotal() {
return new Accumulator(System.currentTimeMillis(), 0, 0, total, 0, 0, 0, 0, 0);
return new Accumulator(RequestCounter.this.time.getMilliseconds(), 0, 0, total, 0, 0, 0, 0, 0);
}

public Accumulator(long startTimeMS, long count, long totalTimeNS, long total, long numEmptyResponses, long maxLatencyNS, long totalBytes, long maxBytes, long getAllAggregatedCount) {
Expand Down
19 changes: 14 additions & 5 deletions test/unit/voldemort/store/stats/StatsTest.java
@@ -1,9 +1,12 @@
package voldemort.store.stats;

import org.junit.Test;
import voldemort.utils.Time;

import static voldemort.utils.Time.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static voldemort.utils.Time.NS_PER_MS;


public class StatsTest {
Expand Down Expand Up @@ -62,8 +65,14 @@ public void maxValueIsAccurate() {

@Test
public void statsExpireOnTime() throws InterruptedException {
// Create a quickly expring counter
RequestCounter rc = new RequestCounter(1000);
final long startTime = 1445468640; // Oct 21, 2015
final int delay = 1000;
Time mockTime = mock(Time.class);

when(mockTime.getMilliseconds()).thenReturn(startTime);

RequestCounter rc = new RequestCounter(delay, mockTime);

// Add some new stats and verify they were calculated correctly
rc.addRequest(100 * NS_PER_MS, 1, 200, 1);
rc.addRequest(50 * NS_PER_MS, 0, 1000, 2);
Expand All @@ -73,8 +82,8 @@ public void statsExpireOnTime() throws InterruptedException {
assertEquals(1000, rc.getMaxSizeInBytes());
assertEquals(3, rc.getGetAllAggregatedCount());

// Sleep to let the counter expire
Thread.sleep(1500);
// Jump into the future after the counter should have expired
when(mockTime.getMilliseconds()).thenReturn(startTime + delay + 1);

// Now verify that the counter has aged out the previous values
assertEquals(0, rc.getNumEmptyResponses());
Expand Down

0 comments on commit 83e2957

Please sign in to comment.