1 4 package com.tc.management.stats; 5 6 import java.util.Date ; 7 8 import com.tc.test.TCTestCase; 9 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", 10); 34 populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100, 1000 }); 35 populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100, 1000 }); 36 Thread.sleep(900); 37 assertTrue(failureExplanation, stat.getSampleRate(QUARTER_SECOND) >= 2); 38 assertTrue(failureExplanation, stat.getSampleRate(HALF_SECOND) >= 5); 39 assertTrue(failureExplanation, stat.getSampleRate(SECOND) >= 10); 40 } 41 42 public void testGetName() { 43 assertEquals("Testing statistic", stat.getName()); 44 } 45 46 public void testGetMaximum() { 47 populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100 }); 48 assertEquals(100, stat.getMaximum()); 49 } 50 51 public void testGetMinimum() { 52 populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100 }); 53 assertEquals(-100, stat.getMinimum()); 54 } 55 56 public void testGetN() { 57 populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100 }); 58 assertEquals(9, stat.getN()); 59 } 60 61 public void testGetSum() { 62 populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100 }); 63 assertEquals(0, stat.getSum()); 64 stat.addSample(100); 65 assertEquals(100, stat.getSum()); 66 stat.addSample(1000); 67 assertEquals(1100, stat.getSum()); 68 } 69 70 public void testGetAverage() { 71 populate(new int[] { -100, -50, -10, -1, 0, 1, 10, 50, 100 }); 72 assertEquals(0, (int) stat.getAverage()); 73 stat.addSample(10); 74 assertEquals(1, (int) stat.getAverage()); 75 stat.reset(); 76 assertEquals(0, (int) stat.getAverage()); 77 stat.addSample(10); 78 stat.addSample(20); 79 assertEquals(15, (int) stat.getAverage()); 80 stat.addSample(0); 81 assertEquals(10, (int) stat.getAverage()); 82 } 83 84 private void populate(final int[] samples) { 85 for (int pos = 0; pos < samples.length; ++pos) 86 stat.addSample(samples[pos]); 87 } 88 89 } 90 | Popular Tags |