001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.spi;
015
016import ch.qos.logback.core.helpers.CyclicBuffer;
017
018import static org.junit.Assert.assertEquals;
019import static org.junit.Assert.assertNotNull;
020
021import org.junit.Test;
022
023
024/**
025 * @author Ceki Gülcü
026 */
027public class CyclicBufferTrackerTest {
028
029    CyclicBufferTracker<Object> tracker = new CyclicBufferTracker<Object>();
030    String key = "a";
031
032    @Test
033    public void empty0() {
034        long now = 3000;
035        tracker.removeStaleComponents(now);
036        assertEquals(0, tracker.liveKeysAsOrderedList().size());
037        assertEquals(0, tracker.getComponentCount());
038    }
039
040    @Test
041    public void empty1() {
042        long now = 3000;
043        assertNotNull(tracker.getOrCreate(key, now++));
044        now += ComponentTracker.DEFAULT_TIMEOUT + 1000;
045        tracker.removeStaleComponents(now);
046        assertEquals(0, tracker.liveKeysAsOrderedList().size());
047        assertEquals(0, tracker.getComponentCount());
048
049        assertNotNull(tracker.getOrCreate(key, now++));
050    }
051
052    @Test
053    public void smoke() {
054        long now = 3000;
055        CyclicBuffer<Object> cb = tracker.getOrCreate(key, now);
056        assertEquals(cb, tracker.getOrCreate(key, now++));
057        now += CyclicBufferTracker.DEFAULT_TIMEOUT + 1000;
058        tracker.removeStaleComponents(now);
059        assertEquals(0, tracker.liveKeysAsOrderedList().size());
060        assertEquals(0, tracker.getComponentCount());
061    }
062
063    @Test
064    public void destroy() {
065        long now = 3000;
066        CyclicBuffer<Object> cb = tracker.getOrCreate(key, now);
067        cb.add(new Object());
068        assertEquals(1, cb.length());
069        tracker.endOfLife(key);
070        now += CyclicBufferTracker.LINGERING_TIMEOUT + 10;
071        tracker.removeStaleComponents(now);
072        assertEquals(0, tracker.liveKeysAsOrderedList().size());
073        assertEquals(0, tracker.getComponentCount());
074        assertEquals(0, cb.length());
075    }
076
077}