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.classic.issue.logback416;
15  
16  import ch.qos.logback.classic.ClassicTestConstants;
17  import ch.qos.logback.classic.Logger;
18  import ch.qos.logback.classic.LoggerContext;
19  import ch.qos.logback.classic.issue.lbclassic135.LoggingRunnable;
20  import ch.qos.logback.classic.issue.logback416.InstanceCountingAppender;
21  import ch.qos.logback.classic.joran.JoranConfigurator;
22  import ch.qos.logback.core.contention.MultiThreadedHarness;
23  import ch.qos.logback.core.contention.RunnableWithCounterAndDone;
24  import ch.qos.logback.core.joran.spi.JoranException;
25  
26  import org.junit.Test;
27  
28  import static org.junit.Assert.assertEquals;
29  
30  public class ConcurrentSiftingTest {
31      final static int THREAD_COUNT = 5;
32      static String FOLDER_PREFIX = ClassicTestConstants.JORAN_INPUT_PREFIX + "sift/";
33  
34      LoggerContext loggerContext = new LoggerContext();
35      protected Logger logger = loggerContext.getLogger(this.getClass().getName());
36      protected Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
37  
38      int totalTestDuration = 50;
39      MultiThreadedHarness harness = new MultiThreadedHarness(totalTestDuration);
40      RunnableWithCounterAndDone[] runnableArray = buildRunnableArray();
41  
42      protected void configure(String file) throws JoranException {
43          JoranConfigurator jc = new JoranConfigurator();
44          jc.setContext(loggerContext);
45          jc.doConfigure(file);
46      }
47  
48      RunnableWithCounterAndDone[] buildRunnableArray() {
49          RunnableWithCounterAndDone[] rArray = new RunnableWithCounterAndDone[THREAD_COUNT];
50          for (int i = 0; i < THREAD_COUNT; i++) {
51              rArray[i] = new LoggingRunnable(logger);
52          }
53          return rArray;
54      }
55  
56      @Test
57      public void concurrentAccess() throws JoranException, InterruptedException {
58          configure(FOLDER_PREFIX + "logback_416.xml");
59          harness.execute(runnableArray);
60          assertEquals(1, InstanceCountingAppender.INSTANCE_COUNT.get());
61      }
62  }