Skip to content

Commit

Permalink
Throw normal IOException when canceled to match OkHttp.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Dec 21, 2015
1 parent c342d65 commit 73b88a4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
7 changes: 3 additions & 4 deletions retrofit-mock/src/main/java/retrofit2/mock/BehaviorCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package retrofit2.mock;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -59,7 +58,7 @@ private boolean delaySleep() {
try {
Thread.sleep(sleepMs);
} catch (InterruptedException e) {
callback.onFailure(new InterruptedIOException("canceled"));
callback.onFailure(new IOException("canceled"));
return false;
}
}
Expand All @@ -68,7 +67,7 @@ private boolean delaySleep() {

@Override public void run() {
if (canceled) {
callback.onFailure(new InterruptedIOException("canceled"));
callback.onFailure(new IOException("canceled"));
} else if (behavior.calculateIsFailure()) {
if (delaySleep()) {
callback.onFailure(behavior.failureException());
Expand Down Expand Up @@ -114,7 +113,7 @@ private boolean delaySleep() {
try {
latch.await();
} catch (InterruptedException e) {
throw new InterruptedIOException("canceled");
throw new IOException("canceled");
}
Response<T> response = responseRef.get();
if (response != null) return response;
Expand Down
13 changes: 6 additions & 7 deletions retrofit-mock/src/test/java/retrofit2/mock/MockRetrofitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package retrofit2.mock;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -222,8 +221,8 @@ interface DoWorkService {
try {
call.execute();
fail();
} catch (InterruptedIOException e) {
assertThat(e).hasMessage("canceled");
} catch (IOException e) {
assertThat(e).isExactlyInstanceOf(IOException.class).hasMessage("canceled");
}
}

Expand Down Expand Up @@ -252,7 +251,7 @@ interface DoWorkService {
call.cancel();

assertTrue(latch.await(1, SECONDS));
assertThat(failureRef.get()).isInstanceOf(InterruptedIOException.class).hasMessage("canceled");
assertThat(failureRef.get()).isExactlyInstanceOf(IOException.class).hasMessage("canceled");
}

@Test public void syncCanceledBeforeStart() throws IOException {
Expand All @@ -266,8 +265,8 @@ interface DoWorkService {
try {
call.execute();
fail();
} catch (InterruptedIOException e) {
assertThat(e).hasMessage("canceled");
} catch (IOException e) {
assertThat(e).isExactlyInstanceOf(IOException.class).hasMessage("canceled");
}
}

Expand All @@ -293,6 +292,6 @@ interface DoWorkService {
});

assertTrue(latch.await(1, SECONDS));
assertThat(failureRef.get()).isInstanceOf(InterruptedIOException.class).hasMessage("canceled");
assertThat(failureRef.get()).isExactlyInstanceOf(IOException.class).hasMessage("canceled");
}
}

0 comments on commit 73b88a4

Please sign in to comment.