1616 */
1717package org .apache .logging .log4j .async .logger ;
1818
19- import static java . util . concurrent . TimeUnit . SECONDS ;
19+ import static org . apache . logging . log4j . async . logger . QueueFullAsyncAbstractTest . assertAsyncLogger ;
2020import static org .apache .logging .log4j .core .test .TestConstants .ASYNC_FORMAT_MESSAGES_IN_BACKGROUND ;
2121import static org .apache .logging .log4j .core .test .TestConstants .ASYNC_LOGGER_RING_BUFFER_SIZE ;
2222import static org .apache .logging .log4j .core .test .TestConstants .ASYNC_QUEUE_FULL_POLICY_CLASS_NAME ;
23+ import static org .apache .logging .log4j .core .test .internal .GcHelper .awaitGarbageCollection ;
2324import static org .assertj .core .api .Assertions .assertThat ;
24- import static org .junit .Assert .assertTrue ;
2525
26+ import java .util .List ;
2627import java .util .concurrent .CountDownLatch ;
28+ import java .util .stream .Collectors ;
29+ import java .util .stream .IntStream ;
2730import org .apache .logging .log4j .Logger ;
28- import org .apache .logging .log4j .core .GarbageCollectionHelper ;
2931import org .apache .logging .log4j .core .LoggerContext ;
3032import org .apache .logging .log4j .core .async .AsyncQueueFullPolicyFactory ;
3133import org .apache .logging .log4j .core .async .DiscardingAsyncQueueFullPolicy ;
3234import org .apache .logging .log4j .core .impl .CoreProperties ;
3335import org .apache .logging .log4j .core .test .async .BlockingAppender ;
36+ import org .apache .logging .log4j .core .test .async .QueueFullAbstractTest ;
3437import org .apache .logging .log4j .core .test .junit .ContextSelectorType ;
3538import org .apache .logging .log4j .core .test .junit .LoggerContextSource ;
3639import org .apache .logging .log4j .core .test .junit .Named ;
3740import org .apache .logging .log4j .message .Message ;
41+ import org .apache .logging .log4j .message .SimpleMessage ;
3842import org .apache .logging .log4j .status .StatusLogger ;
3943import org .apache .logging .log4j .test .junit .SetTestProperty ;
4044import org .junit .jupiter .api .Test ;
41- import org .junit .jupiter .api .Timeout ;
4245
4346/**
4447 * Tests queue full scenarios with pure AsyncLoggers (all loggers async).
4750@ SetTestProperty (key = ASYNC_LOGGER_RING_BUFFER_SIZE , value = "128" )
4851@ SetTestProperty (key = ASYNC_FORMAT_MESSAGES_IN_BACKGROUND , value = "true" )
4952@ SetTestProperty (key = ASYNC_QUEUE_FULL_POLICY_CLASS_NAME , value = "Discard" )
50- public class QueueFullAsyncLogger3Test extends QueueFullAsyncAbstractTest {
53+ class QueueFullAsyncLogger3Test extends QueueFullAbstractTest {
5154
5255 @ Override
5356 protected void checkConfig (final LoggerContext ctx ) {
@@ -60,64 +63,21 @@ protected void checkConfig(final LoggerContext ctx) {
6063 }
6164
6265 @ Test
63- @ Timeout (value = 15 , unit = SECONDS )
6466 @ LoggerContextSource
65- public void discardedMessagesShouldBeGarbageCollected (
67+ void discarded_messages_should_be_garbage_collected (
6668 final LoggerContext ctx , final @ Named (APPENDER_NAME ) BlockingAppender blockingAppender )
6769 throws InterruptedException {
68- checkConfig (ctx );
69- final Logger logger = ctx .getLogger (getClass ());
70-
71- blockingAppender .logEvents = null ;
72- blockingAppender .countDownLatch = new CountDownLatch (1 );
73- final int count = 200 ;
74- final CountDownLatch garbageCollectionLatch = new CountDownLatch (count );
75- for (int i = 0 ; i < count ; i ++) {
76- logger .info (new CountdownOnGarbageCollectMessage (garbageCollectionLatch ));
77- }
78- blockingAppender .countDownLatch .countDown ();
79-
80- final GarbageCollectionHelper gcHelper = new GarbageCollectionHelper ();
81- gcHelper .run ();
82- try {
83- assertTrue ("Parameter should have been garbage collected" , garbageCollectionLatch .await (30 , SECONDS ));
84- } finally {
85- gcHelper .close ();
86- }
87- }
88-
89- private static final class CountdownOnGarbageCollectMessage implements Message {
90-
91- private final CountDownLatch latch ;
92-
93- CountdownOnGarbageCollectMessage (final CountDownLatch latch ) {
94- this .latch = latch ;
95- }
96-
97- @ Override
98- public String getFormattedMessage () {
99- return "formatted" ;
100- }
101-
102- @ Override
103- public String getFormat () {
104- return null ;
105- }
106-
107- @ Override
108- public Object [] getParameters () {
109- return org .apache .logging .log4j .util .Constants .EMPTY_OBJECT_ARRAY ;
110- }
111-
112- @ Override
113- public Throwable getThrowable () {
114- return null ;
115- }
116-
117- @ Override
118- protected void finalize () throws Throwable {
119- latch .countDown ();
120- super .finalize ();
121- }
70+ awaitGarbageCollection (() -> {
71+ checkConfig (ctx );
72+ final Logger logger = ctx .getLogger (getClass ());
73+ blockingAppender .logEvents = null ;
74+ blockingAppender .countDownLatch = new CountDownLatch (1 );
75+ final List <Message > messages = IntStream .range (0 , 200 )
76+ .mapToObj (messageIndex -> new SimpleMessage ("message " + messageIndex ))
77+ .collect (Collectors .toList ());
78+ messages .forEach (logger ::info );
79+ blockingAppender .countDownLatch .countDown ();
80+ return messages ;
81+ });
12282 }
12383}
0 commit comments