Skip to content

Commit

Permalink
Upgrading Unit Tests to Junit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlock-lin committed May 15, 2024
1 parent 0f4dfcf commit 44ce835
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,47 @@
*/
package org.apache.bookkeeper.client;

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

import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* Test for extracting codes from BKException.
*/
public class BKExceptionTest {

@Test
public void testBKExceptionCode() {
Assert.assertEquals(BKException.Code.WriteException,
BKException.getExceptionCode(new BKException.BKWriteException(),
BKException.Code.ReadException));
assertEquals(BKException.Code.WriteException,
BKException.getExceptionCode(new BKException.BKWriteException(),
BKException.Code.ReadException));
}

@Test
public void testNonBKExceptionCode() {
Assert.assertEquals(BKException.Code.ReadException,
BKException.getExceptionCode(new Exception(),
BKException.Code.ReadException));

assertEquals(BKException.Code.ReadException,
BKException.getExceptionCode(new Exception(),
BKException.Code.ReadException));
}

@Test
public void testNestedBKExceptionCode() {
Assert.assertEquals(BKException.Code.WriteException,
BKException.getExceptionCode(
new ExecutionException("test", new BKException.BKWriteException()),
BKException.Code.ReadException));
assertEquals(BKException.Code.WriteException,
BKException.getExceptionCode(
new ExecutionException("test", new BKException.BKWriteException()),
BKException.Code.ReadException));
}

@Test
public void testDoubleNestedBKExceptionCode() {
Assert.assertEquals(BKException.Code.WriteException,
BKException.getExceptionCode(
new ExecutionException("test",
new CompletionException("blah",
new BKException.BKWriteException())),
BKException.Code.ReadException));

assertEquals(BKException.Code.WriteException,
BKException.getExceptionCode(
new ExecutionException("test",
new CompletionException("blah",
new BKException.BKWriteException())),
BKException.Code.ReadException));
}
}

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

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

