Skip to content

Commit

Permalink
HBASE-22787 Cleaned up tests in hbase-zookeeper
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Somogyi <psomogyi@apache.org>
  • Loading branch information
HorizonNet authored Aug 4, 2019
1 parent a6f0653 commit abb60fa
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
*/
@InterfaceAudience.Public
public class HBaseZKTestingUtility extends HBaseCommonTestingUtility {

private MiniZooKeeperCluster zkCluster;

/**
Expand All @@ -55,8 +54,7 @@ public HBaseZKTestingUtility(Configuration conf) {

/**
* @return Where the cluster will write data on the local subsystem. Creates it if it does not
* exist already. A subdir of {@link #getBaseTestDir()}
* @see #getTestFileSystem()
* exist already. A subdir of {@code HBaseCommonTestingUtility#getBaseTestDir()}
*/
Path getClusterTestDir() {
if (clusterTestDir == null) {
Expand Down Expand Up @@ -124,8 +122,7 @@ private MiniZooKeeperCluster startMiniZKCluster(File dir, int zooKeeperServerNum

if (clientPortList != null) {
// Ignore extra client ports
int clientPortListSize = (clientPortList.length <= zooKeeperServerNum) ? clientPortList.length
: zooKeeperServerNum;
int clientPortListSize = Math.min(clientPortList.length, zooKeeperServerNum);
for (int i = 0; i < clientPortListSize; i++) {
this.zkCluster.addClientPort(clientPortList[i]);
}
Expand Down Expand Up @@ -182,7 +179,7 @@ public boolean isAborted() {
* Gets a ZKWatcher.
*/
public static ZKWatcher getZooKeeperWatcher(HBaseZKTestingUtility testUtil) throws IOException {
ZKWatcher zkw = new ZKWatcher(testUtil.getConfiguration(), "unittest", new Abortable() {
return new ZKWatcher(testUtil.getConfiguration(), "unittest", new Abortable() {
boolean aborted = false;

@Override
Expand All @@ -196,7 +193,6 @@ public boolean isAborted() {
return aborted;
}
});
return zkw;
}

/**
Expand All @@ -207,7 +203,7 @@ public boolean cleanupTestDir() throws IOException {
boolean ret = super.cleanupTestDir();
if (deleteDir(this.clusterTestDir)) {
this.clusterTestDir = null;
return ret & true;
return ret;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.io.IOException;
import java.util.Properties;
Expand All @@ -41,7 +42,6 @@
*/
@Category({ ZKTests.class, MediumTests.class })
public class TestHQuorumPeer {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestHQuorumPeer.class);
Expand Down Expand Up @@ -75,7 +75,7 @@ public class TestHQuorumPeer {
assertEquals(Integer.valueOf(PORT_NO),
Integer.valueOf(properties.getProperty("clientPort")));
assertEquals("localhost:2888:3888", properties.get("server.0"));
assertEquals(null, properties.get("server.1"));
assertNull(properties.get("server.1"));

String oldValue = conf.get(HConstants.ZOOKEEPER_QUORUM);
conf.set(HConstants.ZOOKEEPER_QUORUM, "a.foo.bar,b.foo.bar,c.foo.bar");
Expand All @@ -86,7 +86,7 @@ public class TestHQuorumPeer {
assertEquals("a.foo.bar:2888:3888", properties.get("server.0"));
assertEquals("b.foo.bar:2888:3888", properties.get("server.1"));
assertEquals("c.foo.bar:2888:3888", properties.get("server.2"));
assertEquals(null, properties.get("server.3"));
assertNull(properties.get("server.3"));
conf.set(HConstants.ZOOKEEPER_QUORUM, oldValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ public class TestReadOnlyZKClient {

private static HBaseZKTestingUtility UTIL = new HBaseZKTestingUtility();

private static int PORT;

private static String PATH = "/test";

private static byte[] DATA;
Expand All @@ -84,9 +82,9 @@ public class TestReadOnlyZKClient {

@BeforeClass
public static void setUp() throws Exception {
PORT = UTIL.startMiniZKCluster().getClientPort();
final int port = UTIL.startMiniZKCluster().getClientPort();

ZooKeeper zk = ZooKeeperHelper.getConnectedZooKeeper("localhost:" + PORT, 10000);
ZooKeeper zk = ZooKeeperHelper.getConnectedZooKeeper("localhost:" + port, 10000);
DATA = new byte[10];
ThreadLocalRandom.current().nextBytes(DATA);
zk.create(PATH, DATA, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Expand All @@ -95,7 +93,7 @@ public static void setUp() throws Exception {
}
zk.close();
Configuration conf = UTIL.getConfiguration();
conf.set(HConstants.ZOOKEEPER_QUORUM, "localhost:" + PORT);
conf.set(HConstants.ZOOKEEPER_QUORUM, "localhost:" + port);
conf.setInt(ReadOnlyZKClient.RECOVERY_RETRY, 3);
conf.setInt(ReadOnlyZKClient.RECOVERY_RETRY_INTERVAL_MILLIS, 100);
conf.setInt(ReadOnlyZKClient.KEEPALIVE_MILLIS, 3000);
Expand All @@ -116,12 +114,12 @@ private void waitForIdleConnectionClosed() throws Exception {
UTIL.waitFor(10000, new ExplainingPredicate<Exception>() {

@Override
public boolean evaluate() throws Exception {
public boolean evaluate() {
return RO_ZK.zookeeper == null;
}

@Override
public String explainFailure() throws Exception {
public String explainFailure() {
return "Connection to zookeeper is still alive";
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@

@Category({ ZKTests.class, MediumTests.class })
public class TestRecoverableZooKeeper {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestRecoverableZooKeeper.class);

private final static HBaseZKTestingUtility TEST_UTIL = new HBaseZKTestingUtility();

Abortable abortable = new Abortable() {
private Abortable abortable = new Abortable() {
@Override
public void abort(String why, Throwable e) {

}

@Override
Expand Down Expand Up @@ -94,16 +92,15 @@ public void testSetDataVersionMismatchInLoop() throws Exception {
assertTrue(Bytes.equals(opened, data));
}

class ZookeeperStub extends ZooKeeper {

static class ZookeeperStub extends ZooKeeper {
private int throwExceptionInNumOperations;

public ZookeeperStub(String connectString, int sessionTimeout, Watcher watcher)
ZookeeperStub(String connectString, int sessionTimeout, Watcher watcher)
throws IOException {
super(connectString, sessionTimeout, watcher);
}

public void setThrowExceptionInNumOperations(int throwExceptionInNumOperations) {
void setThrowExceptionInNumOperations(int throwExceptionInNumOperations) {
this.throwExceptionInNumOperations = throwExceptionInNumOperations;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static class MockLeader extends Thread implements Stoppable {
private AtomicBoolean master = new AtomicBoolean(false);
private int index;

public MockLeader(ZKWatcher watcher, int index) {
MockLeader(ZKWatcher watcher, int index) {
setDaemon(true);
setName("TestZKLeaderManager-leader-" + index);
this.index = index;
Expand Down Expand Up @@ -113,7 +113,7 @@ public void run() {
}
}

public void abdicate() {
void abdicate() {
zkLeader.stepDownAsLeader();
master.set(false);
}
Expand Down Expand Up @@ -214,9 +214,9 @@ public void testLeaderSelection() throws Exception {
assertNotNull("New leader should exist", currentLeader);
}

private MockLeader getCurrentLeader() throws Exception {
private MockLeader getCurrentLeader() {
MockLeader currentLeader = null;
outer:

// Wait up to 10 secs for initial leader
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < CANDIDATES.length; j++) {
Expand All @@ -229,18 +229,16 @@ private MockLeader getCurrentLeader() throws Exception {
}
}
if (currentLeader != null) {
break outer;
break;
}
Threads.sleep(100);
}
return currentLeader;
}

private static ZKWatcher newZK(Configuration conf, String name,
Abortable abort) throws Exception {
private static ZKWatcher newZK(Configuration conf, String name, Abortable abort)
throws Exception {
Configuration copy = HBaseConfiguration.create(conf);
ZKWatcher zk = new ZKWatcher(copy, name, abort);
return zk;
return new ZKWatcher(copy, name, abort);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

@Category({ ZKTests.class, SmallTests.class })
public class TestZKMainServer {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestZKMainServer.class);
Expand All @@ -44,10 +43,9 @@ public class TestZKMainServer {
// http://stackoverflow.com/questions/309396/java-how-to-test-methods-that-call-system-exit
protected static class ExitException extends SecurityException {
private static final long serialVersionUID = 1L;
private final int status;
public ExitException(int status) {

ExitException() {
super("There is no escape!");
this.status = status;
}
}

Expand All @@ -65,7 +63,7 @@ public void checkPermission(Permission perm, Object context) {
@Override
public void checkExit(int status) {
super.checkExit(status);
throw new ExitException(status);
throw new ExitException();
}
}

Expand Down Expand Up @@ -116,7 +114,7 @@ public void testHostPortParse() {
// multiple servers with its own port
c.set("hbase.zookeeper.quorum", "example1.com:5678,example2.com:9012,example3.com:3456");
ensemble = parser.parse(c);
assertEquals(ensemble, "example1.com:5678,example2.com:9012,example3.com:3456");
assertEquals("example1.com:5678,example2.com:9012,example3.com:3456", ensemble);

// some servers without its own port, which will be assigned the default client port
c.set("hbase.zookeeper.quorum", "example1.com:5678,example2.com:9012,example3.com");
Expand All @@ -142,11 +140,10 @@ public void testHostPortParse() {
// some servers(IPv6) with an invaild Ipv6 address in it
c.set("hbase.zookeeper.quorum", "[1001:db8:1::242:ac11:8], [2001:db8:1::242:df23:2]:9876," +
"[1001:db8:1::242:ac11:8:89:67]:5678");
ensemble = parser.parse(c);
parser.parse(c);
Assert.fail("IPv6 address should be 8 groups.");
} catch (IllegalArgumentException e) {
//expected
}

}
}
Loading

0 comments on commit abb60fa

Please sign in to comment.