01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.management.stats;
05:
06: import java.util.Date;
07:
08: import com.tc.test.TCTestCase;
09:
10: public class AggregateIntegerTest extends TCTestCase {
11:
12: private AggregateInteger stat;
13:
14: public AggregateIntegerTest() {
15: disableTestUntil("testGetSampleRate", new Date(Long.MAX_VALUE));
16: }
17:
18: protected void setUp() throws Exception {
19: stat = new AggregateInteger("Testing statistic");
20: super .setUp();
21: }
22:
23: protected void tearDown() throws Exception {
24: stat = null;
25: super .tearDown();
26: }
27:
28: public void testGetSampleRate() throws InterruptedException {
29: final String failureExplanation = "Since this is a timing test it is likely that it might fail from time to time on a loaded machine";
30: final long SECOND = 1000;
31: final long HALF_SECOND = SECOND / 2;
32: final long QUARTER_SECOND = SECOND / 4;
33: stat = new AggregateInteger("Testing statistic with history",
34: 10);
35: populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100,
36: 1000 });
37: populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100,
38: 1000 });
39: Thread.sleep(900);
40: assertTrue(failureExplanation, stat
41: .getSampleRate(QUARTER_SECOND) >= 2);
42: assertTrue(failureExplanation,
43: stat.getSampleRate(HALF_SECOND) >= 5);
44: assertTrue(failureExplanation, stat.getSampleRate(SECOND) >= 10);
45: }
46:
47: public void testGetName() {
48: assertEquals("Testing statistic", stat.getName());
49: }
50:
51: public void testGetMaximum() {
52: populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100 });
53: assertEquals(100, stat.getMaximum());
54: }
55:
56: public void testGetMinimum() {
57: populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100 });
58: assertEquals(-100, stat.getMinimum());
59: }
60:
61: public void testGetN() {
62: populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100 });
63: assertEquals(9, stat.getN());
64: }
65:
66: public void testGetSum() {
67: populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100 });
68: assertEquals(0, stat.getSum());
69: stat.addSample(100);
70: assertEquals(100, stat.getSum());
71: stat.addSample(1000);
72: assertEquals(1100, stat.getSum());
73: }
74:
75: public void testGetAverage() {
76: populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100 });
77: assertEquals(0, (int) stat.getAverage());
78: stat.addSample(10);
79: assertEquals(1, (int) stat.getAverage());
80: stat.reset();
81: assertEquals(0, (int) stat.getAverage());
82: stat.addSample(10);
83: stat.addSample(20);
84: assertEquals(15, (int) stat.getAverage());
85: stat.addSample(0);
86: assertEquals(10, (int) stat.getAverage());
87: }
88:
89: private void populate(final int[] samples) {
90: for (int pos = 0; pos < samples.length; ++pos)
91: stat.addSample(samples[pos]);
92: }
93:
94: }
|