You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by kr...@apache.org on 2015/12/09 20:51:14 UTC

svn commit: r1718945 - in /commons/proper/io/trunk/src: main/java/org/apache/commons/io/ test/java/org/apache/commons/io/ test/java/org/apache/commons/io/comparator/ test/java/org/apache/commons/io/filefilter/ test/java/org/apache/commons/io/input/ tes...

Author: krosenvold
Date: Wed Dec  9 19:51:14 2015
New Revision: 1718945

URL: http://svn.apache.org/viewvc?rev=1718945&view=rev
Log:
Added private and test-scoped sleep method that actually sleeps for a guaranteed minimal time

Modified:
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/ThreadMonitor.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/DemuxTestCase.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileCleaningTrackerTestCase.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/ThreadMonitorTestCase.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/AbstractMonitorTestCase.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/testtools/TestUtils.java

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/ThreadMonitor.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/ThreadMonitor.java?rev=1718945&r1=1718944&r2=1718945&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/ThreadMonitor.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/ThreadMonitor.java Wed Dec  9 19:51:14 2015
@@ -105,10 +105,30 @@ class ThreadMonitor implements Runnable
      */
     public void run() {
         try {
-            Thread.sleep(timeout);
+            sleep(timeout);
             thread.interrupt();
         } catch (final InterruptedException e) {
             // timeout not reached
         }
     }
+
+    /**
+     * Sleep for a guaranteed minimum number of milliseconds unless interrupted.
+     *
+     * This method exists because Thread.sleep(100) can sleep for 0, 70, 100 or 200ms or anything else
+     * it deems appropriate. Read the docs on Thread.sleep for further interesting details.
+     * @
+     * @param ms the number of milliseconds to sleep for
+     * @throws InterruptedException if interrupted
+     */
+    private static void sleep(long ms) throws InterruptedException {
+        long finishAt = System.currentTimeMillis() + ms;
+        long remaining = ms;
+        do {
+            Thread.sleep(remaining);
+            remaining = finishAt - System.currentTimeMillis();
+        } while (remaining > 0);
+    }
+
+
 }
\ No newline at end of file

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/DemuxTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/DemuxTestCase.java?rev=1718945&r1=1718944&r2=1718945&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/DemuxTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/DemuxTestCase.java Wed Dec  9 19:51:14 2015
@@ -28,6 +28,7 @@ import java.util.Random;
 import org.apache.commons.io.input.DemuxInputStream;
 import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.apache.commons.io.output.DemuxOutputStream;
