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.classic.turbo;
15  
16  import ch.qos.logback.core.spi.FilterReply;
17  import org.junit.jupiter.api.Test;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  public class DuplicateMessageFilterTest {
22  
23      @Test
24      public void smoke() {
25          DuplicateMessageFilter dmf = new DuplicateMessageFilter();
26          dmf.setAllowedRepetitions(0);
27          dmf.start();
28          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "x", null, null));
29          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "y", null, null));
30          assertEquals(FilterReply.DENY, dmf.decide(null, null, null, "x", null, null));
31          assertEquals(FilterReply.DENY, dmf.decide(null, null, null, "y", null, null));
32      }
33  
34      @Test
35      public void memoryLoss() {
36          DuplicateMessageFilter dmf = new DuplicateMessageFilter();
37          dmf.setAllowedRepetitions(1);
38          dmf.setCacheSize(1);
39          dmf.start();
40          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "a", null, null));
41          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "b", null, null));
42          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "a", null, null));
43      }
44  
45      @Test
46      public void many() {
47          DuplicateMessageFilter dmf = new DuplicateMessageFilter();
48          dmf.setAllowedRepetitions(0);
49          int cacheSize = 10;
50          int margin = 2;
51          dmf.setCacheSize(cacheSize);
52          dmf.start();
53          for (int i = 0; i < cacheSize + margin; i++) {
54              assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "a" + i, null, null));
55          }
56          for (int i = cacheSize - 1; i >= margin; i--) {
57              assertEquals(FilterReply.DENY, dmf.decide(null, null, null, "a" + i, null, null));
58          }
59          for (int i = margin - 1; i >= 0; i--) {
60              assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, "a" + i, null, null));
61          }
62      }
63  
64      @Test
65      // isXXXEnabled invokes decide with a null format
66      // http://jira.qos.ch/browse/LBCLASSIC-134
67      public void nullFormat() {
68          DuplicateMessageFilter dmf = new DuplicateMessageFilter();
69          dmf.setAllowedRepetitions(0);
70          dmf.setCacheSize(10);
71          dmf.start();
72          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, null, null, null));
73          assertEquals(FilterReply.NEUTRAL, dmf.decide(null, null, null, null, null, null));
74      }
75  
76  }