View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.filter;
15  
16  import static org.junit.Assert.assertEquals;
17  
18  import org.junit.Test;
19  
20  import ch.qos.logback.core.util.TimeUtil;
21  
22  public class StatsByDayTest {
23  
24      @Test
25      public void testBasic() {
26          // Tue Nov 21 18:05:36 CET 2006
27          long now = 1164128736369L;
28          StatsByDay statsByDay = new StatsByDay(now);
29  
30          int total = 0;
31          // test fresh start
32          statsByDay.update(now, 0);
33          assertEquals(0, statsByDay.getLastCount());
34          assertEquals(0, statsByDay.getAverage(), 0.01);
35  
36          total++;
37          statsByDay.update(now, total);
38          assertEquals(0, statsByDay.getLastCount());
39          assertEquals(0.0, statsByDay.getAverage(), 0.01);
40  
41          long nextDay0 = TimeUtil.computeStartOfNextDay(now);
42          nextDay0 += 99;
43  
44          // there should be one event the next day, avg should also be 1
45          statsByDay.update(nextDay0, total);
46          assertEquals(1.0, statsByDay.getLastCount(), 0.01);
47          assertEquals(1.0, statsByDay.getAverage(), 0.01);
48  
49          total += 2;
50  
51          statsByDay.update(nextDay0, total);
52          assertEquals(1, statsByDay.getLastCount());
53          assertEquals(1.0, statsByDay.getAverage(), 0.01);
54  
55          long nextDay1 = TimeUtil.computeStartOfNextDay(nextDay0) + 6747;
56          statsByDay.update(nextDay1, total);
57          assertEquals(2, statsByDay.getLastCount());
58          assertEquals(1.5, statsByDay.getAverage(), 0.01);
59  
60          nextDay1 += 4444;
61          total += 4;
62  
63          statsByDay.update(nextDay1, total);
64          // values should remain unchanged
65          assertEquals(2, statsByDay.getLastCount());
66          assertEquals(1.5, statsByDay.getAverage(), 0.01);
67  
68          long nextDay2 = TimeUtil.computeStartOfNextDay(nextDay1) + 11177;
69  
70          statsByDay.update(nextDay2, total);
71          // values should remain unchanged
72          assertEquals(4, statsByDay.getLastCount());
73          assertEquals(7.0 / 3, statsByDay.getAverage(), 0.01);
74      }
75  
76  }