mirror of https://github.com/qos-ch/slf4j
clean MultithreadedInitializationTest(s)
This commit is contained in:
parent
9d7ad8152b
commit
f3ca982135
|
|
@ -0,0 +1,57 @@
|
|||
/**
|
||||
* Copyright (c) 2004-2016 QOS.ch
|
||||
* All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
package org.slf4j;
|
||||
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public class LoggerAccessingThread extends Thread {
|
||||
private static int LOOP_LEN = 64;
|
||||
|
||||
final CyclicBarrier barrier;
|
||||
final int count;
|
||||
final AtomicLong eventCount;
|
||||
|
||||
public LoggerAccessingThread(final CyclicBarrier barrier, final int count, final AtomicLong eventCount) {
|
||||
this.barrier = barrier;
|
||||
this.count = count;
|
||||
this.eventCount = eventCount;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
barrier.await();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String loggerNamePrefix = this.getClass().getName();
|
||||
for (int i = 0; i < LOOP_LEN; i++) {
|
||||
Logger logger = LoggerFactory.getLogger(loggerNamePrefix + "-" + count + "-" + i);
|
||||
logger.info("in run method");
|
||||
eventCount.getAndIncrement();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -39,15 +39,15 @@ import org.junit.After;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerAccessingThread;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MultithreadedInitializationTest {
|
||||
public class JDK14MultithreadedInitializationTest {
|
||||
|
||||
final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;
|
||||
|
||||
private static AtomicLong EVENT_COUNT = new AtomicLong(0);
|
||||
|
||||
final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
|
||||
final private AtomicLong eventCount = new AtomicLong(0);
|
||||
final private CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
|
||||
|
||||
int diff = new Random().nextInt(10000);
|
||||
String packagePrefix = "org.slf4j.impl.MultithreadedInitializationTest" + diff;
|
||||
|
|
@ -56,9 +56,18 @@ public class MultithreadedInitializationTest {
|
|||
|
||||
@Before
|
||||
public void addRecordingHandler() {
|
||||
System.out.println("THREAD_COUNT=" + THREAD_COUNT);
|
||||
removeAllHandlersForRoot();
|
||||
julLogger.addHandler(new RecordingHandler());
|
||||
}
|
||||
|
||||
private void removeAllHandlersForRoot() {
|
||||
Handler[] handlers = julLogger.getHandlers();
|
||||
for (int i = 0; i < handlers.length; i++) {
|
||||
julLogger.removeHandler(handlers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
Handler[] handlers = julLogger.getHandlers();
|
||||
|
|
@ -71,24 +80,15 @@ public class MultithreadedInitializationTest {
|
|||
|
||||
@Test
|
||||
public void multiThreadedInitialization() throws InterruptedException, BrokenBarrierException {
|
||||
System.out.println("THREAD_COUNT=" + THREAD_COUNT);
|
||||
@SuppressWarnings("unused")
|
||||
LoggerAccessingThread[] accessors = harness();
|
||||
|
||||
for (int i = 0; i < accessors.length; i++) {
|
||||
LoggerAccessingThread accessor = accessors[i];
|
||||
EVENT_COUNT.getAndIncrement();
|
||||
if (accessor.logger == null) {
|
||||
fail("logger for LoggerAccessingThread " + i + " is not set");
|
||||
}
|
||||
accessor.logger.info("post harness");
|
||||
}
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(packagePrefix + ".test");
|
||||
logger.info("hello");
|
||||
EVENT_COUNT.getAndIncrement();
|
||||
eventCount.getAndIncrement();
|
||||
|
||||
List<LogRecord> records = getRecordedEvents();
|
||||
assertEquals(EVENT_COUNT.get(), records.size());
|
||||
assertEquals(eventCount.get(), records.size());
|
||||
}
|
||||
|
||||
private List<LogRecord> getRecordedEvents() {
|
||||
|
|
@ -110,9 +110,8 @@ public class MultithreadedInitializationTest {
|
|||
|
||||
private LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
|
||||
LoggerAccessingThread[] threads = new LoggerAccessingThread[THREAD_COUNT];
|
||||
final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
|
||||
for (int i = 0; i < THREAD_COUNT; i++) {
|
||||
threads[i] = new LoggerAccessingThread(barrier, i);
|
||||
threads[i] = new LoggerAccessingThread(barrier, i, eventCount);
|
||||
threads[i].start();
|
||||
}
|
||||
|
||||
|
|
@ -126,28 +125,4 @@ public class MultithreadedInitializationTest {
|
|||
return threads;
|
||||
}
|
||||
|
||||
class LoggerAccessingThread extends Thread {
|
||||
final CyclicBarrier barrier;
|
||||
volatile Logger logger;
|
||||
final int count;
|
||||
|
||||
LoggerAccessingThread(CyclicBarrier barrier, int count) {
|
||||
this.barrier = barrier;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
barrier.await();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (int i = 0; i < 64; i++) {
|
||||
logger = LoggerFactory.getLogger(packagePrefix + ".LoggerAccessingThread" + count + "-" + i);
|
||||
logger.info("in run method");
|
||||
EVENT_COUNT.getAndIncrement();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -27,6 +27,14 @@
|
|||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<type>test-jar</type>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
@ -35,23 +35,30 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.spi.LoggingEvent;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerAccessingThread;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MultithreadedInitializationTest {
|
||||
public class Log4j12MultithreadedInitializationTest {
|
||||
|
||||
// value of LogManager.DEFAULT_CONFIGURATION_KEY;
|
||||
static String CONFIG_FILE_KEY = "log4j.configuration";
|
||||
|
||||
final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;
|
||||
|
||||
private static AtomicLong EVENT_COUNT = new AtomicLong(0);
|
||||
private final AtomicLong eventCount = new AtomicLong(0);
|
||||
|
||||
final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
|
||||
private final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
|
||||
|
||||
int diff = new Random().nextInt(10000);
|
||||
String loggerName = "org.slf4j.impl.RecursiveInitializationTest";
|
||||
final int diff = new Random().nextInt(10000);
|
||||
final String loggerName = this.getClass().getName();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
System.out.println("THREAD_COUNT=" + THREAD_COUNT);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
|
|
@ -60,22 +67,19 @@ public class MultithreadedInitializationTest {
|
|||
|
||||
@Test
|
||||
public void multiThreadedInitialization() throws InterruptedException, BrokenBarrierException {
|
||||
System.out.println("THREAD_COUNT=" + THREAD_COUNT);
|
||||
|
||||
System.setProperty(CONFIG_FILE_KEY, "recursiveInitWithActivationDelay.properties");
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
LoggerAccessingThread[] accessors = harness();
|
||||
|
||||
for (LoggerAccessingThread accessor : accessors) {
|
||||
EVENT_COUNT.getAndIncrement();
|
||||
accessor.logger.info("post harness");
|
||||
}
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(loggerName + ".slowInitialization-" + diff);
|
||||
logger.info("hello");
|
||||
EVENT_COUNT.getAndIncrement();
|
||||
eventCount.getAndIncrement();
|
||||
|
||||
List<LoggingEvent> events = getRecordedEvents();
|
||||
// 3 evetns generated by RecursiveAppender
|
||||
assertEquals(EVENT_COUNT.get() + 3, events.size());
|
||||
int NUM_LINES_BY_RECURSIVE_APPENDER = 3;
|
||||
assertEquals(eventCount.get() + NUM_LINES_BY_RECURSIVE_APPENDER, events.size());
|
||||
}
|
||||
|
||||
private List<LoggingEvent> getRecordedEvents() {
|
||||
|
|
@ -85,11 +89,10 @@ public class MultithreadedInitializationTest {
|
|||
return ra.events;
|
||||
}
|
||||
|
||||
private static LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
|
||||
private LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
|
||||
LoggerAccessingThread[] threads = new LoggerAccessingThread[THREAD_COUNT];
|
||||
final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
|
||||
for (int i = 0; i < THREAD_COUNT; i++) {
|
||||
threads[i] = new LoggerAccessingThread(barrier, i);
|
||||
threads[i] = new LoggerAccessingThread(barrier, i, eventCount);
|
||||
threads[i].start();
|
||||
}
|
||||
|
||||
|
|
@ -100,28 +103,4 @@ public class MultithreadedInitializationTest {
|
|||
return threads;
|
||||
}
|
||||
|
||||
static class LoggerAccessingThread extends Thread {
|
||||
final CyclicBarrier barrier;
|
||||
Logger logger;
|
||||
int count;
|
||||
|
||||
LoggerAccessingThread(CyclicBarrier barrier, int count) {
|
||||
this.barrier = barrier;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
barrier.await();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (int i = 0; i < 64; i++) {
|
||||
logger = LoggerFactory.getLogger(this.getClass().getName() + "-" + count + "-" + i);
|
||||
logger.info("in run method");
|
||||
EVENT_COUNT.getAndIncrement();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -21,6 +21,14 @@
|
|||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<type>test-jar</type>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
@ -39,58 +39,57 @@ import org.junit.After;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerAccessingThread;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.LoggerFactoryFriend;
|
||||
|
||||
public class MultithreadedInitializationTest {
|
||||
public class SimpleLoggerMultithreadedInitializationTest {
|
||||
|
||||
final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;
|
||||
|
||||
private static AtomicLong EVENT_COUNT = new AtomicLong(0);
|
||||
|
||||
private final AtomicLong eventCount = new AtomicLong(0);
|
||||
private final PrintStream oldErr = System.err;
|
||||
final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
|
||||
private final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
|
||||
|
||||
int diff = new Random().nextInt(10000);
|
||||
String loggerName = "org.slf4j.impl.MultithreadedInitializationTest";
|
||||
StringPrintStream sps = new StringPrintStream(oldErr);
|
||||
final int diff = new Random().nextInt(10000);
|
||||
final String loggerName = this.getClass().getName();
|
||||
StringPrintStream sps = new StringPrintStream(oldErr, true);
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
LoggerFactoryFriend.reset();
|
||||
System.out.println("THREAD_COUNT=" + THREAD_COUNT);
|
||||
System.setErr(sps);
|
||||
System.setProperty(SimpleLogger.LOG_FILE_KEY, "System.err");
|
||||
LoggerFactoryFriend.reset();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
LoggerFactoryFriend.reset();
|
||||
System.clearProperty(SimpleLogger.LOG_FILE_KEY);
|
||||
System.setErr(oldErr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiThreadedInitialization() throws InterruptedException, BrokenBarrierException {
|
||||
System.out.println("THREAD_COUNT=" + THREAD_COUNT);
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
LoggerAccessingThread[] accessors = harness();
|
||||
|
||||
for (LoggerAccessingThread accessor : accessors) {
|
||||
EVENT_COUNT.getAndIncrement();
|
||||
accessor.logger.info("post harness");
|
||||
}
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(loggerName + ".slowInitialization-" + diff);
|
||||
Logger logger = LoggerFactory.getLogger(loggerName + diff);
|
||||
logger.info("hello");
|
||||
EVENT_COUNT.getAndIncrement();
|
||||
eventCount.getAndIncrement();
|
||||
|
||||
int NUM_LINES_IN_SLF4J_REPLAY_WARNING = 3;
|
||||
assertEquals(EVENT_COUNT.get() + NUM_LINES_IN_SLF4J_REPLAY_WARNING, sps.stringList.size());
|
||||
assertEquals(eventCount.get() + NUM_LINES_IN_SLF4J_REPLAY_WARNING, sps.stringList.size());
|
||||
}
|
||||
|
||||
private static LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
|
||||
LoggerAccessingThread[] threads = new LoggerAccessingThread[THREAD_COUNT];
|
||||
final CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
|
||||
private LoggerAccessingThread[] harness() throws InterruptedException, BrokenBarrierException {
|
||||
final LoggerAccessingThread[] threads = new LoggerAccessingThread[THREAD_COUNT];
|
||||
for (int i = 0; i < THREAD_COUNT; i++) {
|
||||
threads[i] = new LoggerAccessingThread(barrier, i);
|
||||
threads[i].start();
|
||||
LoggerAccessingThread simpleLoggerThread = new LoggerAccessingThread(barrier, i, eventCount);
|
||||
threads[i] = simpleLoggerThread;
|
||||
simpleLoggerThread.start();
|
||||
}
|
||||
|
||||
barrier.await();
|
||||
|
|
@ -100,53 +99,40 @@ public class MultithreadedInitializationTest {
|
|||
return threads;
|
||||
}
|
||||
|
||||
static class LoggerAccessingThread extends Thread {
|
||||
final CyclicBarrier barrier;
|
||||
Logger logger;
|
||||
int count;
|
||||
|
||||
LoggerAccessingThread(CyclicBarrier barrier, int count) {
|
||||
this.barrier = barrier;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
barrier.await();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (int i = 0; i < 64; i++) {
|
||||
logger = LoggerFactory.getLogger(this.getClass().getName() + "-" + count+"-"+i);
|
||||
logger.info("in run method");
|
||||
EVENT_COUNT.getAndIncrement();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public static class StringPrintStream extends PrintStream {
|
||||
static class StringPrintStream extends PrintStream {
|
||||
|
||||
public static final String LINE_SEP = System.getProperty("line.separator");
|
||||
PrintStream other;
|
||||
boolean duplicate = false;
|
||||
|
||||
List<String> stringList = Collections.synchronizedList(new ArrayList<String>());
|
||||
|
||||
public StringPrintStream(PrintStream ps) {
|
||||
public StringPrintStream(PrintStream ps, boolean duplicate) {
|
||||
super(ps);
|
||||
other = ps;
|
||||
this.duplicate = duplicate;
|
||||
}
|
||||
|
||||
public StringPrintStream(PrintStream ps) {
|
||||
this(ps, false);
|
||||
}
|
||||
|
||||
public void print(String s) {
|
||||
other.print(s);
|
||||
if (duplicate)
|
||||
other.print(s);
|
||||
stringList.add(s);
|
||||
}
|
||||
|
||||
public void println(String s) {
|
||||
other.println(s);
|
||||
if (duplicate)
|
||||
other.println(s);
|
||||
stringList.add(s);
|
||||
}
|
||||
|
||||
public void println(Object o) {
|
||||
other.println(o);
|
||||
if (duplicate)
|
||||
other.println(o);
|
||||
stringList.add(o.toString());
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue