Skip to content

Commit bf0ba17

Browse files
committed
Merge pull request square#52 from square/jwilson_0427_zero_timeout
Use 0 as a sentinel for no timeout.
2 parents 2f8dd5c + a2c6051 commit bf0ba17

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

okio/src/main/java/okio/AsyncTimeout.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public final void enter() {
6262
if (inQueue) throw new IllegalStateException("Unbalanced enter/exit");
6363
long timeoutNanos = timeoutNanos();
6464
boolean hasDeadline = hasDeadline();
65-
if (timeoutNanos == -1 && !hasDeadline) {
65+
if (timeoutNanos == 0 && !hasDeadline) {
6666
return; // No timeout and no deadline? Don't bother with the queue.
6767
}
6868
inQueue = true;
@@ -78,11 +78,11 @@ private static synchronized void scheduleTimeout(
7878
}
7979

8080
long now = System.nanoTime();
81-
if (timeoutNanos != -1 && hasDeadline) {
81+
if (timeoutNanos != 0 && hasDeadline) {
8282
// Compute the earliest event; either timeout or deadline. Because nanoTime can wrap around,
8383
// Math.min() is undefined for absolute values, but meaningful for relative ones.
8484
node.timeoutAt = now + Math.min(timeoutNanos, node.deadlineNanoTime() - now);
85-
} else if (timeoutNanos != -1) {
85+
} else if (timeoutNanos != 0) {
8686
node.timeoutAt = now + timeoutNanos;
8787
} else if (hasDeadline) {
8888
node.timeoutAt = node.deadlineNanoTime();

okio/src/main/java/okio/Timeout.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ public class Timeout {
6060

6161
/**
6262
* True if {@code deadlineNanoTime} is defined. There is no equivalent to null
63-
* or -1 for {@link System#nanoTime}.
63+
* or 0 for {@link System#nanoTime}.
6464
*/
6565
private boolean hasDeadline;
6666
private long deadlineNanoTime;
67-
private long timeoutNanos = -1;
67+
private long timeoutNanos;
6868

6969
public Timeout() {
7070
}
@@ -73,15 +73,18 @@ public Timeout() {
7373
* Wait at most {@code timeout} time before aborting an operation. Using a
7474
* per-operation timeout means that as long as forward progress is being made,
7575
* no sequence of operations will fail.
76+
*
77+
* <p>If {@code timeout == 0}, operations will run indefinitely. (Operating
78+
* system timeouts may still apply.)
7679
*/
7780
public Timeout timeout(long timeout, TimeUnit unit) {
78-
if (timeout <= 0) throw new IllegalArgumentException("timeout <= 0: " + timeout);
81+
if (timeout < 0) throw new IllegalArgumentException("timeout < 0: " + timeout);
7982
if (unit == null) throw new IllegalArgumentException("unit == null");
8083
this.timeoutNanos = unit.toNanos(timeout);
8184
return this;
8285
}
8386

84-
/** Returns the timeout in nanoseconds, or {@code -1} for no timeout. */
87+
/** Returns the timeout in nanoseconds, or {@code 0} for no timeout. */
8588
public long timeoutNanos() {
8689
return timeoutNanos;
8790
}
@@ -122,7 +125,7 @@ public final Timeout deadline(long duration, TimeUnit unit) {
122125

123126
/** Clears the timeout. Operating system timeouts may still apply. */
124127
public Timeout clearTimeout() {
125-
this.timeoutNanos = -1;
128+
this.timeoutNanos = 0;
126129
return this;
127130
}
128131

okio/src/test/java/okio/AsyncTimeoutTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public class AsyncTimeoutTest {
4747
d.timeout(1000, TimeUnit.MILLISECONDS);
4848
}
4949

50+
@Test public void zeroTimeoutIsNoTimeout() throws Exception {
51+
AsyncTimeout timeout = new RecordingAsyncTimeout();
52+
timeout.timeout(0, TimeUnit.MILLISECONDS);
53+
timeout.enter();
54+
Thread.sleep(250);
55+
assertFalse(timeout.exit());
56+
assertTimedOut();
57+
}
58+
5059
@Test public void singleInstanceTimedOut() throws Exception {
5160
a.enter();
5261
Thread.sleep(500);

0 commit comments

Comments
 (0)