diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ActiveStandbyElector.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ActiveStandbyElector.java index cb2e08143e3..9a90ccfdadb 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ActiveStandbyElector.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ha/ActiveStandbyElector.java @@ -667,13 +667,13 @@ synchronized void processWatchEvent(ZooKeeper zk, WatchedEvent event) { /** * Get a new zookeeper client instance. protected so that test class can - * inherit and pass in a mock object for zookeeper + * inherit and mock out the zookeeper instance * * @return new zookeeper client instance * @throws IOException * @throws KeeperException zookeeper connectionloss exception */ - protected synchronized ZooKeeper getNewZooKeeper() throws IOException, + protected synchronized ZooKeeper connectToZooKeeper() throws IOException, KeeperException { // Unfortunately, the ZooKeeper constructor connects to ZooKeeper and @@ -682,7 +682,7 @@ protected synchronized ZooKeeper getNewZooKeeper() throws IOException, // we construct the watcher first, and have it block any events it receives // before we can set its ZooKeeper reference. watcher = new WatcherWithClientRef(); - ZooKeeper zk = new ZooKeeper(zkHostPort, zkSessionTimeout, watcher); + ZooKeeper zk = createZooKeeper(); watcher.setZooKeeperRef(zk); // Wait for the asynchronous success/failure. This may throw an exception @@ -695,6 +695,17 @@ protected synchronized ZooKeeper getNewZooKeeper() throws IOException, return zk; } + /** + * Get a new zookeeper client instance. protected so that test class can + * inherit and pass in a mock object for zookeeper + * + * @return new zookeeper client instance + * @throws IOException + */ + protected ZooKeeper createZooKeeper() throws IOException { + return new ZooKeeper(zkHostPort, zkSessionTimeout, watcher); + } + private void fatalError(String errorMessage) { LOG.fatal(errorMessage); reset(); @@ -830,7 +841,7 @@ private void createConnection() throws IOException, KeeperException { zkClient = null; watcher = null; } - zkClient = getNewZooKeeper(); + zkClient = connectToZooKeeper(); if (LOG.isDebugEnabled()) { LOG.debug("Created new connection for " + this); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestActiveStandbyElector.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestActiveStandbyElector.java index 83a3a4f8c85..8c6f7a3300a 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestActiveStandbyElector.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ha/TestActiveStandbyElector.java @@ -66,7 +66,7 @@ class ActiveStandbyElectorTester extends ActiveStandbyElector { } @Override - public ZooKeeper getNewZooKeeper() { + public ZooKeeper connectToZooKeeper() { ++count; return mockZK; } @@ -749,7 +749,15 @@ public void testWithoutZKServer() throws Exception { try { new ActiveStandbyElector("127.0.0.1", 2000, ZK_PARENT_NAME, Ids.OPEN_ACL_UNSAFE, Collections. emptyList(), mockApp, - CommonConfigurationKeys.HA_FC_ELECTOR_ZK_OP_RETRIES_DEFAULT); + CommonConfigurationKeys.HA_FC_ELECTOR_ZK_OP_RETRIES_DEFAULT) { + + @Override + protected ZooKeeper createZooKeeper() throws IOException { + return Mockito.mock(ZooKeeper.class); + } + }; + + Assert.fail("Did not throw zookeeper connection loss exceptions!"); } catch (KeeperException ke) { GenericTestUtils.assertExceptionContains( "ConnectionLoss", ke);