|
3 | 3 | import static org.junit.Assert.*; |
4 | 4 | import com.skyflow.config.Credentials; |
5 | 5 | import com.skyflow.config.VaultConfig; |
| 6 | +import okhttp3.ConnectionPool; |
| 7 | +import okhttp3.OkHttpClient; |
| 8 | +import java.util.concurrent.TimeUnit; |
6 | 9 | import com.skyflow.enums.Env; |
7 | 10 | import com.skyflow.enums.UpsertType; |
8 | 11 | import com.skyflow.errors.ErrorCode; |
@@ -477,6 +480,71 @@ public void testUpdateExecutorInHTTP() throws Exception { |
477 | 480 | Assert.assertNotNull(recordsApi); |
478 | 481 | } |
479 | 482 |
|
| 483 | + @Test |
| 484 | + public void testConnectionPoolMaxIdleConnections() throws Exception { |
| 485 | + Credentials credentials = new Credentials(); |
| 486 | + credentials.setApiKey("sky-ab123-abcd1234cdef1234abcd4321cdef4321"); |
| 487 | + VaultConfig config = new VaultConfig(); |
| 488 | + config.setVaultId(vaultID); |
| 489 | + config.setClusterId(clusterID); |
| 490 | + config.setEnv(Env.PROD); |
| 491 | + config.setCredentials(credentials); |
| 492 | + |
| 493 | + VaultClient client = new VaultClient(config, credentials); |
| 494 | + client.updateExecutorInHTTP(); |
| 495 | + |
| 496 | + OkHttpClient httpClient = (OkHttpClient) getPrivateField(client, "sharedHttpClient"); |
| 497 | + Assert.assertNotNull(httpClient); |
| 498 | + |
| 499 | + ConnectionPool pool = httpClient.connectionPool(); |
| 500 | + Assert.assertNotNull(pool); |
| 501 | + |
| 502 | + Object delegate = getPrivateField(pool, "delegate"); |
| 503 | + int maxIdleConnections = (int) getPrivateField(delegate, "maxIdleConnections"); |
| 504 | + Assert.assertEquals(10, maxIdleConnections); |
| 505 | + } |
| 506 | + |
| 507 | + @Test |
| 508 | + public void testConnectionPoolKeepAliveDuration() throws Exception { |
| 509 | + Credentials credentials = new Credentials(); |
| 510 | + credentials.setApiKey("sky-ab123-abcd1234cdef1234abcd4321cdef4321"); |
| 511 | + VaultConfig config = new VaultConfig(); |
| 512 | + config.setVaultId(vaultID); |
| 513 | + config.setClusterId(clusterID); |
| 514 | + config.setEnv(Env.PROD); |
| 515 | + config.setCredentials(credentials); |
| 516 | + |
| 517 | + VaultClient client = new VaultClient(config, credentials); |
| 518 | + client.updateExecutorInHTTP(); |
| 519 | + |
| 520 | + OkHttpClient httpClient = (OkHttpClient) getPrivateField(client, "sharedHttpClient"); |
| 521 | + ConnectionPool pool = httpClient.connectionPool(); |
| 522 | + |
| 523 | + Object delegate = getPrivateField(pool, "delegate"); |
| 524 | + long keepAliveDurationNs = (long) getPrivateField(delegate, "keepAliveDurationNs"); |
| 525 | + Assert.assertEquals(TimeUnit.MINUTES.toNanos(1), keepAliveDurationNs); |
| 526 | + } |
| 527 | + |
| 528 | + @Test |
| 529 | + public void testSharedHttpClientIsSingleton() throws Exception { |
| 530 | + Credentials credentials = new Credentials(); |
| 531 | + credentials.setApiKey("sky-ab123-abcd1234cdef1234abcd4321cdef4321"); |
| 532 | + VaultConfig config = new VaultConfig(); |
| 533 | + config.setVaultId(vaultID); |
| 534 | + config.setClusterId(clusterID); |
| 535 | + config.setEnv(Env.PROD); |
| 536 | + config.setCredentials(credentials); |
| 537 | + |
| 538 | + VaultClient client = new VaultClient(config, credentials); |
| 539 | + client.updateExecutorInHTTP(); |
| 540 | + OkHttpClient first = (OkHttpClient) getPrivateField(client, "sharedHttpClient"); |
| 541 | + |
| 542 | + client.updateExecutorInHTTP(); |
| 543 | + OkHttpClient second = (OkHttpClient) getPrivateField(client, "sharedHttpClient"); |
| 544 | + |
| 545 | + Assert.assertSame("sharedHttpClient must not be rebuilt on repeated calls", first, second); |
| 546 | + } |
| 547 | + |
480 | 548 | // Helper methods for reflection field access |
481 | 549 | private Object getPrivateField(Object obj, String fieldName) throws Exception { |
482 | 550 | java.lang.reflect.Field field = obj.getClass().getDeclaredField(fieldName); |
@@ -533,6 +601,57 @@ public void testPrioritiseCredentialsThrowsWhenNoCredentials() throws Exception |
533 | 601 | } |
534 | 602 | } |
535 | 603 |
|
| 604 | + @Test |
| 605 | + public void testApiClientNotReinitializedOnSubsequentCalls() throws Exception { |
| 606 | + Credentials credentials = new Credentials(); |
| 607 | + credentials.setApiKey("sky-ab123-abcd1234cdef1234abcd4321cdef4321"); |
| 608 | + VaultConfig config = new VaultConfig(); |
| 609 | + config.setVaultId(vaultID); |
| 610 | + config.setClusterId(clusterID); |
| 611 | + config.setEnv(Env.PROD); |
| 612 | + config.setCredentials(credentials); |
| 613 | + |
| 614 | + VaultClient client = new VaultClient(config, credentials); |
| 615 | + |
| 616 | + client.setBearerToken(); |
| 617 | + Object apiClientFirst = getPrivateField(client, "apiClient"); |
| 618 | + Assert.assertNotNull(apiClientFirst); |
| 619 | + |
| 620 | + client.setBearerToken(); |
| 621 | + Object apiClientSecond = getPrivateField(client, "apiClient"); |
| 622 | + |
| 623 | + Assert.assertSame("apiClient must not be rebuilt on repeated setBearerToken calls", apiClientFirst, apiClientSecond); |
| 624 | + } |
| 625 | + |
| 626 | + @Test |
| 627 | + public void testTokenUpdatedWithoutReinitializingApiClient() throws Exception { |
| 628 | + Credentials credentials = new Credentials(); |
| 629 | + credentials.setApiKey("sky-ab123-abcd1234cdef1234abcd4321cdef4321"); |
| 630 | + VaultConfig config = new VaultConfig(); |
| 631 | + config.setVaultId(vaultID); |
| 632 | + config.setClusterId(clusterID); |
| 633 | + config.setEnv(Env.PROD); |
| 634 | + config.setCredentials(credentials); |
| 635 | + |
| 636 | + VaultClient client = new VaultClient(config, credentials); |
| 637 | + client.setBearerToken(); |
| 638 | + Object apiClientBefore = getPrivateField(client, "apiClient"); |
| 639 | + String tokenBefore = (String) getPrivateField(client, "token"); |
| 640 | + |
| 641 | + // Rotate to a different API key |
| 642 | + Credentials newCredentials = new Credentials(); |
| 643 | + newCredentials.setApiKey("sky-ab123-1234567890abcdef1234567890abcdef"); |
| 644 | + config.setCredentials(newCredentials); |
| 645 | + |
| 646 | + client.setBearerToken(); |
| 647 | + Object apiClientAfter = getPrivateField(client, "apiClient"); |
| 648 | + String tokenAfter = (String) getPrivateField(client, "token"); |
| 649 | + |
| 650 | + Assert.assertSame("apiClient must not be rebuilt after credential rotation", apiClientBefore, apiClientAfter); |
| 651 | + Assert.assertNotEquals("token must reflect the new credential", tokenBefore, tokenAfter); |
| 652 | + Assert.assertEquals("sky-ab123-1234567890abcdef1234567890abcdef", tokenAfter); |
| 653 | + } |
| 654 | + |
536 | 655 | @Test |
537 | 656 | public void testPrioritiseCredentialsSetsFinalCredentialsFromSysCredentials() throws Exception { |
538 | 657 | VaultConfig config = new VaultConfig(); |
|
0 commit comments