Skip to content

Commit

Permalink
[S3] Fix testRoundTrip to wait for lifecycle configuration to be crea…
Browse files Browse the repository at this point in the history
…ted (#1076)
  • Loading branch information
Karthikeyan authored and palpatim committed Jul 18, 2019
1 parent 2db2334 commit 90ff1c0
Showing 1 changed file with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,8 @@

package com.amazonaws.services.s3;

import android.util.Log;

import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.model.BucketLifecycleConfiguration;
Expand All @@ -29,19 +31,24 @@
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class LifecycleVersioningIntegrationTest extends S3IntegrationTestBase {

private static final String BUCKET_NAME =
"lifecycle-versioning-integration-test-"
+ System.currentTimeMillis();

private static final String TAG = LifecycleVersioningIntegrationTest.class.getSimpleName();

@BeforeClass
public static void setUp() throws Exception {
setUpCredentials();
s3 = new AmazonS3Client(credentials);
s3.setRegion(Region.getRegion(Regions.US_WEST_2));
s3 = new AmazonS3Client(credentials, Region.getRegion(Regions.US_WEST_2));

s3.createBucket(BUCKET_NAME);
S3IntegrationTestBase.waitForBucketCreation(BUCKET_NAME);
assertTrue(BUCKET_NAME + " should exist.", s3.doesBucketExist(BUCKET_NAME));

s3.setBucketVersioningConfiguration(
new SetBucketVersioningConfigurationRequest(
Expand Down Expand Up @@ -80,8 +87,7 @@ public void testRoundTrip() throws Exception {
.withNoncurrentVersionExpirationInDays(60)
));

BucketLifecycleConfiguration result =
s3.getBucketLifecycleConfiguration(BUCKET_NAME);
BucketLifecycleConfiguration result = waitForBucketLifecycleConfigurationCreated(BUCKET_NAME);

Assert.assertEquals(2, result.getRules().size());

Expand All @@ -95,4 +101,28 @@ public void testRoundTrip() throws Exception {
.getNoncurrentVersionTransition().getStorageClass());
Assert.assertEquals(60, result.getRules().get(1).getNoncurrentVersionExpirationInDays());
}

/**
* waiting a lifecycle configuration become deleted When exceed the poll
* time, will throw Max poll time exceeded exception
*/
private static BucketLifecycleConfiguration waitForBucketLifecycleConfigurationCreated(String bucketName)
throws Exception {
long startTime = System.currentTimeMillis();
long endTime = startTime + (10 * 60 * 1000);
int hits = 0;
while (System.currentTimeMillis() < endTime) {
BucketLifecycleConfiguration bucketLifecycleConfiguration = null;

if ((bucketLifecycleConfiguration = s3.getBucketLifecycleConfiguration(bucketName)) == null) {
Log.d(TAG, "Waiting for BucketLifecycleConfiguration for bucket: " + bucketName + " to be created.");
Thread.sleep(1000);
hits = 0;
}
if (hits++ == 10)
return bucketLifecycleConfiguration;
}
maxPollTimeExceeded();
return null;
}
}

0 comments on commit 90ff1c0

Please sign in to comment.