Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw new InterruptedIOException subtype on timeout #697

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions okio/src/jvmMain/kotlin/okio/AsyncTimeout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package okio

import java.io.IOException
import java.io.InterruptedIOException
import java.util.concurrent.TimeUnit

/**
Expand Down Expand Up @@ -163,11 +162,10 @@ open class AsyncTimeout : Timeout() {

/**
* Returns an [IOException] to represent a timeout. By default this method returns
* [InterruptedIOException]. If [cause] is non-null it is set as the cause of the
* returned exception.
* [TimeoutException]. If [cause] is non-null it is set as the cause of the returned exception.
*/
protected open fun newTimeoutException(cause: IOException?): IOException {
val e = InterruptedIOException("timeout")
val e = TimeoutException("timeout")
if (cause != null) {
e.initCause(cause)
}
Expand Down
18 changes: 10 additions & 8 deletions okio/src/jvmMain/kotlin/okio/Timeout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import java.io.IOException
import java.io.InterruptedIOException
import java.util.concurrent.TimeUnit

class TimeoutException(message: String) : InterruptedIOException(message)

actual open class Timeout {
/**
* True if `deadlineNanoTime` is defined. There is no equivalent to null or 0 for
Expand Down Expand Up @@ -87,9 +89,9 @@ actual open class Timeout {
}

/**
* Throws an [InterruptedIOException] if the deadline has been reached or if the current thread
* has been interrupted. This method doesn't detect timeouts; that should be implemented to
* asynchronously abort an in-progress operation.
* Throws a [TimeoutException] if the deadline has been reached or an [InterruptedIOException] if
* the current thread has been interrupted. This method doesn't detect timeouts; that should be
* implemented to asynchronously abort an in-progress operation.
*/
@Throws(IOException::class)
open fun throwIfReached() {
Expand All @@ -99,14 +101,14 @@ actual open class Timeout {
}

if (hasDeadline && deadlineNanoTime - System.nanoTime() <= 0) {
throw InterruptedIOException("deadline reached")
throw TimeoutException("deadline reached")
}
}

/**
* Waits on `monitor` until it is notified. Throws [InterruptedIOException] if either the thread
* is interrupted or if this timeout elapses before `monitor` is notified. The caller must be
* synchronized on `monitor`.
* Waits on `monitor` until it is notified. Throws [InterruptedIOException] if the thread is
* interrupted or [TimeoutException] if this timeout elapses before `monitor` is notified. The
* caller must be synchronized on `monitor`.
*
* Here's a sample class that uses `waitUntilNotified()` to await a specific state. Note that the
* call is made within a loop to avoid unnecessary waiting and to mitigate spurious notifications.
Expand Down Expand Up @@ -170,7 +172,7 @@ actual open class Timeout {

// Throw if the timeout elapsed before the monitor was notified.
if (elapsedNanos >= waitNanos) {
throw InterruptedIOException("timeout")
throw TimeoutException("timeout")
}
} catch (e: InterruptedException) {
Thread.currentThread().interrupt() // Retain interrupted status.
Expand Down
13 changes: 6 additions & 7 deletions okio/src/jvmTest/java/okio/AsyncTimeoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package okio;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -198,7 +197,7 @@ public final class AsyncTimeoutTest {
try {
timeoutSink.write(data, 1);
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
}
}

Expand All @@ -218,7 +217,7 @@ public final class AsyncTimeoutTest {
try {
timeoutSink.flush();
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
}
}

Expand All @@ -238,7 +237,7 @@ public final class AsyncTimeoutTest {
try {
timeoutSink.close();
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
}
}

Expand All @@ -259,7 +258,7 @@ public final class AsyncTimeoutTest {
try {
timeoutSource.read(new Buffer(), 0);
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
}
}

Expand All @@ -279,7 +278,7 @@ public final class AsyncTimeoutTest {
try {
timeoutSource.close();
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
}
}

Expand All @@ -301,7 +300,7 @@ public final class AsyncTimeoutTest {
try {
timeoutSink.write(data, 1);
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
assertEquals("timeout", expected.getMessage());
assertEquals("exception and timeout", expected.getCause().getMessage());
}
Expand Down
5 changes: 2 additions & 3 deletions okio/src/jvmTest/java/okio/PipeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package okio;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -109,7 +108,7 @@ public final class PipeTest {
try {
pipe.sink().write(new Buffer().writeUtf8("def"), 3L);
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
assertEquals("timeout", expected.getMessage());
}
assertElapsed(1000.0, start);
Expand All @@ -127,7 +126,7 @@ public final class PipeTest {
try {
pipe.source().read(readBuffer, 6L);
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
assertEquals("timeout", expected.getMessage());
}
assertElapsed(1000.0, start);
Expand Down
20 changes: 10 additions & 10 deletions okio/src/jvmTest/java/okio/WaitUntilNotifiedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,68 +50,68 @@ public final class WaitUntilNotifiedTest {
assertElapsed(1000.0, start);
}

@Test public synchronized void timeout() {
@Test public synchronized void timeout() throws InterruptedIOException {
Timeout timeout = new Timeout();
timeout.timeout(1000, TimeUnit.MILLISECONDS);
double start = now();
try {
timeout.waitUntilNotified(this);
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
assertEquals("timeout", expected.getMessage());
}
assertElapsed(1000.0, start);
}

@Test public synchronized void deadline() {
@Test public synchronized void deadline() throws InterruptedIOException {
Timeout timeout = new Timeout();
timeout.deadline(1000, TimeUnit.MILLISECONDS);
double start = now();
try {
timeout.waitUntilNotified(this);
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
assertEquals("timeout", expected.getMessage());
}
assertElapsed(1000.0, start);
}

@Test public synchronized void deadlineBeforeTimeout() {
@Test public synchronized void deadlineBeforeTimeout() throws InterruptedIOException {
Timeout timeout = new Timeout();
timeout.timeout(5000, TimeUnit.MILLISECONDS);
timeout.deadline(1000, TimeUnit.MILLISECONDS);
double start = now();
try {
timeout.waitUntilNotified(this);
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
assertEquals("timeout", expected.getMessage());
}
assertElapsed(1000.0, start);
}

@Test public synchronized void timeoutBeforeDeadline() {
@Test public synchronized void timeoutBeforeDeadline() throws InterruptedIOException {
Timeout timeout = new Timeout();
timeout.timeout(1000, TimeUnit.MILLISECONDS);
timeout.deadline(5000, TimeUnit.MILLISECONDS);
double start = now();
try {
timeout.waitUntilNotified(this);
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
assertEquals("timeout", expected.getMessage());
}
assertElapsed(1000.0, start);
}

@Test public synchronized void deadlineAlreadyReached() {
@Test public synchronized void deadlineAlreadyReached() throws InterruptedIOException {
Timeout timeout = new Timeout();
timeout.deadlineNanoTime(System.nanoTime());
double start = now();
try {
timeout.waitUntilNotified(this);
fail();
} catch (InterruptedIOException expected) {
} catch (TimeoutException expected) {
assertEquals("timeout", expected.getMessage());
}
assertElapsed(0.0, start);
Expand Down