001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.access.filter; 015 016import static org.junit.Assert.assertEquals; 017 018import org.junit.Test; 019 020import ch.qos.logback.core.util.TimeUtil; 021 022public class StatsByDayTest { 023 024 @Test 025 public void testBasic() { 026 // Tue Nov 21 18:05:36 CET 2006 027 long now = 1164128736369L; 028 StatsByDay statsByDay = new StatsByDay(now); 029 030 int total = 0; 031 // test fresh start 032 statsByDay.update(now, 0); 033 assertEquals(0, statsByDay.getLastCount()); 034 assertEquals(0, statsByDay.getAverage(), 0.01); 035 036 total++; 037 statsByDay.update(now, total); 038 assertEquals(0, statsByDay.getLastCount()); 039 assertEquals(0.0, statsByDay.getAverage(), 0.01); 040 041 long nextDay0 = TimeUtil.computeStartOfNextDay(now); 042 nextDay0 += 99; 043 044 // there should be one event the next day, avg should also be 1 045 statsByDay.update(nextDay0, total); 046 assertEquals(1.0, statsByDay.getLastCount(), 0.01); 047 assertEquals(1.0, statsByDay.getAverage(), 0.01); 048 049 total += 2; 050 051 statsByDay.update(nextDay0, total); 052 assertEquals(1, statsByDay.getLastCount()); 053 assertEquals(1.0, statsByDay.getAverage(), 0.01); 054 055 long nextDay1 = TimeUtil.computeStartOfNextDay(nextDay0) + 6747; 056 statsByDay.update(nextDay1, total); 057 assertEquals(2, statsByDay.getLastCount()); 058 assertEquals(1.5, statsByDay.getAverage(), 0.01); 059 060 nextDay1 += 4444; 061 total += 4; 062 063 statsByDay.update(nextDay1, total); 064 // values should remain unchanged 065 assertEquals(2, statsByDay.getLastCount()); 066 assertEquals(1.5, statsByDay.getAverage(), 0.01); 067 068 long nextDay2 = TimeUtil.computeStartOfNextDay(nextDay1) + 11177; 069 070 statsByDay.update(nextDay2, total); 071 // values should remain unchanged 072 assertEquals(4, statsByDay.getLastCount()); 073 assertEquals(7.0 / 3, statsByDay.getAverage(), 0.01); 074 } 075 076}