Skip to content

Commit

Permalink
fix loading block manager in consumer mode
Browse files Browse the repository at this point in the history
Signed-off-by: georgi-l95 <[email protected]>
  • Loading branch information
georgi-l95 committed Nov 27, 2024
1 parent 87b6d89 commit b6e5369
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public class BlockAsDirBlockStreamManager implements BlockStreamManager {
@Inject
public BlockAsDirBlockStreamManager(@NonNull BlockGeneratorConfig blockGeneratorConfig) {
this.rootFolder = blockGeneratorConfig.folderRootPath();
}

/**
* Initialize the block stream manager and load blocks into memory.
*/
@Override
public void init() {
try {
this.loadBlocks();
} catch (IOException | ParseException | IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public class BlockAsFileBlockStreamManager implements BlockStreamManager {
@Inject
public BlockAsFileBlockStreamManager(@NonNull BlockGeneratorConfig blockStreamConfig) {
this.rootFolder = blockStreamConfig.folderRootPath();
}

/**
* Initialize the block stream manager and load blocks into memory.
*/
@Override
public void init() {
try {
this.loadBlocks();
} catch (IOException | ParseException | IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public BlockAsFileLargeDataSets(@NonNull BlockGeneratorConfig config) {
this.formatString = "%0" + config.paddedLength() + "d" + config.fileExtension();
}

/**
* Initialize the block stream manager and load blocks into memory.
*/
@Override
public void init() {
// Do nothing, because we don't have real initializing and loading blocks into memory for this implementation.
}

Check warning on line 74 in simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileLargeDataSets.java

View check run for this annotation

Codecov / codecov/patch

simulator/src/main/java/com/hedera/block/simulator/generator/BlockAsFileLargeDataSets.java#L74

Added line #L74 was not covered by tests

@Override
public GenerationMode getGenerationMode() {
return GenerationMode.DIR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
/** The block stream manager interface. */
public interface BlockStreamManager {

/**
* Initialize the block stream manager and load blocks into memory.
*/
void init();

/**
* Get the generation mode.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public PublisherModeHandler(
}

public void init() {
blockStreamManager.init();
publishStreamGrpcClient.init();
LOGGER.log(INFO, "gRPC Channel initialized for publishing blocks.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void getGenerationMode() {
@Test
void getNextBlockItem() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager = getBlockAsDirBlockStreamManager(getAbsoluteFolder(rootFolder));
blockStreamManager.init();

for (int i = 0; i < 1000; i++) {
assertNotNull(blockStreamManager.getNextBlockItem());
Expand All @@ -55,6 +56,7 @@ void getNextBlockItem() throws IOException, BlockSimulatorParsingException {
@Test
void getNextBlock() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager = getBlockAsDirBlockStreamManager(getAbsoluteFolder(rootFolder));
blockStreamManager.init();

for (int i = 0; i < 3000; i++) {
assertNotNull(blockStreamManager.getNextBlock());
Expand All @@ -71,7 +73,8 @@ void BlockAsFileBlockStreamManagerInvalidRootPath() {
private BlockStreamManager getBlockAsDirBlockStreamManager(String rootFolder) {
final BlockGeneratorConfig blockGeneratorConfig = new BlockGeneratorConfig(
GenerationMode.DIR, rootFolder, "BlockAsDirBlockStreamManager", 36, ".blk", 0, 0);

return new BlockAsDirBlockStreamManager(blockGeneratorConfig);
final BlockStreamManager blockStreamManager = new BlockAsDirBlockStreamManager(blockGeneratorConfig);
blockStreamManager.init();
return blockStreamManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,38 @@
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import java.io.IOException;
import java.nio.file.Paths;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class BlockAsFileBlockStreamManagerTest {

private final String gzRootFolder = "src/main/resources/block-0.0.3/";
private BlockStreamManager blockStreamManager;

private String getAbsoluteFolder(String relativePath) {
return Paths.get(relativePath).toAbsolutePath().toString();
}

@BeforeEach
void setUp() {
blockStreamManager = getBlockAsFileBlockStreamManager(getAbsoluteFolder(gzRootFolder));
blockStreamManager.init();
}

@Test
void getGenerationMode() {
BlockStreamManager blockStreamManager =
getBlockAsFileBlockStreamManager(getAbsoluteFolder(gzRootFolder));
assertEquals(GenerationMode.DIR, blockStreamManager.getGenerationMode());
}

@Test
void getNextBlock() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsFileBlockStreamManager(getAbsoluteFolder(gzRootFolder));
for (int i = 0; i < 3000; i++) {
assertNotNull(blockStreamManager.getNextBlock());
}
}

@Test
void getNextBlockItem() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsFileBlockStreamManager(getAbsoluteFolder(gzRootFolder));
for (int i = 0; i < 35000; i++) {
assertNotNull(blockStreamManager.getNextBlockItem());
}
Expand All @@ -61,31 +63,30 @@ void getNextBlockItem() throws IOException, BlockSimulatorParsingException {
@Test
void loadBlockBlk() throws IOException, BlockSimulatorParsingException {
String blkRootFolder = "src/test/resources/block-0.0.3-blk/";
BlockStreamManager blockStreamManager =
getBlockAsFileBlockStreamManager(getAbsoluteFolder(blkRootFolder));
BlockStreamManager blockStreamManager = getBlockAsFileBlockStreamManager(getAbsoluteFolder(blkRootFolder));
blockStreamManager.init();

assertNotNull(blockStreamManager.getNextBlock());
}

@Test
void BlockAsFileBlockStreamManagerInvalidRootPath() {
assertThrows(
RuntimeException.class,
() ->
getBlockAsFileBlockStreamManager(
getAbsoluteFolder("src/test/resources/BlockAsDirException/1/")));
() -> getBlockAsFileBlockStreamManager(getAbsoluteFolder("src/test/resources/BlockAsDirException/1/")));
}

private BlockAsFileBlockStreamManager getBlockAsFileBlockStreamManager(String rootFolder) {

BlockGeneratorConfig blockGeneratorConfig =
BlockGeneratorConfig.builder()
.generationMode(GenerationMode.DIR)
.folderRootPath(rootFolder)
.managerImplementation("BlockAsFileBlockStreamManager")
.paddedLength(36)
.fileExtension(".blk")
.build();
private BlockStreamManager getBlockAsFileBlockStreamManager(String rootFolder) {
BlockGeneratorConfig blockGeneratorConfig = BlockGeneratorConfig.builder()
.generationMode(GenerationMode.DIR)
.folderRootPath(rootFolder)
.managerImplementation("BlockAsFileBlockStreamManager")
.paddedLength(36)
.fileExtension(".blk")
.build();

return new BlockAsFileBlockStreamManager(blockGeneratorConfig);
BlockStreamManager blockStreamManager = new BlockAsFileBlockStreamManager(blockGeneratorConfig);
blockStreamManager.init();
return blockStreamManager;
}
}

0 comments on commit b6e5369

Please sign in to comment.