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

Enhance simple test #3675

Merged
merged 8 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -29,6 +29,7 @@
import lombok.experimental.Accessors;
import org.apache.bookkeeper.client.api.BookKeeper;
import org.apache.bookkeeper.client.api.DigestType;
import org.apache.bookkeeper.client.api.ReadHandle;
import org.apache.bookkeeper.client.api.WriteHandle;
import org.apache.bookkeeper.tools.cli.commands.client.SimpleTestCommand.Flags;
import org.apache.bookkeeper.tools.cli.helpers.ClientCommand;
Expand Down Expand Up @@ -61,8 +62,6 @@ public static class Flags extends CliFlags {
private int ackQuorumSize = 2;
@Parameter(names = { "-n", "--num-entries" }, description = "Entries to write (default 100)")
private int numEntries = 100;
@Parameter(names = { "-c", "--clean-up" }, description = "Clean up ledger created after simple test")
Copy link
Member

Choose a reason for hiding this comment

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

It is an easily way to generate a ledger. I am not sure if it should be deleted automatically. If we delete it automatically, the only way to get a ledger is writing a program, it looks very inconvenient.

Copy link
Member Author

Choose a reason for hiding this comment

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

This command is for testing the bookkeeper to create a ledger and read the ledger. If already cover the user's case.
If users want to test by themselves, they should create a new one and read it, not support the ledger generated by SimpleTestCommand.

private boolean cleanup = false;

}
public SimpleTestCommand() {
Expand Down Expand Up @@ -102,10 +101,12 @@ protected void run(BookKeeper bk, Flags flags) throws Exception {
}
}
LOG.info("{} entries written to ledger {}", flags.numEntries, wh.getId());
if (flags.cleanup) {
LOG.info("Cleaning up the ledger {}", wh.getId());
result(bk.newDeleteLedgerOp().withLedgerId(wh.getId()).execute());

try (ReadHandle rh = result(bk.newOpenLedgerOp().withLedgerId(wh.getId()).withDigestType(DigestType.CRC32C)
.withPassword(new byte[0]).execute())) {
rh.read(0, flags.numEntries);
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you please assert the result of rh.read()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fine, the write data is an empty array. I will change the written data and verify it.

}
result(bk.newDeleteLedgerOp().withLedgerId(wh.getId()).execute());
Copy link
Member

Choose a reason for hiding this comment

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

In Finally?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fine.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.bookkeeper.client.api.CreateBuilder;
import org.apache.bookkeeper.client.api.DeleteBuilder;
import org.apache.bookkeeper.client.api.DigestType;
import org.apache.bookkeeper.client.api.OpenBuilder;
import org.apache.bookkeeper.client.api.ReadHandle;
import org.apache.bookkeeper.client.api.WriteHandle;
import org.apache.bookkeeper.client.impl.LedgerEntriesImpl;
import org.apache.bookkeeper.client.impl.LedgerEntryImpl;
import org.apache.bookkeeper.common.concurrent.FutureUtils;
import org.apache.bookkeeper.tools.cli.helpers.ClientCommandTestBase;
import org.junit.Test;
Expand All @@ -53,8 +58,7 @@ public void testCommandShortArgs() throws Exception {
"-e", "5",
"-w", "3",
"-a", "3",
"-n", "10",
"-c");
"-n", "10");
}

@Test
Expand All @@ -63,8 +67,7 @@ public void testCommandLongArgs() throws Exception {
"--ensemble-size", "5",
"--write-quorum-size", "3",
"--ack-quorum-size", "3",
"--num-entries", "10",
"-c");
"--num-entries", "10");
}

@SuppressWarnings("unchecked")
Expand All @@ -82,6 +85,16 @@ public void testCommand(String... args) throws Exception {
when(createBuilder.withPassword(any(byte[].class))).thenReturn(createBuilder);
when(createBuilder.execute()).thenReturn(CompletableFuture.completedFuture(wh));
when(mockBk.newCreateLedgerOp()).thenReturn(createBuilder);

ReadHandle rh = mock(ReadHandle.class);
when(rh.read(anyLong(), anyLong())).thenReturn(
LedgerEntriesImpl.create(Collections.singletonList(LedgerEntryImpl.create(0, 0))));
OpenBuilder openBuilder = mock(OpenBuilder.class);
when(openBuilder.withLedgerId(anyLong())).thenReturn(openBuilder);
when(openBuilder.withDigestType(any())).thenReturn(openBuilder);
when(openBuilder.withPassword(any())).thenReturn(openBuilder);
when(openBuilder.execute()).thenReturn(CompletableFuture.completedFuture(rh));
when(mockBk.newOpenLedgerOp()).thenReturn(openBuilder);

DeleteBuilder deleteBuilder = mock(DeleteBuilder.class);
when(deleteBuilder.withLedgerId(anyLong())).thenReturn(deleteBuilder);
Expand All @@ -102,12 +115,17 @@ public void testCommand(String... args) throws Exception {
verify(createBuilder, times(1)).withDigestType(eq(DigestType.CRC32C));
verify(createBuilder, times(1)).withPassword(eq(new byte[0]));
verify(createBuilder, times(1)).execute();


verify(openBuilder, times(1)).withLedgerId(eq(0L));
verify(openBuilder, times(1)).execute();

verify(deleteBuilder, times(1)).withLedgerId(eq(0L));
verify(deleteBuilder, times(1)).execute();

// verify appends
verify(wh, times(10)).append(eq(new byte[100]));

verify(rh, times(1)).read(anyLong(), anyLong());
}

}