Skip to content

Commit

Permalink
191 wire simulator with test infra (#243)
Browse files Browse the repository at this point in the history
Signed-off-by: georgi-l95 <[email protected]>
  • Loading branch information
georgi-l95 authored Oct 10, 2024
1 parent 70a588d commit 316fb86
Show file tree
Hide file tree
Showing 38 changed files with 145 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static java.lang.System.Logger.Level.INFO;

import com.hedera.pbj.runtime.ParseException;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.swirlds.config.api.Configuration;
import com.swirlds.config.api.ConfigurationBuilder;
import com.swirlds.config.extensions.sources.ClasspathFileConfigSource;
Expand All @@ -41,10 +41,10 @@ private BlockStreamSimulator() {}
* @param args the arguments to be passed to the block stream simulator
* @throws IOException if an I/O error occurs
* @throws InterruptedException if the thread is interrupted
* @throws ParseException if a parse error occurs
* @throws BlockSimulatorParsingException if a parse error occurs
*/
public static void main(String[] args)
throws IOException, InterruptedException, ParseException {
throws IOException, InterruptedException, BlockSimulatorParsingException {

LOGGER.log(INFO, "Starting Block Stream Simulator");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
package com.hedera.block.simulator;

import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.hedera.block.simulator.generator.BlockStreamManager;
import com.hedera.block.simulator.grpc.PublishStreamGrpcClient;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import com.swirlds.config.api.Configuration;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.inject.Inject;

/** BlockStream Simulator App */
Expand All @@ -39,7 +40,7 @@ public class BlockStreamSimulatorApp {

private final int delayBetweenBlockItems;

boolean isRunning = false;
private final AtomicBoolean isRunning = new AtomicBoolean(false);

/**
* Creates a new BlockStreamSimulatorApp instance.
Expand All @@ -66,14 +67,14 @@ public BlockStreamSimulatorApp(
* Starts the block stream simulator.
*
* @throws InterruptedException if the thread is interrupted
* @throws ParseException if a parse error occurs
* @throws BlockSimulatorParsingException if a parse error occurs
* @throws IOException if an I/O error occurs
*/
public void start() throws InterruptedException, ParseException, IOException {
public void start() throws InterruptedException, BlockSimulatorParsingException, IOException {
int delayMSBetweenBlockItems = delayBetweenBlockItems / 1_000_000;
int delayNSBetweenBlockItems = delayBetweenBlockItems % 1_000_000;

isRunning = true;
isRunning.set(true);
LOGGER.log(System.Logger.Level.INFO, "Block Stream Simulator has started");

boolean streamBlockItem = true;
Expand Down Expand Up @@ -113,12 +114,12 @@ public void start() throws InterruptedException, ParseException, IOException {
* @return true if the block stream simulator is running, false otherwise
*/
public boolean isRunning() {
return isRunning;
return isRunning.get();
}

/** Stops the block stream simulator. */
public void stop() {
isRunning = false;
isRunning.set(false);
LOGGER.log(System.Logger.Level.INFO, "Block Stream Simulator has stopped");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.block.simulator.exception;

/** Use this checked exception to represent a Block Simulator parsing exception. */
public class BlockSimulatorParsingException extends Exception {
/**
* Constructs a new parsing exception with the specified detail message.
*
* @param message the detail message
*/
public BlockSimulatorParsingException(String message) {
super(message);
}

Check warning on line 28 in simulator/src/main/java/com/hedera/block/simulator/exception/BlockSimulatorParsingException.java

View check run for this annotation

Codecov / codecov/patch

simulator/src/main/java/com/hedera/block/simulator/exception/BlockSimulatorParsingException.java#L27-L28

Added lines #L27 - L28 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.config.types.GenerationMode;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
Expand Down Expand Up @@ -59,7 +60,7 @@ public GenerationMode getGenerationMode() {
}

@Override
public BlockItem getNextBlockItem() throws IOException, ParseException {
public BlockItem getNextBlockItem() throws IOException, BlockSimulatorParsingException {
if (currentBlock != null && currentBlock.items().size() > currentBlockItemIndex) {
return currentBlock.items().get(currentBlockItemIndex++);
} else {
Expand All @@ -74,22 +75,26 @@ public BlockItem getNextBlockItem() throws IOException, ParseException {
}

@Override
public Block getNextBlock() throws IOException, ParseException {
public Block getNextBlock() throws IOException, BlockSimulatorParsingException {
currentBlockIndex++;

String nextBlockFileName = String.format(formatString, currentBlockIndex);
File blockFile = new File(blockstreamPath, nextBlockFileName);

if (blockFile.exists()) {
if (!blockFile.exists()) {
return null;
}

try {
byte[] blockBytes = readFileBytes(blockFile.toPath());

LOGGER.log(INFO, "Loading block: " + blockFile.getName());

Block block = Block.PROTOBUF.parse(Bytes.wrap(blockBytes));
LOGGER.log(INFO, "block loaded with items size= " + block.items().size());
return block;
} catch (ParseException e) {
throw new BlockSimulatorParsingException(e.getMessage());

Check warning on line 97 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#L96-L97

Added lines #L96 - L97 were not covered by tests
}

return null; // No more blocks found
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package com.hedera.block.simulator.generator;

import com.hedera.block.simulator.config.types.GenerationMode;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import java.io.IOException;

/** The block stream manager interface. */
Expand All @@ -36,17 +36,17 @@ public interface BlockStreamManager {
* Get the next block item.
*
* @return the next block item
* @throws IOException if an I/O error occurs
* @throws ParseException if a parse error occurs
* @throws IOException if a I/O error occurs
* @throws BlockSimulatorParsingException if a parse error occurs
*/
BlockItem getNextBlockItem() throws IOException, ParseException;
BlockItem getNextBlockItem() throws IOException, BlockSimulatorParsingException;

/**
* Get the next block.
*
* @return the next block
* @throws IOException if an I/O error occurs
* @throws ParseException if a parse error occurs
* @throws IOException if a I/O error occurs
* @throws BlockSimulatorParsingException if a parse error occurs
*/
Block getNextBlock() throws IOException, ParseException;
Block getNextBlock() throws IOException, BlockSimulatorParsingException;
}
1 change: 1 addition & 0 deletions simulator/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/** Runtime module of the simulator. */
module com.hedera.block.simulator {
exports com.hedera.block.simulator.config.data;
exports com.hedera.block.simulator.exception;
exports com.hedera.block.simulator;

requires static com.github.spotbugs.annotations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.hedera.block.simulator.generator.BlockStreamManager;
import com.hedera.block.simulator.grpc.PublishStreamGrpcClient;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import com.swirlds.config.api.Configuration;
import java.io.IOException;
import java.nio.file.Paths;
Expand Down Expand Up @@ -63,13 +63,15 @@ void tearDown() {
}

@Test
void start_logsStartedMessage() throws InterruptedException, ParseException, IOException {
void start_logsStartedMessage()
throws InterruptedException, BlockSimulatorParsingException, IOException {
blockStreamSimulator.start();
assertTrue(blockStreamSimulator.isRunning());
}

@Test
void start_exitByBlockNull() throws InterruptedException, ParseException, IOException {
void start_exitByBlockNull()
throws InterruptedException, BlockSimulatorParsingException, IOException {

BlockStreamManager blockStreamManager = Mockito.mock(BlockStreamManager.class);
when(blockStreamManager.getNextBlockItem()).thenReturn(BlockItem.newBuilder().build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.config.types.GenerationMode;
import com.hedera.pbj.runtime.ParseException;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import java.io.IOException;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
Expand All @@ -45,7 +45,7 @@ void getGenerationMode() {
}

@Test
void getNextBlockItem() throws IOException, ParseException {
void getNextBlockItem() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsDirBlockStreamManager(getAbsoluteFolder(rootFolder));

Expand All @@ -55,7 +55,7 @@ void getNextBlockItem() throws IOException, ParseException {
}

@Test
void getNextBlock() throws IOException, ParseException {
void getNextBlock() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsDirBlockStreamManager(getAbsoluteFolder(rootFolder));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.config.types.GenerationMode;
import com.hedera.pbj.runtime.ParseException;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import java.io.IOException;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
Expand All @@ -41,7 +41,7 @@ void getGenerationMode() {
}

@Test
void getNextBlock() throws IOException, ParseException {
void getNextBlock() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsFileBlockStreamManager(getAbsoluteFolder(gzRootFolder));
for (int i = 0; i < 3000; i++) {
Expand All @@ -50,7 +50,7 @@ void getNextBlock() throws IOException, ParseException {
}

@Test
void getNextBlockItem() throws IOException, ParseException {
void getNextBlockItem() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsFileBlockStreamManager(getAbsoluteFolder(gzRootFolder));
for (int i = 0; i < 35000; i++) {
Expand All @@ -59,7 +59,7 @@ void getNextBlockItem() throws IOException, ParseException {
}

@Test
void loadBlockBlk() throws IOException, ParseException {
void loadBlockBlk() throws IOException, BlockSimulatorParsingException {
String blkRootFolder = "src/test/resources/block-0.0.3-blk/";
BlockStreamManager blockStreamManager =
getBlockAsFileBlockStreamManager(getAbsoluteFolder(blkRootFolder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.config.types.GenerationMode;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
Expand Down Expand Up @@ -51,7 +51,7 @@ void getGenerationMode() {
}

@Test
void getNextBlock() throws IOException, ParseException {
void getNextBlock() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsFileLargeDatasetsBlockStreamManager(getAbsoluteFolder(rootFolder));
for (int i = 0; i < filesInFolder; i++) {
Expand All @@ -62,7 +62,7 @@ void getNextBlock() throws IOException, ParseException {
}

@Test
void getNextBlockItem() throws IOException, ParseException {
void getNextBlockItem() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsFileLargeDatasetsBlockStreamManager(getAbsoluteFolder(rootFolder));

Expand Down
8 changes: 7 additions & 1 deletion suites/src/main/java/com/hedera/block/suites/BaseSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ public static GenericContainer<?> getConfiguration() {
return blockNodeContainer;
}

private static Configuration loadDefaultConfiguration() throws IOException {
/**
* Builds the default block simulator configuration
*
* @return default block simulator configuration
* @throws IOException if an I/O error occurs
*/
protected static Configuration loadDefaultConfiguration() throws IOException {
ConfigurationBuilder configurationBuilder =
ConfigurationBuilder.create()
.withSource(SystemEnvironmentConfigSource.getInstance())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.block.suites.persistence;

import com.hedera.block.suites.persistence.positive.PositiveDataPersistenceTests;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

/**
* Test suite for running data persistence tests, including both positive and negative test
* scenarios.
*
* <p>This suite aggregates the tests from {@link PositiveDataPersistenceTests}. The {@code @Suite}
* annotation allows running all selected classes in a single test run.
*/
@Suite
@SelectClasses({PositiveDataPersistenceTests.class})
public class DataPersistenceTestSuites {

/**
* Default constructor for the {@link DataPersistenceTestSuites} class. This constructor is
* empty as it does not need to perform any initialization.
*/
public DataPersistenceTestSuites() {}
}
Loading

0 comments on commit 316fb86

Please sign in to comment.