16
16
*/
17
17
package org .apache .logging .log4j .async .logger ;
18
18
19
- import static java . util . concurrent . TimeUnit . SECONDS ;
19
+ import static org . apache . logging . log4j . async . logger . QueueFullAsyncAbstractTest . assertAsyncLogger ;
20
20
import static org .apache .logging .log4j .core .test .TestConstants .ASYNC_FORMAT_MESSAGES_IN_BACKGROUND ;
21
21
import static org .apache .logging .log4j .core .test .TestConstants .ASYNC_LOGGER_RING_BUFFER_SIZE ;
22
22
import 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 ;
23
24
import static org .assertj .core .api .Assertions .assertThat ;
24
- import static org .junit .Assert .assertTrue ;
25
25
26
+ import java .util .List ;
26
27
import java .util .concurrent .CountDownLatch ;
28
+ import java .util .stream .Collectors ;
29
+ import java .util .stream .IntStream ;
27
30
import org .apache .logging .log4j .Logger ;
28
- import org .apache .logging .log4j .core .GarbageCollectionHelper ;
29
31
import org .apache .logging .log4j .core .LoggerContext ;
30
32
import org .apache .logging .log4j .core .async .AsyncQueueFullPolicyFactory ;
31
33
import org .apache .logging .log4j .core .async .DiscardingAsyncQueueFullPolicy ;
32
34
import org .apache .logging .log4j .core .impl .CoreProperties ;
33
35
import org .apache .logging .log4j .core .test .async .BlockingAppender ;
36
+ import org .apache .logging .log4j .core .test .async .QueueFullAbstractTest ;
34
37
import org .apache .logging .log4j .core .test .junit .ContextSelectorType ;
35
38
import org .apache .logging .log4j .core .test .junit .LoggerContextSource ;
36
39
import org .apache .logging .log4j .core .test .junit .Named ;
37
40
import org .apache .logging .log4j .message .Message ;
41
+ import org .apache .logging .log4j .message .SimpleMessage ;
38
42
import org .apache .logging .log4j .status .StatusLogger ;
39
43
import org .apache .logging .log4j .test .junit .SetTestProperty ;
40
44
import org .junit .jupiter .api .Test ;
41
- import org .junit .jupiter .api .Timeout ;
42
45
43
46
/**
44
47
* Tests queue full scenarios with pure AsyncLoggers (all loggers async).
47
50
@ SetTestProperty (key = ASYNC_LOGGER_RING_BUFFER_SIZE , value = "128" )
48
51
@ SetTestProperty (key = ASYNC_FORMAT_MESSAGES_IN_BACKGROUND , value = "true" )
49
52
@ SetTestProperty (key = ASYNC_QUEUE_FULL_POLICY_CLASS_NAME , value = "Discard" )
50
- public class QueueFullAsyncLogger3Test extends QueueFullAsyncAbstractTest {
53
+ class QueueFullAsyncLogger3Test extends QueueFullAbstractTest {
51
54
52
55
@ Override
53
56
protected void checkConfig (final LoggerContext ctx ) {
@@ -60,64 +63,21 @@ protected void checkConfig(final LoggerContext ctx) {
60
63
}
61
64
62
65
@ Test
63
- @ Timeout (value = 15 , unit = SECONDS )
64
66
@ LoggerContextSource
65
- public void discardedMessagesShouldBeGarbageCollected (
67
+ void discarded_messages_should_be_garbage_collected (
66
68
final LoggerContext ctx , final @ Named (APPENDER_NAME ) BlockingAppender blockingAppender )
67
69
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
+ });
122
82
}
123
83
}
0 commit comments