StartTxSearchThread test added

pull/93/merge
moneroexamples 6 years ago
parent bc396e1e23
commit 6e616f367f

@ -660,7 +660,8 @@ CurrentBlockchainStatus::get_output_key(
} }
bool bool
CurrentBlockchainStatus::start_tx_search_thread(XmrAccount acc) CurrentBlockchainStatus::start_tx_search_thread(
XmrAccount acc, std::unique_ptr<TxSearch> tx_search)
{ {
std::lock_guard<std::mutex> lck (searching_threads_map_mtx); std::lock_guard<std::mutex> lck (searching_threads_map_mtx);
@ -673,19 +674,12 @@ CurrentBlockchainStatus::start_tx_search_thread(XmrAccount acc)
try try
{ {
// make a tx_search object for the given xmr account // launch SearchTx thread 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
// create SearchTx object. It does not start searching yet searching_threads.insert(
{acc.address, ThreadRAII2<TxSearch>(std::move(tx_search))});
ThreadRAII2<TxSearch> t(std::make_unique<TxSearch>(acc, shared_from_this()), OMINFO << "Search thread created for address: " << acc.address;
ThreadRAII::DtorAction::join);
searching_threads.insert({acc.address, std::move(t)});
OMINFO << "Search thread created for address " << acc.address;
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {

@ -166,7 +166,8 @@ public:
// definitions of these function are at the end of this file // definitions of these function are at the end of this file
// due to forward declaraions of TxSearch // due to forward declaraions of TxSearch
virtual bool virtual bool
start_tx_search_thread(XmrAccount acc); start_tx_search_thread(XmrAccount acc,
std::unique_ptr<TxSearch> tx_search);
virtual bool virtual bool
ping_search_thread(const string& address); ping_search_thread(const string& address);

@ -8,15 +8,6 @@
namespace xmreg namespace xmreg
{ {
void
MicroBlockchainLMDB::sync()
{
//cout << "MicroBlockchainLMDB::sync()\n\n";
// we open in readonly, so dont sync anything
}
/** /**
* The constructor is interesting, as * The constructor is interesting, as
* m_mempool and m_blockchain_storage depend * m_mempool and m_blockchain_storage depend
@ -50,15 +41,12 @@ MicroCore::init(const string& _blockchain_path, network_type nt)
nettype = 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
{ {
// try opening lmdb database files // 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) catch (const std::exception& e)
{ {

@ -17,17 +17,6 @@ using namespace cryptonote;
using namespace crypto; using namespace crypto;
using namespace std; using namespace std;
class MicroBlockchainLMDB : public BlockchainLMDB
{
public:
using BlockchainLMDB::BlockchainLMDB;
virtual void sync() override;
};
/** /**
* Micro version of cryptonode::core class * Micro version of cryptonode::core class
* Micro version of constructor, * Micro version of constructor,
@ -40,8 +29,8 @@ class MicroCore {
string blockchain_path; string blockchain_path;
Blockchain core_storage;
tx_memory_pool m_mempool; tx_memory_pool m_mempool;
Blockchain core_storage;
hw::device* m_device; hw::device* m_device;

@ -9,6 +9,7 @@
#include "tools.h" #include "tools.h"
#include <map> #include <map>
#include <utility>
namespace xmreg namespace xmreg
{ {

@ -16,9 +16,15 @@ ThreadRAII::~ThreadRAII()
if (t.joinable()) if (t.joinable())
{ {
if (action == DtorAction::join) if (action == DtorAction::join)
{
std::cout << "\nThreadRAII::~ThreadRAII() t.join()\n";
t.join(); t.join();
}
else else
{
t.detach(); t.detach();
std::cout << "\nThreadRAII::~ThreadRAII() t.detach()\n";
}
} }
} }

@ -6,6 +6,7 @@
#define OPENMONERO_THREADRAII_H #define OPENMONERO_THREADRAII_H
#include <thread> #include <thread>
#include <iostream>
namespace xmreg namespace xmreg
{ {
@ -36,7 +37,7 @@ class ThreadRAII2 : public ThreadRAII
public: public:
ThreadRAII2(std::unique_ptr<T> _functor, ThreadRAII2(std::unique_ptr<T> _functor,
DtorAction _action) DtorAction _action = DtorAction::join)
:ThreadRAII(std::thread(std::ref(*_functor)), _action), :ThreadRAII(std::thread(std::ref(*_functor)), _action),
f {std::move(_functor)} f {std::move(_functor)}
{} {}

@ -78,6 +78,9 @@ class TxSearch
public: public:
// make default constructor. useful in testing
TxSearch() = default;
TxSearch(XmrAccount& _acc, std::shared_ptr<CurrentBlockchainStatus> _current_bc_status); TxSearch(XmrAccount& _acc, std::shared_ptr<CurrentBlockchainStatus> _current_bc_status);
virtual void virtual void

@ -573,8 +573,8 @@ YourMoneroRequests::get_unspent_outs(const shared_ptr< Session > session, const
{ {
uint64_t total_outputs_amount {0}; uint64_t total_outputs_amount {0};
uint64_t current_blockchain_height // uint64_t current_blockchain_height
= current_bc_status->get_current_blockchain_height(); // = current_bc_status->get_current_blockchain_height();
vector<XmrTransaction> txs; vector<XmrTransaction> txs;
@ -1697,7 +1697,10 @@ YourMoneroRequests::login_and_start_search_thread(
// to do anything except looking for tx and updating mysql // to do anything except looking for tx and updating mysql
// with relative tx information // with relative tx information
if (current_bc_status->start_tx_search_thread(acc)) auto tx_search = std::make_unique<TxSearch>(acc, current_bc_status);
if (current_bc_status->start_tx_search_thread(
acc, std::move(tx_search)))
{ {
j_response["status"] = "success"; j_response["status"] = "success";
j_response["new_address"] = false; j_response["new_address"] = false;

@ -115,6 +115,14 @@ public:
bool do_not_relay)); 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<network_type> class BCSTATUS_TEST : public ::testing::TestWithParam<network_type>
{ {
public: public:
@ -869,7 +877,31 @@ TEST_P(BCSTATUS_TEST, GetOutputKey)
444 /* any */); 444 /* any */);
EXPECT_EQ(result.pubkey, output_to_return.pubkey); 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<MockTxSearch>();
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( INSTANTIATE_TEST_CASE_P(

Loading…
Cancel
Save