+import org.apache.commons.io.testtools.TestUtils;
 import org.junit.Test;
 
 /**
@@ -165,7 +166,7 @@ public class DemuxTestCase {
                     m_buffer.append((char) ch);
 
                     final int sleepTime = Math.abs(c_random.nextInt() % 10);
-                    Thread.sleep(sleepTime);
+                    TestUtils.sleep(sleepTime);
                     ch = m_demux.read();
                 }
             } catch (final Exception e) {
@@ -198,7 +199,7 @@ public class DemuxTestCase {
                     //System.out.println( "Writing: " + (char)m_data[ i ] );
                     m_demux.write(element);
                     final int sleepTime = Math.abs(c_random.nextInt() % 10);
-                    Thread.sleep(sleepTime);
+                    TestUtils.sleep(sleepTime);
                 } catch (final Exception e) {
                     e.printStackTrace();
                 }

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=1718945&r1=1718944&r2=1718945&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 Wed Dec  9 19:51:14 2015
@@ -296,7 +296,7 @@ public class FileCleaningTrackerTestCase
         assertFalse(theInstance.exitWhenFinished);
         theInstance.exitWhenFinished();
         for (int i = 0; i < 20 && theInstance.reaper.isAlive(); i++) {
-            Thread.sleep(500L);  // allow reaper thread to die
+            TestUtils.sleep(500L);  // allow reaper thread to die
         }
         assertTrue(theInstance.exitWhenFinished);
         assertFalse(theInstance.reaper.isAlive());
@@ -307,7 +307,7 @@ public class FileCleaningTrackerTestCase
         int count = 0;
         while(file.exists() && count++ < 40) {
             try {
-                Thread.sleep(500L);
+                TestUtils.sleep(500L);
             } catch (final InterruptedException ignore) {
             }
             file = new File(file.getPath());
@@ -323,7 +323,7 @@ public class FileCleaningTrackerTestCase
 
     private void waitUntilTrackCount() throws Exception {
         System.gc();
-        Thread.sleep(500);
+        TestUtils.sleep(500);
         int count = 0;
         while(theInstance.getTrackCount() != 0 && count++ < 5) {
             List<String> list = new ArrayList<String>();
@@ -336,7 +336,7 @@ public class FileCleaningTrackerTestCase
             }
             list = null;
             System.gc();
-            Thread.sleep(1000);
+            TestUtils.sleep(1000);
         }
         if (theInstance.getTrackCount() != 0) {
             throw new IllegalStateException("Your JVM is not releasing References, try running the testcase with less memory (-Xmx)");

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java?rev=1718945&r1=1718944&r2=1718945&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Wed Dec  9 19:51:14 2015
@@ -1048,7 +1048,7 @@ public class FileUtilsTestCase extends F
 
         do {
             try {
-                Thread.sleep(1000);
+                TestUtils.sleep(1000);
             } catch (final InterruptedException ie) {
                 // ignore
             }
@@ -1070,7 +1070,7 @@ public class FileUtilsTestCase extends F
 
         do {
             try {
-                Thread.sleep(1000);
+                TestUtils.sleep(1000);
             } catch (final InterruptedException ie) {
                 // ignore
             }

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/ThreadMonitorTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/ThreadMonitorTestCase.java?rev=1718945&r1=1718944&r2=1718945&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/ThreadMonitorTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/ThreadMonitorTestCase.java Wed Dec  9 19:51:14 2015
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.io;
 
+import org.apache.commons.io.testtools.TestUtils;
 import org.junit.Test;
 
 import static org.junit.Assert.assertNull;
@@ -33,7 +34,7 @@ public class ThreadMonitorTestCase {
     public void testTimeout() {
         try {
             final Thread monitor = ThreadMonitor.start(100);
-            Thread.sleep(200);
+            TestUtils.sleep(200);
             ThreadMonitor.stop(monitor);
             fail("Expected InterruptedException");
         } catch (final InterruptedException e) {
@@ -48,7 +49,7 @@ public class ThreadMonitorTestCase {
     public void testCompletedWithoutTimeout() {
         try {
             final Thread monitor = ThreadMonitor.start(200);
-            Thread.sleep(100);
+            TestUtils.sleep(100);
             ThreadMonitor.stop(monitor);
         } catch (final InterruptedException e) {
             fail("Timed Out");
@@ -65,7 +66,7 @@ public class ThreadMonitorTestCase {
         try {
             final Thread monitor = ThreadMonitor.start(-1);
             assertNull("Timeout -1, Monitor should be null", monitor);
-            Thread.sleep(100);
+            TestUtils.sleep(100);
             ThreadMonitor.stop(monitor);
         } catch (final Exception e) {
             fail("Timeout -1, threw " + e);
@@ -75,7 +76,7 @@ public class ThreadMonitorTestCase {
         try {
             final Thread monitor = ThreadMonitor.start(0);
             assertNull("Timeout 0, Monitor should be null", monitor);
-            Thread.sleep(100);
+            TestUtils.sleep(100);
             ThreadMonitor.stop(monitor);
         } catch (final Exception e) {
             fail("Timeout 0, threw " + e);

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java?rev=1718945&r1=1718944&r2=1718945&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java Wed Dec  9 19:51:14 2015
@@ -61,11 +61,7 @@ public class LastModifiedFileComparatorT
             IOUtils.closeQuietly(output1);
         }
         do {
-            try { 
-                Thread.sleep(300);
-            } catch(final InterruptedException ie) {
-                // ignore
-            }
+            TestUtils.sleepQuietly(300);
             equalFile.setLastModified(System.currentTimeMillis());
         } while( olderFile.lastModified() == equalFile.lastModified() );
 
@@ -82,11 +78,7 @@ public class LastModifiedFileComparatorT
             IOUtils.closeQuietly(output);
         }
         do {
-            try { 
-                Thread.sleep(300);
-            } catch(final InterruptedException ie) {
-                // ignore
-            }
+            TestUtils.sleepQuietly(300);
             newerFile.setLastModified(System.currentTimeMillis());
         } while( equalFile.lastModified() == newerFile.lastModified() );
         equalFile1 = equalFile;

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java?rev=1718945&r1=1718944&r2=1718945&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java Wed Dec  9 19:51:14 2015
@@ -822,7 +822,7 @@ public class FileFilterTestCase extends
 
         do {
             try {
-                Thread.sleep(1000);
+                TestUtils.sleep(1000);
             } catch(final InterruptedException ie) {
                 // ignore
             }
@@ -844,7 +844,7 @@ public class FileFilterTestCase extends
 
         do {
             try {
-                Thread.sleep(1000);
+                TestUtils.sleep(1000);
             } catch(final InterruptedException ie) {
                 // ignore
             }

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java?rev=1718945&r1=1718944&r2=1718945&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java Wed Dec  9 19:51:14 2015
@@ -52,10 +52,10 @@ public class TailerTest extends FileBase
     public void tearDown() throws Exception {
         if (tailer != null) {
             tailer.stop();
-            Thread.sleep(1000);
+            TestUtils.sleep(1000);
         }
         FileUtils.deleteDirectory(getTestDirectory());
-        Thread.sleep(1000);
+        TestUtils.sleep(1000);
     }
 
     @Test
@@ -141,7 +141,7 @@ public class TailerTest extends FileBase
             out.close(); // ensure data is written
 
            final long testDelayMillis = delay * 10;
-           Thread.sleep(testDelayMillis);
+            TestUtils.sleep(testDelayMillis);
            List<String> tailerlines = listener.getLines();
            assertEquals("line count",lines.size(),tailerlines.size());
            for(int i = 0,len = lines.size();i<len;i++){
@@ -176,12 +176,12 @@ public class TailerTest extends FileBase
         try {
             writeString(file, "Line");
 
-            Thread.sleep(delay * 2);
+            TestUtils.sleep(delay * 2);
             List<String> lines = listener.getLines();
             assertEquals("1 line count", 0, lines.size());
 
             writeString(file, " one\n");
-            Thread.sleep(delay * 2);
+            TestUtils.sleep(delay * 2);
             lines = listener.getLines();
 
             assertEquals("1 line count", 1, lines.size());
@@ -190,7 +190,7 @@ public class TailerTest extends FileBase
             listener.clear();
         } finally {
             tailer.stop();
-            Thread.sleep(delay * 2);
+            TestUtils.sleep(delay * 2);
             IOUtils.closeQuietly(writer);
         }
     }
@@ -212,7 +212,7 @@ public class TailerTest extends FileBase
         // Write some lines to the file
         write(file, "Line one", "Line two");
         final long testDelayMillis = delayMillis * 10;
-        Thread.sleep(testDelayMillis);
+        TestUtils.sleep(testDelayMillis);
         List<String> lines = listener.getLines();
         assertEquals("1 line count", 2, lines.size());
         assertEquals("1 line 1", "Line one", lines.get(0));
@@ -221,7 +221,7 @@ public class TailerTest extends FileBase
 
         // Write another line to the file
         write(file, "Line three");
-        Thread.sleep(testDelayMillis);
+        TestUtils.sleep(testDelayMillis);
         lines = listener.getLines();
         assertEquals("2 line count", 1, lines.size());
         assertEquals("2 line 3", "Line three", lines.get(0));
@@ -239,11 +239,11 @@ public class TailerTest extends FileBase
         final boolean exists = file.exists();
         assertFalse("File should not exist", exists);
         createFile(file, 0);
-        Thread.sleep(testDelayMillis);
+        TestUtils.sleep(testDelayMillis);
 
         // Write another line
         write(file, "Line four");
-        Thread.sleep(testDelayMillis);
+        TestUtils.sleep(testDelayMillis);
         lines = listener.getLines();
         assertEquals("4 line count", 1, lines.size());
         assertEquals("4 line 3", "Line four", lines.get(0));
@@ -253,7 +253,7 @@ public class TailerTest extends FileBase
         tailer.stop();
         tailer=null;
         thread.interrupt();
-        Thread.sleep(testDelayMillis * 4);
+        TestUtils.sleep(testDelayMillis * 4);
         write(file, "Line five");
         assertEquals("4 line count", 0, listener.getLines().size());
         assertNotNull("Missing InterruptedException", listener.exception);
@@ -279,15 +279,15 @@ public class TailerTest extends FileBase
 
         // write a few lines
         write(file, "line1", "line2", "line3");
-        Thread.sleep(testDelayMillis);
+        TestUtils.sleep(testDelayMillis);
 
         // write a few lines
         write(file, "line4", "line5", "line6");
-        Thread.sleep(testDelayMillis);
+        TestUtils.sleep(testDelayMillis);
 
         // write a few lines
         write(file, "line7", "line8", "line9");
-        Thread.sleep(testDelayMillis);
+        TestUtils.sleep(testDelayMillis);
 
         assertEquals("end of file reached 3 times", 3, listener.reachedEndOfFile);
     }
@@ -316,7 +316,7 @@ public class TailerTest extends FileBase
                 } catch (final FileNotFoundException ignore) {
                 }
                 try {
-                    Thread.sleep(200L);
+                    TestUtils.sleep(200L);
                 } catch (final InterruptedException ignore) {
                     // ignore
                 }
@@ -361,10 +361,10 @@ public class TailerTest extends FileBase
         final int delay = 100;
         final int idle = 50; // allow time for thread to work
         tailer = Tailer.create(file, listener, delay, false);
-        Thread.sleep(idle);
+        TestUtils.sleep(idle);
         tailer.stop();
         tailer=null;
-        Thread.sleep(delay+idle);
+        TestUtils.sleep(delay+idle);
         assertNull("Should not generate Exception", listener.exception);
         assertEquals("Expected init to be called", 1 , listener.initialised);
         assertTrue("fileNotFound should be called", listener.notFound > 0);
@@ -387,10 +387,10 @@ public class TailerTest extends FileBase
         final Thread thread = new Thread(tailer);
         thread.setDaemon(true);
         thread.start();
-        Thread.sleep(idle);
+        TestUtils.sleep(idle);
         thread.interrupt();
         tailer = null;
-        Thread.sleep(delay + idle);
+        TestUtils.sleep(delay + idle);
         assertNotNull("Missing InterruptedException", listener.exception);
         assertTrue("Unexpected Exception: " + listener.exception, listener.exception instanceof InterruptedException);
         assertEquals("Expected init to be called", 1, listener.initialised);
@@ -409,10 +409,10 @@ public class TailerTest extends FileBase
         tailer = new Tailer(file, listener, delay, false);
         final Executor exec = new ScheduledThreadPoolExecutor(1);
         exec.execute(tailer);
-        Thread.sleep(idle);
+        TestUtils.sleep(idle);
         tailer.stop();
         tailer=null;
-        Thread.sleep(delay+idle);
+        TestUtils.sleep(delay+idle);
         assertNull("Should not generate Exception", listener.exception);
         assertEquals("Expected init to be called", 1 , listener.initialised);
         assertTrue("fileNotFound should be called", listener.notFound > 0);
@@ -434,7 +434,7 @@ public class TailerTest extends FileBase
         // Write some lines to the file
         writeString(file, "CRLF\r\n", "LF\n", "CR\r", "CRCR\r\r", "trail");
         final long testDelayMillis = delayMillis * 10;
-        Thread.sleep(testDelayMillis);
+        TestUtils.sleep(testDelayMillis);
         final List<String> lines = listener.getLines();
         assertEquals("line count", 4, lines.size());
         assertEquals("line 1", "CRLF", lines.get(0));
@@ -446,7 +446,7 @@ public class TailerTest extends FileBase
         tailer.stop();
         tailer=null;
         thread.interrupt();
-        Thread.sleep(testDelayMillis);
+        TestUtils.sleep(testDelayMillis);
     }
 
     /**

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/AbstractMonitorTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/AbstractMonitorTestCase.java?rev=1718945&r1=1718944&r2=1718945&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/AbstractMonitorTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/AbstractMonitorTestCase.java Wed Dec  9 19:51:14 2015
@@ -19,15 +19,15 @@ package org.apache.commons.io.monitor;
 import java.io.File;
 import java.io.FileFilter;
 
-import junit.framework.TestCase;
-
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.filefilter.FileFilterUtils;
 import org.apache.commons.io.filefilter.HiddenFileFilter;
 import org.apache.commons.io.filefilter.IOFileFilter;
+import org.apache.commons.io.testtools.TestUtils;
 import org.junit.After;
 import org.junit.Before;
 
+import static org.apache.commons.io.testtools.TestUtils.sleepQuietly;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
@@ -149,27 +149,15 @@ public abstract class AbstractMonitorTes
             FileUtils.touch(file);
             file = new File(file.getParent(), file.getName());
             while (lastModified == file.lastModified()) {
-                sleepHandleInterruped(pauseTime);
+                sleepQuietly(pauseTime);
                 FileUtils.touch(file);
                 file = new File(file.getParent(), file.getName());
             }
         } catch (final Exception e) {
             fail("Touching " + file + ": " + e);
         }
-        sleepHandleInterruped(pauseTime);
+        sleepQuietly(pauseTime);
         return file;
     }
 
-    /**
-     * Thread.sleep(timeInMilliseconds) - ignore InterruptedException
-     * 
-     * @param timeInMilliseconds the time to sleep
-     */
-    protected void sleepHandleInterruped(final long timeInMilliseconds) {
-        try {
-            Thread.sleep(timeInMilliseconds);
-        } catch(final InterruptedException ie) {
-            // ignore
-        }
-    }
 }

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java?rev=1718945&r1=1718944&r2=1718945&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/monitor/FileAlterationMonitorTestCase.java Wed Dec  9 19:51:14 2015
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.io.monitor;
 
