Skip to content

Commit

Permalink
Updated exception handling in ITStorageTest, minor refactoring
Browse files Browse the repository at this point in the history
- deleteBucketRecursively renamed to forceDelete
- Add explicit shutdown of executor to forceDelete
- Make itegration test fail if env variables are not set
- Log warning if forceDelete times out
  • Loading branch information
mziccard committed Oct 7, 2015
1 parent bfd4fc4 commit 3f9f621
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -49,37 +50,29 @@ public class ITStorageTest {
private static Storage storage;
private static RemoteGcsHelper gcsHelper;

private static final Logger log = Logger.getLogger(ITStorageTest.class.getName());
private static final String bucket = RemoteGcsHelper.generateBucketName();
private static final String CONTENT_TYPE = "text/plain";
private static final byte[] BLOB_BYTE_CONTENT = {0xD, 0xE, 0xA, 0xD};
private static final String BLOB_STRING_CONTENT = "Hello Google Cloud Storage!";

@BeforeClass
public static void beforeClass() {
try {
gcsHelper = RemoteGcsHelper.create();
storage = StorageFactory.instance().get(gcsHelper.options());
storage.create(BucketInfo.of(bucket));
} catch (RemoteGcsHelper.GcsHelperException e) {
// ignore
}
gcsHelper = RemoteGcsHelper.create();
storage = StorageFactory.instance().get(gcsHelper.options());
storage.create(BucketInfo.of(bucket));
}

@AfterClass
public static void afterClass()
throws ExecutionException, TimeoutException, InterruptedException {
if (storage != null) {
if (!RemoteGcsHelper.deleteBucketRecursively(storage, bucket, 5, TimeUnit.SECONDS)) {
throw new RuntimeException("Bucket deletion timed out. Could not delete non-empty bucket");
if (!RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS)) {
if (log.isLoggable(Level.WARNING)) {
log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", bucket);
}
}
}

@Before
public void beforeMethod() {
org.junit.Assume.assumeNotNull(storage);
}

@Test(timeout = 5000)
public void testListBuckets() throws InterruptedException {
Iterator<BucketInfo> bucketIterator =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public StorageOptions options() {
}

/**
* Delete a bucket recursively. Objects in the bucket are listed and deleted until bucket deletion
* succeeds or {@code timeout} expires.
* Deletes a bucket, even if non-empty. Objects in the bucket are listed and deleted until bucket
* deletion succeeds or {@code timeout} expires.
*
* @param storage the storage service to be used to issue requests
* @param bucket the bucket to be deleted
Expand All @@ -71,14 +71,16 @@ public StorageOptions options() {
* @throws InterruptedException if the thread deleting the bucket is interrupted while waiting
* @throws ExecutionException if an exception was thrown while deleting bucket or bucket objects
*/
public static Boolean deleteBucketRecursively(Storage storage, String bucket, long timeout,
TimeUnit unit) throws InterruptedException, ExecutionException {
public static Boolean forceDelete(Storage storage, String bucket, long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Boolean> future = executor.submit(new DeleteBucketTask(storage, bucket));
try {
return future.get(timeout, unit);
} catch (TimeoutException ex) {
return false;
} finally {
executor.shutdown();
}
}

Expand All @@ -95,8 +97,8 @@ public static String generateBucketName() {
* @param options creation options
* @return A {@code RemoteGcsHelper} object for the provided options.
* @throws com.google.gcloud.storage.RemoteGcsHelper.GcsHelperException if environment variables
* {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY_PATH} are not set or if the file
* pointed by {@code GCLOUD_TESTS_KEY_PATH} does not exist
* {@code GCLOUD_TESTS_PROJECT_ID} and {@code GCLOUD_TESTS_KEY} are not set or if the file
* pointed by {@code GCLOUD_TESTS_KEY} does not exist
*/
public static RemoteGcsHelper create(Option... options) throws GcsHelperException {
boolean keyFromClassPath = false;
Expand Down

0 comments on commit 3f9f621

Please sign in to comment.