Skip to content

Commit

Permalink
address some pr comments
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Atanasov <[email protected]>
  • Loading branch information
ata-nas committed Dec 9, 2024
1 parent 515a0f2 commit 5f08f5c
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.util.stream.Stream;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -63,59 +62,60 @@ class BlockAsLocalFileReaderTest {
@BeforeEach
void setUp() throws IOException {
blockNodeContext = TestConfigUtil.getTestBlockNodeContext(
Map.of(PERSISTENCE_STORAGE_LIVE_ROOT_PATH_KEY, testLiveRootPath.toString()));
Map.of(PERSISTENCE_STORAGE_LIVE_ROOT_PATH_KEY, testLiveRootPath.toString()));
testConfig = blockNodeContext.configuration().getConfigData(PersistenceStorageConfig.class);

final String testConfigLiveRootPath = testConfig.liveRootPath();
assertThat(testConfigLiveRootPath).isEqualTo(testLiveRootPath.toString());

blockAsLocalFileResolverMock = spy(BlockAsLocalFilePathResolver.of(testLiveRootPath));
blockAsLocalFileWriterMock = spy(BlockAsLocalFileWriter.of(blockNodeContext, blockAsLocalFileResolverMock));
blockAsLocalFileWriterMock = spy(
BlockAsLocalFileWriter.of(blockNodeContext, blockAsLocalFileResolverMock));
toTest = BlockAsLocalFileReader.of(blockAsLocalFileResolverMock);
}