+import org.apache.commons.io.testtools.TestUtils;
 import org.junit.Test;
 
 import java.io.File;
@@ -179,7 +180,7 @@ public class FileAlterationMonitorTestCa
             if (files.contains(file)) {
                 return; // found, test passes
             }
-            sleepHandleInterruped(pauseTime);
+            TestUtils.sleepQuietly(pauseTime);
         }
         fail(label + " " + file + " not found");
     }

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/testtools/TestUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/testtools/TestUtils.java?rev=1718945&r1=1718944&r2=1718945&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/testtools/TestUtils.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/testtools/TestUtils.java Wed Dec  9 19:51:14 2015
@@ -16,31 +16,21 @@
  */
 package org.apache.commons.io.testtools;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
-import java.io.Reader;
-import java.io.Writer;
-import java.util.Arrays;
-
 import junit.framework.AssertionFailedError;
-
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.output.ByteArrayOutputStream;
 
+import java.io.*;
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
 /**
  * Base class for testcases doing tests with files.
  */
-public abstract class TestUtils  {
+public abstract class TestUtils {
 
     private TestUtils() {
 
@@ -77,7 +67,7 @@ public abstract class TestUtils  {
             //output.write((byte)'X');
 
             // nice varied byte pattern compatible with Readers and Writers
-            out.write( (byte)( (i % 127) + 1) );
+            out.write((byte) ((i % 127) + 1));
         }
     }
 
@@ -96,7 +86,7 @@ public abstract class TestUtils  {
     }
 
     public static File newFile(File testDirectory, final String filename) throws IOException {
-        final File destination = new File( testDirectory, filename );
+        final File destination = new File(testDirectory, filename);
         /*
         assertTrue( filename + "Test output data file shouldn't previously exist",
                     !destination.exists() );
@@ -107,41 +97,41 @@ public abstract class TestUtils  {
         return destination;
     }
 
-    public static void checkFile( final File file, final File referenceFile )
+    public static void checkFile(final File file, final File referenceFile)
             throws Exception {
-        assertTrue( "Check existence of output file", file.exists() );
-        assertEqualContent( referenceFile, file );
+        assertTrue("Check existence of output file", file.exists());
+        assertEqualContent(referenceFile, file);
     }
 
-    /** Assert that the content of two files is the same. */
-    private static void assertEqualContent( final File f0, final File f1 )
-            throws IOException
-    {
+    /**
+     * Assert that the content of two files is the same.
+     */
+    private static void assertEqualContent(final File f0, final File f1)
+            throws IOException {
         /* This doesn't work because the filesize isn't updated until the file
          * is closed.
         assertTrue( "The files " + f0 + " and " + f1 +
                     " have differing file sizes (" + f0.length() +
                     " vs " + f1.length() + ")", ( f0.length() == f1.length() ) );
         */
-        final InputStream is0 = new java.io.FileInputStream( f0 );
+        final InputStream is0 = new java.io.FileInputStream(f0);
         try {
-            final InputStream is1 = new java.io.FileInputStream( f1 );
+            final InputStream is1 = new java.io.FileInputStream(f1);
             try {
-                final byte[] buf0 = new byte[ 1024 ];
-                final byte[] buf1 = new byte[ 1024 ];
+                final byte[] buf0 = new byte[1024];
+                final byte[] buf1 = new byte[1024];
                 int n0 = 0;
                 int n1;
 
-                while( -1 != n0 )
-                {
-                    n0 = is0.read( buf0 );
-                    n1 = is1.read( buf1 );
-                    assertTrue( "The files " + f0 + " and " + f1 +
+                while (-1 != n0) {
+                    n0 = is0.read(buf0);
+                    n1 = is1.read(buf1);
+                    assertTrue("The files " + f0 + " and " + f1 +
                             " have differing number of bytes available (" + n0 +
-                            " vs " + n1 + ")", ( n0 == n1 ) );
+                            " vs " + n1 + ")", (n0 == n1));
 
-                    assertTrue( "The files " + f0 + " and " + f1 +
-                            " have different content", Arrays.equals( buf0, buf1 ) );
+                    assertTrue("The files " + f0 + " and " + f1 +
+                            " have different content", Arrays.equals(buf0, buf1));
                 }
             } finally {
                 is1.close();
@@ -154,9 +144,8 @@ public abstract class TestUtils  {
     /**
      * Assert that the content of a file is equal to that in a byte[].
      *
-     * @param b0 the expected contents
+     * @param b0   the expected contents
      * @param file the file to check
-     *
      * @throws IOException If an I/O error occurs while reading the file contents
      */
     public static void assertEqualContent(final byte[] b0, final File file) throws IOException {
@@ -180,9 +169,8 @@ public abstract class TestUtils  {
     /**
      * Assert that the content of a file is equal to that in a char[].
      *
-     * @param c0 the expected contents
+     * @param c0   the expected contents
      * @param file the file to check
-     *
      * @throws IOException If an I/O error occurs while reading the file contents
      */
     public static void assertEqualContent(final char[] c0, final File file) throws IOException {
@@ -225,12 +213,36 @@ public abstract class TestUtils  {
         }
     }
 
-    public static void deleteFile( final File file )
+    public static void deleteFile(final File file)
             throws Exception {
         if (file.exists()) {
             assertTrue("Couldn't delete file: " + file, file.delete());
         }
     }
 
+    /**
+     * Sleep for a guaranteed number of milliseconds unless interrupted.
+     *
+     * This method exists because Thread.sleep(100) can sleep for 0, 70, 100 or 200ms or anything else
+     * it deems appropriate. Read the docs on Thread.sleep for further details.
+     * @
+     * @param ms the number of milliseconds to sleep for
+     * @throws InterruptedException if interrupted
+     */
+    public static void sleep(long ms) throws InterruptedException {
+        long finishAt = System.currentTimeMillis() + ms;
+        long remaining = ms;
+        do {
+            Thread.sleep(remaining);
+            remaining = finishAt - System.currentTimeMillis();
+        } while (remaining > 0);
+    }
+
+    public static void sleepQuietly(long ms) {
+        try {
+            sleep(ms);
+        } catch (InterruptedException ignored){
+        }
+    }
 
 }