BackoffOnNotFoundWhenGetBucketACL extends CacheLoader<String, AccessControlList> { @Override public AccessControlList load(String bucketName) { ResourceNotFoundException last = null; for (int currentTries = 0; currentTries < maxTries; currentTries++) { try { return client.getBucketACL(bucketName); } catch (ResourceNotFoundException e) { imposeBackoffExponentialDelay(100l, 200l, 2, currentTries, maxTries); last = e; } } throw last; } @Inject  BackoffOnNotFoundWhenGetBucketACL(S3Client client); @Override AccessControlList load(String bucketName); @Override String toString();  }
@Test void testMaxRetriesNotExceededReturnsValue() { AccessControlList acl = createMock(AccessControlList.class); int attempts = 5; BackoffOnNotFoundWhenGetBucketACL backoff = new BackoffOnNotFoundWhenGetBucketACL(mock); expect(mock.getBucketACL("foo")).andThrow(new ResourceNotFoundException()).times(attempts - 1); expect(mock.getBucketACL("foo")).andReturn(acl); replay(mock); assertSame(backoff.load("foo"), acl); verify(mock); }
@Test(expectedExceptions = ResourceNotFoundException.class) void testMaxRetriesExceededThrowsException() { int attempts = 5; BackoffOnNotFoundWhenGetBucketACL backoff = new BackoffOnNotFoundWhenGetBucketACL(mock); expect(mock.getBucketACL("foo")).andThrow(new ResourceNotFoundException()).times(attempts); replay(mock); backoff.load("foo"); }
@Test(expectedExceptions = UncheckedExecutionException.class) void testDoesntCatchOtherExceptions() { BackoffOnNotFoundWhenGetBucketACL backoff = new BackoffOnNotFoundWhenGetBucketACL(mock); expect(mock.getBucketACL("foo")).andThrow(new UncheckedExecutionException(new TimeoutException())); replay(mock); backoff.load("foo"); verify(mock); }