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.core.spi;
15  
16  import ch.qos.logback.core.helpers.CyclicBuffer;
17  import org.junit.jupiter.api.Test;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  
22  
23  /**
24   * @author Ceki Gülcü
25   */
26  public class CyclicBufferTrackerTest {
27  
28      CyclicBufferTracker<Object> tracker = new CyclicBufferTracker<Object>();
29      String key = "a";
30  
31      @Test
32      public void empty0() {
33          long now = 3000;
34          tracker.removeStaleComponents(now);
35          assertEquals(0, tracker.liveKeysAsOrderedList().size());
36          assertEquals(0, tracker.getComponentCount());
37      }
38  
39      @Test
40      public void empty1() {
41          long now = 3000;
42          assertNotNull(tracker.getOrCreate(key, now++));
43          now += ComponentTracker.DEFAULT_TIMEOUT + 1000;
44          tracker.removeStaleComponents(now);
45          assertEquals(0, tracker.liveKeysAsOrderedList().size());
46          assertEquals(0, tracker.getComponentCount());
47  
48          assertNotNull(tracker.getOrCreate(key, now++));
49      }
50  
51      @Test
52      public void smoke() {
53          long now = 3000;
54          CyclicBuffer<Object> cb = tracker.getOrCreate(key, now);
55          assertEquals(cb, tracker.getOrCreate(key, now++));
56          now += CyclicBufferTracker.DEFAULT_TIMEOUT + 1000;
57          tracker.removeStaleComponents(now);
58          assertEquals(0, tracker.liveKeysAsOrderedList().size());
59          assertEquals(0, tracker.getComponentCount());
60      }
61  
62      @Test
63      public void destroy() {
64          long now = 3000;
65          CyclicBuffer<Object> cb = tracker.getOrCreate(key, now);
66          cb.add(new Object());
67          assertEquals(1, cb.length());
68          tracker.endOfLife(key);
69          now += CyclicBufferTracker.LINGERING_TIMEOUT + 10;
70          tracker.removeStaleComponents(now);
71          assertEquals(0, tracker.liveKeysAsOrderedList().size());
72          assertEquals(0, tracker.getComponentCount());
73          assertEquals(0, cb.length());
74      }
75  
76  }