From 3f9f621b755e9ac3fd8af399e7ddeea098163aa4 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 7 Oct 2015 11:17:50 +0200 Subject: [PATCH] Updated exception handling in ITStorageTest, minor refactoring - 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 --- .../google/gcloud/storage/ITStorageTest.java | 25 +++++++------------ .../gcloud/storage/RemoteGcsHelper.java | 14 ++++++----- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/ITStorageTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/ITStorageTest.java index 74833bd8fe3d..d2056e8cbbb1 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/ITStorageTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/ITStorageTest.java @@ -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; @@ -49,6 +50,7 @@ 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}; @@ -56,30 +58,21 @@ public class ITStorageTest { @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 bucketIterator = diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java index 2c79aaa78afe..f4c9b22a47b5 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java @@ -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 @@ -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 future = executor.submit(new DeleteBucketTask(storage, bucket)); try { return future.get(timeout, unit); } catch (TimeoutException ex) { return false; + } finally { + executor.shutdown(); } } @@ -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;