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

Revert "Update recoverable error codes (#170)" #172

Merged
merged 1 commit into from
Nov 20, 2024
Merged
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
4 changes: 3 additions & 1 deletion code/app-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ dependencies {

implementation("com.adobe.marketing.mobile:core:$mavenCoreVersion")
implementation("com.adobe.marketing.mobile:edgeidentity:$mavenEdgeIdentityVersion")
implementation("com.adobe.marketing.mobile:edgeconsent:3.0.0")
implementation("com.adobe.marketing.mobile:edgeconsent:$mavenEdgeConsentVersion") {
exclude(group = "com.adobe.marketing.mobile", module = "edge")
}
implementation("com.adobe.marketing.mobile:assurance:3.0.0")

implementation("androidx.constraintlayout:constraintlayout:2.0.4")
Expand Down
4 changes: 3 additions & 1 deletion code/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ dependencies {

implementation("com.adobe.marketing.mobile:core:$mavenCoreVersion")
implementation("com.adobe.marketing.mobile:edgeidentity:$mavenEdgeIdentityVersion")
implementation("com.adobe.marketing.mobile:edgeconsent:3.0.0")
implementation("com.adobe.marketing.mobile:edgeconsent:$mavenEdgeConsentVersion") {
exclude(group = "com.adobe.marketing.mobile", module = "edge")
}
implementation("com.adobe.marketing.mobile:assurance:3.0.0")

implementation("androidx.constraintlayout:constraintlayout:2.0.4")
Expand Down
5 changes: 4 additions & 1 deletion code/edge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ dependencies {
testImplementation("com.github.adobe:aepsdk-testutils-android:$mavenTestUtilsVersion")

androidTestImplementation("com.github.adobe:aepsdk-testutils-android:$mavenTestUtilsVersion")
androidTestImplementation("com.adobe.marketing.mobile:edgeconsent:3.0.0")
androidTestImplementation("com.adobe.marketing.mobile:edgeconsent:$mavenEdgeConsentVersion")
{
exclude(group = "com.adobe.marketing.mobile", module = "edge")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@
import com.adobe.marketing.mobile.services.Log;
import com.adobe.marketing.mobile.services.NetworkRequest;
import com.adobe.marketing.mobile.services.Networking;
import com.adobe.marketing.mobile.services.NetworkingConstants;
import com.adobe.marketing.mobile.util.StringUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -100,13 +98,14 @@ interface ResponseCallback {
private static final String DEFAULT_GENERIC_ERROR_MESSAGE =
"Request to Edge Network failed with an unknown exception";

static final Set<Integer> recoverableNetworkErrorCodes = mergeUnique(
NetworkingConstants.RECOVERABLE_ERROR_CODES,
static final List<Integer> recoverableNetworkErrorCodes = new ArrayList<>(
Arrays.asList(
-1, // returned for SocketTimeoutException
429, // too many requests - The user has sent too many requests in a given amount of time ("rate limiting").
507, // insufficient storage
HttpURLConnection.HTTP_BAD_GATEWAY
HttpURLConnection.HTTP_CLIENT_TIMEOUT,
HttpURLConnection.HTTP_BAD_GATEWAY,
HttpURLConnection.HTTP_UNAVAILABLE,
HttpURLConnection.HTTP_GATEWAY_TIMEOUT
)
);

Expand Down Expand Up @@ -259,10 +258,9 @@ RetryResult doRequest(
private int computeRetryInterval(final HttpConnecting connection) {
String header = connection.getResponsePropertyValue(EdgeConstants.NetworkKeys.HEADER_KEY_RETRY_AFTER);

int retryAfter = EdgeConstants.Defaults.RETRY_INTERVAL_SECONDS;
if (header != null && header.matches("\\d+")) {
try {
retryAfter = Integer.parseInt(header);
return Integer.parseInt(header);
} catch (NumberFormatException e) {
Log.debug(
LOG_TAG,
Expand All @@ -274,7 +272,7 @@ private int computeRetryInterval(final HttpConnecting connection) {
}
}

return retryAfter > 0 ? retryAfter : EdgeConstants.Defaults.RETRY_INTERVAL_SECONDS;
return EdgeConstants.Defaults.RETRY_INTERVAL_SECONDS;
}

/**
Expand Down Expand Up @@ -579,11 +577,4 @@ private String readInputStream(final InputStream inputStream) {
return composeGenericErrorAsJson(e.getMessage());
}
}

private static Set<Integer> mergeUnique(final List<Integer> arr1, final List<Integer> arr2) {
Set<Integer> merged = new HashSet<>();
merged.addAll(arr1);
merged.addAll(arr2);
return merged;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@

import com.adobe.marketing.mobile.services.HttpMethod;
import com.adobe.marketing.mobile.services.NetworkRequest;
import com.adobe.marketing.mobile.services.NetworkingConstants;
import com.adobe.marketing.mobile.util.MockConnection;
import com.adobe.marketing.mobile.util.MockNetworkService;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.json.JSONException;
Expand All @@ -54,7 +50,6 @@ public class EdgeNetworkServiceTest {
private static final String TEST_URL = "https://test.com";
private static final String HEADER_CONTENT_TYPE = "Content-Type";
private static final String HEADER_CONTENT_TYPE_JSON_VALUE = "application/json";
private static final String HEADER_RETRY_AFTER = "Retry-After";
private final Map<String, String> requestHeaders = new HashMap<String, String>() {
{
put("key1", "value1");
Expand Down Expand Up @@ -365,65 +360,6 @@ public void testDoRequest_whenConnection_RecoverableResponseCode_504_ReturnsRetr
testRecoverableNetworkResponse(504, "Gateway Timeout");
}

@Test
public void testDoRequest_whenConnection_RecoverableResponseCode_507_ReturnsRetryYes_AndNoResponseCallback_AndNoErrorCallback() {
testRecoverableNetworkResponse(507, "Insufficient Storage");
}

@Test
public void testDoRequest_whenConnection_RecoverableResponseCodeAndRetryAfter_ReturnsRetryTimeout() {
Set<Integer> recoverableNetworkErrorCodes = new HashSet<>();
recoverableNetworkErrorCodes.addAll(NetworkingConstants.RECOVERABLE_ERROR_CODES);
recoverableNetworkErrorCodes.addAll(Arrays.asList(429, 502, 507, -1));

for (int responseCode : recoverableNetworkErrorCodes) {
testRecoverableWithRetryAfter(responseCode, "30", 30);
}
}

@Test
public void testDoRequest_whenConnection_InvalidRetryAfter_ReturnsDefaultRetryTimeout() {
testRecoverableNetworkResponse(507, "Insufficient Storage");
String[] invalidRetryAfter = { "InvalidRetryAfter", "A", "", "-1", "0", " " };

for (String retryAfter : invalidRetryAfter) {
testRecoverableWithRetryAfter(503, retryAfter, 5); // expecting default timeout
}
}

@Test
public void testDoRequest_whenConnection_ValidRetryAfter_ReturnsCorrectRetryTimeout() {
testRecoverableNetworkResponse(507, "Insufficient Storage");
String[] invalidRetryAfter = { "1", "5", "30", "60", "180", "300" };

for (String retryAfter : invalidRetryAfter) {
testRecoverableWithRetryAfter(503, retryAfter, Integer.parseInt(retryAfter)); // expecting provided timeout
}
}

private void testRecoverableWithRetryAfter(
final int responseCode,
final String retryAfter,
final int expectedRetryAfter
) {
// setup
final String url = "https://test.com";
final String jsonRequest = "{}";
final Map<String, String> headers = new HashMap<>();
headers.put(HEADER_RETRY_AFTER, retryAfter);
MockConnection mockConnection = new MockConnection(responseCode, null, "error", headers);
mockNetworkService.reset();
mockNetworkService.setMockResponseFor(url, mockConnection);
networkService = new EdgeNetworkService(mockNetworkService);

// test
DoRequestResult result = doRequestSync(url, jsonRequest);

// verify
assertEquals(EdgeNetworkService.Retry.YES, result.retryResult.getShouldRetry());
assertEquals(expectedRetryAfter, result.retryResult.getRetryIntervalSeconds());
}

private void testRecoverableNetworkResponse(final int responseCode, final String errorString) {
// setup
final String url = "https://test.com";
Expand Down