import org.apache.bookkeeper.net.BookieId;
import org.apache.bookkeeper.net.BookieSocketAddress;
import org.apache.bookkeeper.proto.BookieAddressResolver;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* Unit test of {@link BookieAddressResolverDisabled}.
Expand All @@ -36,18 +39,18 @@ public void testResolve() {
BookieAddressResolver resolver = new BookieAddressResolverDisabled();

BookieSocketAddress addr1 = resolver.resolve(BookieId.parse("127.0.0.1:3181"));
Assert.assertEquals("127.0.0.1", addr1.getHostName());
Assert.assertEquals(3181, addr1.getPort());
assertEquals("127.0.0.1", addr1.getHostName());
assertEquals(3181, addr1.getPort());

BookieSocketAddress addr2 = resolver.resolve(BookieId.parse("localhost:3182"));
Assert.assertEquals("localhost", addr2.getHostName());
Assert.assertEquals(3182, addr2.getPort());
assertEquals("localhost", addr2.getHostName());
assertEquals(3182, addr2.getPort());

try {
resolver.resolve(BookieId.parse("foobar"));
Assert.fail("Non-legacy style bookie id should fail to resolve address");
fail("Non-legacy style bookie id should fail to resolve address");
} catch (Exception e) {
Assert.assertTrue(e instanceof BookieAddressResolver.BookieIdNotResolvedException);
assertTrue(e instanceof BookieAddressResolver.BookieIdNotResolvedException);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
package org.apache.bookkeeper.client;

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

import java.util.Iterator;
import java.util.LinkedList;
Expand All @@ -35,7 +35,8 @@
import org.apache.bookkeeper.meta.ZkLedgerUnderreplicationManager;
import org.apache.bookkeeper.net.BookieId;
import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
import org.junit.Test;
import org.junit.jupiter.api.Test;


/**
* Unit test of bookie decommission operations.
Expand Down Expand Up @@ -149,7 +150,7 @@ public void testDecommissionForLedgersWithMultipleSegmentsAndNotWriteClosed() th

startNewBookie();

assertEquals("Number of Available Bookies", NUM_OF_BOOKIES + 1, bkAdmin.getAvailableBookies().size());
assertEquals(NUM_OF_BOOKIES + 1, bkAdmin.getAvailableBookies().size(), "Number of Available Bookies");

BookieId killedBookieId = getBookie(0);
log.warn("Killing bookie {}", killedBookieId);
Expand Down Expand Up @@ -224,12 +225,12 @@ public void testDecommissionForEmptyLedgers() throws Exception {

startNewBookie();

assertEquals("Number of Available Bookies", NUM_OF_BOOKIES + 1, bkAdmin.getAvailableBookies().size());
assertEquals(NUM_OF_BOOKIES + 1, bkAdmin.getAvailableBookies().size(), "Number of Available Bookies");

BookieId killedBookieId = getBookie(0);
log.warn("Killing bookie {}", killedBookieId);
killBookie(0);
assertEquals("Number of Available Bookies", NUM_OF_BOOKIES, bkAdmin.getAvailableBookies().size());
assertEquals(NUM_OF_BOOKIES, bkAdmin.getAvailableBookies().size(), "Number of Available Bookies");

bkAdmin.decommissionBookie(killedBookieId);
bkAdmin.triggerAudit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
*/
package org.apache.bookkeeper.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

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

import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.client.BKException.BKBookieHandleNotAvailableException;
Expand All @@ -32,8 +33,8 @@
import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.discover.ZKRegistrationClient;
import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

/**
* Tests of the main BookKeeper client and the BP-41 bookieAddressTracking feature.
Expand Down Expand Up @@ -85,7 +86,7 @@ public void testFollowBookieAddressChange() throws Exception {
}

@Test
@Ignore("PLSR-1850 Seems like restart of the bookie always comes up on same port hence failing this test")
@Disabled("PLSR-1850 Seems like restart of the bookie always comes up on same port hence failing this test")
public void testFollowBookieAddressChangeTrckingDisabled() throws Exception {
ClientConfiguration conf = new ClientConfiguration();
conf.setMetadataServiceUri(zkUtil.getMetadataServiceUri());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
*/
package org.apache.bookkeeper.client;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import io.netty.buffer.ByteBuf;
import java.io.IOException;
Expand All @@ -46,9 +46,9 @@
import org.apache.bookkeeper.proto.BookieProtocol;
import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback;
import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -61,6 +61,7 @@ public class BookieRecoveryTest extends BookKeeperClusterTestCase {

// Object used for synchronizing async method calls
class SyncObject {

boolean value;

public SyncObject() {
Expand Down Expand Up @@ -104,7 +105,7 @@ public BookieRecoveryTest() {
baseClientConf.setLedgerManagerFactoryClassName(ledgerManagerFactory);
}

@Before
@BeforeEach
@Override
public void setUp() throws Exception {
// Set up the configuration properties needed.
Expand All @@ -119,7 +120,7 @@ public void setUp() throws Exception {
bkAdmin = new BookKeeperAdmin(adminConf);
}

@After
@AfterEach
@Override
public void tearDown() throws Exception {
// Release any resources used by the BookieRecoveryTest instance.
Expand Down Expand Up @@ -279,7 +280,7 @@ void metadataConflictWithRecovery(BookKeeper bkc) throws Exception {
startAndAddBookie(confOfKilledBookie);

// all ensembles should be fully replicated since it is recovered
assertTrue("Not fully replicated", verifyFullyReplicated(lh, 3 * numEntries));
assertTrue(verifyFullyReplicated(lh, 3 * numEntries), "Not fully replicated");
lh.close();
}

Expand Down Expand Up @@ -607,7 +608,7 @@ public void testBookieRecoveryOnClosedLedgers() throws Exception {

bkAdmin.recoverBookieData(bookieToKill);
for (LedgerHandle lh : lhs) {
assertTrue("Not fully replicated", verifyFullyReplicated(lh, numMsgs));
assertTrue(verifyFullyReplicated(lh, numMsgs), "Not fully replicated");
lh.close();
}
}
Expand Down Expand Up @@ -637,7 +638,7 @@ public void testBookieRecoveryOnOpenedLedgers() throws Exception {
bkAdmin.recoverBookieData(bookieToKill);

for (LedgerHandle lh : lhs) {
assertTrue("Not fully replicated", verifyFullyReplicated(lh, numMsgs));
assertTrue(verifyFullyReplicated(lh, numMsgs), "Not fully replicated");
}

try {
Expand Down Expand Up @@ -694,7 +695,7 @@ public void testBookieRecoveryOnInRecoveryLedger() throws Exception {
bkAdmin.recoverBookieData(bookieToKill);

for (LedgerHandle lh : lhs) {
assertTrue("Not fully replicated", verifyFullyReplicated(lh, numMsgs));
assertTrue(verifyFullyReplicated(lh, numMsgs), "Not fully replicated");
}

// open ledgers to read metadata
Expand Down Expand Up @@ -770,13 +771,13 @@ public void testSyncBookieRecoveryToRandomBookiesCheckForDupes() throws Exceptio
sync.value = false;
bkAdmin.recoverBookieData(bookieSrc);

assertFalse("Dupes exist in ensembles", findDupesInEnsembles(lhs));
assertFalse(findDupesInEnsembles(lhs), "Dupes exist in ensembles");

// Write some more entries to ensure fencing hasn't broken stuff
writeEntriestoLedgers(numMsgs, numMsgs * 2, lhs);

for (LedgerHandle lh : lhs) {
assertTrue("Not fully replicated", verifyFullyReplicated(lh, numMsgs * 3));
assertTrue(verifyFullyReplicated(lh, numMsgs * 3), "Not fully replicated");
lh.close();
}
}
Expand All @@ -801,7 +802,7 @@ public void recoverWithoutPasswordInConf() throws Exception {

// Check that entries are missing
lh = bkc.openLedgerNoRecovery(ledgerId, digestCorrect, passwdCorrect);
assertFalse("Should be entries missing", verifyFullyReplicated(lh, 100));
assertFalse(verifyFullyReplicated(lh, 100), "Should be entries missing");
lh.close();

// Try to recover with bad password in conf
Expand All @@ -818,7 +819,7 @@ public void recoverWithoutPasswordInConf() throws Exception {
bka.close();

lh = bkc.openLedgerNoRecovery(ledgerId, digestCorrect, passwdCorrect);
assertTrue("Should be back to fully replication", verifyFullyReplicated(lh, 100));
assertTrue(verifyFullyReplicated(lh, 100), "Should be back to fully replication");
lh.close();

bookieSrc = addressByIndex(0);
Expand All @@ -827,7 +828,7 @@ public void recoverWithoutPasswordInConf() throws Exception {

// Check that entries are missing
lh = bkc.openLedgerNoRecovery(ledgerId, digestCorrect, passwdCorrect);
assertFalse("Should be entries missing", verifyFullyReplicated(lh, 100));
assertFalse(verifyFullyReplicated(lh, 100), "Should be entries missing");
lh.close();

// Try to recover with no password in conf
Expand All @@ -840,7 +841,7 @@ public void recoverWithoutPasswordInConf() throws Exception {
bka.close();

lh = bkc.openLedgerNoRecovery(ledgerId, digestCorrect, passwdCorrect);
assertTrue("Should be back to fully replication", verifyFullyReplicated(lh, 100));
assertTrue(verifyFullyReplicated(lh, 100), "Should be back to fully replication");
lh.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
*/
package org.apache.bookkeeper.client;

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

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;


/**
* Tests for Bookie recovery use IO threads.
Expand All @@ -33,14 +35,9 @@ public class BookieRecoveryUseIOThreadTest extends BookKeeperClusterTestCase {

public BookieRecoveryUseIOThreadTest() {
super(1);
}

@Override
public void setUp() throws Exception {
baseConf.setNumAddWorkerThreads(0);
baseConf.setNumReadWorkerThreads(0);
baseConf.setNumHighPriorityWorkerThreads(0);
super.setUp();
}

@Test
Expand Down Expand Up @@ -75,6 +72,6 @@ public void openComplete(int rc, LedgerHandle lh, Object ctx) {
}, null);
latch.await();
}
Assert.assertEquals(finalRc.get(), org.apache.bookkeeper.client.api.BKException.Code.OK);
assertEquals(finalRc.get(), org.apache.bookkeeper.client.api.BKException.Code.OK);
}
}

0 comments on commit 44ce835

Please sign in to comment.