Skip to content

Commit

Permalink
record event count
Browse files Browse the repository at this point in the history
  • Loading branch information
ceki committed Mar 15, 2016
1 parent 3fc58f2 commit 79698d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
@@ -1,18 +1,16 @@
package org.slf4j.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class RecordingHandler extends Handler {

List<LogRecord> records = Collections.synchronizedList(new ArrayList<LogRecord>());
public class CountingHandler extends Handler {

final AtomicLong eventCount = new AtomicLong(0);

@Override
public void publish(LogRecord record) {
records.add(record);
eventCount.getAndIncrement();
}

@Override
Expand Down
Expand Up @@ -27,13 +27,11 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.List;
import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

import org.junit.After;
import org.junit.Before;
Expand All @@ -47,7 +45,7 @@ public class JDK14MultithreadedInitializationTest {
final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;

final private AtomicLong eventCount = new AtomicLong(0);
final private CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);
final private CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT + 1);

int diff = new Random().nextInt(10000);

Expand All @@ -57,25 +55,21 @@ public class JDK14MultithreadedInitializationTest {
@Before
public void addRecordingHandler() {
System.out.println("THREAD_COUNT=" + THREAD_COUNT);
removeAllHandlersForRoot();
julOrgLogger.addHandler(new RecordingHandler());
removeAllHandlers(julRootLogger);
removeAllHandlers(julOrgLogger);
julOrgLogger.addHandler(new CountingHandler());
}

private void removeAllHandlersForRoot() {
Handler[] handlers = julRootLogger.getHandlers();
private void removeAllHandlers(java.util.logging.Logger logger) {
Handler[] handlers = logger.getHandlers();
for (int i = 0; i < handlers.length; i++) {
julRootLogger.removeHandler(handlers[i]);
logger.removeHandler(handlers[i]);
}
}

@After
public void tearDown() throws Exception {
Handler[] handlers = julOrgLogger.getHandlers();
for (int i = 0; i < handlers.length; i++) {
if (handlers[i] instanceof RecordingHandler) {
julOrgLogger.removeHandler(handlers[i]);
}
}
removeAllHandlers(julOrgLogger);
}

@Test
Expand All @@ -87,23 +81,23 @@ public void multiThreadedInitialization() throws InterruptedException, BrokenBar
logger.info("hello");
eventCount.getAndIncrement();

List<LogRecord> records = getRecordedEvents();
assertEquals(eventCount.get(), records.size());
long recordedEventCount = getRecordedEventCount();
assertEquals(eventCount.get(), recordedEventCount);
}

private List<LogRecord> getRecordedEvents() {
RecordingHandler ra = findRecordingHandler();
private long getRecordedEventCount() {
CountingHandler ra = findRecordingHandler();
if (ra == null) {
fail("failed to fing RecordingHandler");
}
return ra.records;
return ra.eventCount.get();
}

private RecordingHandler findRecordingHandler() {
private CountingHandler findRecordingHandler() {
Handler[] handlers = julOrgLogger.getHandlers();
for (Handler h : handlers) {
if (h instanceof RecordingHandler)
return (RecordingHandler) h;
if (h instanceof CountingHandler)
return (CountingHandler) h;
}
return null;
}
Expand Down

0 comments on commit 79698d6

Please sign in to comment.