1313 */
1414package ch .qos .logback .core .rolling ;
1515
16- import static ch .qos .logback .core .testUtil .CoreTestConstants .FAILURE_EXIT_CODE ;
17- import static ch .qos .logback .core .testUtil .CoreTestConstants .SUCCESSFUL_EXIT_CODE ;
18-
1916import java .io .File ;
20- import java .io .FileOutputStream ;
2117import java .io .FilenameFilter ;
2218import java .io .IOException ;
23- import java .io .OutputStream ;
19+ import java .nio .file .Files ;
20+ import java .util .ArrayList ;
21+ import java .util .Arrays ;
22+ import java .util .Comparator ;
23+ import java .util .List ;
2424
2525import ch .qos .logback .core .testUtil .CoreTestConstants ;
26- import ch .qos .logback .core .testUtil .EnvUtilForTests ;
2726
2827import org .junit .jupiter .api .AfterEach ;
2928import org .junit .jupiter .api .Assertions ;
3837import ch .qos .logback .core .encoder .Encoder ;
3938import ch .qos .logback .core .testUtil .RandomUtil ;
4039import ch .qos .logback .core .status .testUtil .StatusChecker ;
40+ import ch .qos .logback .core .util .Duration ;
4141import ch .qos .logback .core .util .FileSize ;
4242import ch .qos .logback .core .util .StatusPrinter ;
4343
@@ -50,16 +50,11 @@ public class MultiThreadedRollingTest {
5050 Encoder <Object > encoder ;
5151 Context context = new ContextBase ();
5252
53- static String VERIFY_SH = "verify.sh" ;
54-
5553 int diff = RandomUtil .getPositiveInt ();
5654 String outputDirStr = CoreTestConstants .OUTPUT_DIR_PREFIX + "multi-" + diff + "/" ;
5755
5856 RollingFileAppender <Object > rfa = new RollingFileAppender <Object >();
5957
60- String pathToBash = EnvUtilForTests .getPathToBash ();
61- OutputStream scriptOS ;
62-
6358 @ BeforeEach
6459 public void setUp () throws Exception {
6560 encoder = new EchoEncoder <Object >();
@@ -70,24 +65,13 @@ public void setUp() throws Exception {
7065 }
7166 System .out .println ("Output dir [" + outputDirStr + "]" );
7267
73- scriptOS = openScript ();
74-
7568 rfa .setName ("rolling" );
7669 rfa .setEncoder (encoder );
7770 rfa .setContext (context );
7871 rfa .setFile (outputDirStr + "output.log" );
7972
8073 }
8174
82- void close (OutputStream os ) {
83- if (os != null ) {
84- try {
85- os .close ();
86- } catch (IOException e ) {
87- }
88- }
89- }
90-
9175 @ AfterEach
9276 public void tearDown () throws Exception {
9377 rfa .stop ();
@@ -109,6 +93,8 @@ public void setUpSizeBasedTriggeringPolicy(RollingFileAppender<Object> rfa) {
10993 SizeBasedTriggeringPolicy <Object > zbtp = new SizeBasedTriggeringPolicy <>();
11094 zbtp .setContext (context );
11195 zbtp .setMaxFileSize (FileSize .valueOf ("100KB" ));
96+ // the default check increment of 60 seconds would prevent any rollover during the test
97+ zbtp .setCheckIncrement (Duration .buildByMilliseconds (10 ));
11298
11399 zbtp .start ();
114100 rfa .setTriggeringPolicy (zbtp );
@@ -132,16 +118,11 @@ RunnableWithCounterAndDone[] buildRunnableArray(boolean withDelay) {
132118 return runnableArray ;
133119 }
134120
135- OutputStream openScript () throws IOException {
136- return new FileOutputStream (outputDirStr + VERIFY_SH );
137- }
138-
139121 @ Test
140122 public void multiThreadedTimedBased () throws InterruptedException , IOException {
141123 setUpTimeBasedTriggeringPolicy (rfa );
142124 executeHarness (TOTAL_DURATION , false );
143- printScriptForTimeBased ();
144- verify ();
125+ verify (timeBasedFilesInOrder ());
145126 }
146127
147128 int testFileCount () {
@@ -158,21 +139,41 @@ public boolean accept(File dir, String name) {
158139 return files .length ;
159140 }
160141
161- void verify () throws IOException , InterruptedException {
162- close (scriptOS );
163- // no point in this test if we don't have bash
164- if (pathToBash == null ) {
165- return ;
166- }
167- ProcessBuilder pb = new ProcessBuilder ();
168- pb .command (pathToBash , VERIFY_SH );
169- pb .directory (new File (outputDirStr ));
170- Process process = pb .start ();
171- process .waitFor ();
172- int exitCode = process .exitValue ();
142+ // archives are named after their date, so sorting by name gives the order of writing
143+ List <File > timeBasedFilesInOrder () {
144+ File [] archives = new File (outputDirStr ).listFiles ((dir , name ) -> name .startsWith ("test-" ));
145+ Arrays .sort (archives , Comparator .comparing (File ::getName ));
146+ List <File > result = new ArrayList <>(Arrays .asList (archives ));
147+ result .add (new File (outputDirStr + "output.log" ));
148+ return result ;
149+ }
173150
174- Assertions .assertEquals (SUCCESSFUL_EXIT_CODE , exitCode );
175- System .out .println ("External script based verification returned with exit code " + exitCode );
151+ // the highest index is the oldest archive
152+ List <File > sizeBasedFilesInOrder (int numFiles ) {
153+ List <File > result = new ArrayList <>();
154+ for (int i = numFiles - 1 ; i >= 0 ; i --) {
155+ result .add (new File (outputDirStr + "test-" + i + ".log" ));
156+ }
157+ result .add (new File (outputDirStr + "output.log" ));
158+ return result ;
159+ }
160+
161+ // each line is "<threadId> <counter>"; the counters of each thread must appear
162+ // in order, without gaps, up to the final value of the thread's counter
163+ void verify (List <File > filesInOrder ) throws IOException {
164+ long [] lastCounter = new long [NUM_THREADS ];
165+ for (File file : filesInOrder ) {
166+ for (String line : Files .readAllLines (file .toPath ())) {
167+ int space = line .indexOf (' ' );
168+ int id = Integer .parseInt (line .substring (0 , space ));
169+ long counter = Long .parseLong (line .substring (space + 1 ));
170+ Assertions .assertEquals (lastCounter [id ] + 1 , counter , "thread " + id + " in " + file .getName ());
171+ lastCounter [id ] = counter ;
172+ }
173+ }
174+ for (int i = 0 ; i < NUM_THREADS ; i ++) {
175+ Assertions .assertEquals (runnableArray [i ].getCounter (), lastCounter [i ], "final counter of thread " + i );
176+ }
176177 }
177178
178179 @ Test
@@ -182,66 +183,7 @@ public void multiThreadedSizeBased() throws InterruptedException, IOException {
182183 // long the MAX_WINDOW_SIZE is reached, resulting in data loss which
183184 // we cannot test for.
184185 executeHarness (TOTAL_DURATION , true );
185- int numFiles = testFileCount ();
186- printScriptForSizeBased (numFiles );
187- verify ();
188- }
189-
190- private void printScriptHeader (String type ) throws IOException {
191- out ("# ====================================================" );
192- out ("# A script to check the exactness of the output " );
193- out ("# produced by " + type + " test" );
194- out ("# ====================================================" );
195- out ("# " );
196- }
197-
198- private void printCommonScriptCore () throws IOException {
199- out ("" );
200- out ("for t in $(seq 0 1 " + (NUM_THREADS - 1 ) + ")" );
201- out ("do" );
202- out (" echo \" Testing results of thread $t\" " );
203- out (" grep \" $t \" aggregated | cut -d ' ' -f 2 > ${t}-sample" );
204- out (" for j in $(seq 1 1 ${end[$t]}); do echo $j; done > ${t}-witness" );
205- out (" diff -q -w ${t}-sample ${t}-witness;" );
206- out (" res=$?" );
207- out (" if [ $res != \" 0\" ]; then" );
208- out (" echo \" FAILED for $t\" " );
209- out (" exit " + FAILURE_EXIT_CODE );
210- out (" fi" );
211- out ("done" );
212- out ("" );
213- out ("exit " + SUCCESSFUL_EXIT_CODE );
214- }
215-
216- private void printScriptForTimeBased () throws IOException {
217- printScriptHeader ("TimeBased" );
218- for (int i = 0 ; i < NUM_THREADS ; i ++) {
219- out ("end[" + i + "]=" + this .runnableArray [i ].getCounter ());
220- }
221- out ("" );
222- out ("rm aggregated" );
223- out ("cat test* output.log >> aggregated" );
224- printCommonScriptCore ();
225-
226- }
227-
228- private void printScriptForSizeBased (int numfiles ) throws IOException {
229- printScriptHeader ("SizeBased" );
230-
231- for (int i = 0 ; i < NUM_THREADS ; i ++) {
232- out ("end[" + i + "]=" + this .runnableArray [i ].getCounter ());
233- }
234- out ("" );
235- out ("rm aggregated" );
236- out ("for i in $(seq " + (numfiles - 1 ) + " -1 0); do cat test-$i.log >> aggregated; done" );
237- out ("cat output.log >> aggregated" );
238- out ("" );
239- printCommonScriptCore ();
240- }
241-
242- private void out (String msg ) throws IOException {
243- scriptOS .write (msg .getBytes ());
244- scriptOS .write ("\n " .getBytes ());
186+ verify (sizeBasedFilesInOrder (testFileCount ()));
245187 }
246188
247189 private void executeHarness (int duration , boolean withDelay ) throws InterruptedException {
0 commit comments