17
17
*/
18
18
19
19
import java .io .IOException ;
20
+ import java .io .OutputStream ;
20
21
import java .nio .charset .StandardCharsets ;
21
22
import java .nio .file .Files ;
22
23
import java .nio .file .Path ;
23
24
import java .nio .file .Paths ;
24
25
import java .nio .file .attribute .FileTime ;
26
+ import java .util .Objects ;
25
27
26
28
import org .junit .Before ;
27
29
import org .junit .Test ;
@@ -36,13 +38,33 @@ public class CachingOutputStreamTest
36
38
{
37
39
38
40
Path tempDir ;
41
+ Path checkLastModified ;
42
+ FileTime lm ;
39
43
40
44
@ Before
41
45
public void setup () throws IOException
42
46
{
43
47
Path dir = Paths .get ( "target/io" );
44
48
Files .createDirectories ( dir );
45
49
tempDir = Files .createTempDirectory ( dir , "temp-" );
50
+ checkLastModified = tempDir .resolve ( ".check" );
51
+ Files .newOutputStream ( checkLastModified ).close ();
52
+ lm = Files .getLastModifiedTime ( checkLastModified );
53
+ }
54
+
55
+ private void waitLastModified () throws IOException , InterruptedException
56
+ {
57
+ while ( true )
58
+ {
59
+ Files .newOutputStream ( checkLastModified ).close ();
60
+ FileTime nlm = Files .getLastModifiedTime ( checkLastModified );
61
+ if ( !Objects .equals ( nlm , lm ) )
62
+ {
63
+ lm = nlm ;
64
+ break ;
65
+ }
66
+ Thread .sleep ( 10 );
67
+ }
46
68
}
47
69
48
70
@ Test
@@ -61,7 +83,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
61
83
assertArrayEquals ( data , read );
62
84
FileTime modified = Files .getLastModifiedTime ( path );
63
85
64
- Thread . sleep ( 250 );
86
+ waitLastModified ( );
65
87
66
88
try ( CachingOutputStream cos = new CachingOutputStream ( path , 4 ) )
67
89
{
@@ -74,7 +96,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
74
96
assertEquals ( modified , newModified );
75
97
modified = newModified ;
76
98
77
- Thread . sleep ( 250 );
99
+ waitLastModified ( );
78
100
79
101
// write longer data
80
102
data = "Good morning!" .getBytes ( StandardCharsets .UTF_8 );
@@ -89,7 +111,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
89
111
assertNotEquals ( modified , newModified );
90
112
modified = newModified ;
91
113
92
- Thread . sleep ( 250 );
114
+ waitLastModified ( );
93
115
94
116
// different data same size
95
117
data = "Good mornong!" .getBytes ( StandardCharsets .UTF_8 );
@@ -104,7 +126,7 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
104
126
assertNotEquals ( modified , newModified );
105
127
modified = newModified ;
106
128
107
- Thread . sleep ( 250 );
129
+ waitLastModified ( );
108
130
109
131
// same data but shorter
110
132
data = "Good mornon" .getBytes ( StandardCharsets .UTF_8 );
@@ -119,4 +141,5 @@ public void testWriteNoExistingFile() throws IOException, InterruptedException
119
141
assertNotEquals ( modified , newModified );
120
142
modified = newModified ;
121
143
}
144
+
122
145
}
0 commit comments