gnodet commented on code in PR #26845: URL: https://github.com/apache/camel/pull/26845#discussion_r4132807971
########## components/camel-sql/src/test/java/org/apache/camel/component/sql/DataSourceHelperAgroalIntegrationTest.java: ########## @@ -0,0 +1,98 @@ +/* + * 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.sql; + +import java.sql.Connection; + +import io.agroal.api.AgroalDataSource; +import io.agroal.api.AgroalDataSourceMetrics; +import io.agroal.api.configuration.supplier.AgroalDataSourceConfigurationSupplier; +import io.agroal.api.security.NamePrincipal; +import io.agroal.api.security.SimplePassword; +import org.apache.camel.support.DataSourceHelper; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Integration test for {@link DataSourceHelper#evictDataSourceConnections} against a real {@link AgroalDataSource} + * backed by h2. Verifies that Agroal's {@code flush(GRACEFUL)} is correctly detected and invoked via reflection. + */ +class DataSourceHelperAgroalIntegrationTest { + + private static AgroalDataSource createDataSource(String dbName) throws Exception { + return AgroalDataSource.from(new AgroalDataSourceConfigurationSupplier() + .metricsEnabled() + .connectionPoolConfiguration(pool -> pool + .maxSize(1) + .minSize(1) + .connectionFactoryConfiguration(factory -> factory + .jdbcUrl("jdbc:h2:mem:" + dbName + ";DB_CLOSE_DELAY=-1") + .credential(new NamePrincipal("sa")) + .credential(new SimplePassword(""))))); + } + + @Test + void evict_startedPool_evictsConnections() throws Exception { + try (AgroalDataSource ds = createDataSource("agroal_evict_started")) { + AgroalDataSourceMetrics metrics = ds.getMetrics(); + + // Grab a connection and remember the underlying physical connection + Connection physicalBefore; + try (Connection conn = ds.getConnection()) { + physicalBefore = conn.unwrap(Connection.class); + assertNotNull(physicalBefore); + } + + long flushCountBefore = metrics.flushCount(); + + // Evict — flush(GRACEFUL) via reflection + DataSourceHelper.evictDataSourceConnections(ds, "test-rotation"); + + // GRACEFUL flush marks idle connections for eviction asynchronously; + // allow a short window for Agroal's housekeeping to destroy them + Thread.sleep(500); Review Comment: Fixed in a54a9dcf2af6. Replaced `Thread.sleep(500)` with `await().atMost(5, SECONDS).untilAsserted()` — properly handles the async nature of Agroal's `flush(GRACEFUL)` and complies with the project's no-Thread.sleep rule. -- 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]
