nastra commented on code in PR #14161: URL: https://github.com/apache/iceberg/pull/14161#discussion_r2533094651
########## aws/src/test/java/org/apache/iceberg/aws/TestHttpClientCache.java: ########## @@ -0,0 +1,282 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.iceberg.aws; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.concurrent.ConcurrentMap; +import java.util.function.Supplier; +import org.apache.iceberg.aws.HttpClientCache.ManagedHttpClient; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import software.amazon.awssdk.http.SdkHttpClient; + +public class TestHttpClientCache { + + @Mock private SdkHttpClient mockHttpClient1; + @Mock private SdkHttpClient mockHttpClient2; + @Mock private Supplier<SdkHttpClient> mockFactory1; + @Mock private Supplier<SdkHttpClient> mockFactory2; + + private HttpClientCache cache; + + @BeforeEach + public void before() { + MockitoAnnotations.openMocks(this); + cache = HttpClientCache.getInstance(); + // Clean up any existing clients from previous tests + cache.shutdown(); + + when(mockFactory1.get()).thenReturn(mockHttpClient1); + when(mockFactory2.get()).thenReturn(mockHttpClient2); + } + + @Test + public void testSingletonPattern() { + HttpClientCache instance1 = HttpClientCache.getInstance(); + HttpClientCache instance2 = HttpClientCache.getInstance(); + + assertThat(instance1).isSameAs(instance2); + } + + @Test + public void testClientCaching() { + final String cacheKey = "test-key"; + + // First call should create client and increment ref count + SdkHttpClient client1 = cache.getOrCreateClient(cacheKey, mockFactory1); + verify(mockFactory1, times(1)).get(); + + // Second call with same key should return cached client and increment ref count again + SdkHttpClient client2 = cache.getOrCreateClient(cacheKey, mockFactory1); + verify(mockFactory1, times(1)).get(); // Factory should not be called again + + assertThat(client1).isSameAs(client2); + + // Verify reference count is 2 + ManagedHttpClient managedClient = cache.clientMap().get(cacheKey); + assertThat(managedClient.refCount()).isEqualTo(2); + } + + @Test + public void testDifferentKeysCreateDifferentClients() { + final String cacheKey1 = "test-key-1"; Review Comment: can be inlined -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
