forked from GoogleCloudPlatform/appengine-pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Worklytics/s192-better-ds-exception-retry
s192 - Better handle wrapped DatastoreExceptions
- Loading branch information
Showing
4 changed files
with
97 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
java/src/test/java/com/google/appengine/tools/mapreduce/RetryUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.google.appengine.tools.mapreduce; | ||
|
||
import com.google.cloud.BaseServiceException; | ||
import com.google.cloud.datastore.DatastoreException; | ||
import com.google.common.collect.ImmutableSet; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.function.IntSupplier; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class RetryUtilsTest { | ||
|
||
@Test | ||
void handleDatastoreExceptionRetry() { | ||
|
||
// copied from DatastoreException.RETRYABLE_ERROR_CODES (private) | ||
ImmutableSet<BaseServiceException.Error> retryableErrors = ImmutableSet.of( | ||
new BaseServiceException.Error(10, "ABORTED", false), | ||
new BaseServiceException.Error(4, "DEADLINE_EXCEEDED", false), | ||
new BaseServiceException.Error(14, "UNAVAILABLE", true)); | ||
ImmutableSet<BaseServiceException.Error> nonRetryableErrors = ImmutableSet.of( | ||
new BaseServiceException.Error(52222, "WHATEVER", false), | ||
new BaseServiceException.Error(989898, "WHO_KNOWS", false)); | ||
|
||
for (BaseServiceException.Error error : retryableErrors) { | ||
DatastoreException de = new DatastoreException(error.getCode(), "something broke", error.getReason(), true, null); | ||
assertTrue(RetryUtils.handleDatastoreExceptionRetry().test(de)); | ||
} | ||
for (BaseServiceException.Error error : nonRetryableErrors) { | ||
DatastoreException de = new DatastoreException(error.getCode(), "something broke", error.getReason(), false, null); | ||
assertFalse(RetryUtils.handleDatastoreExceptionRetry().test(de)); | ||
} | ||
|
||
// Test with message containing "retry the transaction" | ||
for (BaseServiceException.Error error : nonRetryableErrors) { | ||
DatastoreException de = new DatastoreException(error.getCode(), "RETRY THE TRANSACTION", error.getReason(), false, null); | ||
assertTrue(RetryUtils.handleDatastoreExceptionRetry().test(de)); | ||
} | ||
|
||
// embedded DatastoreException | ||
retryableErrors.forEach(error -> { | ||
DatastoreException root = new DatastoreException(error.getCode(), "something broken", error.getReason(), true, null); | ||
assertTrue(RetryUtils.handleDatastoreExceptionRetry().test(root)); | ||
Exception chain1 = new Exception("Test Exception 1", root); | ||
Exception chain2 = new Exception("Test Exception 2", chain1); | ||
assertTrue(RetryUtils.handleDatastoreExceptionRetry().test(chain2)); | ||
}); | ||
|
||
} | ||
} |