aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/apache/commons/io/input/TailerTest.java
blob: f0e1f3405e4796b64a92490b0794fe76c25fd807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.io.input;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.RandomAccessFileMode;
import org.apache.commons.io.TestResources;
import org.apache.commons.io.test.TestUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
 * Test for {@link Tailer}.
 */
public class TailerTest {

    private static class NonStandardTailable implements Tailer.Tailable {

        private final File file;

        public NonStandardTailable(final File file) {
            this.file = file;
        }

        @Override
        public Tailer.RandomAccessResourceBridge getRandomAccess(final String mode) throws FileNotFoundException {
            return new Tailer.RandomAccessResourceBridge() {

                private final RandomAccessFile randomAccessFile = new RandomAccessFile(file, mode);

                @Override
                public void close() throws IOException {
                    randomAccessFile.close();
                }

                @Override
                public long getPointer() throws IOException {
                    return randomAccessFile.getFilePointer();
                }

                @Override
                public int read(final byte[] b) throws IOException {
                    return randomAccessFile.read(b);
                }

                @Override
                public void seek(final long position) throws IOException {
                    randomAccessFile.seek(position);
                }
            };
        }

        @Override
        public boolean isNewer(final FileTime fileTime) throws IOException {
            return FileUtils.isFileNewer(file, fileTime);
        }

        @Override
        public FileTime lastModifiedFileTime() throws IOException {
            return FileUtils.lastModifiedFileTime(file);
        }

        @Override
        public long size() {
            return file.length();
        }
    }

    /**
     * Test {@link TailerListener} implementation.
     */
    private static class TestTailerListener extends TailerListenerAdapter {

        // Must be synchronized because it is written by one thread and read by another
        private final List<String> lines = Collections.synchronizedList(new ArrayList<>());

        private final CountDownLatch latch;

        volatile Exception exception;

        volatile int notFound;

        volatile int rotated;

        volatile int initialized;

        volatile int reachedEndOfFile;

        public TestTailerListener() {
            latch = new CountDownLatch(1);
        }

        public TestTailerListener(final int expectedLines) {
            latch = new CountDownLatch(expectedLines);
        }

        public boolean awaitExpectedLines(final long timeout, final TimeUnit timeUnit) throws InterruptedException {
            return latch.await(timeout, timeUnit);
        }

        public void clear() {
            lines.clear();
        }

        @Override
        public void endOfFileReached() {
            reachedEndOfFile++; // not atomic, but OK because only updated here.
        }

        @Override
        public void fileNotFound() {
            notFound++; // not atomic, but OK because only updated here.
        }

        @Override
        public void fileRotated() {
            rotated++; // not atomic, but OK because only updated here.
        }

        public List<String> getLines() {
            return lines;
        }

        @Override
        public void handle(final Exception e) {
            exception = e;
        }

        @Override
        public void handle(final String line) {
            lines.add(line);
            latch.countDown();
        }

        @Override
        public void init(final Tailer tailer) {
            initialized++; // not atomic, but OK because only updated here.
        }
    }

    private static final int TEST_BUFFER_SIZE = 1024;

    private static final int TEST_DELAY_MILLIS = 1500;

    @TempDir
    public static File temporaryFolder;

    protected void createFile(final File file, final long size) throws IOException {
        assertTrue(file.getParentFile().exists(), () -> "Cannot create file " + file + " as the parent directory does not exist");
        try (BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(file.toPath()))) {
            TestUtils.generateTestData(output, size);
        }

