pan3793 commented on code in PR #8522: URL: https://github.com/apache/hadoop/pull/8522#discussion_r3471553724
########## hadoop-common-project/hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/util/TestSubjectPropagation.java: ########## @@ -0,0 +1,179 @@ +/** + * 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.hadoop.security.authentication.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; +import javax.security.auth.Subject; + +/** + * Verifies the JDK22+ Subject-propagation cascade restored by the + * {@link InheritableThreadLocal}-based mechanism in + * {@link SubjectUtil#callAs(Subject, java.util.concurrent.Callable)}. + * + * <p>The mechanism relies solely on Hadoop's own InheritableThreadLocal layer and + * the JVM's standard {@code Thread.<init>}-time {@code InheritableThreadLocal} copy. + * No special {@code Thread} subclass is required: any platform thread ({@link Thread}, + * {@link java.util.concurrent.ForkJoinWorkerThread}, Netty's + * {@code FastThreadLocalThread}, …) constructed inside a {@code SubjectUtil.callAs} + * scope inherits the active Subject and observes it via {@link SubjectUtil#current()}. + */ +public class TestSubjectPropagation { + + /** Plain Thread inside callAs sees parent's Subject via SubjectUtil.current(). */ + @Test + public void testPlainThreadInheritsSubjectViaSubjectUtilCallAs() { + Subject parent = new Subject(); + AtomicReference<Subject> seen = new AtomicReference<>(); + SubjectUtil.callAs(parent, () -> { + Thread t = new Thread(() -> seen.set(SubjectUtil.current()), "plain-child"); + t.start(); + t.join(50000); + return null; + }); + assertEquals(parent, seen.get(), Review Comment: updated -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
