2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowjava.statistics;
10 import org.junit.After;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
18 * General tests for StatisticsCounters class
22 public class StatisticsCountersTest {
24 private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsCounters.class);
25 private StatisticsCounters statCounters;
28 * Initialize StatisticsCounters before each test, reset counters
31 public void initTest(){
32 statCounters = StatisticsCounters.getInstance();
33 statCounters.startCounting(false, 0);
37 * Stop counting after each test
40 public void tierDown(){
41 statCounters.stopCounting();
45 * Basic test of increment and reset counters
48 public void testCounterAll() {
50 for(CounterEventTypes cet : CounterEventTypes.values()){
51 if(statCounters.isCounterEnabled(cet)){
52 incrementCounter(cet,testCount);
53 Assert.assertEquals("Wrong - bad counter value " + cet, testCount, statCounters.getCounter(cet).getCounterValue());
55 Assert.assertNull("Wrong - not enabled counter give not null value", statCounters.getCounter(cet));
59 statCounters.resetCounters();
60 for(CounterEventTypes cet : CounterEventTypes.values()){
61 if(statCounters.isCounterEnabled(cet)){
62 Assert.assertEquals("Wrong - bad counter value after reset " + cet, 0, statCounters.getCounter(cet).getCounterValue());
68 * Test to store current and last read value of counter (at least one counter has to be enabled)
71 public void testCounterLastRead() {
73 CounterEventTypes firstEnabledCET = null;
74 for(CounterEventTypes cet : CounterEventTypes.values()){
75 if(statCounters.isCounterEnabled(cet)){
76 firstEnabledCET = cet;
80 if(firstEnabledCET == null){
81 Assert.fail("No counter is enabled");
83 incrementCounter(firstEnabledCET,testCount);
84 LOGGER.debug("Waiting to process event queue");
85 Assert.assertEquals("Wrong - bad last read value.", 0,statCounters.getCounter(firstEnabledCET).getCounterLastReadValue());
86 Assert.assertEquals("Wrong - bad value", testCount,statCounters.getCounter(firstEnabledCET).getCounterValue(false));
87 Assert.assertEquals("Wrong - bad last read value.", 0,statCounters.getCounter(firstEnabledCET).getCounterLastReadValue());
88 Assert.assertEquals("Wrong - bad last read value.", testCount,statCounters.getCounter(firstEnabledCET).getCounterValue());
89 Assert.assertEquals("Wrong - bad last read value.", testCount,statCounters.getCounter(firstEnabledCET).getCounterLastReadValue());
90 incrementCounter(firstEnabledCET,testCount);
91 Assert.assertEquals("Wrong - bad last read value.", testCount,statCounters.getCounter(firstEnabledCET).getCounterLastReadValue());
92 Assert.assertEquals("Wrong - bad last read value.", 2*testCount,statCounters.getCounter(firstEnabledCET).getCounterValue());
96 * Test start and stop log reporter
99 public void testStartStopLogReporter(){
100 int testDelay = 10000;
101 statCounters.startLogReport(testDelay);
102 Assert.assertTrue("Wrong - logRepoter is not running", statCounters.isRunLogReport());
103 Assert.assertEquals("Wrong - bad logReportPeriod", testDelay, statCounters.getLogReportPeriod());
104 statCounters.stopLogReport();
105 Assert.assertFalse("Wrong - logRepoter is running", statCounters.isRunLogReport());
106 statCounters.startLogReport(StatisticsCounters.MINIMAL_LOG_REPORT_PERIOD / 2);
107 Assert.assertTrue("Wrong - logRepoter is not running", statCounters.isRunLogReport());
108 Assert.assertEquals("Wrong - bad logReportPeriod", StatisticsCounters.MINIMAL_LOG_REPORT_PERIOD, statCounters.getLogReportPeriod());
109 statCounters.stopCounting();
110 Assert.assertFalse("Wrong - logRepoter is running", statCounters.isRunLogReport());
114 * Test start log report with bad logReportDealy
116 @Test(expected = IllegalArgumentException.class)
117 public void testLogReportBadPeriod(){
118 statCounters.startLogReport(0);
122 * Test to get counter with null key
124 @Test(expected = IllegalArgumentException.class)
125 public void testGetCounterbyNull(){
126 statCounters.getCounter(null);
129 private void incrementCounter(CounterEventTypes cet, int count){
130 if(!statCounters.isCounterEnabled(cet)){
133 for(int i = 0; i< count; i++){
134 statCounters.incrementCounter(cet);