1   package ch.qos.logback.access.filter;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import org.junit.Test;
6   
7   import ch.qos.logback.core.util.TimeUtil;
8   
9   public class StatsByDayTest {
10  
11    @Test
12    public void testBasic() {
13      // Tue Nov 21 18:05:36 CET 2006
14      long now = 1164128736369L;
15      StatsByDay statsByDay = new StatsByDay(now);
16  
17      int total = 0;
18      // test fresh start
19      statsByDay.update(now, 0);
20      assertEquals(0, statsByDay.getLastCount());
21      assertEquals(0, statsByDay.getAverage(), 0.01);
22  
23      total++;
24      statsByDay.update(now, total);
25      assertEquals(0, statsByDay.getLastCount());
26      assertEquals(0.0, statsByDay.getAverage(), 0.01);
27  
28      long nextDay0 = TimeUtil.computeStartOfNextDay(now);
29      nextDay0 += 99;
30  
31      // there should be one event the next day, avg should also be 1
32      statsByDay.update(nextDay0, total);
33      assertEquals(1.0, statsByDay.getLastCount(), 0.01);
34      assertEquals(1.0, statsByDay.getAverage(), 0.01);
35  
36      total += 2;
37  
38      statsByDay.update(nextDay0, total);
39      assertEquals(1, statsByDay.getLastCount());
40      assertEquals(1.0, statsByDay.getAverage(), 0.01);
41  
42      long nextDay1 = TimeUtil.computeStartOfNextDay(nextDay0) + 6747;
43      statsByDay.update(nextDay1, total);
44      assertEquals(2, statsByDay.getLastCount());
45      assertEquals(1.5, statsByDay.getAverage(), 0.01);
46  
47      nextDay1 += 4444;
48      total += 4;
49  
50      statsByDay.update(nextDay1, total);
51      // values should remain unchanged
52      assertEquals(2, statsByDay.getLastCount());
53      assertEquals(1.5, statsByDay.getAverage(), 0.01);
54  
55      long nextDay2 = TimeUtil.computeStartOfNextDay(nextDay1) + 11177;
56  
57      statsByDay.update(nextDay2, total);
58      // values should remain unchanged
59      assertEquals(4, statsByDay.getLastCount());
60      assertEquals(7.0 / 3, statsByDay.getAverage(), 0.01);
61    }
62  
63  }