Skip to content

Commit

Permalink
Fixing intermittent test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothchandar committed Jan 14, 2013
1 parent 3b6c282 commit 2d12189
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions test/unit/voldemort/store/stats/SimpleCounterTest.java
Expand Up @@ -15,17 +15,18 @@

public class SimpleCounterTest {

final private static int COUNTER_RESET_INTERVAL_MS = 50;
final private static int COUNTER_RESET_INTERVAL_MS = 100;
private SimpleCounter simpleCounter;

@Before
public void setUp() {
simpleCounter = new SimpleCounter(COUNTER_RESET_INTERVAL_MS);
}

private static void sleepForResetInterval() {
private static void sleepForResetInterval(long startTimeMs) {
try {
Thread.sleep(COUNTER_RESET_INTERVAL_MS);
long stopTimeMs = startTimeMs + COUNTER_RESET_INTERVAL_MS;
Thread.sleep(stopTimeMs - System.currentTimeMillis() + 1);
} catch(InterruptedException e) {
e.printStackTrace();
}
Expand All @@ -37,33 +38,35 @@ public void testSingleThread() {
assertEquals(0.0, simpleCounter.getAvgEventValue(), 0.0);
assertEquals(0.0, simpleCounter.getEventRate(), 0.0);

// Interval 1
// add some samples
// Interval 1- add some samples
long startTimeMs = System.currentTimeMillis();
for(int i = 0; i < 10; i++)
simpleCounter.count();
sleepForResetInterval();
sleepForResetInterval(startTimeMs);

// Interval 2
startTimeMs = System.currentTimeMillis();
for(int i = 0; i < 10; i++)
simpleCounter.count(100);
// verify the stats returned are for the first interval
assertEquals(0.0, simpleCounter.getAvgEventValue(), 0.0);
assertEquals(10 / ((COUNTER_RESET_INTERVAL_MS * 1.0) / Time.MS_PER_SECOND),
simpleCounter.getEventRate(),
0.0);
sleepForResetInterval();
sleepForResetInterval(startTimeMs);

// Interval 3
// verify the stats returned are for the second interval and that
// multiple calls during the current interval will always provide the
// same result
startTimeMs = System.currentTimeMillis();
for(int i = 0; i < 10; i++) {
assertEquals(100.0, simpleCounter.getAvgEventValue(), 0.0);
assertEquals(10 / ((COUNTER_RESET_INTERVAL_MS * 1.0) / Time.MS_PER_SECOND),
simpleCounter.getEventRate(),
0.0);
}
sleepForResetInterval();
sleepForResetInterval(startTimeMs);

// No activity
assertEquals(0.0, simpleCounter.getAvgEventValue(), 0.0);
Expand All @@ -76,6 +79,8 @@ public void testMultipleThreads() throws InterruptedException {
try {
final int NUM_THREADS = 5;
final int NUM_OPS = 10000;

long startTimeMs = System.currentTimeMillis();
executorService = Executors.newFixedThreadPool(NUM_THREADS);
final CountDownLatch latch1 = new CountDownLatch(NUM_THREADS);
final CountDownLatch latch0 = new CountDownLatch(1);
Expand All @@ -102,14 +107,15 @@ public void run() {
latch1.await();
// one more sleep so we expire the current interval where all the
// action happened
sleepForResetInterval();
sleepForResetInterval(startTimeMs);

startTimeMs = System.currentTimeMillis();
assertEquals(300.0, simpleCounter.getAvgEventValue(), 0.0);
assertEquals((NUM_OPS * NUM_THREADS)
/ ((COUNTER_RESET_INTERVAL_MS * 1.0) / Time.MS_PER_SECOND),
simpleCounter.getEventRate(),
0.0);
sleepForResetInterval();
sleepForResetInterval(startTimeMs);

// Run for a long period spannning multiple intervals and see if we
// observe if we see consitent metrics
Expand All @@ -118,16 +124,16 @@ public void run() {
final int NUM_INTERVALS = 30;
final CountDownLatch latch2 = new CountDownLatch(NUM_THREADS);
for(int i = 0; i < NUM_THREADS; i++) {
final int threadId = i;
executorService.submit(new Runnable() {

public void run() {
try {
for(int interval = 0; interval < NUM_INTERVALS; interval++) {
sleepForResetInterval();
long startTimeMs = System.currentTimeMillis();
for(int j = 0; j < NUM_OPS; j++) {
simpleCounter.count(100);
}
sleepForResetInterval(startTimeMs);
}
observedEventRate.add(simpleCounter.getEventRate());
observedEventValueAvg.add(simpleCounter.getAvgEventValue());
Expand Down

0 comments on commit 2d12189

Please sign in to comment.