[logback-dev] svn commit: r2286 - logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133

noreply.ceki at qos.ch noreply.ceki at qos.ch
Sat Jun 13 17:16:07 CEST 2009


Author: ceki
Date: Sat Jun 13 17:16:07 2009
New Revision: 2286

Added:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/Accessor.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/DeadlockTest.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/Worker.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/logback-test.xml

Log:
files associated with LBCLASSIC-133

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/Accessor.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/Accessor.java	Sat Jun 13 17:16:07 2009
@@ -0,0 +1,23 @@
+package ch.qos.logback.classic.issue.lbclassic133;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class Accessor {
+  private static Logger s_logger = LoggerFactory.getLogger(Accessor.class);
+
+  public void access(Worker worker) {
+    /**
+     * at some point, for any reason, this method decides to log the status of
+     * the worker object.
+     */
+    s_logger.debug("Current worker status is: {}.", worker);
+    /**
+     * the following line would not cause the deadlock, because the thread never
+     * tries to acquire a lock on worker while already having a lock on the
+     * logger.
+     */
+    //s_logger.debug("Current worker status (not deadlocking) is: {}.", worker
+    //    .toString());
+  }
+}

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/DeadlockTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/DeadlockTest.java	Sat Jun 13 17:16:07 2009
@@ -0,0 +1,37 @@
+package ch.qos.logback.classic.issue.lbclassic133;
+
+import static org.junit.Assert.assertFalse;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DeadlockTest {
+  private static Logger s_logger = LoggerFactory.getLogger(DeadlockTest.class);
+
+  @Test
+  public void deadlockTest() throws Exception {
+    s_logger.debug("Starting test.");
+
+    final Worker worker = new Worker();
+    final Thread workerThread = new Thread(new Runnable() {
+      public void run() {
+        worker.work();
+      }
+    });
+    workerThread.setName("WorkerThread");
+
+    final Thread accessorThread = new Thread(new Runnable() {
+      public void run() {
+        new Accessor().access(worker);
+      }
+    });
+    accessorThread.setName("AccessorThread");
+
+    workerThread.start();
+    accessorThread.start();
+
+    workerThread.join(5 * 1000);
+    assertFalse("Worker thread seems locked.", workerThread.isAlive());
+  }
+}

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/Worker.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/Worker.java	Sat Jun 13 17:16:07 2009
@@ -0,0 +1,40 @@
+package ch.qos.logback.classic.issue.lbclassic133;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class Worker {
+  private static Logger s_logger = LoggerFactory.getLogger(Worker.class);
+
+  private final Object m_lock = new Object();
+
+  public void work() {
+    // Locks the object to do some work changing internal status.
+    synchronized (m_lock) {
+      // Does some work...
+      try {
+        Thread.sleep(1 * 1000);
+      } catch (InterruptedException exc) {
+        //
+      }
+      // Then calls logger, while still holding the lock.
+      s_logger.debug("Did some work, result is: {}."/* ,... */);
+    }
+  }
+
+  public String getStatus() {
+    // Locks the object to make sure the status snapshot is coherent. (Will
+    // deadlock.)
+    synchronized (m_lock) {
+      // Packs some status information, e.g. in a StringBuffer.
+      final StringBuffer buf = new StringBuffer("STATUS");
+      // Returns the string.
+      return buf.toString();
+    }
+  }
+
+  @Override
+  public String toString() {
+    return getStatus();
+  }
+}
\ No newline at end of file

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/logback-test.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/issue/lbclassic133/logback-test.xml	Sat Jun 13 17:16:07 2009
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration>
+
+	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+		<layout class="ch.qos.logback.classic.PatternLayout">
+			<param name="Pattern"
+				value="%relative %level [%thread] %logger{25} %message %nopex\n" />
+		</layout>
+	</appender>
+
+	<root>
+		<level value="DEBUG" />
+		<appender-ref ref="STDOUT" />
+	</root>
+
+</configuration>
\ No newline at end of file


More information about the logback-dev mailing list