@@ -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