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