Skip to content

Commit 7202d09

Browse files
author
Gary Gregory
committed
Avoid Code Duplication: Reuse Sleep from ThreadMonitor.
- Redo changes from PR #66 from DaGeRe. - Use sleepQuietly.
1 parent 704e9d6 commit 7202d09

5 files changed

Lines changed: 22 additions & 22 deletions

File tree

src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ The <action> type attribute can be add,update,fix,remove.
8080
<action issue="IO-749" dev="ggregory" type="fix" due-to="haihuiyang, Gary Gregory">
8181
FileUtils.listFiles() does not list matching files if File parameter is a symbolic link.
8282
</action>
83-
<action issue="IO-749" dev="ggregory" type="fix" due-to="niranjanghule, Gary Gregory">
83+
<action dev="ggregory" type="fix" due-to="niranjanghule, Gary Gregory">
8484
Fix typo in Javadocs for FileUtils#convertFileCollectionToFileArray() #276.
8585
</action>
86+
<action dev="ggregory" type="fix" due-to="DaGeRe, Gary Gregory">
87+
Avoid Code Duplication: Reuse Sleep from ThreadMonitor #66.
88+
</action>
8689
<!-- ADD -->
8790
<action dev="ggregory" type="add" due-to="Gary Gregory">
8891
Add BrokenReader.INSTANCE.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void run() {
117117
* @param duration the sleep duration.
118118
* @throws InterruptedException if interrupted.
119119
*/
120-
private static void sleep(final Duration duration) throws InterruptedException {
120+
static void sleep(final Duration duration) throws InterruptedException {
121121
final Instant finishInstant = Instant.now().plus(duration);
122122
Duration remainingDuration = duration;
123123
do {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public void run() {
6464
//System.out.println( "Reading: " + (char)ch );
6565
stringBuffer.append((char) ch);
6666

67-
final int sleepTime = Math.abs(c_random.nextInt() % 10);
68-
TestUtils.sleep(sleepTime);
67+
final int sleepMillis = Math.abs(c_random.nextInt() % 10);
68+
TestUtils.sleep(sleepMillis);
6969
ch = demuxInputStream.read();
7070
}
7171
} catch (final Exception e) {
@@ -96,8 +96,8 @@ public void run() {
9696
try {
9797
//System.out.println( "Writing: " + (char)byteArray[ i ] );
9898
demuxOutputStream.write(element);
99-
final int sleepTime = Math.abs(c_random.nextInt() % 10);
100-
TestUtils.sleep(sleepTime);
99+
final int sleepMillis = Math.abs(c_random.nextInt() % 10);
100+
TestUtils.sleep(sleepMillis);
101101
} catch (final Exception e) {
102102
e.printStackTrace();
103103
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,11 +1668,7 @@ public void testIsFileNewerOlder() throws Exception {
16681668
}
16691669

16701670
do {
1671-
try {
1672-
TestUtils.sleep(1000);
1673-
} catch (final InterruptedException ie) {
1674-
// ignore
1675-
}
1671+
TestUtils.sleepQuietly(1000);
16761672
if (!reference.getParentFile().exists()) {
16771673
throw new IOException("Cannot create file " + reference
16781674
+ " as the parent directory does not exist");

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
import java.io.Writer;
3434
import java.nio.charset.StandardCharsets;
3535
import java.nio.file.Files;
36+
import java.time.Duration;
3637

3738
import org.apache.commons.io.FileUtils;
3839
import org.apache.commons.io.output.ByteArrayOutputStream;
40+
import org.apache.commons.lang3.ThreadUtils;
3941

4042
/**
4143
* Base class for testcases doing tests with files.
@@ -185,8 +187,7 @@ public static byte[] generateTestData(final long size) {
185187
}
186188
}
187189

188-
public static void generateTestData(final OutputStream out, final long size)
189-
throws IOException {
190+
public static void generateTestData(final OutputStream out, final long size) throws IOException {
190191
for (int i = 0; i < size; i++) {
191192
// output.write((byte)'X');
192193
// nice varied byte pattern compatible with Readers and Writers
@@ -207,23 +208,23 @@ public static File newFile(final File testDirectory, final String filename) thro
207208
}
208209

209210
/**
210-
* Sleep for a guaranteed number of milliseconds unless interrupted.
211+
* Sleeps for a guaranteed number of milliseconds unless interrupted.
211212
*
212213
* This method exists because Thread.sleep(100) can sleep for 0, 70, 100 or 200ms or anything else
213214
* it deems appropriate. Read the docs on Thread.sleep for further details.
214215
*
215-
* @param millis the number of milliseconds to sleep for
216-
* @throws InterruptedException if interrupted
216+
* @param millis the number of milliseconds to sleep.
217+
* @throws InterruptedException if interrupted.
217218
*/
218219
public static void sleep(final long millis) throws InterruptedException {
219-
final long finishAtMillis = System.currentTimeMillis() + millis;
220-
long remainingMillis = millis;
221-
do {
222-
Thread.sleep(remainingMillis);
223-
remainingMillis = finishAtMillis - System.currentTimeMillis();
224-
} while (remainingMillis > 0);
220+
ThreadUtils.sleep(Duration.ofMillis(millis));
225221
}
226222

223+
/**
224+
* Sleeps and swallows InterruptedException.
225+
*
226+
* @param millis the number of milliseconds to sleep.
227+
*/
227228
public static void sleepQuietly(final long millis) {
228229
try {
229230
sleep(millis);

0 commit comments

Comments
 (0)