From 6e616f367fff76c8eae6b7e66dcc34f7bcba3679 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Sat, 28 Jul 2018 13:11:24 +0800 Subject: [PATCH] StartTxSearchThread test added --- src/CurrentBlockchainStatus.cpp | 18 ++++++------------ src/CurrentBlockchainStatus.h | 3 ++- src/MicroCore.cpp | 14 +------------- src/MicroCore.h | 13 +------------ src/OutputInputIdentification.h | 1 + src/ThreadRAII.cpp | 6 ++++++ src/ThreadRAII.h | 3 ++- src/TxSearch.h | 3 +++ src/YourMoneroRequests.cpp | 9 ++++++--- tests/bcstatus_tests.cpp | 32 ++++++++++++++++++++++++++++++++ 10 files changed, 60 insertions(+), 42 deletions(-) diff --git a/src/CurrentBlockchainStatus.cpp b/src/CurrentBlockchainStatus.cpp index 1081687..b5895af 100755 --- a/src/CurrentBlockchainStatus.cpp +++ b/src/CurrentBlockchainStatus.cpp @@ -660,7 +660,8 @@ CurrentBlockchainStatus::get_output_key( } bool -CurrentBlockchainStatus::start_tx_search_thread(XmrAccount acc) +CurrentBlockchainStatus::start_tx_search_thread( + XmrAccount acc, std::unique_ptr tx_search) { std::lock_guard lck (searching_threads_map_mtx); @@ -673,19 +674,12 @@ CurrentBlockchainStatus::start_tx_search_thread(XmrAccount acc) try { - // make a tx_search object for the given xmr account - //searching_threads.emplace(acc.address, new TxSearch(acc)); - // does not work on older gcc - // such as the one in ubuntu 16.04 + // launch SearchTx thread for the given xmr account - // create SearchTx object. It does not start searching yet + searching_threads.insert( + {acc.address, ThreadRAII2(std::move(tx_search))}); - ThreadRAII2 t(std::make_unique(acc, shared_from_this()), - ThreadRAII::DtorAction::join); - - searching_threads.insert({acc.address, std::move(t)}); - - OMINFO << "Search thread created for address " << acc.address; + OMINFO << "Search thread created for address: " << acc.address; } catch (const std::exception& e) { diff --git a/src/CurrentBlockchainStatus.h b/src/CurrentBlockchainStatus.h index 01e4884..5be0c1e 100755 --- a/src/CurrentBlockchainStatus.h +++ b/src/CurrentBlockchainStatus.h @@ -166,7 +166,8 @@ public: // definitions of these function are at the end of this file // due to forward declaraions of TxSearch virtual bool - start_tx_search_thread(XmrAccount acc); + start_tx_search_thread(XmrAccount acc, + std::unique_ptr tx_search); virtual bool ping_search_thread(const string& address); diff --git a/src/MicroCore.cpp b/src/MicroCore.cpp index 34e671d..2f50919 100755 --- a/src/MicroCore.cpp +++ b/src/MicroCore.cpp @@ -8,15 +8,6 @@ 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 @@ -50,15 +41,12 @@ MicroCore::init(const string& _blockchain_path, network_type nt) nettype = nt; - //std::unique_ptr db = std::make_unique(); - std::unique_ptr db = std::make_unique(); + std::unique_ptr db = std::make_unique(); try { // try opening lmdb database files 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) { diff --git a/src/MicroCore.h b/src/MicroCore.h index 5900512..781e21d 100755 --- a/src/MicroCore.h +++ b/src/MicroCore.h @@ -17,17 +17,6 @@ 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, @@ -40,8 +29,8 @@ class MicroCore { string blockchain_path; - Blockchain core_storage; tx_memory_pool m_mempool; + Blockchain core_storage; hw::device* m_device; diff --git a/src/OutputInputIdentification.h b/src/OutputInputIdentification.h index dcad1ae..9286bb6 100755 --- a/src/OutputInputIdentification.h +++ b/src/OutputInputIdentification.h @@ -9,6 +9,7 @@ #include "tools.h" #include +#include namespace xmreg { diff --git a/src/ThreadRAII.cpp b/src/ThreadRAII.cpp index 91506fb..82a93b1 100644 --- a/src/ThreadRAII.cpp +++ b/src/ThreadRAII.cpp @@ -16,9 +16,15 @@ ThreadRAII::~ThreadRAII() if (t.joinable()) { if (action == DtorAction::join) + { + std::cout << "\nThreadRAII::~ThreadRAII() t.join()\n"; t.join(); + } else + { t.detach(); + std::cout << "\nThreadRAII::~ThreadRAII() t.detach()\n"; + } } } diff --git a/src/ThreadRAII.h b/src/ThreadRAII.h index c075bde..cca3211 100644 --- a/src/ThreadRAII.h +++ b/src/ThreadRAII.h @@ -6,6 +6,7 @@ #define OPENMONERO_THREADRAII_H #include +#include namespace xmreg { @@ -36,7 +37,7 @@ class ThreadRAII2 : public ThreadRAII public: ThreadRAII2(std::unique_ptr _functor, - DtorAction _action) + DtorAction _action = DtorAction::join) :ThreadRAII(std::thread(std::ref(*_functor)), _action), f {std::move(_functor)} {} diff --git a/src/TxSearch.h b/src/TxSearch.h index f03c2e1..abf5965 100755 --- a/src/TxSearch.h +++ b/src/TxSearch.h @@ -78,6 +78,9 @@ class TxSearch public: + // make default constructor. useful in testing + TxSearch() = default; + TxSearch(XmrAccount& _acc, std::shared_ptr _current_bc_status); virtual void diff --git a/src/YourMoneroRequests.cpp b/src/YourMoneroRequests.cpp index 755c679..2242a4f 100755 --- a/src/YourMoneroRequests.cpp +++ b/src/YourMoneroRequests.cpp @@ -573,8 +573,8 @@ YourMoneroRequests::get_unspent_outs(const shared_ptr< Session > session, const { uint64_t total_outputs_amount {0}; - uint64_t current_blockchain_height - = current_bc_status->get_current_blockchain_height(); +// uint64_t current_blockchain_height +// = current_bc_status->get_current_blockchain_height(); vector txs; @@ -1697,7 +1697,10 @@ YourMoneroRequests::login_and_start_search_thread( // to do anything except looking for tx and updating mysql // with relative tx information - if (current_bc_status->start_tx_search_thread(acc)) + auto tx_search = std::make_unique(acc, current_bc_status); + + if (current_bc_status->start_tx_search_thread( + acc, std::move(tx_search))) { j_response["status"] = "success"; j_response["new_address"] = false; diff --git a/tests/bcstatus_tests.cpp b/tests/bcstatus_tests.cpp index 68eeab4..57a2771 100644 --- a/tests/bcstatus_tests.cpp +++ b/tests/bcstatus_tests.cpp @@ -115,6 +115,14 @@ public: bool do_not_relay)); }; +class MockTxSearch : public xmreg::TxSearch +{ +public: + MOCK_METHOD0(operator_fcall, void()); + virtual void operator()() override {operator_fcall();} +}; + + class BCSTATUS_TEST : public ::testing::TestWithParam { public: @@ -869,7 +877,31 @@ TEST_P(BCSTATUS_TEST, GetOutputKey) 444 /* any */); EXPECT_EQ(result.pubkey, output_to_return.pubkey); +} + +ACTION(MockSearchWhile) +{ + cout << "\nMocking while search in txsearch class\n" << endl; +} + +TEST_P(BCSTATUS_TEST, StartTxSearchThread) +{ + xmreg::XmrAccount acc; // empty, mock account + + auto tx_search = std::make_unique(); + + EXPECT_CALL(*tx_search, operator_fcall()) // mock operator() + .WillOnce(MockSearchWhile()); + + EXPECT_TRUE(bcs->start_tx_search_thread(acc, std::move(tx_search))); + + // trying launching the same thread for aleary running account + // should also return true as this is fine + EXPECT_TRUE(bcs->start_tx_search_thread(acc, std::move(tx_search))); + // wait a bit, just to give time to tx_search mock thread to finish + // its mocking action + std::this_thread::sleep_for(1s); } INSTANTIATE_TEST_CASE_P(