more mocks added

pull/93/merge
moneroexamples 6 years ago
parent 9e10445241
commit aed9eff7aa

@ -111,8 +111,8 @@ set(LIBRARIES
myext
restbed
wallet
blockchain_db
cryptonote_core
blockchain_db
cryptonote_protocol
cryptonote_basic
daemonizer

@ -6,7 +6,6 @@
#include "tools.h"
#include "mylmdb.h"
#include "rpccalls.h"
#include "MySqlAccounts.h"
#include "TxSearch.h"
@ -60,7 +59,7 @@ CurrentBlockchainStatus::start_monitor_blockchain_thread()
uint64_t
CurrentBlockchainStatus::get_current_blockchain_height()
{
return mcore.get_current_blockchain_height() - 1;
return mcore->get_current_blockchain_height() - 1;
}

@ -7,6 +7,16 @@
namespace xmreg
{
void
MicroBlockchainLMDB::sync()
{
//cout << "MicroBlockchainLMDB::sync()\n\n";
// we open in readonly, so dont sync anything
}
/**
* The constructor is interesting, as
* m_mempool and m_blockchain_storage depend
@ -40,12 +50,15 @@ MicroCore::init(const string& _blockchain_path, network_type nt)
nettype = nt;
std::unique_ptr<BlockchainDB> db = std::make_unique<BlockchainLMDB>();
//std::unique_ptr<BlockchainDB> db = std::make_unique<BlockchainLMDB>();
std::unique_ptr<BlockchainDB> db = std::make_unique<MicroBlockchainLMDB>();
try
{
// try opening lmdb database files
db->open(blockchain_path, DBF_RDONLY);
db->open(blockchain_path, DBF_RDONLY);
//db->open(blockchain_path, DBF_RDONLY | MDB_NOSYNC);
//db->open(blockchain_path, MDB_RDONLY | MDB_NOSYNC);
}
catch (const std::exception& e)
{
@ -72,6 +85,8 @@ MicroCore::init(const string& _blockchain_path, network_type nt)
return false;
}
initialization_succeded = true;
return true;
}
@ -129,5 +144,24 @@ MicroCore::get_device() const
return m_device;
}
bool
MicroCore::init_success() const
{
return initialization_succeded;
}
MicroCore::~MicroCore()
{
//cout << "\n\nMicroCore::~MicroCore()\n\n";
if (initialization_succeded)
{
//core_storage.get_db().safesyncmode(true);
if (core_storage.get_db().is_open())
core_storage.get_db().close();
//cout << "\n\n core_storage.get_db().close();;\n\n";
}
}
}

@ -17,6 +17,17 @@ using namespace cryptonote;
using namespace crypto;
using namespace std;
class MicroBlockchainLMDB : public BlockchainLMDB
{
public:
using BlockchainLMDB::BlockchainLMDB;
virtual void sync() override;
};
/**
* Micro version of cryptonode::core class
* Micro version of constructor,
@ -36,6 +47,8 @@ class MicroCore {
network_type nettype;
bool initialization_succeded {false};
public:
MicroCore();
@ -64,16 +77,19 @@ public:
return core_storage.get_db().get_output_key(std::forward<T>(args)...);
}
template <typename... T>
auto get_transactions(T&&... args) const
virtual bool
get_transactions(
const std::vector<crypto::hash>& txs_ids,
std::vector<transaction>& txs,
std::vector<crypto::hash>& missed_txs) const
{
return core_storage.get_transactions(std::forward<T>(args)...);
return core_storage.get_transactions(txs_ids, txs, missed_txs);
}
template <typename... T>
auto get_blocks_range(T&&... args) const
virtual std::vector<block>
get_blocks_range(const uint64_t& h1, const uint64_t& h2) const
{
return core_storage.get_db().get_blocks_range(std::forward<T>(args)...);
return core_storage.get_db().get_blocks_range(h1, h2);
}
template <typename... T>
@ -123,6 +139,11 @@ public:
virtual bool
get_tx(crypto::hash const& tx_hash, transaction& tx) const;
virtual bool
init_success() const;
virtual ~MicroCore();
};
}

@ -10,6 +10,7 @@ macro(add_om_test _TEST_NAME)
target_link_libraries(${_TEST_NAME}_tests
gtest gtest_main
gmock gmock_main
${LIBRARIES})
add_test(NAME ${_TEST_NAME}_tests COMMAND ${_TEST_NAME}_tests)

@ -26,9 +26,24 @@ using ::testing::Ge;
using ::testing::Le;
using ::testing::HasSubstr;
using ::testing::Not;
using ::testing::Return;
using ::testing::Throw;
using ::testing::_;
using ::testing::internal::FilePath;
class MockMicroCore : public xmreg::MicroCore
{
public:
MOCK_METHOD2(init, bool(const string& _blockchain_path, network_type nt));
MOCK_CONST_METHOD2(get_block_from_height, bool(uint64_t height, block& blk));
MOCK_CONST_METHOD2(get_blocks_range, std::vector<block>(const uint64_t& h1, const uint64_t& h2));
MOCK_CONST_METHOD3(get_transactions, bool(const std::vector<crypto::hash>& txs_ids,
std::vector<transaction>& txs,
std::vector<crypto::hash>& missed_txs));
};
class BCSTATUS_TEST : public ::testing::Test
{
public:
@ -37,20 +52,27 @@ public:
SetUpTestCase()
{
string config_path {"../config/config.json"};
config_json = xmreg::BlockchainSetup::read_config(config_path);
config_json = xmreg::BlockchainSetup::read_config(config_path);
}
protected:
virtual void
SetUp()
{
{
bc_setup = xmreg::BlockchainSetup{net_type, do_not_relay, config_json};
mcore = std::make_unique<MockMicroCore>();
mcore_ptr = mcore.get();
bcs = std::make_unique<xmreg::CurrentBlockchainStatus>(bc_setup, std::move(mcore));
}
network_type net_type {network_type::STAGENET};
bool do_not_relay {false};
xmreg::BlockchainSetup bc_setup;
std::unique_ptr<MockMicroCore> mcore;
std::unique_ptr<xmreg::CurrentBlockchainStatus> bcs;
MockMicroCore* mcore_ptr;
static json config_json;
};
@ -63,21 +85,67 @@ TEST_F(BCSTATUS_TEST, DefaultConstruction)
EXPECT_TRUE(true);
}
class MockMicroCore : public xmreg::MicroCore
{
bool
init(const string& _blockchain_path, network_type nt)
{
return true;
}
};
TEST_F(BCSTATUS_TEST, InitMoneroBlockchain)
{
std::unique_ptr<MockMicroCore> mcore = std::make_unique<MockMicroCore>();
xmreg::CurrentBlockchainStatus bcs {bc_setup, std::move(mcore)};
EXPECT_TRUE(bcs.init_monero_blockchain());
EXPECT_CALL(*mcore_ptr, init(_, _))
.WillOnce(Return(true));
EXPECT_TRUE(bcs->init_monero_blockchain());
}
TEST_F(BCSTATUS_TEST, GetBlock)
{
EXPECT_CALL(*mcore_ptr, get_block_from_height(_, _))
.WillOnce(Return(true));
uint64_t height = 1000;
block blk;
EXPECT_TRUE(bcs->get_block(height, blk));
}
ACTION(ThrowBlockDNE)
{
throw BLOCK_DNE("ddddd");
}
TEST_F(BCSTATUS_TEST, GetBlockRange)
{
vector<block> blocks_to_return {block(), block(), block()};
EXPECT_CALL(*mcore_ptr, get_blocks_range(_, _))
.WillOnce(Return(blocks_to_return));
uint64_t h1 = 1000;
uint64_t h2 = h1+2;
vector<block> blocks = bcs->get_blocks_range(h1, h2);
EXPECT_EQ(blocks, blocks_to_return);
EXPECT_CALL(*mcore_ptr, get_blocks_range(_, _))
.WillOnce(ThrowBlockDNE());
blocks = bcs->get_blocks_range(h1, h2);
EXPECT_TRUE(blocks.empty());
}
TEST_F(BCSTATUS_TEST, GetBlockTxs)
{
EXPECT_CALL(*mcore_ptr, get_transactions(_, _, _))
.WillOnce(Return(true));
const block dummy_blk;
vector<transaction> blk_txs;
vector<crypto::hash> missed_txs;
EXPECT_TRUE(bcs->get_block_txs(dummy_blk, blk_txs, missed_txs));
}
}

Loading…
Cancel
Save