Skip to content

Commit 538376d

Browse files
authored
fix(auth): AuthExampleIT flaky test (#10024)
* fix: there was a race condition when running auth test where keys were getting deleted while still being used, so make each key name unique * validate which key is being used/deleted * validate which key is being used/deleted * validate which key is being used/deleted * clean up test strings * use random number generator * confirm deleting is the problem * confirm deleting is the problem * this error might be misleading there might be a delay in creating and API being available, so add additional tries if it fails for that reason * fix lint * fixed exception I was catching * fixed exception I was catching * lint fix * remove debug line
1 parent 6d0a235 commit 538376d

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

auth/src/test/java/com/google/cloud/auth/samples/AuthExampleIT.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import static org.junit.Assert.assertTrue;
2121

2222
import com.google.api.apikeys.v2.Key;
23+
import com.google.api.gax.rpc.InvalidArgumentException;
2324
import com.google.cloud.ServiceOptions;
25+
import io.grpc.StatusRuntimeException;
2426
import java.io.ByteArrayOutputStream;
2527
import java.io.IOException;
2628
import java.io.PrintStream;
@@ -61,10 +63,8 @@ public void testAuthExplicitNoPath() throws IOException {
6163
assertTrue(output.contains("Buckets:"));
6264
}
6365

64-
@Ignore("Temporarily disabled due to failing test (Issue #10023).")
6566
@Test
6667
public void testAuthApiKey() throws IOException, IllegalStateException {
67-
//TODO: Re-enable this test after fixing issue #10023.
6868
String projectId = ServiceOptions.getDefaultProjectId();
6969
String keyDisplayName = "Test API Key";
7070
String service = "language.googleapis.com";
@@ -73,7 +73,7 @@ public void testAuthApiKey() throws IOException, IllegalStateException {
7373
try {
7474
apiKey = AuthTestUtils.createTestApiKey(projectId, keyDisplayName, service, method);
7575

76-
String output = ApiKeyAuthExample.authenticateUsingApiKey(apiKey.getKeyString());
76+
String output = authenticateUsingApiKeyWithRetry(apiKey.getKeyString());
7777

7878
assertTrue(output.contains("magnitude:"));
7979
} finally {
@@ -82,4 +82,28 @@ public void testAuthApiKey() throws IOException, IllegalStateException {
8282
}
8383
}
8484
}
85+
86+
static String authenticateUsingApiKeyWithRetry(String apiKey) throws IOException {
87+
int retries = 5;
88+
int delay = 2000; // 2 seconds
89+
90+
for (int i = 0; i < retries; i++) {
91+
try {
92+
return ApiKeyAuthExample.authenticateUsingApiKey(apiKey);
93+
} catch (StatusRuntimeException | InvalidArgumentException e) {
94+
if (e.getMessage().contains("API key expired")) {
95+
System.out.println("API key not yet active, retrying...");
96+
try {
97+
Thread.sleep(delay);
98+
} catch (InterruptedException ignored) {
99+
// ignore iterrupted exception and retry test
100+
}
101+
} else {
102+
throw e;
103+
}
104+
}
105+
}
106+
107+
throw new IOException("API key never became active after retries.");
108+
}
85109
}

0 commit comments

Comments
 (0)