|
17 | 17 | package io.cdap.plugin.gcp.gcs; |
18 | 18 |
|
19 | 19 | import com.google.api.client.auth.oauth2.Credential; |
| 20 | +import com.google.auth.oauth2.AccessToken; |
20 | 21 | import com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystemConfiguration; |
21 | 22 | import com.google.cloud.hadoop.util.AccessTokenProvider; |
22 | 23 | import com.google.cloud.hadoop.util.CredentialFactory; |
23 | 24 | import com.google.cloud.hadoop.util.CredentialFromAccessTokenProviderClassFactory; |
24 | 25 | import com.google.cloud.hadoop.util.HadoopCredentialConfiguration; |
25 | 26 | import com.google.common.collect.ImmutableList; |
26 | 27 | import io.cdap.plugin.gcp.common.GCPUtils; |
| 28 | +import io.cdap.plugin.gcp.common.ServerErrorException; |
27 | 29 | import org.apache.hadoop.conf.Configuration; |
28 | 30 | import org.junit.Assert; |
29 | 31 | import org.junit.Test; |
| 32 | +import org.mockito.Mockito; |
| 33 | + |
30 | 34 | import java.io.IOException; |
| 35 | +import java.time.Instant; |
| 36 | +import java.util.Date; |
31 | 37 | import java.util.Map; |
32 | 38 |
|
33 | 39 | /** |
@@ -102,4 +108,36 @@ public void testIsServerErrorWith5xxErrorCode503() { |
102 | 108 | "service account."); |
103 | 109 | Assert.assertTrue(ServiceAccountAccessTokenProvider.isServerError(serverError)); |
104 | 110 | } |
| 111 | + |
| 112 | + @Test(expected = ServerErrorException.class) |
| 113 | + public void testRetryMechanismFailsAfterMaxRetries() throws IOException { |
| 114 | + ServiceAccountAccessTokenProvider provider = Mockito.spy(new ServiceAccountAccessTokenProvider()); |
| 115 | + Mockito.doThrow(new ServerErrorException(503, "Unexpected Error code 503 trying to get security access token " + |
| 116 | + "from Compute Engine metadata for the default service account.", null)) |
| 117 | + .when(provider).retrieveAccessToken(); |
| 118 | + provider.getAccessToken(); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testRetryMechanismSucceedsAfterFewRetries() throws IOException { |
| 123 | + ServiceAccountAccessTokenProvider provider = Mockito.spy(new ServiceAccountAccessTokenProvider()); |
| 124 | + |
| 125 | + // Create a valid token with future expiration |
| 126 | + AccessToken validToken = new AccessToken("valid-token", Date.from(Instant.now().plusSeconds(3600))); |
| 127 | + |
| 128 | + // Fail first 2 attempts, then succeed |
| 129 | + Mockito.doThrow(new ServerErrorException(503, "Unexpected Error code 503 trying to get security access token " + |
| 130 | + "from Compute Engine metadata for the default service account.", null)) |
| 131 | + .doThrow(new ServerErrorException(500, "Unexpected Error code 500 trying to get security access token " + |
| 132 | + "from Compute Engine metadata for the default service account.", null)) |
| 133 | + .doReturn(validToken) |
| 134 | + .when(provider).retrieveAccessToken(); |
| 135 | + AccessTokenProvider.AccessToken accessToken = provider.getAccessToken(); |
| 136 | + |
| 137 | + Assert.assertNotNull(accessToken); |
| 138 | + Assert.assertEquals("valid-token", accessToken.getToken()); |
| 139 | + |
| 140 | + // Verify that retrieveAccessToken was called 3 times (2 failures + 1 success) |
| 141 | + Mockito.verify(provider, Mockito.times(3)).retrieveAccessToken(); |
| 142 | + } |
105 | 143 | } |
0 commit comments