Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate client module api tests to junit 5 #4377

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
import org.apache.bookkeeper.versioning.Versioned;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.Stubber;
Expand Down Expand Up @@ -141,6 +143,7 @@ public MockEntry(byte[] payload, long lastAddConfirmed) {

}

@BeforeEach
@Before

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about add some todo here: // TODO: delete @Before when only run with junit5

public void setup() throws Exception {
maxNumberOfAvailableBookies = Integer.MAX_VALUE;
Expand Down Expand Up @@ -264,6 +267,7 @@ private DigestManager getDigestType(long ledgerId) throws GeneralSecurityExcepti
UnpooledByteBufAllocator.DEFAULT, false);
}

@AfterEach
@After

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about add some todo here: // TODO: delete @Before when only run with junit5

public void tearDown() {
scheduler.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
package org.apache.bookkeeper.client.api;

import static org.apache.bookkeeper.client.api.BKException.Code.UnexpectedConditionException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.lang.reflect.Field;
import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* Tests for BKException methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import io.netty.buffer.Unpooled;
import java.nio.ByteBuffer;
Expand All @@ -47,7 +48,7 @@
import org.apache.bookkeeper.conf.ClientConfiguration;
import org.apache.bookkeeper.util.LoggerOutput;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.event.LoggingEvent;

/**
Expand All @@ -62,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 @@ -128,41 +138,45 @@ public void testWriteAdvHandleWithFixedLedgerId() throws Exception {
}
}

@Test(expected = BKDuplicateEntryIdException.class)
@Test
public void testWriteAdvHandleBKDuplicateEntryId() throws Exception {
try (WriteAdvHandle writer = result(newCreateLedgerOp()
.withAckQuorumSize(1)
.withWriteQuorumSize(2)
.withEnsembleSize(3)
.withPassword(password)
.makeAdv()
.withLedgerId(1234)
.execute())) {
assertEquals(1234, writer.getId());
long entryId = 0;
writer.write(entryId++, ByteBuffer.wrap(data));
assertEquals(data.length, writer.getLength());
writer.write(entryId - 1, ByteBuffer.wrap(data));
}
assertThrows(BKDuplicateEntryIdException.class, () -> {
try (WriteAdvHandle writer = result(newCreateLedgerOp()
.withAckQuorumSize(1)
.withWriteQuorumSize(2)
.withEnsembleSize(3)
.withPassword(password)
.makeAdv()
.withLedgerId(1234)
.execute())) {
assertEquals(1234, writer.getId());
long entryId = 0;
writer.write(entryId++, ByteBuffer.wrap(data));
assertEquals(data.length, writer.getLength());
writer.write(entryId - 1, ByteBuffer.wrap(data));
}
});
}

@Test(expected = BKUnauthorizedAccessException.class)
@Test
public void testOpenLedgerUnauthorized() throws Exception {
long lId;
try (WriteHandle writer = result(newCreateLedgerOp()
.withAckQuorumSize(1)
.withWriteQuorumSize(2)
.withEnsembleSize(3)
.withPassword(password)
.execute())) {
lId = writer.getId();
assertEquals(-1L, writer.getLastAddPushed());
}
try (ReadHandle ignored = result(newOpenLedgerOp()
.withPassword("bad-password".getBytes(UTF_8))
.withLedgerId(lId)
.execute())) {
}
assertThrows(BKUnauthorizedAccessException.class, () -> {
long lId;
try (WriteHandle writer = result(newCreateLedgerOp()
.withAckQuorumSize(1)
.withWriteQuorumSize(2)
.withEnsembleSize(3)
.withPassword(password)
.execute())) {
lId = writer.getId();
assertEquals(-1L, writer.getLastAddPushed());
}
try (ReadHandle ignored = result(newOpenLedgerOp()
.withPassword("bad-password".getBytes(UTF_8))
.withLedgerId(lId)
.execute())) {
}
});
}

/**
Expand All @@ -172,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 @@ -198,7 +212,6 @@ public void testLedgerDigests() throws Exception {
}
}


@Test
public void testOpenLedgerDigestUnmatchedWhenAutoDetectionEnabled() throws Exception {
testOpenLedgerDigestUnmatched(true);
Expand Down Expand Up @@ -226,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 @@ -280,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 @@ -294,82 +307,87 @@ 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();
}
}

@Test(expected = BKLedgerFencedException.class)
@Test
public void testOpenLedgerWithRecovery() throws Exception {
assertThrows(BKLedgerFencedException.class, () -> {
loggerOutput.expect((List<LoggingEvent> logEvents) -> {
assertThat(logEvents, hasItem(hasProperty("message",
containsString("due to LedgerFencedException: "
+ "Ledger has been fenced off. Some other client must have opened it to read")
)));
});

loggerOutput.expect((List<LoggingEvent> logEvents) -> {
assertThat(logEvents, hasItem(hasProperty("message",
containsString("due to LedgerFencedException: "
+ "Ledger has been fenced off. Some other client must have opened it to read")
)));
});

long lId;
try (WriteHandle writer = result(newCreateLedgerOp()
.withAckQuorumSize(1)
.withWriteQuorumSize(2)
.withEnsembleSize(3)
.withPassword(password)
.execute())) {
lId = writer.getId();

writer.append(ByteBuffer.wrap(data));
writer.append(ByteBuffer.wrap(data));
assertEquals(1L, writer.getLastAddPushed());

// open with fencing
try (ReadHandle reader = result(newOpenLedgerOp()
long lId;
try (WriteHandle writer = result(newCreateLedgerOp()
.withAckQuorumSize(1)
.withWriteQuorumSize(2)
.withEnsembleSize(3)
.withPassword(password)
.withRecovery(true)
.withLedgerId(lId)
.execute())) {
assertTrue(reader.isClosed());
assertEquals(1L, reader.getLastAddConfirmed());
}
lId = writer.getId();

writer.append(ByteBuffer.wrap(data));
writer.append(ByteBuffer.wrap(data));
writer.append(ByteBuffer.wrap(data));
assertEquals(1L, writer.getLastAddPushed());

// open with fencing
try (ReadHandle reader = result(newOpenLedgerOp()
.withPassword(password)
.withRecovery(true)
.withLedgerId(lId)
.execute())) {
assertTrue(reader.isClosed());
assertEquals(1L, reader.getLastAddConfirmed());
}

}
writer.append(ByteBuffer.wrap(data));

}
});
}

@Test(expected = BKNoSuchLedgerExistsOnMetadataServerException.class)
@Test
public void testDeleteLedger() throws Exception {
long lId;
assertThrows(BKNoSuchLedgerExistsOnMetadataServerException.class, () -> {
long lId;

try (WriteHandle writer = result(newCreateLedgerOp()
.withPassword(password)
.execute())) {
lId = writer.getId();
assertEquals(-1L, writer.getLastAddPushed());
}
try (WriteHandle writer = result(newCreateLedgerOp()
.withPassword(password)
.execute())) {
lId = writer.getId();
assertEquals(-1L, writer.getLastAddPushed());
}

result(newDeleteLedgerOp().withLedgerId(lId).execute());
result(newDeleteLedgerOp().withLedgerId(lId).execute());

result(newOpenLedgerOp()
.withPassword(password)
.withLedgerId(lId)
.execute());
result(newOpenLedgerOp()
.withPassword(password)
.withLedgerId(lId)
.execute());
});
}

@Test(expected = BKNoSuchLedgerExistsOnMetadataServerException.class)
@Test
public void testCannotDeleteLedgerTwice() throws Exception {
long lId;
assertThrows(BKNoSuchLedgerExistsOnMetadataServerException.class, () -> {
long lId;

try (WriteHandle writer = result(newCreateLedgerOp()
.withPassword(password)
.execute())) {
lId = writer.getId();
assertEquals(-1L, writer.getLastAddPushed());
}
result(newDeleteLedgerOp().withLedgerId(lId).execute());
result(newDeleteLedgerOp().withLedgerId(lId).execute());
try (WriteHandle writer = result(newCreateLedgerOp()
.withPassword(password)
.execute())) {
lId = writer.getId();
assertEquals(-1L, writer.getLastAddPushed());
}
result(newDeleteLedgerOp().withLedgerId(lId).execute());
result(newDeleteLedgerOp().withLedgerId(lId).execute());
});
}

@Test
Expand Down Expand Up @@ -404,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 @@ -426,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());
}
}
}
Loading
Loading