From d69088a097ff6f6bb25203bdc8ac5b71f8243c97 Mon Sep 17 00:00:00 2001 From: Akira Ajisaka Date: Thu, 1 Apr 2021 01:57:31 +0900 Subject: [PATCH] HADOOP-17608. Fix NPE in TestKMS (#2828) --- .../hadoop/crypto/key/kms/server/TestKMS.java | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMS.java b/hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMS.java index 3d59e6f5be..dbe685b6a3 100644 --- a/hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMS.java +++ b/hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMS.java @@ -18,6 +18,8 @@ package org.apache.hadoop.crypto.key.kms.server; import java.util.function.Supplier; + +import org.apache.commons.lang3.ThreadUtils; import org.apache.hadoop.thirdparty.com.google.common.cache.LoadingCache; import org.apache.curator.test.TestingServer; import org.apache.hadoop.conf.Configuration; @@ -525,6 +527,7 @@ public void testStartStop(final boolean ssl, final boolean kerberos) if (ssl) { sslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, conf); try { + // the first reloader thread is created here sslFactory.init(); } catch (GeneralSecurityException ex) { throw new IOException(ex); @@ -541,31 +544,29 @@ public Void call() throws Exception { final URI uri = createKMSUri(getKMSUrl()); if (ssl) { + // the second reloader thread is created here KeyProvider testKp = createProvider(uri, conf); - ThreadGroup threadGroup = Thread.currentThread().getThreadGroup(); - while (threadGroup.getParent() != null) { - threadGroup = threadGroup.getParent(); - } - Thread[] threads = new Thread[threadGroup.activeCount()]; - threadGroup.enumerate(threads); - Thread reloaderThread = null; - for (Thread thread : threads) { - if ((thread.getName() != null) - && (thread.getName().contains(SSL_RELOADER_THREAD_NAME))) { - reloaderThread = thread; - } - } - Assert.assertTrue("Reloader is not alive", reloaderThread.isAlive()); - // Explicitly close the provider so we can verify the internal thread - // is shutdown + Collection reloaderThreads = + ThreadUtils.findThreadsByName(SSL_RELOADER_THREAD_NAME); + // now there are two active reloader threads + assertEquals(2, reloaderThreads.size()); + // Explicitly close the provider so we can verify + // the second reloader thread is shutdown testKp.close(); boolean reloaderStillAlive = true; for (int i = 0; i < 10; i++) { - reloaderStillAlive = reloaderThread.isAlive(); - if (!reloaderStillAlive) break; + for (Thread thread : reloaderThreads) { + if (!thread.isAlive()) { + reloaderStillAlive = false; + break; + } + } Thread.sleep(1000); } Assert.assertFalse("Reloader is still alive", reloaderStillAlive); + reloaderThreads = + ThreadUtils.findThreadsByName(SSL_RELOADER_THREAD_NAME); + assertEquals(1, reloaderThreads.size()); } if (kerberos) {