package com.android.dialer.helpdesk.http; import static com.android.dialer.helpdesk.http.HttpRetryPolicy.Action.BACKOFF_RETRY; import static com.android.dialer.helpdesk.http.HttpRetryPolicy.Action.DROP; import static com.android.dialer.helpdesk.http.HttpRetryPolicy.Action.REFRESH_RETRY; import static com.android.dialer.helpdesk.http.HttpRetryPolicy.Action.SUCCESS; /** Standalone off-device test for {@link HttpRetryPolicy}. */ public class HttpRetryPolicyTest { static int pass = 0, fail = 0; static void eq(Object got, Object want, String d) { if (got == null ? want == null : got.equals(want)) { pass++; System.out.println(" ok " + d); } else { fail++; System.out.println(" FAIL " + d + " (got: " + got + ")"); } } static void truthy(boolean x, String d) { if (x) { pass++; System.out.println(" ok " + d); } else { fail++; System.out.println(" FAIL " + d); } } public static void main(String[] args) { eq(HttpRetryPolicy.classify(200), SUCCESS, "200 -> SUCCESS"); eq(HttpRetryPolicy.classify(202), SUCCESS, "202 -> SUCCESS"); eq(HttpRetryPolicy.classify(204), SUCCESS, "204 -> SUCCESS"); eq(HttpRetryPolicy.classify(422), DROP, "422 -> DROP (bad body/verb)"); eq(HttpRetryPolicy.classify(400), DROP, "400 -> DROP"); eq(HttpRetryPolicy.classify(404), DROP, "404 -> DROP"); eq(HttpRetryPolicy.classify(409), DROP, "409 -> DROP"); eq(HttpRetryPolicy.classify(401), REFRESH_RETRY, "401 -> REFRESH_RETRY"); eq(HttpRetryPolicy.classify(403), REFRESH_RETRY, "403 -> REFRESH_RETRY"); eq(HttpRetryPolicy.classify(500), BACKOFF_RETRY, "500 -> BACKOFF_RETRY"); eq(HttpRetryPolicy.classify(503), BACKOFF_RETRY, "503 -> BACKOFF_RETRY"); eq(HttpRetryPolicy.classify(429), BACKOFF_RETRY, "429 -> BACKOFF_RETRY"); eq(HttpRetryPolicy.classify(408), BACKOFF_RETRY, "408 -> BACKOFF_RETRY"); eq(HttpRetryPolicy.classify(0), BACKOFF_RETRY, "no response (0) -> BACKOFF_RETRY"); eq(HttpRetryPolicy.classify(-1), BACKOFF_RETRY, "network error (-1) -> BACKOFF_RETRY"); eq(HttpRetryPolicy.backoffMs(0, 1000, 30000), 1000L, "backoff attempt 0 = base"); eq(HttpRetryPolicy.backoffMs(1, 1000, 30000), 2000L, "backoff attempt 1 = 2x"); eq(HttpRetryPolicy.backoffMs(2, 1000, 30000), 4000L, "backoff attempt 2 = 4x"); eq(HttpRetryPolicy.backoffMs(5, 1000, 30000), 30000L, "backoff clamps to cap"); eq(HttpRetryPolicy.backoffMs(100, 1000, 30000), 30000L, "backoff never exceeds cap (no overflow)"); truthy(HttpRetryPolicy.backoffMs(3, 1000, 30000) >= HttpRetryPolicy.backoffMs(2, 1000, 30000), "backoff is non-decreasing"); System.out.println("\n" + pass + " passed, " + fail + " failed"); System.exit(fail == 0 ? 0 : 1); } }