Skip to content

Commit

Permalink
clean MultithreadedInitializationTest(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceki committed Mar 15, 2016
1 parent 9d7ad81 commit f3ca982
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 133 deletions.
57 changes: 57 additions & 0 deletions slf4j-api/src/test/java/org/slf4j/LoggerAccessingThread.java
@@ -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();
}
}
}
Expand Up @@ -39,15 +39,15 @@
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;
Expand All @@ -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();
Expand All @@ -71,24 +80,15 @@ public void tearDown() throws Exception {

@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() {
Expand All @@ -110,9 +110,8 @@ private RecordingHandler findRecordingHandler() {

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();
}

Expand All @@ -126,28 +125,4 @@ private LoggerAccessingThread[] harness() throws InterruptedException, BrokenBar
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();
}
}
};

}
8 changes: 8 additions & 0 deletions slf4j-log4j12/pom.xml
Expand Up @@ -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>
Expand Up @@ -35,23 +35,30 @@
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 {
Expand All @@ -60,22 +67,19 @@ public void tearDown() throws Exception {

@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() {
Expand All @@ -85,11 +89,10 @@ private List<LoggingEvent> getRecordedEvents() {
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();
}

Expand All @@ -100,28 +103,4 @@ private static LoggerAccessingThread[] harness() throws InterruptedException, Br
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();
}
}
};

}
8 changes: 8 additions & 0 deletions slf4j-simple/pom.xml
Expand Up @@ -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>

0 comments on commit f3ca982

Please sign in to comment.