zentol commented on a change in pull request #16345: URL: https://github.com/apache/flink/pull/16345#discussion_r668619127
########## File path: flink-rpc/flink-rpc-akka/src/test/java/org/apache/flink/runtime/rpc/akka/ContextClassLoadingSettingTest.java ########## @@ -0,0 +1,381 @@ +/* + * 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.flink.runtime.rpc.akka; + +import org.apache.flink.api.common.time.Time; +import org.apache.flink.runtime.concurrent.akka.AkkaFutureUtils; +import org.apache.flink.runtime.rpc.RpcEndpoint; +import org.apache.flink.runtime.rpc.RpcGateway; +import org.apache.flink.runtime.rpc.RpcService; +import org.apache.flink.util.TestLogger; +import org.apache.flink.util.concurrent.FutureUtils; + +import akka.actor.ActorSystem; +import akka.actor.Terminated; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Arrays; +import java.util.concurrent.Callable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.apache.flink.runtime.concurrent.akka.ClassLoadingUtils.runWithContextClassLoader; +import static org.hamcrest.CoreMatchers.either; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +/** + * Tests the context class loader handling in various parts of the akka rpc system. + * + * <p>The tests check cases where we call from the akka rpc system into Flink, in which case the + * context class loader must be set to the Flink class loader. This ensures that the Akka class + * loader does not get accidentally leaked, e.g., via thread locals or thread pools on the Flink + * side. + */ +public class ContextClassLoadingSettingTest extends TestLogger { + + private static final Time TIMEOUT = Time.milliseconds(10000L); + + // Many of the contained tests assert that a future is completed with a specific context class + // loader by applying a synchronous operation. + // If the initial future is completed by the time we apply the synchronous operation the test + // thread will execute the operation instead. The tests are thus susceptible to timing issues. + // We hence take a probabilistic approach: Assume that this timing is rare, guard these calls in + // the test with a temporary class loader context, and assert that the actually used + // context class loader is _either_ the one we truly expect or the temporary one. + private static final ClassLoader testClassLoader = + new URLClassLoader(new URL[0], ContextClassLoadingSettingTest.class.getClassLoader()); + + private ClassLoader pretendFlinkClassLoader; + private ActorSystem actorSystem; + private AkkaRpcService akkaRpcService; + + @Before + public void setup() { + pretendFlinkClassLoader = + new URLClassLoader( + new URL[0], ContextClassLoadingSettingTest.class.getClassLoader()); + actorSystem = AkkaUtils.createDefaultActorSystem(); + akkaRpcService = + new AkkaRpcService( + actorSystem, + AkkaRpcServiceConfiguration.defaultConfiguration(), + pretendFlinkClassLoader); + } + + @After Review comment: > Ok, I see, we stop it in a test. Yes until that test was added it was reused across tests. -- 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]
