Skip to content

Commit

Permalink
#646 move setting of initPending to false only after initializing the…
Browse files Browse the repository at this point in the history
… jdk log manager
  • Loading branch information
syjer committed Feb 8, 2021
1 parent 0130ccf commit 2de727f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ private void init(boolean useParent, Level level, Handler handler, Formatter for
if (!initPending) {
return;
}
//now change this immediately, in case something fails
initPending = false;

initializeJDKLogManager(useParent, level, handler, formatter);
try {
initializeJDKLogManager(useParent, level, handler, formatter);
} finally {
initPending = false;
}
}

private void initializeJDKLogManager(boolean useParent, Level level, Handler handler, Formatter formatter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;

public class XRLogTest {
Expand All @@ -14,5 +16,31 @@ public void testDisableLogBeforeFirstLog() {
XRLog.log(Level.INFO, LogMessageId.LogMessageId0Param.LOAD_UNABLE_TO_DISABLE_XML_EXTERNAL_ENTITIES);
Assert.assertFalse(XRLog.isLoggingEnabled());
}

/**
* See issue https://github.com/danfickle/openhtmltopdf/issues/646
*/
@Test
public void testConcurrentInitLog() throws InterruptedException {
int p = 20;
CountDownLatch latch = new CountDownLatch(p);
CountDownLatch end = new CountDownLatch(p);
AtomicInteger counter = new AtomicInteger();
for (int i = 0; i < p;i++) {
new Thread(() -> {
latch.countDown();
try {
latch.await();
XRLog.log(Level.SEVERE, LogMessageId.LogMessageId0Param.CASCADE_IS_ABSOLUTE_CSS_UNKNOWN_GIVEN);
counter.incrementAndGet();
end.countDown();
} catch (Throwable e) {
end.countDown();
}
}).start();
}
end.await();
Assert.assertEquals(p, counter.get()); //we expect 0 NPE -> counter = 20
}

}

0 comments on commit 2de727f

Please sign in to comment.