gnodet-bot commented on code in PR #29489: URL: https://github.com/apache/camel/pull/29489#discussion_r4541289738
########## components/camel-azure/camel-azure-iothub/src/test/java/org/apache/camel/component/azure/iothub/client/DeviceClientPoolTest.java: ########## @@ -0,0 +1,417 @@ +/* + * 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.camel.component.azure.iothub.client; + +import java.lang.reflect.Field; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; + +import com.microsoft.azure.sdk.iot.device.ConnectionStatusChangeContext; +import com.microsoft.azure.sdk.iot.device.DeviceClient; +import com.microsoft.azure.sdk.iot.device.IotHubClientProtocol; +import com.microsoft.azure.sdk.iot.device.IotHubConnectionStatusChangeCallback; +import com.microsoft.azure.sdk.iot.device.IotHubConnectionStatusChangeReason; +import com.microsoft.azure.sdk.iot.device.transport.IotHubConnectionStatus; +import org.awaitility.Awaitility; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +class DeviceClientPoolTest { + + private static final String CS = "HostName=h;DeviceId=d;SharedAccessKey=k"; + + @Test + void testLeaseInUseIsReusedAndReleased() throws Exception { + DeviceClient client = mock(DeviceClient.class); + DeviceClientPool pool = new DeviceClientPool(Duration.ofMinutes(5), Duration.ofSeconds(30)); + pool.setDeviceClientFactory((cs, protocol) -> client); + + try (DeviceClientPool.Lease first = pool.lease("d", CS, IotHubClientProtocol.MQTT)) { + assertSame(client, first.getClient()); + assertEquals(1, pool.getActiveClientCount()); + + try (DeviceClientPool.Lease second = pool.lease("d", CS, IotHubClientProtocol.MQTT)) { + assertSame(client, second.getClient()); + assertEquals(1, pool.getActiveClientCount()); + } + } + + assertEquals(1, pool.getActiveClientCount()); + } + + @Test + void testConcurrentFirstAcquireCreatesSingleClient() throws Exception { + DeviceClient client = mock(DeviceClient.class); + AtomicInteger created = new AtomicInteger(); + CountDownLatch factoryStarted = new CountDownLatch(1); + CountDownLatch releaseFactory = new CountDownLatch(1); + DeviceClientPool pool = new DeviceClientPool(Duration.ofMinutes(5), Duration.ofSeconds(30)); + pool.setDeviceClientFactory((cs, protocol) -> { + created.incrementAndGet(); + factoryStarted.countDown(); + try { + assertTrue(releaseFactory.await(5, TimeUnit.SECONDS), "factory should be released after both tasks start"); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException(e); + } + return client; + }); + + ExecutorService executor = Executors.newFixedThreadPool(2); + List<DeviceClientPool.Lease> leases = new ArrayList<>(); + List<Future<?>> futures = new ArrayList<>(); + + for (int i = 0; i < 2; i++) { + futures.add(executor.submit(() -> { + DeviceClientPool.Lease lease = pool.lease("d", CS, IotHubClientProtocol.MQTT); + synchronized (leases) { + leases.add(lease); + } + return null; + })); + } + + assertTrue(factoryStarted.await(5, TimeUnit.SECONDS), "one thread should open the first client"); + releaseFactory.countDown(); + executor.shutdown(); + assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS), "worker tasks did not finish"); + for (Future<?> future : futures) { + future.get(); + } + + assertEquals(1, created.get(), "only one client should be created for first acquire"); + assertEquals(1, pool.getActiveClientCount()); + for (DeviceClientPool.Lease lease : leases) { + lease.close(); + } + } + + @Test + void testGetOrCreateCreatesOnceAndReuses() throws Exception { + AtomicInteger created = new AtomicInteger(); + DeviceClient client = mock(DeviceClient.class); + + DeviceClientPool pool = new DeviceClientPool(Duration.ofMinutes(5), Duration.ofSeconds(30)); + pool.setDeviceClientFactory((cs, protocol) -> { + created.incrementAndGet(); + return client; + }); + + DeviceClient first = pool.getOrCreate("d", CS, IotHubClientProtocol.MQTT); + DeviceClient second = pool.getOrCreate("d", CS, IotHubClientProtocol.MQTT); + + assertSame(client, first); + assertSame(first, second); + assertEquals(1, created.get(), "client should be created once and reused"); + assertEquals(1, pool.getActiveClientCount()); + verify(client, times(1)).open(true); + } + + @Test + void testRejectsMismatchedDeviceId() { + DeviceClientPool pool = new DeviceClientPool(Duration.ofMinutes(5), Duration.ofSeconds(30)); + pool.setDeviceClientFactory((cs, protocol) -> mock(DeviceClient.class)); + + assertThrows(IllegalArgumentException.class, () -> pool.getOrCreate("dev1", CS, IotHubClientProtocol.MQTT)); + } + + @Test + void testRemoveClosesClient() throws Exception { + DeviceClient client = mock(DeviceClient.class); + DeviceClientPool pool = new DeviceClientPool(Duration.ofMinutes(5), Duration.ofSeconds(30)); + pool.setDeviceClientFactory((cs, protocol) -> client); + + pool.getOrCreate("d", CS, IotHubClientProtocol.MQTT); + pool.remove("d"); + + assertEquals(0, pool.getActiveClientCount()); + verify(client, times(1)).close(); + } + + @Test + void testShutdownClosesAllAndBlocksCreation() throws Exception { + DeviceClient client = mock(DeviceClient.class); + DeviceClientPool pool = new DeviceClientPool(Duration.ofMinutes(5), Duration.ofSeconds(30)); + pool.setDeviceClientFactory((cs, protocol) -> client); + + pool.getOrCreate("d", CS, IotHubClientProtocol.MQTT); + pool.shutdown(); + + assertEquals(0, pool.getActiveClientCount()); + verify(client, times(1)).close(); + assertThrows(IllegalStateException.class, () -> pool.getOrCreate("d", CS, IotHubClientProtocol.MQTT)); + } + + @Test + void testIdleClientsAreEvicted() throws Exception { + DeviceClient client = mock(DeviceClient.class); + AtomicLong clock = new AtomicLong(0); + + DeviceClientPool pool = new DeviceClientPool(Duration.ofMillis(10), Duration.ofMillis(20)); + pool.setDeviceClientFactory((cs, protocol) -> client); + pool.setNowMillisSupplier(clock::get); + pool.start(); + try { + pool.getOrCreate("d", CS, IotHubClientProtocol.MQTT); + assertEquals(1, pool.getActiveClientCount()); + + clock.set(10_000); + Awaitility.await().atMost(2, TimeUnit.SECONDS).until(() -> pool.getActiveClientCount() == 0); + assertEquals(0, pool.getActiveClientCount(), "idle client should have been evicted"); + verify(client, Mockito.atLeastOnce()).close(); + } finally { + pool.shutdown(); + } + } + + @Test + void testCreationFailurePropagates() { + DeviceClientPool pool = new DeviceClientPool(Duration.ofMinutes(5), Duration.ofSeconds(30)); + pool.setDeviceClientFactory((cs, protocol) -> { + throw new IllegalArgumentException("bad connection string"); + }); + + assertThrows(RuntimeException.class, () -> pool.getOrCreate("d", CS, IotHubClientProtocol.MQTT)); + assertEquals(0, pool.getActiveClientCount()); + } + + @Test + void testCreationErrorCompletesFutureForWaiters() throws Exception { + DeviceClientPool pool = new DeviceClientPool(Duration.ofMinutes(5), Duration.ofSeconds(30)); + CountDownLatch factoryStarted = new CountDownLatch(1); + CountDownLatch waiterReady = new CountDownLatch(1); + CountDownLatch allowFactoryToThrow = new CountDownLatch(1); + AtomicReference<Throwable> creatorFailure = new AtomicReference<>(); + AtomicReference<Throwable> waiterFailure = new AtomicReference<>(); + + pool.setDeviceClientFactory((cs, protocol) -> { + factoryStarted.countDown(); + try { + if (!allowFactoryToThrow.await(5, TimeUnit.SECONDS)) { + throw new IllegalStateException("factory should be released after the waiter starts"); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("factory interrupted while waiting to fail", e); + } + throw new NoClassDefFoundError("missing Azure SDK transitive"); + }); + + Thread creator = new Thread(() -> { + try { + pool.getOrCreate("d", CS, IotHubClientProtocol.MQTT); + } catch (Throwable t) { + creatorFailure.set(t); + } + }, "device-client-pool-creator"); + + Thread waiter = new Thread(() -> { + waiterReady.countDown(); Review Comment: ⚠️ **Minor test robustness gap** `waiterReady.countDown()` fires *before* `pool.getOrCreate()` is called on the waiter thread. A scheduler preemption in that window could cause the waiter to create its own client rather than piggyback on the in-flight creation future — meaning this test would validate error propagation via the direct-creation path instead of the future-sharing path. In practice this is vanishingly unlikely: the factory is blocked on `allowFactoryToThrow` while the waiter starts, so the in-flight future is almost certainly still active when the waiter enters `getOrCreate()`. But a second `CountDownLatch` signalled from *inside* `getOrCreate()` (before the pool lock is released) would make the synchronisation deterministic. Not blocking — noting for completeness. -- 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]
