Skip to content

Commit 3f9f621

Browse files
committed
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
1 parent bfd4fc4 commit 3f9f621

2 files changed

Lines changed: 17 additions & 22 deletions

File tree

gcloud-java-storage/src/test/java/com/google/gcloud/storage/ITStorageTest.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
import java.util.concurrent.ExecutionException;
3939
import java.util.concurrent.TimeUnit;
4040
import java.util.concurrent.TimeoutException;
41+
import java.util.logging.Level;
42+
import java.util.logging.Logger;
4143

4244
import org.junit.AfterClass;
43-
import org.junit.Before;
4445
import org.junit.BeforeClass;
4546
import org.junit.Test;
4647

@@ -49,37 +50,29 @@ public class ITStorageTest {
4950
private static Storage storage;
5051
private static RemoteGcsHelper gcsHelper;
5152

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

5759
@BeforeClass
5860
public static void beforeClass() {
59-
try {
60-
gcsHelper = RemoteGcsHelper.create();
61-
storage = StorageFactory.instance().get(gcsHelper.options());
62-
storage.create(BucketInfo.of(bucket));
63-
} catch (RemoteGcsHelper.GcsHelperException e) {
64-
// ignore
65-
}
61+
gcsHelper = RemoteGcsHelper.create();
62+
storage = StorageFactory.instance().get(gcsHelper.options());
63+
storage.create(BucketInfo.of(bucket));
6664
}
6765

6866
@AfterClass
6967
public static void afterClass()
7068
throws ExecutionException, TimeoutException, InterruptedException {
71-
if (storage != null) {
72-
if (!RemoteGcsHelper.deleteBucketRecursively(storage, bucket, 5, TimeUnit.SECONDS)) {
73-
throw new RuntimeException("Bucket deletion timed out. Could not delete non-empty bucket");
69+
if (!RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS)) {
70+
if (log.isLoggable(Level.WARNING)) {
71+
log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", bucket);
7472
}
7573
}
7674
}
7775

78-
@Before
79-
public void beforeMethod() {
80-
org.junit.Assume.assumeNotNull(storage);
81-
}
82-
8376
@Test(timeout = 5000)
8477
public void testListBuckets() throws InterruptedException {
8578
Iterator<BucketInfo> bucketIterator =

gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelper.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public StorageOptions options() {
6060
}
6161

6262
/**
63-
* Delete a bucket recursively. Objects in the bucket are listed and deleted until bucket deletion
64-
* succeeds or {@code timeout} expires.
63+
* Deletes a bucket, even if non-empty. Objects in the bucket are listed and deleted until bucket
64+
* deletion succeeds or {@code timeout} expires.
6565
*
6666
* @param storage the storage service to be used to issue requests
6767
* @param bucket the bucket to be deleted
@@ -71,14 +71,16 @@ public StorageOptions options() {
7171
* @throws InterruptedException if the thread deleting the bucket is interrupted while waiting
7272
* @throws ExecutionException if an exception was thrown while deleting bucket or bucket objects
7373
*/
74-
public static Boolean deleteBucketRecursively(Storage storage, String bucket, long timeout,
75-
TimeUnit unit) throws InterruptedException, ExecutionException {
74+
public static Boolean forceDelete(Storage storage, String bucket, long timeout, TimeUnit unit)
75+
throws InterruptedException, ExecutionException {
7676
ExecutorService executor = Executors.newSingleThreadExecutor();
7777
Future<Boolean> future = executor.submit(new DeleteBucketTask(storage, bucket));
7878
try {
7979
return future.get(timeout, unit);
8080
} catch (TimeoutException ex) {
8181
return false;
82+
} finally {
83+
executor.shutdown();
8284
}
8385
}
8486

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

0 commit comments

Comments
 (0)