1717package org .apache .commons .io .input ;
1818
1919import java .io .File ;
20+ import java .io .FileNotFoundException ;
2021import java .io .FileWriter ;
22+ import java .io .IOException ;
23+ import java .io .RandomAccessFile ;
2124import java .util .ArrayList ;
2225import java .util .List ;
2326import java .util .concurrent .Executor ;
@@ -42,9 +45,9 @@ public void testTailer() throws Exception {
4245
4346 // Create & start the Tailer
4447 long delay = 50 ;
45- File file = new File (getTestDirectory (), "tailer1-test.txt" );
48+ final File file = new File (getTestDirectory (), "tailer1-test.txt" );
4649 createFile (file , 0 );
47- TestTailerListener listener = new TestTailerListener ();
50+ final TestTailerListener listener = new TestTailerListener ();
4851 final Tailer tailer = new Tailer (file , listener , delay , false );
4952 final Thread thread = new Thread (tailer );
5053 thread .start ();
@@ -98,6 +101,30 @@ public void testTailer() throws Exception {
98101 assertEquals ("fileRotated should be be called" , 1 , listener .rotated );
99102 }
100103
104+ protected void createFile (File file , long size )
105+ throws IOException {
106+ super .createFile (file , size );
107+
108+ // try to make sure file is found
109+ // (to stop continuum occasionally failing)
110+ RandomAccessFile reader = null ;
111+ try {
112+ while (reader == null ) {
113+ try {
114+ reader = new RandomAccessFile (file .getPath (), "r" );
115+ } catch (FileNotFoundException e ) {
116+ }
117+ try {
118+ Thread .sleep (200L );
119+ } catch (InterruptedException e ) {
120+ // ignore
121+ }
122+ }
123+ } finally {
124+ IOUtils .closeQuietly (reader );
125+ }
126+ }
127+
101128 /** Append some lines to a file */
102129 private void write (File file , String ... lines ) throws Exception {
103130 FileWriter writer = null ;
@@ -112,23 +139,23 @@ private void write(File file, String... lines) throws Exception {
112139 }
113140
114141 public void testStopWithNoFile () throws Exception {
115- File file = new File (getTestDirectory (),"nosuchfile" );
142+ final File file = new File (getTestDirectory (),"nosuchfile" );
116143 assertFalse ("nosuchfile should not exist" , file .exists ());
117- TestTailerListener listener = new TestTailerListener ();
144+ final TestTailerListener listener = new TestTailerListener ();
118145 int delay = 100 ;
119146 int idle = 50 ; // allow time for thread to work
120- Tailer tailer = Tailer .create (file , listener , delay , false );
147+ final Tailer tailer = Tailer .create (file , listener , delay , false );
121148 Thread .sleep (idle );
122149 tailer .stop ();
123150 Thread .sleep (delay +idle );
124151 assertNull ("Should not generate Exception" , listener .exception );
125152 assertEquals ("Expected init to be called" , 1 , listener .initialised );
126- assertEquals ("fileNotFound should be called" , 1 , listener .notFound );
153+ assertTrue ("fileNotFound should be called" , listener .notFound > 0 );
127154 assertEquals ("fileRotated should be not be called" , 0 , listener .rotated );
128155 }
129156
130157 public void testStopWithNoFileUsingExecutor () throws Exception {
131- File file = new File (getTestDirectory (),"nosuchfile" );
158+ final File file = new File (getTestDirectory (),"nosuchfile" );
132159 assertFalse ("nosuchfile should not exist" , file .exists ());
133160 TestTailerListener listener = new TestTailerListener ();
134161 int delay = 100 ;
@@ -141,7 +168,7 @@ public void testStopWithNoFileUsingExecutor() throws Exception {
141168 Thread .sleep (delay +idle );
142169 assertNull ("Should not generate Exception" , listener .exception );
143170 assertEquals ("Expected init to be called" , 1 , listener .initialised );
144- assertEquals ("fileNotFound should be called" , 1 , listener .notFound );
171+ assertTrue ("fileNotFound should be called" , listener .notFound > 0 );
145172 assertEquals ("fileRotated should be not be called" , 0 , listener .rotated );
146173 }
147174
0 commit comments