private void encodeRetryStrategy(RetryStrategy retryStrategy, Bundle data) { if (retryStrategy == null) { retryStrategy = RetryStrategy.DEFAULT_EXPONENTIAL; } data.putInt( prefix + BundleProtocol.PACKED_PARAM_RETRY_STRATEGY_POLICY, retryStrategy.getPolicy()); data.putInt( prefix + BundleProtocol.PACKED_PARAM_RETRY_STRATEGY_INITIAL_BACKOFF_SECONDS, retryStrategy.getInitialBackoff()); data.putInt( prefix + BundleProtocol.PACKED_PARAM_RETRY_STRATEGY_MAXIMUM_BACKOFF_SECONDS, retryStrategy.getMaximumBackoff()); }
@Override public List<String> answer(InvocationOnMock invocation) throws Throwable { RetryStrategy rs = (RetryStrategy) invocation.getArguments()[0]; // only succeed if initialBackoff == 30s return rs.getInitialBackoff() == 30 ? null : Arrays.asList("foo", "bar"); } });
private static void writeRetryStrategyToBundle(JobParameters job, Bundle b) { RetryStrategy strategy = job.getRetryStrategy(); Bundle rb = new Bundle(); rb.putInt( REQUEST_PARAM_RETRY_STRATEGY_POLICY, convertRetryPolicyToLegacyVersion(strategy.getPolicy())); rb.putInt(REQUEST_PARAM_RETRY_STRATEGY_INITIAL_BACKOFF_SECONDS, strategy.getInitialBackoff()); rb.putInt(REQUEST_PARAM_RETRY_STRATEGY_MAXIMUM_BACKOFF_SECONDS, strategy.getMaximumBackoff()); b.putBundle(REQUEST_PARAM_RETRY_STRATEGY, rb); }
/** * Attempts to validate the provided RetryStrategy. If valid, null is returned. Otherwise a list * of errors will be returned. */ @Nullable @Override @CallSuper public List<String> validate(@NonNull RetryStrategy retryStrategy) { List<String> errors; int policy = retryStrategy.getPolicy(); int initial = retryStrategy.getInitialBackoff(); int maximum = retryStrategy.getMaximumBackoff(); errors = addErrorsIf( policy != RETRY_POLICY_EXPONENTIAL && policy != RETRY_POLICY_LINEAR, null, "Unknown retry policy provided"); errors = addErrorsIf( maximum < initial, errors, "Maximum backoff must be greater than or equal to initial backoff"); errors = addErrorsIf(300 > maximum, errors, "Maximum backoff must be greater than 300s (5 minutes)"); errors = addErrorsIf(initial < 30, errors, "Initial backoff must be at least 30s"); return errors; }
static void assertRetryStrategiesEqual(RetryStrategy in, RetryStrategy out) { String prefix = "getRetryStrategy()."; assertEquals(prefix + "getPolicy()", in.getPolicy(), out.getPolicy()); assertEquals(prefix + "getInitialBackoff()", in.getInitialBackoff(), out.getInitialBackoff()); assertEquals(prefix + "getMaximumBackoff()", in.getMaximumBackoff(), out.getMaximumBackoff()); }
@Test public void testWriteToBundle_backoffSeconds() { for (RetryStrategy retryStrategy : Arrays.asList(RetryStrategy.DEFAULT_EXPONENTIAL, RetryStrategy.DEFAULT_LINEAR)) { Bundle b = writer .writeToBundle( initializeDefaultBuilder().setRetryStrategy(retryStrategy).build(), new Bundle()) .getBundle("retryStrategy"); assertEquals( "initial_backoff_seconds", retryStrategy.getInitialBackoff(), b.getInt("initial_backoff_seconds")); assertEquals( "maximum_backoff_seconds", retryStrategy.getMaximumBackoff(), b.getInt("maximum_backoff_seconds")); } }