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;
15  
16  import java.io.ByteArrayInputStream;
17  import java.util.concurrent.TimeUnit;
18  
19  import org.junit.jupiter.api.AfterEach;
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  
23  import ch.qos.logback.classic.joran.JoranConfigurator;
24  import ch.qos.logback.core.joran.spi.JoranException;
25  import org.junit.jupiter.api.Timeout;
26  
27  public class LoggerContextDeadlockTest {
28  
29      LoggerContext loggerContext = new LoggerContext();
30  
31      GetLoggerThread getLoggerThread = new GetLoggerThread(loggerContext);
32  
33      @BeforeEach
34      public void setUp() throws Exception {
35  
36      }
37  
38      @AfterEach
39      public void tearDown() throws Exception {
40      }
41  
42      // LBCLASSIC_81
43      // LOGBACK-394
44      @Test
45      @Timeout(value = 20, unit= TimeUnit.SECONDS)
46      public void test_LOGBACK_394() throws JoranException {
47  
48          getLoggerThread.start();
49          for (int i = 0; i < 500; i++) {
50              JoranConfigurator jc = new JoranConfigurator();
51              jc.setContext(loggerContext);
52              ByteArrayInputStream baos = new ByteArrayInputStream(
53                      "<configuration><root level=\"DEBUG\"/></configuration>".getBytes());
54              jc.doConfigure(baos);
55          }
56      }
57  
58      class GetLoggerThread extends Thread {
59  
60          final LoggerContext loggerContext;
61  
62          GetLoggerThread(LoggerContext loggerContext) {
63              this.loggerContext = loggerContext;
64          }
65  
66          @Override
67          public void run() {
68              for (int i = 0; i < 10000; i++) {
69                  if (i % 100 == 0) {
70                      try {
71                          Thread.sleep(1);
72                      } catch (InterruptedException e) {
73                      }
74                  }
75                  loggerContext.getLogger("a" + i);
76              }
77          }
78      }
79  
80  }