commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r1005707 - in /commons/proper/io/trunk/src: main/java/org/apache/commons/io/FileCleaningTracker.java test/java/org/apache/commons/io/FileCleaningTrackerTestCase.java
Date Fri, 08 Oct 2010 04:02:57 GMT
Author: niallp
Date: Fri Oct  8 04:02:57 2010
New Revision: 1005707

URL: http://svn.apache.org/viewvc?rev=1005707&view=rev
Log:
Fix to resolve OutOfMemoryError has now caused another issue. Try adding a *pause* to see
if additional times allows the file delete to complete. If not provide a facility to show
that the delete failed.

Modified:
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileCleaningTracker.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileCleaningTrackerTestCase.java

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileCleaningTracker.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileCleaningTracker.java?rev=1005707&r1=1005706&r2=1005707&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileCleaningTracker.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileCleaningTracker.java Fri
Oct  8 04:02:57 2010
@@ -19,9 +19,11 @@ package org.apache.commons.io;
 import java.io.File;
 import java.lang.ref.PhantomReference;
 import java.lang.ref.ReferenceQueue;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.List;
 
 /**
  * Keeps track of files awaiting deletion, and deletes them when an associated
@@ -51,6 +53,10 @@ public class FileCleaningTracker {
      */
     final Collection<Tracker> trackers = Collections.synchronizedSet(new HashSet<Tracker>());
// synchronized
     /**
+     * Collection of File paths that failed to delete.
+     */
+    final List<String> deleteFailures = Collections.synchronizedList(new ArrayList<String>());
+    /**
      * Whether to terminate the thread when the tracking is complete.
      */
     volatile boolean exitWhenFinished = false;
@@ -151,6 +157,16 @@ public class FileCleaningTracker {
     }
 
     /**
+     * Return the file paths that failed to delete.
+     *
+     * @return the file paths that failed to delete
+     * @since Commons IO 2.0
+     */
+    public List<String> getDeleteFailures() {
+        return deleteFailures;
+    }
+
+    /**
      * Call this method to cause the file cleaner thread to terminate when
      * there are no more objects being tracked for deletion.
      * <p>
@@ -205,7 +221,9 @@ public class FileCleaningTracker {
                     // Wait for a tracker to remove.
                     Tracker tracker = (Tracker) q.remove(); // cannot return null
                     trackers.remove(tracker);
-                    tracker.delete();
+                    if (!tracker.delete()) {
+                        deleteFailures.add(tracker.getPath());
+                    }
                     tracker.clear();
                 } catch (InterruptedException e) {
                     continue;
@@ -244,6 +262,15 @@ public class FileCleaningTracker {
         }
 
         /**
+         * Return the path.
+         *
+         * @return the path
+         */
+        public String getPath() {
+            return path;
+        }
+
+        /**
          * Deletes the file associated with this tracker instance.
          *
          * @return <code>true</code> if the file was deleted successfully;

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileCleaningTrackerTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileCleaningTrackerTestCase.java?rev=1005707&r1=1005706&r2=1005707&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileCleaningTrackerTestCase.java
(original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileCleaningTrackerTestCase.java
Fri Oct  8 04:02:57 2010
@@ -70,6 +70,7 @@ public class FileCleaningTrackerTestCase
         {
             theInstance.q = new ReferenceQueue<Object>();
             theInstance.trackers.clear();
+            theInstance.deleteFailures.clear();
             theInstance.exitWhenFinished = false;
             theInstance.reaper = null;
         }
@@ -94,9 +95,10 @@ public class FileCleaningTrackerTestCase
         r = null;
 
         waitUntilTrackCount();
+        pauseForDeleteToComplete(new File(path));
         
         assertEquals(0, theInstance.getTrackCount());
-        assertEquals(false, new File(path).exists());
+        assertEquals(showFailures(), false, new File(path).exists());
     }
 
     public void testFileCleanerDirectory() throws Exception {
@@ -150,10 +152,11 @@ public class FileCleaningTrackerTestCase
         obj = null;
 
         waitUntilTrackCount();
+        pauseForDeleteToComplete(testFile.getParentFile());
         
         assertEquals(0, theInstance.getTrackCount());
-        assertEquals(false, testFile.exists());
-        assertEquals(false, testFile.getParentFile().exists());
+        assertEquals(showFailures(), false, testFile.exists());
+        assertEquals(showFailures(), false, testFile.getParentFile().exists());
     }
 
     public void testFileCleanerNull() throws Exception {
@@ -237,9 +240,10 @@ public class FileCleaningTrackerTestCase
         r = null;
 
         waitUntilTrackCount();
+        pauseForDeleteToComplete(new File(path));
         
         assertEquals("10-Track Count", 0, theInstance.getTrackCount());
-        assertEquals("11-testFile exists", false, new File(path).exists());
+        assertEquals("11-testFile exists " + showFailures(), false, new File(path).exists());
         assertEquals("12-exitWhenFinished", true, theInstance.exitWhenFinished);
         assertEquals("13-reaper.isAlive", false, theInstance.reaper.isAlive());
     }
@@ -262,9 +266,10 @@ public class FileCleaningTrackerTestCase
         r = null;
 
         waitUntilTrackCount();
+        pauseForDeleteToComplete(new File(path));
         
         assertEquals(0, theInstance.getTrackCount());
-        assertEquals(false, new File(path).exists());
+        assertEquals(showFailures(), false, new File(path).exists());
         assertEquals(false, theInstance.exitWhenFinished);
         assertEquals(true, theInstance.reaper.isAlive());
         
@@ -278,6 +283,16 @@ public class FileCleaningTrackerTestCase
     }
 
     //-----------------------------------------------------------------------
+    private void pauseForDeleteToComplete(File file) throws Exception {
+        int count = 0;
+        while(file.exists() && count++ < 20) {
+            Thread.sleep(500L);
+        }
+    }
+    private String showFailures() throws Exception {
+        return "Failed to delete " + theInstance.deleteFailures.size() + " files";
+    }
+
     private void waitUntilTrackCount() throws Exception {
         int count = 0;
         while(theInstance.getTrackCount() != 0 && count++ < 5) {



Mime
View raw message