Skip to content

Commit

Permalink
migrate client module bookie tests to junit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlock-lin committed May 31, 2024
1 parent 03df884 commit 2615c36
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public class BookKeeperApiTest extends MockBookKeeperTestCase {
@Rule
public LoggerOutput loggerOutput = new LoggerOutput();

private static void checkEntries(LedgerEntries entries, byte[] data)
throws InterruptedException, BKException {
Iterator<LedgerEntry> iterator = entries.iterator();
while (iterator.hasNext()) {
LedgerEntry entry = iterator.next();
assertArrayEquals(data, entry.getEntryBytes());
}
}

@Test
public void testWriteHandle() throws Exception {
try (WriteHandle writer = result(newCreateLedgerOp()
Expand Down Expand Up @@ -177,7 +186,7 @@ public void testOpenLedgerUnauthorized() throws Exception {
*/
@Test
public void testLedgerDigests() throws Exception {
for (DigestType type: DigestType.values()) {
for (DigestType type : DigestType.values()) {
long lId;
try (WriteHandle writer = result(newCreateLedgerOp()
.withAckQuorumSize(1)
Expand All @@ -203,7 +212,6 @@ public void testLedgerDigests() throws Exception {
}
}


@Test
public void testOpenLedgerDigestUnmatchedWhenAutoDetectionEnabled() throws Exception {
testOpenLedgerDigestUnmatched(true);
Expand Down Expand Up @@ -231,10 +239,10 @@ private void testOpenLedgerDigestUnmatched(boolean autodetection) throws Excepti
assertEquals(-1L, writer.getLastAddPushed());
}
try (ReadHandle ignored = result(newOpenLedgerOp()
.withDigestType(DigestType.CRC32)
.withPassword(password)
.withLedgerId(lId)
.execute())) {
.withDigestType(DigestType.CRC32)
.withPassword(password)
.withLedgerId(lId)
.execute())) {
if (!autodetection) {
fail("Should fail to open read handle if digest type auto detection is disabled.");
}
Expand Down Expand Up @@ -285,10 +293,10 @@ public void testOpenLedgerRead() throws Exception {
}

try (ReadHandle reader = result(newOpenLedgerOp()
.withPassword(password)
.withRecovery(false)
.withLedgerId(lId)
.execute())) {
.withPassword(password)
.withRecovery(false)
.withLedgerId(lId)
.execute())) {
assertTrue(reader.isClosed());
assertEquals(2, reader.getLastAddConfirmed());
assertEquals(3 * data.length, reader.getLength());
Expand All @@ -299,7 +307,7 @@ public void testOpenLedgerRead() throws Exception {

// test readLastAddConfirmedAndEntry
LastConfirmedAndEntry lastConfirmedAndEntry =
reader.readLastAddConfirmedAndEntry(0, 999, false);
reader.readLastAddConfirmedAndEntry(0, 999, false);
assertEquals(2L, lastConfirmedAndEntry.getLastAddConfirmed());
assertArrayEquals(data, lastConfirmedAndEntry.getEntry().getEntryBytes());
lastConfirmedAndEntry.close();
Expand Down Expand Up @@ -414,9 +422,9 @@ public void testLedgerEntriesIterable() throws Exception {
}
i.set(0);
entries.forEach((e) -> {
assertEquals(i.getAndIncrement(), e.getEntryId());
assertArrayEquals(data, e.getEntryBytes());
});
assertEquals(i.getAndIncrement(), e.getEntryId());
assertArrayEquals(data, e.getEntryBytes());
});
}
}
}
Expand All @@ -436,13 +444,4 @@ public void testBKExceptionCodeLogger() {
assertEquals("123: Unexpected condition", BKException.codeLogger(123).toString());
assertEquals("-201: Unexpected condition", BKException.codeLogger(-201).toString());
}

private static void checkEntries(LedgerEntries entries, byte[] data)
throws InterruptedException, BKException {
Iterator<LedgerEntry> iterator = entries.iterator();
while (iterator.hasNext()) {
LedgerEntry entry = iterator.next();
assertArrayEquals(data, entry.getEntryBytes());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.doAnswer;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.bookkeeper.client.BKException;
Expand All @@ -39,7 +37,7 @@
import org.apache.bookkeeper.proto.BookieProtocol;
import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

/**
* Tests for BookKeeper open ledger operations.
Expand All @@ -54,21 +52,8 @@ public class BookKeeperBuildersOpenLedgerTest extends MockBookKeeperTestCase {
private static final byte[] password = new byte[3];
private static final byte[] entryData = new byte[32];

private boolean withRecovery;

public void initBookKeeperBuildersOpenLedgerTest(boolean withRecovery) {
this.withRecovery = withRecovery;
}

public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{true},
{false}
});
}

@MethodSource("data")
@ParameterizedTest(name = "withRecovery:({0})")
@ParameterizedTest
@ValueSource(booleans = {true, false})
public void testOpenLedger(boolean withRecovery) throws Exception {
LedgerMetadata ledgerMetadata = generateLedgerMetadata(ensembleSize,
writeQuorumSize, ackQuorumSize, password, customMetadata);
Expand All @@ -89,8 +74,8 @@ public void testOpenLedger(boolean withRecovery) throws Exception {
.execute());
}

@MethodSource("data")
@ParameterizedTest(name = "withRecovery:({0})")
@ParameterizedTest
@ValueSource(booleans = {true, false})
public void testOpenLedgerWithTimeoutEx(boolean withRecovery) throws Exception {
mockReadEntryTimeout();
LedgerMetadata ledgerMetadata = generateLedgerMetadata(ensembleSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,20 @@
import io.netty.buffer.ByteBufUtil;
import java.nio.ByteBuffer;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.bookkeeper.client.extension.TestContextExtension;
import org.apache.bookkeeper.common.concurrent.FutureUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.TestInfo;

/**
* Unit test for {@link WriteAdvHandle}.
*/
public class WriteAdvHandleTest {

@RegisterExtension
TestContextExtension testContextExtension = new TestContextExtension();

private final long entryId;
private final WriteAdvHandle handle = mock(WriteAdvHandle.class);
private final LinkedBlockingQueue<ByteBuf> entryQueue;
private volatile String testName;

public WriteAdvHandleTest() {
this.entryId = System.currentTimeMillis();
Expand All @@ -65,9 +63,14 @@ public WriteAdvHandleTest() {
when(handle.writeAsync(anyLong(), any(ByteBuffer.class))).thenCallRealMethod();
}

@BeforeEach
public void setUp(TestInfo testInfo) throws Exception {
testName = testInfo.getDisplayName();
}

@Test
public void testAppendBytes() throws Exception {
byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8);
byte[] testData = testName.getBytes(UTF_8);
handle.writeAsync(entryId, testData);

ByteBuf buffer = entryQueue.take();
Expand All @@ -78,7 +81,7 @@ public void testAppendBytes() throws Exception {

@Test
public void testAppendBytes2() throws Exception {
byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8);
byte[] testData = testName.getBytes(UTF_8);
handle.writeAsync(entryId, testData, 1, testData.length / 2);
byte[] expectedData = new byte[testData.length / 2];
System.arraycopy(testData, 1, expectedData, 0, testData.length / 2);
Expand All @@ -91,7 +94,7 @@ public void testAppendBytes2() throws Exception {

@Test
public void testAppendByteBuffer() throws Exception {
byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8);
byte[] testData = testName.getBytes(UTF_8);
handle.writeAsync(entryId, ByteBuffer.wrap(testData, 1, testData.length / 2));
byte[] expectedData = new byte[testData.length / 2];
System.arraycopy(testData, 1, expectedData, 0, testData.length / 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,19 @@
import java.nio.ByteBuffer;
import java.util.concurrent.LinkedBlockingQueue;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.client.extension.TestContextExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.TestInfo;

/**
* Unit test for the default methods in {@link WriteHandle}.
*/
@Slf4j
public class WriteHandleTest {

@RegisterExtension
TestContextExtension testContextExtension = new TestContextExtension();

private final WriteHandle handle = mock(WriteHandle.class);
private final LinkedBlockingQueue<ByteBuf> entryQueue;
private volatile String testName;

public WriteHandleTest() throws Exception {
this.entryQueue = new LinkedBlockingQueue<>();
Expand All @@ -62,9 +60,14 @@ public WriteHandleTest() throws Exception {
when(handle.append(any(ByteBuffer.class))).thenCallRealMethod();
}

@BeforeEach
public void setUp(TestInfo testInfo) throws Exception {
testName = testInfo.getDisplayName();
}

@Test
public void testAppendBytes() throws Exception {
byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8);
byte[] testData = testName.getBytes(UTF_8);
handle.append(testData);

ByteBuf buffer = entryQueue.take();
Expand All @@ -75,7 +78,7 @@ public void testAppendBytes() throws Exception {

@Test
public void testAppendBytes2() throws Exception {
byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8);
byte[] testData = testName.getBytes(UTF_8);
handle.append(testData, 1, testData.length / 2);
byte[] expectedData = new byte[testData.length / 2];
System.arraycopy(testData, 1, expectedData, 0, testData.length / 2);
Expand All @@ -88,7 +91,7 @@ public void testAppendBytes2() throws Exception {

@Test
public void testAppendByteBuffer() throws Exception {
byte[] testData = testContextExtension.getMethodName().getBytes(UTF_8);
byte[] testData = testName.getBytes(UTF_8);
handle.append(ByteBuffer.wrap(testData, 1, testData.length / 2));
byte[] expectedData = new byte[testData.length / 2];
System.arraycopy(testData, 1, expectedData, 0, testData.length / 2);
Expand Down

This file was deleted.

0 comments on commit 2615c36

Please sign in to comment.