Skip to content

Commit

Permalink
Add test docs for block_loader_test
Browse files Browse the repository at this point in the history
Signed-off-by: Kitsu <[email protected]>
  • Loading branch information
l4l committed Feb 14, 2018
1 parent bf34ee9 commit 992d242
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions irohad/network/block_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace iroha {
* @param peer_pubkey - peer for requesting blocks
* @param block_hash - requested block hash
* @return block on success, nullopt on failure
* TODO 14/02/17 (@l4l) IR-960 rework method with returning result
*/
virtual nonstd::optional<Wrapper<shared_model::interface::Block>>
retrieveBlock(
Expand Down
1 change: 1 addition & 0 deletions irohad/network/impl/block_loader_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace iroha {
* Retrieve peers from database, and find the requested peer by pubkey
* @param pubkey - public key of requested peer
* @return peer, if it was found, otherwise nullopt
* TODO 14/02/17 (@l4l) IR-960 rework method with returning result
*/
nonstd::optional<model::Peer> findPeer(
const shared_model::crypto::PublicKey &pubkey);
Expand Down
48 changes: 36 additions & 12 deletions test/module/irohad/network/block_loader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ class BlockLoaderTest : public testing::Test {
ASSERT_NE(port, 0);
}

auto base() const {
auto getBaseBlockBuilder() const {
constexpr auto kTotal = (1 << 5) - 1;
return shared_model::proto::TemplateBlockBuilder<
kTotal,
shared_model::validation::DefaultBlockValidator,
shared_model::proto::Block>()
.txNumber(0)
.height(0)
.height(1)
.prevHash(Hash(std::string(32, '0')))
.createdTime(iroha::time::now());
}
Expand All @@ -93,9 +93,14 @@ class BlockLoaderTest : public testing::Test {
std::unique_ptr<grpc::Server> server;
};

/**
* @given empty storage, related block loader and base block
* @when retrieveBlocks is called
* @then nothing is returned
*/
TEST_F(BlockLoaderTest, ValidWhenSameTopBlock) {
// Current block height 1 => Other block height 1 => no blocks received
auto block = base().height(1).build();
auto block = getBaseBlockBuilder().build();
std::unique_ptr<iroha::model::Block> old_block(block.makeOldModel());

EXPECT_CALL(*peer_query, getLedgerPeers()).WillOnce(Return(peers));
Expand All @@ -110,12 +115,17 @@ TEST_F(BlockLoaderTest, ValidWhenSameTopBlock) {
ASSERT_TRUE(wrapper.validate());
}

/**
* @given block loader and a pair of consecutive blocks
* @when retrieveBlocks is called
* @then the last one is returned
*/
TEST_F(BlockLoaderTest, ValidWhenOneBlock) {
// Current block height 1 => Other block height 2 => one block received
auto block = base().height(1).build();
auto block = getBaseBlockBuilder().build();
std::unique_ptr<iroha::model::Block> old_block(block.makeOldModel());

auto top_block = base().height(block.height() + 1).build();
auto top_block = getBaseBlockBuilder().height(block.height() + 1).build();
std::unique_ptr<iroha::model::Block> old_top_block(top_block.makeOldModel());

EXPECT_CALL(*provider, verify(A<const Block &>())).WillOnce(Return(true));
Expand All @@ -126,24 +136,28 @@ TEST_F(BlockLoaderTest, ValidWhenOneBlock) {
.WillOnce(Return(rxcpp::observable<>::just(*old_top_block)));
auto wrapper =
make_test_subscriber<CallExact>(loader->retrieveBlocks(peer_key), 1);
wrapper.subscribe([&top_block](auto block) {
ASSERT_EQ(block->height(), top_block.height());
});
wrapper.subscribe(
[&top_block](auto block) { ASSERT_EQ(*block.operator->(), top_block); });

ASSERT_TRUE(wrapper.validate());
}

/**
* @given block loader, a block, and additional num_blocks blocks
* @when retrieveBlocks is called
* @then it returns consecutive heights
*/
TEST_F(BlockLoaderTest, ValidWhenMultipleBlocks) {
// Current block height 1 => Other block height n => n-1 blocks received
auto block = base().height(1).build();
auto block = getBaseBlockBuilder().build();
std::unique_ptr<iroha::model::Block> old_block(block.makeOldModel());

auto num_blocks = 2;
auto next_height = block.height() + 1;

std::vector<iroha::model::Block> blocks;
for (auto i = next_height; i < next_height + num_blocks; ++i) {
auto blk = base().height(i).build();
auto blk = getBaseBlockBuilder().height(i).build();
std::unique_ptr<iroha::model::Block> old(blk.makeOldModel());
blocks.push_back(*old);
}
Expand All @@ -165,9 +179,14 @@ TEST_F(BlockLoaderTest, ValidWhenMultipleBlocks) {
ASSERT_TRUE(wrapper.validate());
}

/**
* @given block loader with a block
* @when retrieveBlock is called with the related hash
* @then it returns the same block
*/
TEST_F(BlockLoaderTest, ValidWhenBlockPresent) {
// Request existing block => success
auto requested = base().build();
auto requested = getBaseBlockBuilder().build();
std::unique_ptr<iroha::model::Block> old_requested(requested.makeOldModel());

EXPECT_CALL(*provider, verify(A<const Block &>())).WillOnce(Return(true));
Expand All @@ -180,9 +199,14 @@ TEST_F(BlockLoaderTest, ValidWhenBlockPresent) {
ASSERT_EQ(*block.value().operator->(), requested);
}

/**
* @given block loader and a block
* @when retrieveBlock is called with a different hash
* @then nothing is returned
*/
TEST_F(BlockLoaderTest, ValidWhenBlockMissing) {
// Request nonexisting block => failure
auto present = base().build();
auto present = getBaseBlockBuilder().build();
std::unique_ptr<iroha::model::Block> old_present(present.makeOldModel());

EXPECT_CALL(*peer_query, getLedgerPeers()).WillOnce(Return(peers));
Expand Down

0 comments on commit 992d242

Please sign in to comment.