        // try to make sure file is found
        // (to stop continuum occasionally failing)
        RandomAccessFile reader = null;
        try {
            while (reader == null) {
                try {
                    reader = RandomAccessFileMode.READ_ONLY.create(file);
                } catch (final FileNotFoundException ignore) {
                    // ignore
                }
                TestUtils.sleepQuietly(200L);
            }
        } finally {
            IOUtils.closeQuietly(reader);
        }
        // sanity checks
        assertTrue(file.exists());
        assertEquals(size, file.length());
    }

    @Test
    @SuppressWarnings("squid:S2699") // Suppress "Add at least one assertion to this test case"
    public void testBufferBreak() throws Exception {
        final long delay = 50;

        final File file = new File(temporaryFolder, "testBufferBreak.txt");
        createFile(file, 0);
        writeString(file, "SBTOURIST\n");

        final TestTailerListener listener = new TestTailerListener();
        try (Tailer tailer = new Tailer(file, listener, delay, false, 1)) {
            final Thread thread = new Thread(tailer);
            thread.start();

            List<String> lines = listener.getLines();
            while (lines.isEmpty() || !lines.get(lines.size() - 1).equals("SBTOURIST")) {
                lines = listener.getLines();
            }

            listener.clear();
        }
    }

    @Test
    public void testBuilderWithNonStandardTailable() throws Exception {
        final File file = new File(temporaryFolder, "tailer-create-with-delay-and-from-start-with-reopen-and-buffersize-and-charset.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = Tailer.builder()
                .setExecutorService(Executors.newSingleThreadExecutor())
                .setTailable(new NonStandardTailable(file))
                .setTailerListener(listener)
                .get()) {
            assertTrue(tailer.getTailable() instanceof NonStandardTailable);
            validateTailer(listener, file);
        }
    }

    @Test
    public void testCreate() throws Exception {
        final File file = new File(temporaryFolder, "tailer-create.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = Tailer.create(file, listener)) {
            validateTailer(listener, file);
        }
    }

    @Test
    public void testCreatorWithDelayAndFromStartWithReopen() throws Exception {
        final File file = new File(temporaryFolder, "tailer-create-with-delay-and-from-start-with-reopen.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = Tailer.create(file, listener, TEST_DELAY_MILLIS, false, false)) {
            validateTailer(listener, file);
        }
    }

    @Test
    public void testCreateWithDelay() throws Exception {
        final File file = new File(temporaryFolder, "tailer-create-with-delay.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = Tailer.create(file, listener, TEST_DELAY_MILLIS)) {
            validateTailer(listener, file);
        }
    }

    @Test
    public void testCreateWithDelayAndFromStart() throws Exception {
        final File file = new File(temporaryFolder, "tailer-create-with-delay-and-from-start.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = Tailer.create(file, listener, TEST_DELAY_MILLIS, false)) {
            validateTailer(listener, file);
        }
    }

    @Test
    public void testCreateWithDelayAndFromStartWithBufferSize() throws Exception {
        final File file = new File(temporaryFolder, "tailer-create-with-delay-and-from-start-with-buffersize.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = Tailer.create(file, listener, TEST_DELAY_MILLIS, false, TEST_BUFFER_SIZE)) {
            validateTailer(listener, file);
        }
    }

    @Test
    public void testCreateWithDelayAndFromStartWithReopenAndBufferSize() throws Exception {
        final File file = new File(temporaryFolder, "tailer-create-with-delay-and-from-start-with-reopen-and-buffersize.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = Tailer.create(file, listener, TEST_DELAY_MILLIS, false, true, TEST_BUFFER_SIZE)) {
            validateTailer(listener, file);
        }
    }

    @Test
    public void testCreateWithDelayAndFromStartWithReopenAndBufferSizeAndCharset() throws Exception {
        final File file = new File(temporaryFolder, "tailer-create-with-delay-and-from-start-with-reopen-and-buffersize-and-charset.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = Tailer.create(file, StandardCharsets.UTF_8, listener, TEST_DELAY_MILLIS, false, true, TEST_BUFFER_SIZE)) {
            validateTailer(listener, file);
        }
    }

    /*
     * Tests [IO-357][Tailer] InterruptedException while the thread is sleeping is silently ignored.
     */
    @Test
    public void testInterrupt() throws Exception {
        final File file = new File(temporaryFolder, "nosuchfile");
        assertFalse(file.exists(), "nosuchfile should not exist");
        final TestTailerListener listener = new TestTailerListener();
        // Use a long delay to try to make sure the test thread calls interrupt() while the tailer thread is sleeping.
        final int delay = 1000;
        final int idle = 50; // allow time for thread to work
        try (Tailer tailer = new Tailer(file, listener, delay, false, IOUtils.DEFAULT_BUFFER_SIZE)) {
            final Thread thread = new Thread(tailer);
            thread.setDaemon(true);
            thread.start();
            TestUtils.sleep(idle);
            thread.interrupt();
            TestUtils.sleep(delay + idle);
            assertNotNull(listener.exception, "Missing InterruptedException");
            assertTrue(listener.exception instanceof InterruptedException, "Unexpected Exception: " + listener.exception);
            assertEquals(1, listener.initialized, "Expected init to be called");
            assertTrue(listener.notFound > 0, "fileNotFound should be called");
            assertEquals(0, listener.rotated, "fileRotated should be not be called");
            assertEquals(0, listener.reachedEndOfFile, "end of file never reached");
        }
    }

    @Test
    public void testIO335() throws Exception { // test CR behavior
        // Create & start the Tailer
        final long delayMillis = 50;
        final File file = new File(temporaryFolder, "tailer-testio334.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener();
        try (Tailer tailer = new Tailer(file, listener, delayMillis, false)) {
            final Thread thread = new Thread(tailer);
            thread.start();

            // Write some lines to the file
            writeString(file, "CRLF\r\n", "LF\n", "CR\r", "CRCR\r\r", "trail");
            final long testDelayMillis = delayMillis * 10;
            TestUtils.sleep(testDelayMillis);
            final List<String> lines = listener.getLines();
            assertEquals(4, lines.size(), "line count");
            assertEquals("CRLF", lines.get(0), "line 1");
            assertEquals("LF", lines.get(1), "line 2");
            assertEquals("CR", lines.get(2), "line 3");
            assertEquals("CRCR\r", lines.get(3), "line 4");
        }
    }

    @Test
    @SuppressWarnings("squid:S2699") // Suppress "Add at least one assertion to this test case"
    public void testLongFile() throws Exception {
        final long delay = 50;

        final File file = new File(temporaryFolder, "testLongFile.txt");
        createFile(file, 0);
        try (Writer writer = Files.newBufferedWriter(file.toPath(), StandardOpenOption.APPEND)) {
            for (int i = 0; i < 100000; i++) {
                writer.write("LineLineLineLineLineLineLineLineLineLine\n");
            }
            writer.write("SBTOURIST\n");
        }

        final TestTailerListener listener = new TestTailerListener();
        try (Tailer tailer = new Tailer(file, listener, delay, false)) {

            // final long start = System.currentTimeMillis();

            final Thread thread = new Thread(tailer);
            thread.start();

            List<String> lines = listener.getLines();
            while (lines.isEmpty() || !lines.get(lines.size() - 1).equals("SBTOURIST")) {
                lines = listener.getLines();
            }
            // System.out.println("Elapsed: " + (System.currentTimeMillis() - start));

            listener.clear();
        }
    }

    @Test
    public void testMultiByteBreak() throws Exception {
        // System.out.println("testMultiByteBreak() Default charset: " + Charset.defaultCharset().displayName());
        final long delay = 50;
        final File origin = TestResources.getFile("test-file-utf8.bin");
        final File file = new File(temporaryFolder, "testMultiByteBreak.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener();
        final String osname = System.getProperty("os.name");
        final boolean isWindows = osname.startsWith("Windows");
        // Need to use UTF-8 to read & write the file otherwise it can be corrupted (depending on the default charset)
        final Charset charsetUTF8 = StandardCharsets.UTF_8;
        try (Tailer tailer = new Tailer(file, charsetUTF8, listener, delay, false, isWindows, IOUtils.DEFAULT_BUFFER_SIZE)) {
            final Thread thread = new Thread(tailer);
            thread.start();

            try (Writer out = new OutputStreamWriter(Files.newOutputStream(file.toPath()), charsetUTF8);
                BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(origin.toPath()), charsetUTF8))) {
                final List<String> lines = new ArrayList<>();
                String line;
                while ((line = reader.readLine()) != null) {
                    out.write(line);
                    out.write("\n");
                    lines.add(line);
                }
                out.close(); // ensure data is written

                final long testDelayMillis = delay * 10;
                TestUtils.sleep(testDelayMillis);
                final List<String> tailerlines = listener.getLines();
                assertEquals(lines.size(), tailerlines.size(), "line count");
                for (int i = 0, len = lines.size(); i < len; i++) {
                    final String expected = lines.get(i);
                    final String actual = tailerlines.get(i);
                    if (!expected.equals(actual)) {
                        fail("Line: " + i + "\nExp: (" + expected.length() + ") " + expected + "\nAct: (" + actual.length() + ") " + actual);
                    }
                }
            }
        }
    }

    @Test
    public void testSimpleConstructor() throws Exception {
        final File file = new File(temporaryFolder, "tailer-simple-constructor.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = new Tailer(file, listener)) {
            final Thread thread = new Thread(tailer);
            thread.start();
            validateTailer(listener, file);
        }
    }

    @Test
    public void testSimpleConstructorWithDelay() throws Exception {
        final File file = new File(temporaryFolder, "tailer-simple-constructor-with-delay.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = new Tailer(file, listener, TEST_DELAY_MILLIS)) {
            final Thread thread = new Thread(tailer);
            thread.start();
            validateTailer(listener, file);
        }
    }

    @Test
    public void testSimpleConstructorWithDelayAndFromStart() throws Exception {
        final File file = new File(temporaryFolder, "tailer-simple-constructor-with-delay-and-from-start.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = new Tailer(file, listener, TEST_DELAY_MILLIS, false)) {
            final Thread thread = new Thread(tailer);
            thread.start();
            validateTailer(listener, file);
        }
    }

    @Test
    public void testSimpleConstructorWithDelayAndFromStartWithBufferSize() throws Exception {
        final File file = new File(temporaryFolder, "tailer-simple-constructor-with-delay-and-from-start-with-buffersize.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = new Tailer(file, listener, TEST_DELAY_MILLIS, false, TEST_BUFFER_SIZE)) {
            final Thread thread = new Thread(tailer);
            thread.start();
            validateTailer(listener, file);
        }
    }

    @Test
    public void testSimpleConstructorWithDelayAndFromStartWithReopen() throws Exception {
        final File file = new File(temporaryFolder, "tailer-simple-constructor-with-delay-and-from-start-with-reopen.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = new Tailer(file, listener, TEST_DELAY_MILLIS, false, false)) {
            final Thread thread = new Thread(tailer);
            thread.start();
            validateTailer(listener, file);
        }
    }

    @Test
    public void testSimpleConstructorWithDelayAndFromStartWithReopenAndBufferSize() throws Exception {
        final File file = new File(temporaryFolder, "tailer-simple-constructor-with-delay-and-from-start-with-reopen-and-buffersize.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = new Tailer(file, listener, TEST_DELAY_MILLIS, false, true, TEST_BUFFER_SIZE)) {
            final Thread thread = new Thread(tailer);
            thread.start();
            validateTailer(listener, file);
        }
    }

    @Test
    public void testSimpleConstructorWithDelayAndFromStartWithReopenAndBufferSizeAndCharset() throws Exception {
        final File file = new File(temporaryFolder, "tailer-simple-constructor-with-delay-and-from-start-with-reopen-and-buffersize-and-charset.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener(1);
        try (Tailer tailer = new Tailer(file, StandardCharsets.UTF_8, listener, TEST_DELAY_MILLIS, false, true, TEST_BUFFER_SIZE)) {
            final Thread thread = new Thread(tailer);
            thread.start();
            validateTailer(listener, file);
        }
    }

    @Test
    public void testStopWithNoFile() throws Exception {
        final File file = new File(temporaryFolder, "nosuchfile");
        assertFalse(file.exists(), "nosuchfile should not exist");
        final TestTailerListener listener = new TestTailerListener();
        final int delay = 100;
        final int idle = 50; // allow time for thread to work
        try (Tailer tailer = Tailer.create(file, listener, delay, false)) {
            TestUtils.sleep(idle);
        }
        TestUtils.sleep(delay + idle);
        if (listener.exception != null) {
            listener.exception.printStackTrace();
        }
        assertNull(listener.exception, "Should not generate Exception");
        assertEquals(1, listener.initialized, "Expected init to be called");
        assertTrue(listener.notFound > 0, "fileNotFound should be called");
        assertEquals(0, listener.rotated, "fileRotated should be not be called");
        assertEquals(0, listener.reachedEndOfFile, "end of file never reached");
    }

    @Test
    public void testStopWithNoFileUsingExecutor() throws Exception {
        final File file = new File(temporaryFolder, "nosuchfile");
        assertFalse(file.exists(), "nosuchfile should not exist");
        final TestTailerListener listener = new TestTailerListener();
        final int delay = 100;
        final int idle = 50; // allow time for thread to work
        try (Tailer tailer = new Tailer(file, listener, delay, false)) {
            final Executor exec = new ScheduledThreadPoolExecutor(1);
            exec.execute(tailer);
            TestUtils.sleep(idle);
        }
        TestUtils.sleep(delay + idle);
        assertNull(listener.exception, "Should not generate Exception");
        assertEquals(1, listener.initialized, "Expected init to be called");
        assertTrue(listener.notFound > 0, "fileNotFound should be called");
        assertEquals(0, listener.rotated, "fileRotated should be not be called");
        assertEquals(0, listener.reachedEndOfFile, "end of file never reached");
    }

    @Test
    public void testTailer() throws Exception {

        // Create & start the Tailer
        final long delayMillis = 50;
        final File file = new File(temporaryFolder, "tailer1-test.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener();
        final String osname = System.getProperty("os.name");
        final boolean isWindows = osname.startsWith("Windows");
        try (Tailer tailer = new Tailer(file, listener, delayMillis, false, isWindows)) {
            final Thread thread = new Thread(tailer);
            thread.start();

            // Write some lines to the file
            write(file, "Line one", "Line two");
            final long testDelayMillis = delayMillis * 10;
            TestUtils.sleep(testDelayMillis);
            List<String> lines = listener.getLines();
            assertEquals(2, lines.size(), "1 line count");
            assertEquals("Line one", lines.get(0), "1 line 1");
            assertEquals("Line two", lines.get(1), "1 line 2");
            listener.clear();

            // Write another line to the file
            write(file, "Line three");
            TestUtils.sleep(testDelayMillis);
            lines = listener.getLines();
            assertEquals(1, lines.size(), "2 line count");
            assertEquals("Line three", lines.get(0), "2 line 3");
            listener.clear();

            // Check file does actually have all the lines
            lines = FileUtils.readLines(file, StandardCharsets.UTF_8);
            assertEquals(3, lines.size(), "3 line count");
            assertEquals("Line one", lines.get(0), "3 line 1");
            assertEquals("Line two", lines.get(1), "3 line 2");
            assertEquals("Line three", lines.get(2), "3 line 3");

            // Delete & re-create
            file.delete();
            assertFalse(file.exists(), "File should not exist");
            createFile(file, 0);
            assertTrue(file.exists(), "File should now exist");
            TestUtils.sleep(testDelayMillis);

            // Write another line
            write(file, "Line four");
            TestUtils.sleep(testDelayMillis);
            lines = listener.getLines();
            assertEquals(1, lines.size(), "4 line count");
            assertEquals("Line four", lines.get(0), "4 line 3");
            listener.clear();

            // Stop
            thread.interrupt();
            TestUtils.sleep(testDelayMillis * 4);
            write(file, "Line five");
            assertEquals(0, listener.getLines().size(), "4 line count");
            assertNotNull(listener.exception, "Missing InterruptedException");
            assertTrue(listener.exception instanceof InterruptedException, "Unexpected Exception: " + listener.exception);
            assertEquals(1, listener.initialized, "Expected init to be called");
            // assertEquals(0 , listener.notFound, "fileNotFound should not be called"); // there is a window when it might be
            // called
            assertEquals(1, listener.rotated, "fileRotated should be called");
        }
    }

    @Test
    public void testTailerEndOfFileReached() throws Exception {
        // Create & start the Tailer
        final long delayMillis = 50;
        final long testDelayMillis = delayMillis * 10;
        final File file = new File(temporaryFolder, "tailer-eof-test.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener();
        final String osname = System.getProperty("os.name");
        final boolean isWindows = osname.startsWith("Windows");
        try (Tailer tailer = new Tailer(file, listener, delayMillis, false, isWindows)) {
            final Thread thread = new Thread(tailer);
            thread.start();

            // write a few lines
            write(file, "line1", "line2", "line3");
            TestUtils.sleep(testDelayMillis);

            // write a few lines
            write(file, "line4", "line5", "line6");
            TestUtils.sleep(testDelayMillis);

            // write a few lines
            write(file, "line7", "line8", "line9");
            TestUtils.sleep(testDelayMillis);

            // May be > 3 times due to underlying OS behavior wrt streams
            assertTrue(listener.reachedEndOfFile >= 3, "end of file reached at least 3 times");
        }
    }

    @Test
    public void testTailerEof() throws Exception {
        // Create & start the Tailer
        final long delayMillis = 100;
        final File file = new File(temporaryFolder, "tailer2-test.txt");
        createFile(file, 0);
        final TestTailerListener listener = new TestTailerListener();
        try (Tailer tailer = new Tailer(file, listener, delayMillis, false)) {
            final Thread thread = new Thread(tailer);
            thread.start();

            // Write some lines to the file
            writeString(file, "Line");

            TestUtils.sleep(delayMillis * 2);
            List<String> lines = listener.getLines();
            assertEquals(0, lines.size(), "1 line count");

            writeString(file, " one\n");
            TestUtils.sleep(delayMillis * 4);
            lines = listener.getLines();

            assertEquals(1, lines.size(), "1 line count");
            assertEquals("Line one", lines.get(0), "1 line 1");

            listener.clear();
        }
    }

    private void validateTailer(final TestTailerListener listener, final File file) throws IOException, InterruptedException {
        write(file, "foo");
        final int timeout = 30;
        final TimeUnit timeoutUnit = TimeUnit.SECONDS;
        assertTrue(listener.awaitExpectedLines(timeout, timeoutUnit), () -> String.format("await timed out after %s %s", timeout, timeoutUnit));
        assertEquals(listener.getLines(), Arrays.asList("foo"), "lines");
    }

    /** Appends lines to a file */
    private void write(final File file, final String... lines) throws IOException {
        try (Writer writer = Files.newBufferedWriter(file.toPath(), StandardOpenOption.APPEND)) {
            for (final String line : lines) {
                writer.write(line + "\n");
            }
        }
    }

    /** Appends strings to a file */
    private void writeString(final File file, final String... strings) throws IOException {
        try (Writer writer = Files.newBufferedWriter(file.toPath(), StandardOpenOption.APPEND)) {
            for (final String string : strings) {
                writer.write(string);
            }
        }
    }
}