1
2
3
4
5
6
7
8
9
10
11
12
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
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 }