allthingssecurity commented on code in PR #26861: URL: https://github.com/apache/camel/pull/26861#discussion_r4113740085
########## core/camel-core/src/test/java/org/apache/camel/impl/ProducerCacheNonSingletonEvictionTest.java: ########## @@ -0,0 +1,164 @@ +/* + * 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.impl; + +import java.util.Map; + +import org.apache.camel.AsyncCallback; +import org.apache.camel.Component; +import org.apache.camel.Consumer; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.support.DefaultAsyncProducer; +import org.apache.camel.support.DefaultComponent; +import org.apache.camel.support.DefaultEndpoint; +import org.apache.camel.support.cache.DefaultProducerCache; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Non-singleton producers are pooled per endpoint. When one of them is evicted from the producer cache, it must not be + * handed out again once it is stopped, and it must not be stopped while it is in use. + */ +class ProducerCacheNonSingletonEvictionTest extends ContextTestSupport { Review Comment: Thanks, added in d76cd4feb. A cache size of 1 cannot reach the `p == null` branch: evicting the last cache entry of a pool is what stops the pool, so no producer of that pool is left in the cache afterwards. So the tests keep a cache size of 2 and use more endpoints: - `testStopPoolWhileProducersInUse`: a1 and a2 of endpoint a are in use; a third pool stops the pool of a when a1 is evicted, and a2 is evicted after its pool is gone (the `p == null` branch). Both stay started while in use, are stopped when released, and the next `acquire(a)` returns a new producer. This one fails on main, which stops a2 while it is in use. - `testStopPoolWithEvictedIdleProducer`: an idle b1 is evicted by the eviction that also stops its pool, so it is only stopped by the `cleanupEvicts()` in `stop()` (the pool is no longer cleaned up afterwards). The `stopped` check in `release` is only reachable when `release` races with `stop()`, so it is not covered by a deterministic test. _Claude Code on behalf of allthingssecurity_ ########## core/camel-support/src/main/java/org/apache/camel/support/cache/ServicePool.java: ########## @@ -92,9 +93,13 @@ protected void onEvict(S s) { // the pool is growing too large, so we need to stop (stop will remove itself from pool) p.stop(); } - } else { + } else if (e.isSingletonProducer()) { // service no longer in a pool (such as being released twice, or can happen during shutdown of Camel etc) stopAndRemove(s); + } else { + // the pool has been stopped, which stopped its idle services, so this service is either stopped already Review Comment: Right, reworded in d76cd4feb: the comment now says there is no pool for the endpoint (it was stopped, or the endpoint of the service is not the instance used as the pool key) and that the service is not stopped there because it may be in use or idle in a live pool. _Claude Code on behalf of allthingssecurity_ -- 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]
