Skip to content

Commit abb60fa

Browse files
authored
HBASE-22787 Cleaned up tests in hbase-zookeeper
Signed-off-by: Peter Somogyi <[email protected]>
1 parent a6f0653 commit abb60fa

File tree

10 files changed

+83
-109
lines changed

10 files changed

+83
-109
lines changed

hbase-zookeeper/src/test/java/org/apache/hadoop/hbase/HBaseZKTestingUtility.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
*/
3333
@InterfaceAudience.Public
3434
public class HBaseZKTestingUtility extends HBaseCommonTestingUtility {
35-
3635
private MiniZooKeeperCluster zkCluster;
3736

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

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

125123
if (clientPortList != null) {
126124
// Ignore extra client ports
127-
int clientPortListSize = (clientPortList.length <= zooKeeperServerNum) ? clientPortList.length
128-
: zooKeeperServerNum;
125+
int clientPortListSize = Math.min(clientPortList.length, zooKeeperServerNum);
129126
for (int i = 0; i < clientPortListSize; i++) {
130127
this.zkCluster.addClientPort(clientPortList[i]);
131128
}
@@ -182,7 +179,7 @@ public boolean isAborted() {
182179
* Gets a ZKWatcher.
183180
*/
184181
public static ZKWatcher getZooKeeperWatcher(HBaseZKTestingUtility testUtil) throws IOException {
185-
ZKWatcher zkw = new ZKWatcher(testUtil.getConfiguration(), "unittest", new Abortable() {
182+
return new ZKWatcher(testUtil.getConfiguration(), "unittest", new Abortable() {
186183
boolean aborted = false;
187184

188185
@Override
@@ -196,7 +193,6 @@ public boolean isAborted() {
196193
return aborted;
197194
}
198195
});
199-
return zkw;
200196
}
201197

202198
/**
@@ -207,7 +203,7 @@ public boolean cleanupTestDir() throws IOException {
207203
boolean ret = super.cleanupTestDir();
208204
if (deleteDir(this.clusterTestDir)) {
209205
this.clusterTestDir = null;
210-
return ret & true;
206+
return ret;
211207
}
212208
return false;
213209
}

hbase-zookeeper/src/test/java/org/apache/hadoop/hbase/zookeeper/TestHQuorumPeer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertNull;
2223

2324
import java.io.IOException;
2425
import java.util.Properties;
@@ -41,7 +42,6 @@
4142
*/
4243
@Category({ ZKTests.class, MediumTests.class })
4344
public class TestHQuorumPeer {
44-
4545
@ClassRule
4646
public static final HBaseClassTestRule CLASS_RULE =
4747
HBaseClassTestRule.forClass(TestHQuorumPeer.class);
@@ -75,7 +75,7 @@ public class TestHQuorumPeer {
7575
assertEquals(Integer.valueOf(PORT_NO),
7676
Integer.valueOf(properties.getProperty("clientPort")));
7777
assertEquals("localhost:2888:3888", properties.get("server.0"));
78-
assertEquals(null, properties.get("server.1"));
78+
assertNull(properties.get("server.1"));
7979

8080
String oldValue = conf.get(HConstants.ZOOKEEPER_QUORUM);
8181
conf.set(HConstants.ZOOKEEPER_QUORUM, "a.foo.bar,b.foo.bar,c.foo.bar");
@@ -86,7 +86,7 @@ public class TestHQuorumPeer {
8686
assertEquals("a.foo.bar:2888:3888", properties.get("server.0"));
8787
assertEquals("b.foo.bar:2888:3888", properties.get("server.1"));
8888
assertEquals("c.foo.bar:2888:3888", properties.get("server.2"));
89-
assertEquals(null, properties.get("server.3"));
89+
assertNull(properties.get("server.3"));
9090
conf.set(HConstants.ZOOKEEPER_QUORUM, oldValue);
9191
}
9292

hbase-zookeeper/src/test/java/org/apache/hadoop/hbase/zookeeper/TestReadOnlyZKClient.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ public class TestReadOnlyZKClient {
7272

7373
private static HBaseZKTestingUtility UTIL = new HBaseZKTestingUtility();
7474

75-
private static int PORT;
76-
7775
private static String PATH = "/test";
7876

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

8583
@BeforeClass
8684
public static void setUp() throws Exception {
87-
PORT = UTIL.startMiniZKCluster().getClientPort();
85+
final int port = UTIL.startMiniZKCluster().getClientPort();
8886

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

118116
@Override
119-
public boolean evaluate() throws Exception {
117+
public boolean evaluate() {
120118
return RO_ZK.zookeeper == null;
121119
}
122120

123121
@Override
124-
public String explainFailure() throws Exception {
122+
public String explainFailure() {
125123
return "Connection to zookeeper is still alive";
126124
}
127125
});

hbase-zookeeper/src/test/java/org/apache/hadoop/hbase/zookeeper/TestRecoverableZooKeeper.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,15 @@
4343

4444
@Category({ ZKTests.class, MediumTests.class })
4545
public class TestRecoverableZooKeeper {
46-
4746
@ClassRule
4847
public static final HBaseClassTestRule CLASS_RULE =
4948
HBaseClassTestRule.forClass(TestRecoverableZooKeeper.class);
5049

5150
private final static HBaseZKTestingUtility TEST_UTIL = new HBaseZKTestingUtility();
5251

53-
Abortable abortable = new Abortable() {
52+
private Abortable abortable = new Abortable() {
5453
@Override
5554
public void abort(String why, Throwable e) {
56-
5755
}
5856

5957
@Override
@@ -94,16 +92,15 @@ public void testSetDataVersionMismatchInLoop() throws Exception {
9492
assertTrue(Bytes.equals(opened, data));
9593
}
9694

97-
class ZookeeperStub extends ZooKeeper {
98-
95+
static class ZookeeperStub extends ZooKeeper {
9996
private int throwExceptionInNumOperations;
10097

101-
public ZookeeperStub(String connectString, int sessionTimeout, Watcher watcher)
98+
ZookeeperStub(String connectString, int sessionTimeout, Watcher watcher)
10299
throws IOException {
103100
super(connectString, sessionTimeout, watcher);
104101
}
105102

106-
public void setThrowExceptionInNumOperations(int throwExceptionInNumOperations) {
103+
void setThrowExceptionInNumOperations(int throwExceptionInNumOperations) {
107104
this.throwExceptionInNumOperations = throwExceptionInNumOperations;
108105
}
109106

hbase-zookeeper/src/test/java/org/apache/hadoop/hbase/zookeeper/TestZKLeaderManager.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private static class MockLeader extends Thread implements Stoppable {
7777
private AtomicBoolean master = new AtomicBoolean(false);
7878
private int index;
7979

80-
public MockLeader(ZKWatcher watcher, int index) {
80+
MockLeader(ZKWatcher watcher, int index) {
8181
setDaemon(true);
8282
setName("TestZKLeaderManager-leader-" + index);
8383
this.index = index;
@@ -113,7 +113,7 @@ public void run() {
113113
}
114114
}
115115

116-
public void abdicate() {
116+
void abdicate() {
117117
zkLeader.stepDownAsLeader();
118118
master.set(false);
119119
}
@@ -214,9 +214,9 @@ public void testLeaderSelection() throws Exception {
214214
assertNotNull("New leader should exist", currentLeader);
215215
}
216216

217-
private MockLeader getCurrentLeader() throws Exception {
217+
private MockLeader getCurrentLeader() {
218218
MockLeader currentLeader = null;
219-
outer:
219+
220220
// Wait up to 10 secs for initial leader
221221
for (int i = 0; i < 1000; i++) {
222222
for (int j = 0; j < CANDIDATES.length; j++) {
@@ -229,18 +229,16 @@ private MockLeader getCurrentLeader() throws Exception {
229229
}
230230
}
231231
if (currentLeader != null) {
232-
break outer;
232+
break;
233233
}
234234
Threads.sleep(100);
235235
}
236236
return currentLeader;
237237
}
238238

239-
private static ZKWatcher newZK(Configuration conf, String name,
240-
Abortable abort) throws Exception {
239+
private static ZKWatcher newZK(Configuration conf, String name, Abortable abort)
240+
throws Exception {
241241
Configuration copy = HBaseConfiguration.create(conf);
242-
ZKWatcher zk = new ZKWatcher(copy, name, abort);
243-
return zk;
242+
return new ZKWatcher(copy, name, abort);
244243
}
245-
246244
}

hbase-zookeeper/src/test/java/org/apache/hadoop/hbase/zookeeper/TestZKMainServer.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
@Category({ ZKTests.class, SmallTests.class })
3737
public class TestZKMainServer {
38-
3938
@ClassRule
4039
public static final HBaseClassTestRule CLASS_RULE =
4140
HBaseClassTestRule.forClass(TestZKMainServer.class);
@@ -44,10 +43,9 @@ public class TestZKMainServer {
4443
// http://stackoverflow.com/questions/309396/java-how-to-test-methods-that-call-system-exit
4544
protected static class ExitException extends SecurityException {
4645
private static final long serialVersionUID = 1L;
47-
private final int status;
48-
public ExitException(int status) {
46+
47+
ExitException() {
4948
super("There is no escape!");
50-
this.status = status;
5149
}
5250
}
5351

@@ -65,7 +63,7 @@ public void checkPermission(Permission perm, Object context) {
6563
@Override
6664
public void checkExit(int status) {
6765
super.checkExit(status);
68-
throw new ExitException(status);
66+
throw new ExitException();
6967
}
7068
}
7169

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

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

0 commit comments

Comments
 (0)