Skip to content

Commit ff74916

Browse files
authored
fix flaky test (Azure#22622)
1 parent f611436 commit ff74916

File tree

2 files changed

+27
-1
lines changed
  • sdk/storage
    • azure-storage-common/src/test-shared/java/com/azure/storage/common/test/shared
    • azure-storage-file-share/src/test/java/com/azure/storage/file/share/specialized

2 files changed

+27
-1
lines changed

sdk/storage/azure-storage-common/src/test-shared/java/com/azure/storage/common/test/shared/StorageSpec.groovy

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import com.azure.core.util.ServiceVersion
1313
import com.azure.identity.EnvironmentCredentialBuilder
1414
import spock.lang.Specification
1515

16+
import java.time.Duration
17+
import java.util.function.Predicate
18+
import java.util.function.Supplier
19+
1620
class StorageSpec extends Specification {
1721
private static final TestEnvironment ENVIRONMENT = TestEnvironment.getInstance()
1822
private static final HttpClient HTTP_CLIENT = new NettyAsyncHttpClientBuilder().build()
@@ -87,4 +91,20 @@ class StorageSpec extends Specification {
8791
.map { it.getToken() }
8892
.block()
8993
}
94+
95+
protected <T, E extends Exception> T retry(
96+
Supplier<T> action, Predicate<E> retryPredicate,
97+
int times=6, Duration delay=Duration.ofSeconds(10)) {
98+
for (i in 0..<times) {
99+
try {
100+
return action.get()
101+
} catch (Exception e) {
102+
if (!retryPredicate(e)) {
103+
throw e
104+
} else {
105+
Thread.sleep(delay.toMillis())
106+
}
107+
}
108+
}
109+
}
90110
}

sdk/storage/azure-storage-file-share/src/test/java/com/azure/storage/file/share/specialized/LeaseAPITest.groovy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
package com.azure.storage.file.share.specialized
55

6+
import com.azure.core.http.rest.Response
67
import com.azure.storage.common.test.shared.extensions.RequiredServiceVersion
78
import com.azure.storage.file.share.APISpec
89
import com.azure.storage.file.share.ShareClient
910
import com.azure.storage.file.share.ShareFileClient
1011
import com.azure.storage.file.share.ShareServiceVersion
1112
import com.azure.storage.file.share.models.LeaseDurationType
1213
import com.azure.storage.file.share.models.LeaseStateType
14+
import com.azure.storage.file.share.models.ShareErrorCode
1315
import com.azure.storage.file.share.models.ShareStorageException
1416
import com.azure.storage.file.share.options.ShareAcquireLeaseOptions
1517
import com.azure.storage.file.share.options.ShareBreakLeaseOptions
@@ -203,9 +205,13 @@ class LeaseAPITest extends APISpec {
203205
setup:
204206
def shareSnapshot = shareClient.createSnapshot().getSnapshot()
205207
def shareClient = shareBuilderHelper(shareClient.getShareName(), shareSnapshot).buildClient()
208+
def leaseClient = createLeaseClient(shareClient)
206209

207210
when:
208-
def resp = createLeaseClient(shareClient).acquireLeaseWithResponse(new ShareAcquireLeaseOptions().setDuration(-1), null, null)
211+
def resp = retry({
212+
leaseClient
213+
.acquireLeaseWithResponse(new ShareAcquireLeaseOptions().setDuration(-1), null, null)
214+
},{ShareStorageException it -> it.errorCode == ShareErrorCode.SHARE_SNAPSHOT_IN_PROGRESS})
209215

210216
then:
211217
resp.getStatusCode() == 201

0 commit comments

Comments
 (0)