Skip to content

Commit

Permalink
[LOGMGR-343] If the AsyncHandler's worker thread and the current thre…
Browse files Browse the repository at this point in the history
…ad are the same, do not add the record the queue. Simply delegate the record down to the nested handlers.

https://issues.redhat.com/browse/LOGMGR-343
Signed-off-by: James R. Perkins <jperkins@redhat.com>
  • Loading branch information
jamezp committed Nov 10, 2023
1 parent 1b97737 commit 17197ed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/jboss/logmanager/handlers/AsyncHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ protected void doPublish(final ExtLogRecord record) {
// Copy the MDC over
record.copyMdc();
}
if (Thread.currentThread() == thread) {
publishToNestedHandlers(record);
return;
}
if (overflowAction == OverflowAction.DISCARD) {
recordQueue.offer(record);
} else {
Expand Down
36 changes: 34 additions & 2 deletions src/test/java/org/jboss/logmanager/handlers/AsyncHandlerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void setup() {
handler = new BlockingQueueHandler();

asyncHandler = new AsyncHandler();
asyncHandler.setOverflowAction(OverflowAction.DISCARD);
asyncHandler.setOverflowAction(OverflowAction.BLOCK);
asyncHandler.addHandler(handler);
}

Expand Down Expand Up @@ -99,6 +99,37 @@ public void testMdc() throws Exception {
Assertions.assertEquals(mdcValue, handler.getFirst());
}

@Test
public void reentry() throws Exception {
final ExtHandler reLogHandler = new ExtHandler() {
private final ThreadLocal<Boolean> entered = ThreadLocal.withInitial(() -> false);

@Override
protected void doPublish(final ExtLogRecord record) {
if (entered.get()) {
return;
}
try {
entered.set(true);
super.doPublish(record);
// Create a new record and act is if this was through a logger
asyncHandler.publish(createRecord());
} finally {
entered.set(false);
}
}
};
handler.addHandler(reLogHandler);
handler.setFormatter(new PatternFormatter("%s"));
asyncHandler.publish(createRecord());
// We should end up with two messages and a third should not happen
Assertions.assertEquals("Test message", handler.getFirst());
Assertions.assertEquals("Test message", handler.getFirst());
// This should time out. Then we end up with a null value. We could instead sleep for a shorter time and check
// if the queue is empty. However, a 5 second delay does not seem too long.
Assertions.assertNull(handler.getFirst(), () -> "Expected no more entries, but found " + handler.queue);
}

static ExtLogRecord createRecord() {
return new ExtLogRecord(Level.INFO, "Test message", AsyncHandlerTests.class.getName());
}
Expand All @@ -107,13 +138,14 @@ static class BlockingQueueHandler extends ExtHandler {
private final BlockingDeque<String> queue;

BlockingQueueHandler() {
queue = new LinkedBlockingDeque<String>();
queue = new LinkedBlockingDeque<>();
setErrorManager(AssertingErrorManager.of());
}

@Override
protected void doPublish(final ExtLogRecord record) {
queue.addLast(getFormatter().format(record));
publishToNestedHandlers(record);
super.doPublish(record);
}

Expand Down

0 comments on commit 17197ed

Please sign in to comment.