Skip to content

Commit

Permalink
migrate-junit5-client-module
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlock-lin committed Aug 29, 2024
1 parent 4936d39 commit bf55d26
Show file tree
Hide file tree
Showing 16 changed files with 1,192 additions and 1,169 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@
*/
package org.apache.bookkeeper.client;

import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import io.netty.buffer.ByteBuf;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.apache.bookkeeper.bookie.BookieException;
import org.apache.bookkeeper.bookie.SortedLedgerStorage;
import org.apache.bookkeeper.client.BookKeeper.DigestType;
import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -93,7 +95,7 @@ public BookKeeperClientTestsWithBookieErrors() {
};
}

@Before
@BeforeEach
public void setUp() throws Exception {
faultInjections.clear();
storageFaultInjectionsMap.clear();
Expand Down Expand Up @@ -124,8 +126,9 @@ public ByteBuf getEntry(long ledgerId, long entryId) throws IOException, BookieE
}

// In this testcase all the bookies will return corrupt entry
@Test(timeout = 60000)
public void testBookkeeperAllDigestErrors() throws Exception {
@Test
@Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
void bookkeeperAllDigestErrors() throws Exception {
ClientConfiguration conf = new ClientConfiguration().setMetadataServiceUri(zkUtil.getMetadataServiceUri());
BookKeeper bkc = new BookKeeper(conf);

Expand Down Expand Up @@ -155,8 +158,9 @@ public void testBookkeeperAllDigestErrors() throws Exception {

// In this testcase first two bookies will sleep (for ReadEntryTimeout+2 secs) before returning the data,
// and the last one will return corrupt data
@Test(timeout = 60000)
public void testBKReadFirstTimeoutThenDigestError() throws Exception {
@Test
@Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
void bKReadFirstTimeoutThenDigestError() throws Exception {
ClientConfiguration conf = new ClientConfiguration().setMetadataServiceUri(zkUtil.getMetadataServiceUri());
BookKeeper bkc = new BookKeeper(conf);

Expand Down Expand Up @@ -185,8 +189,9 @@ public void testBKReadFirstTimeoutThenDigestError() throws Exception {

// In this testcase first one will return corrupt data and the last two bookies will
// sleep (for ReadEntryTimeout+2 secs) before returning the data
@Test(timeout = 60000)
public void testBKReadFirstDigestErrorThenTimeout() throws Exception {
@Test
@Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
void bKReadFirstDigestErrorThenTimeout() throws Exception {
ClientConfiguration conf = new ClientConfiguration().setMetadataServiceUri(zkUtil.getMetadataServiceUri());
BookKeeper bkc = new BookKeeper(conf);

Expand Down Expand Up @@ -215,8 +220,9 @@ public void testBKReadFirstDigestErrorThenTimeout() throws Exception {

// In this testcase first two bookies are killed before making the readentry call
// and the last one will return corrupt data
@Test(timeout = 60000)
public void testBKReadFirstBookiesDownThenDigestError() throws Exception {
@Test
@Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
void bKReadFirstBookiesDownThenDigestError() throws Exception {
ClientConfiguration conf = new ClientConfiguration().setMetadataServiceUri(zkUtil.getMetadataServiceUri());
BookKeeper bkc = new BookKeeper(conf);

Expand Down Expand Up @@ -245,8 +251,9 @@ public void testBKReadFirstBookiesDownThenDigestError() throws Exception {
}

// In this testcase all the bookies will sleep (for ReadEntryTimeout+2 secs) before returning the data
@Test(timeout = 60000)
public void testBKReadAllTimeouts() throws Exception {
@Test
@Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
void bKReadAllTimeouts() throws Exception {
ClientConfiguration conf = new ClientConfiguration().setMetadataServiceUri(zkUtil.getMetadataServiceUri());
BookKeeper bkc = new BookKeeper(conf);

Expand Down Expand Up @@ -275,8 +282,9 @@ public void testBKReadAllTimeouts() throws Exception {

// In this testcase first two bookies will sleep (for ReadEntryTimeout+2 secs) before returning the data,
// but the last one will return as expected
@Test(timeout = 60000)
public void testBKReadTwoBookiesTimeout() throws Exception {
@Test
@Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
void bKReadTwoBookiesTimeout() throws Exception {
ClientConfiguration conf = new ClientConfiguration().setMetadataServiceUri(zkUtil.getMetadataServiceUri());
BookKeeper bkc = new BookKeeper(conf);

Expand All @@ -296,18 +304,18 @@ public void testBKReadTwoBookiesTimeout() throws Exception {

LedgerHandle rlh = bkc.openLedger(id, DigestType.CRC32, passwd);
LedgerEntry entry = rlh.readEntries(4, 4).nextElement();
Assert.assertTrue("The read Entry should match with what have been written",
(new String(entry.getEntry())).equals("foobarfoo"));
assertEquals("foobarfoo", (new String(entry.getEntry())), "The read Entry should match with what have been written");
rlh.close();
bkc.close();
}

// In this testcase first two bookies return the corrupt data,
// but the last one will return as expected
@Test(timeout = 60000)
public void testBKReadTwoBookiesWithDigestError() throws Exception {
@Test
@Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
void bKReadTwoBookiesWithDigestError() throws Exception {
ClientConfiguration conf = new ClientConfiguration()
.setMetadataServiceUri(zkUtil.getMetadataServiceUri());
.setMetadataServiceUri(zkUtil.getMetadataServiceUri());
BookKeeper bkc = new BookKeeper(conf);

byte[] passwd = "AAAAAAA".getBytes();
Expand All @@ -326,8 +334,7 @@ public void testBKReadTwoBookiesWithDigestError() throws Exception {

LedgerHandle rlh = bkc.openLedger(id, DigestType.CRC32, passwd);
LedgerEntry entry = rlh.readEntries(4, 4).nextElement();
Assert.assertTrue("The read Entry should match with what have been written",
(new String(entry.getEntry())).equals("foobarfoo"));
assertEquals("foobarfoo", (new String(entry.getEntry())), "The read Entry should match with what have been written");
rlh.close();
bkc.close();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.bookkeeper.client;

import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
import org.apache.bookkeeper.test.TestCallbacks.AddCallbackFuture;
import org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,32 +39,32 @@ public BookKeeperClientZKSessionExpiry() {
}

@Test
public void testSessionLossWhileWriting() throws Exception {
void sessionLossWhileWriting() throws Exception {

Thread expiryThread = new Thread() {
@Override
public void run() {
try {
while (true) {
Thread.sleep(5000);
long sessionId = bkc.getZkHandle().getSessionId();
byte[] sessionPasswd = bkc.getZkHandle().getSessionPasswd();
@Override
public void run() {
try {
while (true) {
Thread.sleep(5000);
long sessionId = bkc.getZkHandle().getSessionId();
byte[] sessionPasswd = bkc.getZkHandle().getSessionPasswd();

try {
ZooKeeperWatcherBase watcher = new ZooKeeperWatcherBase(10000, false);
ZooKeeper zk = new ZooKeeper(zkUtil.getZooKeeperConnectString(), 10000,
watcher, sessionId, sessionPasswd);
zk.close();
} catch (Exception e) {
LOG.info("Error killing session", e);
}
try {
ZooKeeperWatcherBase watcher = new ZooKeeperWatcherBase(10000, true);
ZooKeeper zk = new ZooKeeper(zkUtil.getZooKeeperConnectString(), 10000,
watcher, sessionId, sessionPasswd);
zk.close();
} catch (Exception e) {
LOG.info("Error killing session", e);
}
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
return;
}
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
return;
}
};
}
};
expiryThread.start();

for (int i = 0; i < 3; i++) {
Expand Down
Loading

0 comments on commit bf55d26

Please sign in to comment.