davsclaus commented on code in PR #30623: URL: https://github.com/apache/camel/pull/30623#discussion_r4666133933
########## core/camel-core/src/test/java/org/apache/camel/support/cache/SinglePoolEvictionLockTest.java: ########## @@ -0,0 +1,237 @@ +/* + * 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.support.cache; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.apache.camel.AsyncCallback; +import org.apache.camel.AsyncProducer; +import org.apache.camel.CamelContext; +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.DefaultEndpoint; +import org.apache.camel.support.service.ServiceHelper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * The LRU cache of a ServicePool calls onEvict while it holds its write lock. The stop of an evicted idle singleton + * producer (and the in-use check and stop of its endpoint, which take the lock of the ServicePool) must therefore run + * after the cache operation, not inside the eviction: otherwise a stop of the ServicePool (which holds its lock and + * clears the cache) deadlocks with an acquire that evicts, and a slow stop of the evicted producer blocks every acquire + * of the cache. + */ +class SinglePoolEvictionLockTest extends ContextTestSupport { + + // daemon threads: on failure they stay blocked, and must not keep the JVM alive + private final ExecutorService executor = Executors.newCachedThreadPool(r -> { + Thread t = new Thread(r, "SinglePoolEvictionLockTest"); + t.setDaemon(true); + return t; + }); + + @AfterEach + void shutdownExecutor() { + executor.shutdownNow(); + } + + @Test + void testStopOfThePoolDoesNotDeadlockWithAnAcquireThatEvicts() throws Exception { + PausingServicePool pool = new PausingServicePool(1); + TestEndpoint a = new TestEndpoint("test:a", context, true); + TestEndpoint b = new TestEndpoint("test:b", context, true); + // the stop of the pool stops the pool of b before the pool of a (the order of the pool map) + Map<Endpoint, Boolean> order = new ConcurrentHashMap<>(); + order.put(a, true); + order.put(b, true); + assertSame(b, order.keySet().iterator().next()); Review Comment: Added the comment in 318a3fe9. Checked the claim: with 16 buckets, test:b is in bucket 0 and test:a in bucket 13. _Claude Code on behalf of Claus Ibsen_ -- 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]
