1717package org .glassfish .main .jul .handler ;
1818
1919import java .io .File ;
20+ import java .io .IOException ;
21+ import java .nio .charset .StandardCharsets ;
2022import java .nio .file .Files ;
2123import java .util .logging .Level ;
2224import java .util .logging .LogManager ;
2325import java .util .logging .Logger ;
26+ import java .util .stream .Stream ;
2427
2528import org .glassfish .main .jul .env .LoggingSystemEnvironment ;
2629import org .glassfish .main .jul .formatter .OneLineFormatter ;
4447import static org .junit .jupiter .api .Assertions .assertNotSame ;
4548import static org .junit .jupiter .api .Assertions .assertSame ;
4649import static org .junit .jupiter .api .Assertions .assertTrue ;
50+ import static org .junit .jupiter .api .Assertions .fail ;
4751
4852/**
4953 * @author David Matejcek
5054 */
5155@ TestMethodOrder (OrderAnnotation .class )
5256public class GlassFishLogHandlerTest {
5357
54- private static final long MILLIS_FOR_PUMP = 20L ;
5558 private static GlassFishLogHandler handler ;
59+ private static File logFile ;
5660
5761 @ BeforeAll
5862 public static void initEnv () throws Exception {
5963 GlassFishLoggingTracer .setTracingEnabled (true );
6064 LogManager .getLogManager ().reset ();
6165 LoggingSystemEnvironment .initialize ();
6266 final GlassFishLogHandlerConfiguration cfg = new GlassFishLogHandlerConfiguration ();
63- final File logFile = File .createTempFile (GlassFishLogHandlerTest .class .getCanonicalName (), ".log" );
67+ logFile = File .createTempFile (GlassFishLogHandlerTest .class .getCanonicalName (), ".log" );
6468 logFile .delete ();
6569 logFile .deleteOnExit ();
6670 cfg .setLogFile (logFile );
@@ -107,11 +111,12 @@ public void enableStandardStreamsLoggers(TestInfo testInfo) throws Exception {
107111
108112 System .out .println ("Tommy, can you hear me?" );
109113 // output stream is pumped in parallel to the error stream, order is not guaranteed between streams
110- Thread . sleep ( MILLIS_FOR_PUMP );
114+ waitForSize ( logFile , 1 );
111115 Logger .getLogger ("some.usual.logger" ).info ("Some info message" );
116+ waitForSize (logFile , 2 );
112117 System .err .println ("Can you feel me near you?" );
113118 System .err .println ("Příliš žluťoučký kůň úpěl ďábelské ódy" );
114- Thread . sleep ( MILLIS_FOR_PUMP );
119+ waitForSize ( logFile , 4 );
115120 assertAll (
116121 () -> assertTrue (handler .isReady (), "handler.ready" ),
117122 () -> assertTrue (cfg .getLogFile ().exists (), "file exists" ),
@@ -135,7 +140,7 @@ public void roll() throws Exception {
135140 assertTrue (handler .isReady (), "handler.ready" );
136141 handler .publish (new GlassFishLogRecord (Level .SEVERE , "File one, record one" , false ));
137142 // pump is now to play
138- Thread . sleep ( MILLIS_FOR_PUMP );
143+ waitForSize ( logFile , 5 );
139144 assertAll (
140145 () -> assertTrue (handler .isReady (), "handler.ready" ),
141146 () -> assertTrue (handler .getConfiguration ().getLogFile ().exists (), "file one exists" ),
@@ -150,8 +155,8 @@ public void roll() throws Exception {
150155 )
151156 );
152157 handler .roll ();
153- // There will be an asynchronous thread.
154- Thread . sleep ( 10L );
158+ // There will be an asynchronous thread archiving the file .
159+ waitForSize ( logFile , 2 );
155160 assertAll (
156161 () -> assertTrue (handler .isReady (), "handler.ready" ),
157162 () -> assertTrue (handler .getConfiguration ().getLogFile ().exists (), "file exists" ),
@@ -160,11 +165,12 @@ public void roll() throws Exception {
160165 stringContainsInOrder ("INFO" , "org.glassfish.main.jul.rotation.LogFileManager.roll" ,
161166 "Rolling the file " , ".log" , "output was originally enabled: true" ),
162167 stringContainsInOrder ("INFO" , "org.glassfish.main.jul.rotation.LogFileManager.moveFile" ,
163- "Archiving file " , ".log" , " to " , ".log_" ) )
168+ "Archiving file " , ".log" , " to " , ".log_" )
169+ )
164170 )
165171 );
166172 handler .publish (new GlassFishLogRecord (Level .SEVERE , "File two, line two" , false ));
167- Thread . sleep ( MILLIS_FOR_PUMP );
173+ waitForSize ( logFile , 3 );
168174 assertAll (
169175 () -> assertTrue (handler .isReady (), "handler.ready" ),
170176 () -> assertTrue (handler .getConfiguration ().getLogFile ().exists (), "file exists" ),
@@ -201,4 +207,21 @@ public void createConfiguration() throws Exception {
201207 .createGlassFishLogHandlerConfiguration (GlassFishLogHandler .class );
202208 assertNotNull (cfg , "cfg" );
203209 }
210+
211+
212+ private static void waitForSize (File file , long minLineCount ) throws IOException {
213+ long start = System .currentTimeMillis ();
214+ do {
215+ Thread .onSpinWait ();
216+ final long lineCount ;
217+ try (Stream <String > stream = Files .lines (file .toPath (), StandardCharsets .UTF_8 )) {
218+ lineCount = stream .count ();
219+ }
220+ if (lineCount >= minLineCount ) {
221+ return ;
222+ }
223+ } while (System .currentTimeMillis () - start < 2000L );
224+ fail ("Timeout waiting until the file " + file + " grows to " + minLineCount + " lines. File content: \n "
225+ + Files .readString (file .toPath (), handler .getConfiguration ().getEncoding ()));
226+ }
204227}
0 commit comments