Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5dd44a

Browse files
committed
Merge pull request googleapis#539 from ajkannan/forcedelete-without-executor
Create simple forceDelete that can be used on App Engine
2 parents 560faa9 + 33f6ad8 commit b5dd44a

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

gcloud-java-storage/src/main/java/com/google/gcloud/storage/testing/RemoteGcsHelper.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ public StorageOptions options() {
6565

6666
/**
6767
* Deletes a bucket, even if non-empty. Objects in the bucket are listed and deleted until bucket
68-
* deletion succeeds or {@code timeout} expires.
68+
* deletion succeeds or {@code timeout} expires. To allow for the timeout, this method uses a
69+
* separate thread to send the delete requests. Use
70+
* {@link #forceDelete(Storage storage, String bucket)} if spawning an additional thread is
71+
* undesirable, such as in the App Engine production runtime.
6972
*
7073
* @param storage the storage service to be used to issue requests
7174
* @param bucket the bucket to be deleted
@@ -88,6 +91,17 @@ public static Boolean forceDelete(Storage storage, String bucket, long timeout,
8891
}
8992
}
9093

94+
/**
95+
* Deletes a bucket, even if non-empty. This method blocks until the deletion completes or fails.
96+
*
97+
* @param storage the storage service to be used to issue requests
98+
* @param bucket the bucket to be deleted
99+
* @throws StorageException if an exception is encountered during bucket deletion
100+
*/
101+
public static void forceDelete(Storage storage, String bucket) {
102+
new DeleteBucketTask(storage, bucket).call();
103+
}
104+
91105
/**
92106
* Returns a bucket name generated using a random UUID.
93107
*/
@@ -157,7 +171,7 @@ public DeleteBucketTask(Storage storage, String bucket) {
157171
}
158172

159173
@Override
160-
public Boolean call() throws Exception {
174+
public Boolean call() {
161175
while (true) {
162176
for (BlobInfo info : storage.list(bucket).values()) {
163177
storage.delete(bucket, info.name());
@@ -167,7 +181,12 @@ public Boolean call() throws Exception {
167181
return true;
168182
} catch (StorageException e) {
169183
if (e.code() == 409) {
170-
Thread.sleep(500);
184+
try {
185+
Thread.sleep(500);
186+
} catch (InterruptedException interruptedException) {
187+
Thread.currentThread().interrupt();
188+
throw e;
189+
}
171190
} else {
172191
throw e;
173192
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,36 @@ public void testForceDeleteFail() throws InterruptedException, ExecutionExceptio
140140
}
141141
}
142142

143+
@Test
144+
public void testForceDeleteNoTimeout() {
145+
Storage storageMock = EasyMock.createMock(Storage.class);
146+
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(BLOB_PAGE);
147+
for (BlobInfo info : BLOB_LIST) {
148+
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
149+
}
150+
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true);
151+
EasyMock.replay(storageMock);
152+
RemoteGcsHelper.forceDelete(storageMock, BUCKET_NAME);
153+
EasyMock.verify(storageMock);
154+
}
155+
156+
@Test
157+
public void testForceDeleteNoTimeoutFail() {
158+
Storage storageMock = EasyMock.createMock(Storage.class);
159+
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(BLOB_PAGE);
160+
for (BlobInfo info : BLOB_LIST) {
161+
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
162+
}
163+
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(FATAL_EXCEPTION);
164+
EasyMock.replay(storageMock);
165+
thrown.expect(StorageException.class);
166+
try {
167+
RemoteGcsHelper.forceDelete(storageMock, BUCKET_NAME);
168+
} finally {
169+
EasyMock.verify(storageMock);
170+
}
171+
}
172+
143173
@Test
144174
public void testCreateFromStream() {
145175
RemoteGcsHelper helper = RemoteGcsHelper.create(PROJECT_ID, JSON_KEY_STREAM);

0 commit comments

Comments
 (0)