diff --git a/src/test/java/org/codehaus/plexus/util/io/CachingOutputStreamTest.java b/src/test/java/org/codehaus/plexus/util/io/CachingOutputStreamTest.java index 6153e9e9..3c329ea9 100644 --- a/src/test/java/org/codehaus/plexus/util/io/CachingOutputStreamTest.java +++ b/src/test/java/org/codehaus/plexus/util/io/CachingOutputStreamTest.java @@ -17,11 +17,13 @@ */ import java.io.IOException; +import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.FileTime; +import java.util.Objects; import org.junit.Before; import org.junit.Test; @@ -36,6 +38,8 @@ public class CachingOutputStreamTest { Path tempDir; + Path checkLastModified; + FileTime lm; @Before public void setup() throws IOException @@ -43,6 +47,24 @@ public void setup() throws IOException Path dir = Paths.get( "target/io" ); Files.createDirectories( dir ); tempDir = Files.createTempDirectory( dir, "temp-" ); + checkLastModified = tempDir.resolve( ".check" ); + Files.newOutputStream( checkLastModified ).close(); + lm = Files.getLastModifiedTime( checkLastModified ); + } + + private void waitLastModified() throws IOException, InterruptedException + { + while ( true ) + { + Files.newOutputStream( checkLastModified ).close(); + FileTime nlm = Files.getLastModifiedTime( checkLastModified ); + if ( !Objects.equals( nlm, lm ) ) + { + lm = nlm; + break; + } + Thread.sleep( 10 ); + } } @Test @@ -61,7 +83,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException assertArrayEquals( data, read ); FileTime modified = Files.getLastModifiedTime( path ); - Thread.sleep( 250 ); + waitLastModified(); try ( CachingOutputStream cos = new CachingOutputStream( path, 4 ) ) { @@ -74,7 +96,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException assertEquals( modified, newModified ); modified = newModified; - Thread.sleep( 250 ); + waitLastModified(); // write longer data data = "Good morning!".getBytes( StandardCharsets.UTF_8 ); @@ -89,7 +111,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException assertNotEquals( modified, newModified ); modified = newModified; - Thread.sleep( 250 ); + waitLastModified(); // different data same size data = "Good mornong!".getBytes( StandardCharsets.UTF_8 ); @@ -104,7 +126,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException assertNotEquals( modified, newModified ); modified = newModified; - Thread.sleep( 250 ); + waitLastModified(); // same data but shorter data = "Good mornon".getBytes( StandardCharsets.UTF_8 ); @@ -119,4 +141,5 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException assertNotEquals( modified, newModified ); modified = newModified; } + } diff --git a/src/test/java/org/codehaus/plexus/util/io/CachingWriterTest.java b/src/test/java/org/codehaus/plexus/util/io/CachingWriterTest.java index 8ca76a6b..f5b95903 100644 --- a/src/test/java/org/codehaus/plexus/util/io/CachingWriterTest.java +++ b/src/test/java/org/codehaus/plexus/util/io/CachingWriterTest.java @@ -22,6 +22,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.FileTime; +import java.util.Objects; import org.junit.Before; import org.junit.Test; @@ -36,6 +37,8 @@ public class CachingWriterTest { Path tempDir; + Path checkLastModified; + FileTime lm; @Before public void setup() throws IOException @@ -43,6 +46,24 @@ public void setup() throws IOException Path dir = Paths.get( "target/io" ); Files.createDirectories( dir ); tempDir = Files.createTempDirectory( dir, "temp-" ); + checkLastModified = tempDir.resolve( ".check" ); + Files.newOutputStream( checkLastModified ).close(); + lm = Files.getLastModifiedTime( checkLastModified ); + } + + private void waitLastModified() throws IOException, InterruptedException + { + while ( true ) + { + Files.newOutputStream( checkLastModified ).close(); + FileTime nlm = Files.getLastModifiedTime( checkLastModified ); + if ( !Objects.equals( nlm, lm ) ) + { + lm = nlm; + break; + } + Thread.sleep( 10 ); + } } @Test @@ -61,7 +82,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException assertEquals( data, read ); FileTime modified = Files.getLastModifiedTime( path ); - Thread.sleep( 250 ); + waitLastModified(); try ( CachingWriter cos = new CachingWriter( path, StandardCharsets.UTF_8, 4 ) ) { @@ -74,7 +95,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException assertEquals( modified, newModified ); modified = newModified; - Thread.sleep( 250 ); + waitLastModified(); // write longer data data = "Good morning!"; @@ -89,7 +110,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException assertNotEquals( modified, newModified ); modified = newModified; - Thread.sleep( 250 ); + waitLastModified(); // different data same size data = "Good mornong!"; @@ -104,7 +125,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException assertNotEquals( modified, newModified ); modified = newModified; - Thread.sleep( 250 ); + waitLastModified(); // same data but shorter data = "Good mornon";