Skip to content

Commit e4e9fc6

Browse files
committed
Added private and test-scoped sleep method that actually sleeps for a guaranteed minimal time
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1718945 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4d49a19 commit e4e9fc6

11 files changed

Lines changed: 123 additions & 108 deletions

File tree

src/main/java/org/apache/commons/io/ThreadMonitor.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,30 @@ private ThreadMonitor(final Thread thread, final long timeout) {
105105
*/
106106
public void run() {
107107
try {
108-
Thread.sleep(timeout);
108+
sleep(timeout);
109109
thread.interrupt();
110110
} catch (final InterruptedException e) {
111111
// timeout not reached
112112
}
113113
}
114+
115+
/**
116+
* Sleep for a guaranteed minimum number of milliseconds unless interrupted.
117+
*
118+
* This method exists because Thread.sleep(100) can sleep for 0, 70, 100 or 200ms or anything else
119+
* it deems appropriate. Read the docs on Thread.sleep for further interesting details.
120+
* @
121+
* @param ms the number of milliseconds to sleep for
122+
* @throws InterruptedException if interrupted
123+
*/
124+
private static void sleep(long ms) throws InterruptedException {
125+
long finishAt = System.currentTimeMillis() + ms;
126+
long remaining = ms;
127+
do {
128+
Thread.sleep(remaining);
129+
remaining = finishAt - System.currentTimeMillis();
130+
} while (remaining > 0);
131+
}
132+
133+
114134
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.commons.io.input.DemuxInputStream;
2929
import org.apache.commons.io.output.ByteArrayOutputStream;
3030
import org.apache.commons.io.output.DemuxOutputStream;
31+
import org.apache.commons.io.testtools.TestUtils;
3132
import org.junit.Test;
3233

3334
/**
@@ -165,7 +166,7 @@ public void run() {
165166
m_buffer.append((char) ch);
166167

167168
final int sleepTime = Math.abs(c_random.nextInt() % 10);
168-
Thread.sleep(sleepTime);
169+
TestUtils.sleep(sleepTime);
169170
ch = m_demux.read();
170171
}
171172
} catch (final Exception e) {
@@ -198,7 +199,7 @@ public void run() {
198199
//System.out.println( "Writing: " + (char)m_data[ i ] );
199200
m_demux.write(element);
200201
final int sleepTime = Math.abs(c_random.nextInt() % 10);
201-
Thread.sleep(sleepTime);
202+
TestUtils.sleep(sleepTime);
202203
} catch (final Exception e) {
203204
e.printStackTrace();
204205
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public void testFileCleanerExitWhenFinished2() throws Exception {
296296
assertFalse(theInstance.exitWhenFinished);
297297
theInstance.exitWhenFinished();
298298
for (int i = 0; i < 20 && theInstance.reaper.isAlive(); i++) {
299-
Thread.sleep(500L); // allow reaper thread to die
299+
TestUtils.sleep(500L); // allow reaper thread to die
300300
}
301301
assertTrue(theInstance.exitWhenFinished);
302302
assertFalse(theInstance.reaper.isAlive());
@@ -307,7 +307,7 @@ private void pauseForDeleteToComplete(File file) {
307307
int count = 0;
308308
while(file.exists() && count++ < 40) {
309309
try {
310-
Thread.sleep(500L);
310+
TestUtils.sleep(500L);
311311
} catch (final InterruptedException ignore) {
312312
}
313313
file = new File(file.getPath());
@@ -323,7 +323,7 @@ private String showFailures() throws Exception {
323323

324324
private void waitUntilTrackCount() throws Exception {
325325
System.gc();
326-
Thread.sleep(500);
326+
TestUtils.sleep(500);
327327
int count = 0;
328328
while(theInstance.getTrackCount() != 0 && count++ < 5) {
329329
List<String> list = new ArrayList<String>();
@@ -336,7 +336,7 @@ private void waitUntilTrackCount() throws Exception {
336336
}
337337
list = null;
338338
System.gc();
339-
Thread.sleep(1000);
339+
TestUtils.sleep(1000);
340340
}
341341
if (theInstance.getTrackCount() != 0) {
342342
throw new IllegalStateException("Your JVM is not releasing References, try running the testcase with less memory (-Xmx)");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ public void testIsFileNewerOlder() throws Exception {
10481048

10491049
do {
10501050
try {
1051-
Thread.sleep(1000);
1051+
TestUtils.sleep(1000);
10521052
} catch (final InterruptedException ie) {
10531053
// ignore
10541054
}
@@ -1070,7 +1070,7 @@ public void testIsFileNewerOlder() throws Exception {
10701070

10711071
do {
10721072
try {
1073-
Thread.sleep(1000);
1073+
TestUtils.sleep(1000);
10741074
} catch (final InterruptedException ie) {
10751075
// ignore
10761076
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.commons.io;
1818

19+
import org.apache.commons.io.testtools.TestUtils;
1920
import org.junit.Test;
2021

2122
import static org.junit.Assert.assertNull;
@@ -33,7 +34,7 @@ public class ThreadMonitorTestCase {
3334
public void testTimeout() {
3435
try {
3536
final Thread monitor = ThreadMonitor.start(100);
36-
Thread.sleep(200);
37+
TestUtils.sleep(200);
3738
ThreadMonitor.stop(monitor);
3839
fail("Expected InterruptedException");
3940
} catch (final InterruptedException e) {
@@ -48,7 +49,7 @@ public void testTimeout() {
4849
public void testCompletedWithoutTimeout() {
4950
try {
5051
final Thread monitor = ThreadMonitor.start(200);
51-
Thread.sleep(100);
52+
TestUtils.sleep(100);
5253
ThreadMonitor.stop(monitor);
5354
} catch (final InterruptedException e) {
5455
fail("Timed Out");
@@ -65,7 +66,7 @@ public void testNoTimeout() {
6566
try {
6667
final Thread monitor = ThreadMonitor.start(-1);
6768
assertNull("Timeout -1, Monitor should be null", monitor);
68-
Thread.sleep(100);
69+
TestUtils.sleep(100);
6970
ThreadMonitor.stop(monitor);
7071
} catch (final Exception e) {
7172
fail("Timeout -1, threw " + e);
@@ -75,7 +76,7 @@ public void testNoTimeout() {
7576
try {
7677
final Thread monitor = ThreadMonitor.start(0);
7778
assertNull("Timeout 0, Monitor should be null", monitor);
78-
Thread.sleep(100);
79+
TestUtils.sleep(100);
7980
ThreadMonitor.stop(monitor);
8081
} catch (final Exception e) {
8182
fail("Timeout 0, threw " + e);

src/test/java/org/apache/commons/io/comparator/LastModifiedFileComparatorTest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ public void setUp() throws Exception {
6161
IOUtils.closeQuietly(output1);
6262
}
6363
do {
64-
try {
65-
Thread.sleep(300);
66-
} catch(final InterruptedException ie) {
67-
// ignore
68-
}
64+
TestUtils.sleepQuietly(300);
6965
equalFile.setLastModified(System.currentTimeMillis());
7066
} while( olderFile.lastModified() == equalFile.lastModified() );
7167

@@ -82,11 +78,7 @@ public void setUp() throws Exception {
8278
IOUtils.closeQuietly(output);
8379
}
8480
do {
85-
try {
86-
Thread.sleep(300);
87-
} catch(final InterruptedException ie) {
88-
// ignore
89-
}
81+
TestUtils.sleepQuietly(300);
9082
newerFile.setLastModified(System.currentTimeMillis());
9183
} while( equalFile.lastModified() == newerFile.lastModified() );
9284
equalFile1 = equalFile;

src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ public void testAgeFilter() throws Exception {
822822

823823
do {
824824
try {
825-
Thread.sleep(1000);
825+
TestUtils.sleep(1000);
826826
} catch(final InterruptedException ie) {
827827
// ignore
828828
}
@@ -844,7 +844,7 @@ public void testAgeFilter() throws Exception {
844844

845845
do {
846846
try {
847-
Thread.sleep(1000);
847+
TestUtils.sleep(1000);
848848
} catch(final InterruptedException ie) {
849849
// ignore
850850
}

src/test/java/org/apache/commons/io/input/TailerTest.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public class TailerTest extends FileBasedTestCase {
5252
public void tearDown() throws Exception {
5353
if (tailer != null) {
5454
tailer.stop();
55-
Thread.sleep(1000);
55+
TestUtils.sleep(1000);
5656
}
5757
FileUtils.deleteDirectory(getTestDirectory());
58-
Thread.sleep(1000);
58+
TestUtils.sleep(1000);
5959
}
6060

6161
@Test
@@ -141,7 +141,7 @@ public void testMultiByteBreak() throws Exception {
141141
out.close(); // ensure data is written
142142

143143
final long testDelayMillis = delay * 10;
144-
Thread.sleep(testDelayMillis);
144+
TestUtils.sleep(testDelayMillis);
145145
List<String> tailerlines = listener.getLines();
146146
assertEquals("line count",lines.size(),tailerlines.size());
147147
for(int i = 0,len = lines.size();i<len;i++){
@@ -176,12 +176,12 @@ public void testTailerEof() throws Exception {
176176
try {
177177
writeString(file, "Line");
178178

179-
Thread.sleep(delay * 2);
179+
TestUtils.sleep(delay * 2);
180180
List<String> lines = listener.getLines();
181181
assertEquals("1 line count", 0, lines.size());
182182

183183
writeString(file, " one\n");
184-
Thread.sleep(delay * 2);
184+
TestUtils.sleep(delay * 2);
185185
lines = listener.getLines();
186186

187187
assertEquals("1 line count", 1, lines.size());
@@ -190,7 +190,7 @@ public void testTailerEof() throws Exception {
190190
listener.clear();
191191
} finally {
192192
tailer.stop();
193-
Thread.sleep(delay * 2);
193+
TestUtils.sleep(delay * 2);
194194
IOUtils.closeQuietly(writer);
195195
}
196196
}
@@ -212,7 +212,7 @@ public void testTailer() throws Exception {
212212
// Write some lines to the file
213213
write(file, "Line one", "Line two");
214214
final long testDelayMillis = delayMillis * 10;
215-
Thread.sleep(testDelayMillis);
215+
TestUtils.sleep(testDelayMillis);
216216
List<String> lines = listener.getLines();
217217
assertEquals("1 line count", 2, lines.size());
218218
assertEquals("1 line 1", "Line one", lines.get(0));
@@ -221,7 +221,7 @@ public void testTailer() throws Exception {
221221

222222
// Write another line to the file
223223
write(file, "Line three");
224-
Thread.sleep(testDelayMillis);
224+
TestUtils.sleep(testDelayMillis);
225225
lines = listener.getLines();
226226
assertEquals("2 line count", 1, lines.size());
227227
assertEquals("2 line 3", "Line three", lines.get(0));
@@ -239,11 +239,11 @@ public void testTailer() throws Exception {
239239
final boolean exists = file.exists();
240240
assertFalse("File should not exist", exists);
241241
createFile(file, 0);
242-
Thread.sleep(testDelayMillis);
242+
TestUtils.sleep(testDelayMillis);
243243

244244
// Write another line
245245
write(file, "Line four");
246-
Thread.sleep(testDelayMillis);
246+
TestUtils.sleep(testDelayMillis);
247247
lines = listener.getLines();
248248
assertEquals("4 line count", 1, lines.size());
249249
assertEquals("4 line 3", "Line four", lines.get(0));
@@ -253,7 +253,7 @@ public void testTailer() throws Exception {
253253
tailer.stop();
254254
tailer=null;
255255
thread.interrupt();
256-
Thread.sleep(testDelayMillis * 4);
256+
TestUtils.sleep(testDelayMillis * 4);
257257
write(file, "Line five");
258258
assertEquals("4 line count", 0, listener.getLines().size());
259259
assertNotNull("Missing InterruptedException", listener.exception);
@@ -279,15 +279,15 @@ public void testTailerEndOfFileReached() throws Exception {
279279

280280
// write a few lines
281281
write(file, "line1", "line2", "line3");
282-
Thread.sleep(testDelayMillis);
282+
TestUtils.sleep(testDelayMillis);
283283

284284
// write a few lines
285285
write(file, "line4", "line5", "line6");
286-
Thread.sleep(testDelayMillis);
286+
TestUtils.sleep(testDelayMillis);
287287

288288
// write a few lines
289289
write(file, "line7", "line8", "line9");
290-
Thread.sleep(testDelayMillis);
290+
TestUtils.sleep(testDelayMillis);
291291

292292
assertEquals("end of file reached 3 times", 3, listener.reachedEndOfFile);
293293
}
@@ -316,7 +316,7 @@ protected void createFile(final File file, final long size)
316316
} catch (final FileNotFoundException ignore) {
317317
}
318318
try {
319-
Thread.sleep(200L);
319+
TestUtils.sleep(200L);
320320
} catch (final InterruptedException ignore) {
321321
// ignore
322322
}
@@ -361,10 +361,10 @@ public void testStopWithNoFile() throws Exception {
361361
final int delay = 100;
362362
final int idle = 50; // allow time for thread to work
363363
tailer = Tailer.create(file, listener, delay, false);
364-
Thread.sleep(idle);
364+
TestUtils.sleep(idle);
365365
tailer.stop();
366366
tailer=null;
367-
Thread.sleep(delay+idle);
367+
TestUtils.sleep(delay+idle);
368368
assertNull("Should not generate Exception", listener.exception);
369369
assertEquals("Expected init to be called", 1 , listener.initialised);
370370
assertTrue("fileNotFound should be called", listener.notFound > 0);
@@ -387,10 +387,10 @@ public void testInterrupt() throws Exception {
387387
final Thread thread = new Thread(tailer);
388388
thread.setDaemon(true);
389389
thread.start();
390-
Thread.sleep(idle);
390+
TestUtils.sleep(idle);
391391
thread.interrupt();
392392
tailer = null;
393-
Thread.sleep(delay + idle);
393+
TestUtils.sleep(delay + idle);
394394
assertNotNull("Missing InterruptedException", listener.exception);
395395
assertTrue("Unexpected Exception: " + listener.exception, listener.exception instanceof InterruptedException);
396396
assertEquals("Expected init to be called", 1, listener.initialised);
@@ -409,10 +409,10 @@ public void testStopWithNoFileUsingExecutor() throws Exception {
409409
tailer = new Tailer(file, listener, delay, false);
410410
final Executor exec = new ScheduledThreadPoolExecutor(1);
411411
exec.execute(tailer);
412-
Thread.sleep(idle);
412+
TestUtils.sleep(idle);
413413
tailer.stop();
414414
tailer=null;
415-
Thread.sleep(delay+idle);
415+
TestUtils.sleep(delay+idle);
416416
assertNull("Should not generate Exception", listener.exception);
417417
assertEquals("Expected init to be called", 1 , listener.initialised);
418418
assertTrue("fileNotFound should be called", listener.notFound > 0);
@@ -434,7 +434,7 @@ public void testIO335() throws Exception { // test CR behaviour
434434
// Write some lines to the file
435435
writeString(file, "CRLF\r\n", "LF\n", "CR\r", "CRCR\r\r", "trail");
436436
final long testDelayMillis = delayMillis * 10;
437-
Thread.sleep(testDelayMillis);
437+
TestUtils.sleep(testDelayMillis);
438438
final List<String> lines = listener.getLines();
439439
assertEquals("line count", 4, lines.size());
440440
assertEquals("line 1", "CRLF", lines.get(0));
@@ -446,7 +446,7 @@ public void testIO335() throws Exception { // test CR behaviour
446446
tailer.stop();
447447
tailer=null;
448448
thread.interrupt();
449-
Thread.sleep(testDelayMillis);
449+
TestUtils.sleep(testDelayMillis);
450450
}
451451

452452
/**

0 commit comments

Comments
 (0)