Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5827 amar.kumar 1
package net.sourceforge.stat4j.calculators;
2
 
3
import net.sourceforge.stat4j.Metric;
4
import net.sourceforge.stat4j.Reading;
5
import junit.framework.TestCase;
6
 
7
import java.util.Properties;
8
 
9
public class RunningRateTest extends TestCase {
10
 
11
    private static class SettableReading extends Reading {
12
        public SettableReading withTimestamp(long ms) {
13
            timestamp = ms;
14
            return this;
15
        }
16
    }
17
 
18
    private RunningRate rate;
19
 
20
    @Override
21
    protected void setUp() throws Exception {
22
        rate = new RunningRate();
23
        Properties properties = new Properties();
24
        properties.setProperty("period", "1000");   // 1 sec
25
        rate.init("RunningRate", properties);
26
    }
27
 
28
 
29
    public void testShouldAccumulateOccurrences() throws Exception {
30
 
31
        Metric metric = new Metric("metric", new Reading());
32
 
33
        for (int i = 0; i < 5; i++) {
34
            rate.applyMetric(metric);
35
        }
36
 
37
        assertEquals(5, (int) rate.getResult());
38
    }
39
 
40
    public void testShouldResetWhenLivingLongerThenItsPeriod() throws Exception {
41
 
42
        long timePeriodPlusOne = rate.starttimestamp + rate.period + 1;
43
 
44
        Reading reading = new SettableReading().withTimestamp(timePeriodPlusOne);
45
        Metric muchYoungerMetric = new Metric("metric", reading);
46
 
47
        // Old period's readings
48
        Metric oldMetric = new Metric("metric", new Reading());
49
        rate.applyMetric(oldMetric);
50
        assertEquals(1, (int) rate.getResult());
51
 
52
        // Force new period by pretending a measure from far future
53
        rate.applyMetric(muchYoungerMetric);
54
        assertEquals("Should have been reset!"
55
                , 1, (int) rate.getResult());
56
    }
57
}