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

fix: restore the thread's interrupted status after catching InterruptedException (#1005) #1006

Merged
merged 4 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public boolean handleIOException(HttpRequest request, boolean supportsRetry) thr
try {
return BackOffUtils.next(sleeper, backOff);
} catch (InterruptedException exception) {
// Mark thread as interrupted since we cannot throw InterruptedException here.
Thread.currentThread().interrupt();
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public boolean handleResponse(HttpRequest request, HttpResponse response, boolea
try {
return BackOffUtils.next(sleeper, backOff);
} catch (InterruptedException exception) {
// ignore
// Mark thread as interrupted since we cannot throw InterruptedException here.
Thread.currentThread().interrupt();
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* try {
* return BackOffUtils.next(sleeper, backOff);
* } catch (InterruptedException exception) {
* Thread.currentThread().interrupt();
* return false;
* }
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import com.google.api.client.testing.util.MockBackOff;
import com.google.api.client.testing.util.MockSleeper;
import com.google.api.client.util.BackOff;
import com.google.api.client.util.Sleeper;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -46,4 +48,32 @@ public void subsetHandle(long count, long millis, boolean retrySupported, BackOf
}
assertEquals(count, sleeper.getCount());
}

public void testHandleIOException_returnsFalseAndThreadRemainsInterrupted_whenSleepIsInterrupted()
throws Exception {
final AtomicBoolean stillInterrupted = new AtomicBoolean(false);
Thread runningThread = new Thread() {
@Override
public void run() {
HttpBackOffIOExceptionHandler testTarget =
new HttpBackOffIOExceptionHandler(
new MockBackOff().setBackOffMillis(Long.MAX_VALUE) // Sleep until we interrupt it.
.setMaxTries(1))
.setSleeper(Sleeper.DEFAULT); // Needs to be a real sleeper so we can interrupt it.

try {
testTarget.handleIOException(null, /* retrySupported= */ true);
} catch (Exception ignored) {
}
stillInterrupted.set(Thread.currentThread().isInterrupted());
}
};
runningThread.start();
// Give runningThread some time to start.
Thread.sleep(500L);
runningThread.interrupt();
runningThread.join();

assertTrue(stillInterrupted.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import com.google.api.client.testing.util.MockBackOff;
import com.google.api.client.testing.util.MockSleeper;
import com.google.api.client.util.BackOff;
import com.google.api.client.util.Sleeper;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -67,4 +69,33 @@ private void subsetHandleResponse(
}
assertEquals(count, sleeper.getCount());
}

public void testHandleResponse_returnsFalseAndThreadRemainsInterrupted_whenSleepIsInterrupted()
throws Exception {
final AtomicBoolean stillInterrupted = new AtomicBoolean(false);
Thread runningThread = new Thread() {
@Override
public void run() {
HttpBackOffUnsuccessfulResponseHandler testTarget =
new HttpBackOffUnsuccessfulResponseHandler(
new MockBackOff().setBackOffMillis(Long.MAX_VALUE) // Sleep until we interrupt it.
.setMaxTries(1))
.setSleeper(Sleeper.DEFAULT) // Needs to be a real sleeper so we can interrupt it.
.setBackOffRequired(BackOffRequired.ALWAYS);

try {
testTarget.handleResponse(null, null, /* retrySupported= */ true);
} catch (Exception ignored) {
}
stillInterrupted.set(Thread.currentThread().isInterrupted());
}
};
runningThread.start();
// Give runningThread some time to start.
Thread.sleep(500L);
runningThread.interrupt();
runningThread.join();

assertTrue(stillInterrupted.get());
}
}