Skip to content

Commit 7df4f8c

Browse files
committed
Fixing #IO-82, in JDK 1.6 the gc call does not seem to be giving us the garbage collection, so Stephen came up with this to force that to happen. Speed seems good in JDK 1.4->1.6 and happily this isn't an issue with the underlying source, just the test.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@463310 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4147a50 commit 7df4f8c

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ public void testFileCleanerFile() throws Exception {
7979
r.close();
8080
testFile = null;
8181
r = null;
82-
while (FileCleaner.getTrackCount() != 0) {
83-
System.gc();
84-
}
82+
83+
waitUntilTrackCount();
8584

8685
assertEquals(0, FileCleaner.getTrackCount());
8786
assertEquals(false, new File(path).exists());
@@ -98,10 +97,9 @@ public void testFileCleanerDirectory() throws Exception {
9897
assertEquals(1, FileCleaner.getTrackCount());
9998

10099
obj = null;
101-
while (FileCleaner.getTrackCount() != 0) {
102-
System.gc();
103-
}
104-
100+
101+
waitUntilTrackCount();
102+
105103
assertEquals(0, FileCleaner.getTrackCount());
106104
assertEquals(true, testFile.exists()); // not deleted, as dir not empty
107105
assertEquals(true, testFile.getParentFile().exists()); // not deleted, as dir not empty
@@ -118,9 +116,8 @@ public void testFileCleanerDirectory_NullStrategy() throws Exception {
118116
assertEquals(1, FileCleaner.getTrackCount());
119117

120118
obj = null;
121-
while (FileCleaner.getTrackCount() != 0) {
122-
System.gc();
123-
}
119+
120+
waitUntilTrackCount();
124121

125122
assertEquals(0, FileCleaner.getTrackCount());
126123
assertEquals(true, testFile.exists()); // not deleted, as dir not empty
@@ -138,9 +135,8 @@ public void testFileCleanerDirectory_ForceStrategy() throws Exception {
138135
assertEquals(1, FileCleaner.getTrackCount());
139136

140137
obj = null;
141-
while (FileCleaner.getTrackCount() != 0) {
142-
System.gc();
143-
}
138+
139+
waitUntilTrackCount();
144140

145141
assertEquals(0, FileCleaner.getTrackCount());
146142
assertEquals(false, testFile.exists());
@@ -174,4 +170,15 @@ public void testFileCleanerNull() throws Exception {
174170
}
175171
}
176172

173+
private void waitUntilTrackCount() {
174+
while (FileCleaner.getTrackCount() != 0) {
175+
int total = 0;
176+
while (FileCleaner.getTrackCount() != 0) {
177+
byte[] b = new byte[1024 * 1024];
178+
b[0] = (byte) System.currentTimeMillis();
179+
total = total + b[0];
180+
System.gc();
181+
}
182+
}
183+
}
177184
}

0 commit comments

Comments
 (0)