Skip to content

Commit 8c39d70

Browse files
committed
Add test
1 parent 35288f9 commit 8c39d70

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/delegation/TestZKDelegationTokenSecretManager.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@
2020

2121
import java.io.IOException;
2222
import java.util.ArrayList;
23+
import java.util.Arrays;
2324
import java.util.List;
2425

26+
import java.util.concurrent.Callable;
27+
import java.util.concurrent.ExecutorService;
28+
import java.util.concurrent.Executors;
29+
import java.util.concurrent.Future;
2530
import java.util.concurrent.TimeUnit;
2631
import java.util.function.Supplier;
2732
import org.apache.curator.RetryPolicy;
@@ -585,4 +590,52 @@ public void testCreateNameSpaceRepeatedly() throws Exception {
585590
"KeeperErrorCode = NodeExists for "+workingPath,
586591
() -> createModeStat.forPath(workingPath));
587592
}
593+
594+
@Test
595+
public void testMultipleInit() throws Exception {
596+
597+
String connectString = zkServer.getConnectString();
598+
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
599+
Configuration conf = getSecretConf(connectString);
600+
CuratorFramework curatorFramework =
601+
CuratorFrameworkFactory.builder()
602+
.connectString(connectString)
603+
.retryPolicy(retryPolicy)
604+
.build();
605+
curatorFramework.start();
606+
ZKDelegationTokenSecretManager.setCurator(curatorFramework);
607+
608+
DelegationTokenManager tm1 = new DelegationTokenManager(conf, new Text("foo"));
609+
DelegationTokenManager tm2 = new DelegationTokenManager(conf, new Text("bar"));
610+
// When the init method is called,
611+
// the ZKDelegationTokenSecretManager#startThread method will be called,
612+
// and the creatingParentContainersIfNeeded will be called to create the nameSpace.
613+
ExecutorService executorService = Executors.newFixedThreadPool(2);
614+
615+
Callable<Boolean> tm1Callable = () -> {
616+
tm1.init();
617+
return true;
618+
};
619+
Callable<Boolean> tm2Callable = () -> {
620+
tm2.init();
621+
return true;
622+
};
623+
List<Future<Boolean>> futures = executorService.invokeAll(Arrays.asList(tm1Callable, tm2Callable));
624+
for(Future<Boolean> future : futures) {
625+
Assert.assertTrue(future.get());
626+
}
627+
executorService.shutdownNow();
628+
Assert.assertTrue(executorService.awaitTermination(1, TimeUnit.SECONDS));
629+
tm1.destroy();
630+
tm2.destroy();
631+
632+
String workingPath = "/" + conf.get(ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH,
633+
ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH_DEAFULT) + "/ZKDTSMRoot";
634+
635+
// Check if the created NameSpace exists.
636+
Stat stat = curatorFramework.checkExists().forPath(workingPath);
637+
Assert.assertNotNull(stat);
638+
639+
curatorFramework.close();
640+
}
588641
}

0 commit comments

Comments
 (0)