/**
* This test aims to verify that the
* {@link BlockAsLocalFileReader#read(long)} correctly reads a block with
* a given block number.
* This test aims to verify that the {@link BlockAsLocalFileReader#read(long)} correctly reads a block with a
* given block number.
*/
@Test
void testSuccessfulBlockRead() throws IOException, ParseException {
final List<BlockItemUnparsed> blockItemUnparsed = PersistTestUtils.generateBlockItemsUnparsed(1);
@ParameterizedTest
@MethodSource("validBlockNumbers")
void testSuccessfulBlockRead(final long blockNumber) throws IOException, ParseException {
final List<BlockItemUnparsed> blockItemUnparsed =
PersistTestUtils.generateBlockItemsUnparsedForWithBlockNumber(blockNumber);
final Optional<List<BlockItemUnparsed>> written = blockAsLocalFileWriterMock.write(blockItemUnparsed);

assertThat(written).isNotNull().isPresent();

final Optional<BlockUnparsed> actual = toTest.read(1);
final Optional<BlockUnparsed> actual = toTest.read(blockNumber);
assertThat(actual)
.isNotNull()
.isPresent()
.get(InstanceOfAssertFactories.type(BlockUnparsed.class))
.isNotNull()
.isExactlyInstanceOf(BlockUnparsed.class)
.returns(1L, from(blockUnparsed -> {
try {
return BlockHeader.PROTOBUF
.parse(Objects.requireNonNull(
blockUnparsed.blockItems().getFirst().blockHeader()))
.number();
} catch (final ParseException e) {
throw new RuntimeException(e);
}
}))
.extracting(BlockUnparsed::blockItems)
.asList()
.isNotNull()
.isNotEmpty()
.hasSize(blockItemUnparsed.size())
.containsExactlyElementsOf(blockItemUnparsed);
.isNotNull()
.isPresent()
.get(InstanceOfAssertFactories.type(BlockUnparsed.class))
.isNotNull()
.isExactlyInstanceOf(BlockUnparsed.class)
.returns(blockNumber, from(blockUnparsed -> {
try {
return BlockHeader.PROTOBUF
.parse(Objects.requireNonNull(
blockUnparsed.blockItems().getFirst().blockHeader()))
.number();
} catch (final ParseException e) {
throw new RuntimeException(e);
}
}))
.extracting(BlockUnparsed::blockItems)
.asList()
.isNotNull()
.isNotEmpty()
.hasSize(blockItemUnparsed.size())
.containsExactlyElementsOf(blockItemUnparsed);
}

/**
* This test aims to verify that the
* {@link BlockAsLocalFileReader#read(long)} correctly throws an
* {@link IllegalArgumentException} when an invalid block number is
* provided. A block number is invalid if it is a strictly negative number.
* This test aims to verify that the {@link BlockAsLocalFileReader#read(long)} correctly throws an
* {@link IllegalArgumentException} when an invalid block number is provided. A block number is invalid if it
* is a strictly negative number.
*
* @param toRead parameterized, block number
*/
Expand All @@ -125,33 +125,64 @@ void testInvalidBlockNumber(final long toRead) {
assertThatIllegalArgumentException().isThrownBy(() -> toTest.read(toRead));
}

/**
* Some valid block numbers.
*
* @return a stream of valid block numbers
*/
public static Stream<Arguments> validBlockNumbers() {
return Stream.of(
Arguments.of(0L),
Arguments.of(1L),
Arguments.of(2L),
Arguments.of(10L),
Arguments.of(100L),
Arguments.of(1_000L),
Arguments.of(10_000L),
Arguments.of(100_000L),
Arguments.of(1_000_000L),
Arguments.of(10_000_000L),
Arguments.of(100_000_000L),
Arguments.of(1_000_000_000L),
Arguments.of(10_000_000_000L),
Arguments.of(100_000_000_000L),
Arguments.of(1_000_000_000_000L),
Arguments.of(10_000_000_000_000L),
Arguments.of(100_000_000_000_000L),
Arguments.of(1_000_000_000_000_000L),
Arguments.of(10_000_000_000_000_000L),
Arguments.of(100_000_000_000_000_000L),
Arguments.of(1_000_000_000_000_000_000L),
Arguments.of(Long.MAX_VALUE));
}

/**
* Some invalid block numbers.
*
* @return a stream of invalid block numbers
*/
public static Stream<Arguments> invalidBlockNumbers() {
return Stream.of(
Arguments.of(-1L),
Arguments.of(-2L),
Arguments.of(-10L),
Arguments.of(-100L),
Arguments.of(-1_000L),
Arguments.of(-10_000L),
Arguments.of(-100_000L),
Arguments.of(-1_000_000L),
Arguments.of(-10_000_000L),
Arguments.of(-100_000_000L),
Arguments.of(-1_000_000_000L),
Arguments.of(-10_000_000_000L),
Arguments.of(-100_000_000_000L),
Arguments.of(-1_000_000_000_000L),
Arguments.of(-10_000_000_000_000L),
Arguments.of(-100_000_000_000_000L),
Arguments.of(-1_000_000_000_000_000L),
Arguments.of(-10_000_000_000_000_000L),
Arguments.of(-100_000_000_000_000_000L),
Arguments.of(-1_000_000_000_000_000_000L),
Arguments.of(Long.MIN_VALUE));
Arguments.of(-1L),
Arguments.of(-2L),
Arguments.of(-10L),
Arguments.of(-100L),
Arguments.of(-1_000L),
Arguments.of(-10_000L),
Arguments.of(-100_000L),
Arguments.of(-1_000_000L),
Arguments.of(-10_000_000L),
Arguments.of(-100_000_000L),
Arguments.of(-1_000_000_000L),
Arguments.of(-10_000_000_000L),
Arguments.of(-100_000_000_000L),
Arguments.of(-1_000_000_000_000L),
Arguments.of(-10_000_000_000_000L),
Arguments.of(-100_000_000_000_000L),
Arguments.of(-1_000_000_000_000_000L),
Arguments.of(-10_000_000_000_000_000L),
Arguments.of(-100_000_000_000_000_000L),
Arguments.of(-1_000_000_000_000_000_000L),
Arguments.of(Long.MIN_VALUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,45 +51,73 @@ public static void writeBytesToPath(final Path path, final byte[] bytes) throws
}
}

public static List<BlockItemUnparsed> generateBlockItemsUnparsed(int numOfBlocks) {

List<BlockItemUnparsed> blockItems = new ArrayList<>();
for (int i = 1; i <= numOfBlocks; i++) {
for (int j = 1; j <= 10; j++) {
switch (j) {
case 1:
// First block is always the header
blockItems.add(BlockItemUnparsed.newBuilder()
.blockHeader(BlockHeader.PROTOBUF.toBytes(BlockHeader.newBuilder()
.number(i)
.softwareVersion(SemanticVersion.newBuilder()
.major(1)
.minor(0)
.build())
.build()))
.build());
break;
case 10:
// Last block is always the state proof
blockItems.add(BlockItemUnparsed.newBuilder()
.blockProof(BlockProof.PROTOBUF.toBytes(
BlockProof.newBuilder().block(i).build()))
.build());
break;
default:
// Middle blocks are events
blockItems.add(BlockItemUnparsed.newBuilder()
.eventHeader(EventHeader.PROTOBUF.toBytes(EventHeader.newBuilder()
.eventCore(EventCore.newBuilder()
.creatorNodeId(i)
.build())
.build()))
.build());
break;
}
/**
* This method generates a list of {@link BlockItemUnparsed} with the input
* blockNumber used to generate the block items for. It generates 10 block
* items starting with the block header, followed by 8 events and ending
* with the block proof.
*
* @param blockNumber the block number to generate the block items for
*
* @return a list of {@link BlockItemUnparsed} with the input blockNumber
* used to generate the block items for
*/
public static List<BlockItemUnparsed> generateBlockItemsUnparsedForWithBlockNumber(final long blockNumber) {
final List<BlockItemUnparsed> result = new ArrayList<>();
for (int j = 1; j <= 10; j++) {
switch (j) {
case 1:
// First block is always the header
result.add(BlockItemUnparsed.newBuilder()
.blockHeader(BlockHeader.PROTOBUF.toBytes(BlockHeader.newBuilder()
.number(blockNumber)
.softwareVersion(SemanticVersion.newBuilder()
.major(1)
.minor(0)
.build())
.build()))
.build());
break;
case 10:
// Last block is always the state proof
result.add(BlockItemUnparsed.newBuilder()
.blockProof(BlockProof.PROTOBUF.toBytes(
BlockProof.newBuilder().block(blockNumber).build()))
.build());
break;
default:
// Middle blocks are events
result.add(BlockItemUnparsed.newBuilder()
.eventHeader(EventHeader.PROTOBUF.toBytes(EventHeader.newBuilder()
.eventCore(EventCore.newBuilder()
.creatorNodeId(blockNumber)
.build())
.build()))
.build());
break;
}
}
return result;
}

/**
* This method generates a list of {@link BlockItemUnparsed} for as many
* blocks as specified by the input parameter numOfBlocks. For each block
* number from 1 to numOfBlocks, it generates 10 block items starting with
* the block header, followed by 8 events and ending with the block proof.
* In a way, this simulates a stream of block items. Each 10 items in the
* list represent a block.
*
* @param numOfBlocks the number of blocks to generate block items for
*
* @return a list of {@link BlockItemUnparsed} for as many blocks as
* specified by the input parameter numOfBlocks
*/
public static List<BlockItemUnparsed> generateBlockItemsUnparsed(int numOfBlocks) {
final List<BlockItemUnparsed> blockItems = new ArrayList<>();
for (int i = 1; i <= numOfBlocks; i++) {
blockItems.addAll(generateBlockItemsUnparsedForWithBlockNumber(i));
}
return blockItems;
}

Expand Down

0 comments on commit 5f08f5c

Please sign in to comment.