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 21, 2024
1 parent c2e8037 commit bcc59bb
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 238 deletions.
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
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
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 @@ -20,17 +20,19 @@
*/
package org.apache.bookkeeper.client.api;


import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.bookkeeper.common.concurrent.FutureUtils.result;
import static org.assertj.core.api.Assertions.assertThat;
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 +49,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 Down Expand Up @@ -128,41 +130,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 Down Expand Up @@ -301,75 +307,80 @@ public void testOpenLedgerRead() throws Exception {
}
}

@Test(expected = BKLedgerFencedException.class)
@Test
public void testOpenLedgerWithRecovery() throws Exception {
assertThrows(BKLedgerFencedException.class, () -> {
loggerOutput.expect((List<LoggingEvent> logEvents) -> {
assertThat(logEvents).isEqualTo(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
Loading

0 comments on commit bcc59bb

Please sign in to comment.