@@ -198,7 +198,6 @@ protected void createFile(final File file, final long size) throws IOException {
198
198
try (BufferedOutputStream output = new BufferedOutputStream (Files .newOutputStream (file .toPath ()))) {
199
199
TestUtils .generateTestData (output , size );
200
200
}
201
-
202
201
// try to make sure file is found
203
202
// (to stop continuum occasionally failing)
204
203
RandomAccessFile reader = null ;
@@ -219,36 +218,33 @@ protected void createFile(final File file, final long size) throws IOException {
219
218
assertEquals (size , file .length ());
220
219
}
221
220
222
- private List <String > expectLinesWithLongTimeout (final TestTailerListener listener , final long minDelay , int count ) throws Exception {
223
- for (int i = 0 ; i < count ; i ++) {
221
+ private List <String > expectLinesWithLongTimeout (final TestTailerListener listener , final long minDelay , final int count ) throws Exception {
222
+ for (int i = 0 ; i < count ; i ++) {
224
223
TestUtils .sleep (minDelay );
225
224
final List <String > lines = listener .getLines ();
226
225
if (lines .size () > 0 ) {
227
226
return lines ;
228
227
}
229
228
}
230
- throw new RuntimeException ("waiting for TestTailerListener.getLines() timed out after " + (count * minDelay ) + " ms" );
229
+ fail ("Waiting for TestTailerListener.getLines() timed out after " + count * minDelay + " ms" );
230
+ return null ;
231
231
}
232
232
233
233
@ Test
234
234
@ SuppressWarnings ("squid:S2699" ) // Suppress "Add at least one assertion to this test case"
235
235
void testBufferBreak () throws Exception {
236
236
final long delay = 50 ;
237
-
238
237
final File file = new File (temporaryFolder , "testBufferBreak.txt" );
239
238
createFile (file , 0 );
240
239
writeStrings (file , "SBTOURIST\n " );
241
-
242
240
final TestTailerListener listener = new TestTailerListener ();
243
241
try (Tailer tailer = new Tailer (file , listener , delay , false , 1 )) {
244
242
final Thread thread = new Thread (tailer );
245
243
thread .start ();
246
-
247
244
List <String > lines = listener .getLines ();
248
245
while (lines .isEmpty () || !lines .get (lines .size () - 1 ).equals ("SBTOURIST" )) {
249
246
lines = listener .getLines ();
250
247
}
251
-
252
248
listener .clear ();
253
249
}
254
250
}
@@ -393,7 +389,6 @@ void testIO335() throws Exception { // test CR behavior
393
389
@ SuppressWarnings ("squid:S2699" ) // Suppress "Add at least one assertion to this test case"
394
390
void testLongFile () throws Exception {
395
391
final long delay = 50 ;
396
-
397
392
final File file = new File (temporaryFolder , "testLongFile.txt" );
398
393
createFile (file , 0 );
399
394
try (Writer writer = Files .newBufferedWriter (file .toPath (), StandardOpenOption .APPEND )) {
@@ -402,21 +397,16 @@ void testLongFile() throws Exception {
402
397
}
403
398
writer .write ("SBTOURIST\n " );
404
399
}
405
-
406
400
final TestTailerListener listener = new TestTailerListener ();
407
401
try (Tailer tailer = new Tailer (file , listener , delay , false )) {
408
-
409
402
// final long start = System.currentTimeMillis();
410
-
411
403
final Thread thread = new Thread (tailer );
412
404
thread .start ();
413
-
414
405
List <String > lines = listener .getLines ();
415
406
while (lines .isEmpty () || !lines .get (lines .size () - 1 ).equals ("SBTOURIST" )) {
416
407
lines = listener .getLines ();
417
408
}
418
409
// System.out.println("Elapsed: " + (System.currentTimeMillis() - start));
419
-
420
410
listener .clear ();
421
411
}
422
412
}
@@ -436,9 +426,8 @@ void testMultiByteBreak() throws Exception {
436
426
try (Tailer tailer = new Tailer (file , charsetUTF8 , listener , delay , false , isWindows , IOUtils .DEFAULT_BUFFER_SIZE )) {
437
427
final Thread thread = new Thread (tailer );
438
428
thread .start ();
439
-
440
429
try (Writer out = new OutputStreamWriter (Files .newOutputStream (file .toPath ()), charsetUTF8 );
441
- BufferedReader reader = new BufferedReader (new InputStreamReader (Files .newInputStream (origin .toPath ()), charsetUTF8 ))) {
430
+ BufferedReader reader = new BufferedReader (new InputStreamReader (Files .newInputStream (origin .toPath ()), charsetUTF8 ))) {
442
431
final List <String > lines = new ArrayList <>();
443
432
String line ;
444
433
while ((line = reader .readLine ()) != null ) {
@@ -447,7 +436,6 @@ void testMultiByteBreak() throws Exception {
447
436
lines .add (line );
448
437
}
449
438
out .close (); // ensure data is written
450
-
451
439
final long testDelayMillis = delay * 10 ;
452
440
TestUtils .sleep (testDelayMillis );
453
441
final List <String > tailerlines = listener .getLines ();
@@ -590,7 +578,6 @@ void testStopWithNoFileUsingExecutor() throws Exception {
590
578
591
579
@ Test
592
580
void testTailer () throws Exception {
593
-
594
581
// Create & start the Tailer
595
582
final long delayMillis = 50 ;
596
583
final File file = new File (temporaryFolder , "tailer1-test.txt" );
@@ -601,7 +588,6 @@ void testTailer() throws Exception {
601
588
try (Tailer tailer = new Tailer (file , listener , delayMillis , false , isWindows )) {
602
589
final Thread thread = new Thread (tailer );
603
590
thread .start ();
604
-
605
591
// Write some lines to the file
606
592
writeLines (file , "Line one" , "Line two" );
607
593
final long testDelayMillis = delayMillis * 10 ;
@@ -611,37 +597,32 @@ void testTailer() throws Exception {
611
597
assertEquals ("Line one" , lines .get (0 ), "1 line 1" );
612
598
assertEquals ("Line two" , lines .get (1 ), "1 line 2" );
613
599
listener .clear ();
614
-
615
600
// Write another line to the file
616
601
writeLines (file , "Line three" );
617
602
TestUtils .sleep (testDelayMillis );
618
603
lines = listener .getLines ();
619
604
assertEquals (1 , lines .size (), "2 line count" );
620
605
assertEquals ("Line three" , lines .get (0 ), "2 line 3" );
621
606
listener .clear ();
622
-
623
607
// Check file does actually have all the lines
624
608
lines = FileUtils .readLines (file , StandardCharsets .UTF_8 );
625
609
assertEquals (3 , lines .size (), "3 line count" );
626
610
assertEquals ("Line one" , lines .get (0 ), "3 line 1" );
627
611
assertEquals ("Line two" , lines .get (1 ), "3 line 2" );
628
612
assertEquals ("Line three" , lines .get (2 ), "3 line 3" );
629
-
630
613
// Delete & re-create
631
614
file .delete ();
632
615
assertFalse (file .exists (), "File should not exist" );
633
616
createFile (file , 0 );
634
617
assertTrue (file .exists (), "File should now exist" );
635
618
TestUtils .sleep (testDelayMillis );
636
-
637
619
// Write another line
638
620
writeLines (file , "Line four" );
639
621
TestUtils .sleep (testDelayMillis );
640
622
lines = listener .getLines ();
641
623
assertEquals (1 , lines .size (), "4 line count" );
642
624
assertEquals ("Line four" , lines .get (0 ), "4 line 3" );
643
625
listener .clear ();
644
-
645
626
// Stop
646
627
thread .interrupt ();
647
628
TestUtils .sleep (testDelayMillis * 4 );
@@ -669,19 +650,15 @@ void testTailerEndOfFileReached() throws Exception {
669
650
try (Tailer tailer = new Tailer (file , listener , delayMillis , false , isWindows )) {
670
651
final Thread thread = new Thread (tailer );
671
652
thread .start ();
672
-
673
653
// write a few lines
674
654
writeLines (file , "line1" , "line2" , "line3" );
675
655
TestUtils .sleep (testDelayMillis );
676
-
677
656
// write a few lines
678
657
writeLines (file , "line4" , "line5" , "line6" );
679
658
TestUtils .sleep (testDelayMillis );
680
-
681
659
// write a few lines
682
660
writeLines (file , "line7" , "line8" , "line9" );
683
661
TestUtils .sleep (testDelayMillis );
684
-
685
662
// May be > 3 times due to underlying OS behavior wrt streams
686
663
assertTrue (listener .reachedEndOfFile >= 3 , "end of file reached at least 3 times" );
687
664
}
@@ -697,21 +674,16 @@ void testTailerEof() throws Exception {
697
674
try (Tailer tailer = new Tailer (file , listener , delayMillis , false )) {
698
675
final Thread thread = new Thread (tailer );
699
676
thread .start ();
700
-
701
677
// Write some lines to the file
702
678
writeStrings (file , "Line" );
703
-
704
679
TestUtils .sleep (delayMillis * 2 );
705
680
List <String > lines = listener .getLines ();
706
681
assertEquals (0 , lines .size (), "1 line count" );
707
-
708
682
writeStrings (file , " one\n " );
709
683
TestUtils .sleep (delayMillis * 4 );
710
684
lines = listener .getLines ();
711
-
712
685
assertEquals (1 , lines .size (), "1 line count" );
713
686
assertEquals ("Line one" , lines .get (0 ), "1 line 1" );
714
-
715
687
listener .clear ();
716
688
}
717
689
}
@@ -723,25 +695,18 @@ void testTailerIgnoreTouch() throws Exception {
723
695
final File file = new File (temporaryFolder , "tailer1-testIgnoreTouch.txt" );
724
696
createFile (file , 0 );
725
697
final TestTailerListener listener = new TestTailerListener ();
726
- try (Tailer tailer = Tailer .builder ()
727
- .setFile (file )
728
- .setTailerListener (listener )
729
- .setDelayDuration (Duration .ofMillis (delayMillis ))
730
- .setStartThread (false )
731
- .setIgnoreTouch (true )
732
- .get ()) {
698
+ try (Tailer tailer = Tailer .builder ().setFile (file ).setTailerListener (listener ).setDelayDuration (Duration .ofMillis (delayMillis )).setStartThread (false )
699
+ .setIgnoreTouch (true ).get ()) {
733
700
final Thread thread = new Thread (tailer );
734
701
thread .start ();
735
-
736
702
// Write some lines to the file
737
703
writeLines (file , "Line one" );
738
704
List <String > lines = expectLinesWithLongTimeout (listener , delayMillis , 20 );
739
705
assertEquals (1 , lines .size (), "1 line count" );
740
706
assertEquals ("Line one" , lines .get (0 ), "1 line 1" );
741
707
listener .clear ();
742
-
743
708
// touch the file
744
- TestUtils .sleepTillNextFullSecond (); // ensure to be within the next second because of posix fs limitation
709
+ TestUtils .sleepToNextSecond (); // ensure to be within the next second because of posix fs limitation
745
710
file .setLastModified (System .currentTimeMillis ());
746
711
TestUtils .sleep (delayMillis * 10 );
747
712
lines = listener .getLines ();
@@ -756,25 +721,18 @@ void testTailerReissueOnTouch() throws Exception {
756
721
final File file = new File (temporaryFolder , "tailer1-testReissueOnTouch.txt" );
757
722
createFile (file , 0 );
758
723
final TestTailerListener listener = new TestTailerListener ();
759
- try (Tailer tailer = Tailer .builder ()
760
- .setFile (file )
761
- .setTailerListener (listener )
762
- .setDelayDuration (Duration .ofMillis (delayMillis ))
763
- .setStartThread (false )
764
- .setIgnoreTouch (false )
765
- .get ()) {
724
+ try (Tailer tailer = Tailer .builder ().setFile (file ).setTailerListener (listener ).setDelayDuration (Duration .ofMillis (delayMillis )).setStartThread (false )
725
+ .setIgnoreTouch (false ).get ()) {
766
726
final Thread thread = new Thread (tailer );
767
727
thread .start ();
768
-
769
728
// Write some lines to the file
770
729
writeLines (file , "Line one" );
771
730
List <String > lines = expectLinesWithLongTimeout (listener , delayMillis , 50 );
772
731
assertEquals (1 , lines .size (), "1 line count" );
773
732
assertEquals ("Line one" , lines .get (0 ), "1 line 1" );
774
733
listener .clear ();
775
-
776
734
// touch the file
777
- TestUtils .sleepTillNextFullSecond (); // ensure to be within the next second because of posix fs limitation
735
+ TestUtils .sleepToNextSecond (); // ensure to be within the next second because of posix fs limitation
778
736
file .setLastModified (System .currentTimeMillis ());
779
737
lines = expectLinesWithLongTimeout (listener , delayMillis , 20 );
780
738
assertEquals (1 , lines .size (), "1 line count" );
0 commit comments