1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.chukwa.datacollection;
19
20 import junit.framework.TestCase;
21
22
23
24
25
26 public class TestOffsetStatsManager extends TestCase {
27
28 org.apache.hadoop.chukwa.datacollection.OffsetStatsManager<DummyKey> statsManager = null;
29 DummyKey dummyKey = new DummyKey();
30
31 protected void setUp() throws Exception {
32 statsManager = new OffsetStatsManager<DummyKey>();
33 dummyKey = new DummyKey();
34 }
35
36 public void testCalcAverageRate() throws InterruptedException {
37
38
39 for (int i = 0; i < 5; i++) {
40 statsManager.addOffsetDataPoint(dummyKey, 1000 * i, System.currentTimeMillis());
41 Thread.sleep(1000);
42 }
43
44
45 double rate = statsManager.calcAverageRate(dummyKey, 5);
46 assertTrue("Invalid average, expected about 1 byte/sec, found " + rate,
47 Math.abs(1000 - rate) < 1.5);
48 }
49
50 public void testCalcAverageRateStaleData() throws InterruptedException {
51
52
53
54 for (int i = 0; i < 5; i++) {
55 statsManager.addOffsetDataPoint(dummyKey, 1000 * i, System.currentTimeMillis() - 3000L);
56 Thread.sleep(1000);
57 }
58
59
60 double rate = statsManager.calcAverageRate(dummyKey, 5);
61 assertEquals("Should have gotten a stale data response", -1.0, rate);
62 }
63
64 public void testCalcAverageRateNotEnoughData() throws InterruptedException {
65
66
67 for (int i = 0; i < 3; i++) {
68 statsManager.addOffsetDataPoint(dummyKey, 1000 * i, System.currentTimeMillis());
69 Thread.sleep(1000);
70 }
71
72
73 double rate = statsManager.calcAverageRate(dummyKey, 5);
74 assertEquals("Should have gotten a stale data response", -1.0, rate);
75 }
76
77 private static class DummyKey {}
78 }