Skip to content

Commit ef1c3f8

Browse files
committed
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
1 parent 976079d commit ef1c3f8

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,35 @@ public void testAuthApiKey() throws IOException, IllegalStateException {
7373
apiKey = AuthTestUtils.createTestApiKey(projectId, keyDisplayName, service, method);
7474
assertNotNull(apiKey);
7575
System.out.println("key string " + apiKey.getKeyString());
76-
String output = ApiKeyAuthExample.authenticateUsingApiKey(apiKey.getKeyString());
76+
String output = authenticateUsingApiKeyWithRetry(apiKey.getKeyString());
7777
assertTrue(output.contains("magnitude:"));
7878
} finally {
7979
if (apiKey != null) {
8080
System.out.println("trying to delete " + apiKey.getKeyString());
81-
// AuthTestUtils.deleteTestApiKey(apiKey.getName());
81+
AuthTestUtils.deleteTestApiKey(apiKey.getName());
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 (IOException 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+
} else {
100+
throw e;
101+
}
102+
}
103+
}
104+
105+
throw new IOException("API key never became active after retries.");
106+
}
85107
}

0 commit comments

Comments
 (0)