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.classic.issue.logback416;
015
016import ch.qos.logback.classic.ClassicTestConstants;
017import ch.qos.logback.classic.Logger;
018import ch.qos.logback.classic.LoggerContext;
019import ch.qos.logback.classic.issue.lbclassic135.LoggingRunnable;
020import ch.qos.logback.classic.issue.logback416.InstanceCountingAppender;
021import ch.qos.logback.classic.joran.JoranConfigurator;
022import ch.qos.logback.core.contention.MultiThreadedHarness;
023import ch.qos.logback.core.contention.RunnableWithCounterAndDone;
024import ch.qos.logback.core.joran.spi.JoranException;
025
026import org.junit.Test;
027
028import static org.junit.Assert.assertEquals;
029
030public class ConcurrentSiftingTest {
031    final static int THREAD_COUNT = 5;
032    static String FOLDER_PREFIX = ClassicTestConstants.JORAN_INPUT_PREFIX + "sift/";
033
034    LoggerContext loggerContext = new LoggerContext();
035    protected Logger logger = loggerContext.getLogger(this.getClass().getName());
036    protected Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
037
038    int totalTestDuration = 50;
039    MultiThreadedHarness harness = new MultiThreadedHarness(totalTestDuration);
040    RunnableWithCounterAndDone[] runnableArray = buildRunnableArray();
041
042    protected void configure(String file) throws JoranException {
043        JoranConfigurator jc = new JoranConfigurator();
044        jc.setContext(loggerContext);
045        jc.doConfigure(file);
046    }
047
048    RunnableWithCounterAndDone[] buildRunnableArray() {
049        RunnableWithCounterAndDone[] rArray = new RunnableWithCounterAndDone[THREAD_COUNT];
050        for (int i = 0; i < THREAD_COUNT; i++) {
051            rArray[i] = new LoggingRunnable(logger);
052        }
053        return rArray;
054    }
055
056    @Test
057    public void concurrentAccess() throws JoranException, InterruptedException {
058        configure(FOLDER_PREFIX + "logback_416.xml");
059        harness.execute(runnableArray);
060        assertEquals(1, InstanceCountingAppender.INSTANCE_COUNT.get());
061    }
062}