Skip to content

Commit a3e33e1

Browse files
authored
[io-279] fixing macos-x-13-java8 unstable test due to posix file time resolution is only 1s. Adding TestUtils.sleepTillNextFullSecond() (#760)
1 parent 4c8226a commit a3e33e1

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/test/java/org/apache/commons/io/input/TailerTest.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -724,21 +724,31 @@ void testTailerIgnoreTouch() throws Exception {
724724

725725
// Write some lines to the file
726726
writeLines(file, "Line one");
727-
final long testDelayMillis = delayMillis * 10;
728-
TestUtils.sleep(testDelayMillis);
729-
List<String> lines = listener.getLines();
727+
List<String> lines = expectLinesWithLongTimeout(listener, delayMillis, 20);
730728
assertEquals(1, lines.size(), "1 line count");
731729
assertEquals("Line one", lines.get(0), "1 line 1");
732730
listener.clear();
733731

734732
// touch the file
733+
TestUtils.sleepTillNextFullSecond(); // ensure to be within the next second because of posix fs limitation
735734
file.setLastModified(System.currentTimeMillis());
736-
TestUtils.sleep(testDelayMillis);
735+
TestUtils.sleep(delayMillis * 10);
737736
lines = listener.getLines();
738737
assertEquals(0, lines.size(), "nothing should have changed by touching");
739738
}
740739
}
741740

741+
private List<String> expectLinesWithLongTimeout(final TestTailerListener listener, final long minDelay, int count) throws Exception {
742+
for (int i = 0; i < count ; i++) {
743+
TestUtils.sleep(minDelay);
744+
final List<String> lines = listener.getLines();
745+
if (lines.size() > 0) {
746+
return lines;
747+
}
748+
}
749+
throw new RuntimeException("waiting for TestTailerListener.getLines() timed out after " + (count * minDelay) + " ms");
750+
}
751+
742752
@Test
743753
void testTailerReissueOnTouch() throws Exception {
744754
// Create & start the Tailer
@@ -758,17 +768,15 @@ void testTailerReissueOnTouch() throws Exception {
758768

759769
// Write some lines to the file
760770
writeLines(file, "Line one");
761-
final long testDelayMillis = delayMillis * 10;
762-
TestUtils.sleep(testDelayMillis);
763-
List<String> lines = listener.getLines();
771+
List<String> lines = expectLinesWithLongTimeout(listener, delayMillis, 50);
764772
assertEquals(1, lines.size(), "1 line count");
765773
assertEquals("Line one", lines.get(0), "1 line 1");
766774
listener.clear();
767775

768776
// touch the file
777+
TestUtils.sleepTillNextFullSecond(); // ensure to be within the next second because of posix fs limitation
769778
file.setLastModified(System.currentTimeMillis());
770-
TestUtils.sleep(testDelayMillis);
771-
lines = listener.getLines();
779+
lines = expectLinesWithLongTimeout(listener, delayMillis, 20);
772780
assertEquals(1, lines.size(), "1 line count");
773781
assertEquals("Line one", lines.get(0), "1 line 1");
774782
listener.clear();

src/test/java/org/apache/commons/io/test/TestUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,19 @@ public static void sleep(final long millis) throws InterruptedException {
251251
ThreadUtils.sleep(Duration.ofMillis(millis));
252252
}
253253

254+
/**
255+
* Sleeps till the next full second.
256+
*
257+
* This method is useful when you want to set a guaranteed newer file system timestamp.
258+
* Posix file systems only guarantee one second resolution, and e.g.
259+
* some mac os x filesystems only offer that.
260+
*
261+
* @throws InterruptedException if interrupted.
262+
*/
263+
public static void sleepTillNextFullSecond() throws InterruptedException {
264+
sleep(1001 - (System.currentTimeMillis() % 1000));
265+
}
266+
254267
/**
255268
* Sleeps and swallows InterruptedException.
256269
*

0 commit comments

Comments
 (0)