test throwing excetpion get_exception_ptr()

pull/85/head
moneroexamples 6 years ago
parent 034512585b
commit a1a44af6ae

@ -48,8 +48,6 @@ CurrentBlockchainStatus::monitor_blockchain()
OMINFO << "Current blockchain height: " << current_height
<< ", no of mempool txs: " << mempool_txs.size();
check_search_threads_for_exceptions();
clean_search_thread_map();
std::this_thread::sleep_for(
@ -956,28 +954,12 @@ CurrentBlockchainStatus::clean_search_thread_map()
if (search_thread_exist(st.first)
&& st.second.get_functor().still_searching() == false)
{
OMINFO << "Ereasing a search thread";
searching_threads.erase(st.first);
}
}
}
bool
CurrentBlockchainStatus::check_search_threads_for_exceptions()
{
bool found_any_exception {false};
std::lock_guard<std::mutex> lck (searching_threads_map_mtx);
for (auto& st: searching_threads)
{
if (search_thread_exist(st.first)
&& st.second.get_functor().still_searching() == false)
{
// before erasing a search thread, check if there was any
// exception thrown by it
try
{
auto eptr = st.second.get_functor().get_exception_ptr();
found_any_exception = true;
if (eptr != nullptr)
std::rethrow_exception(eptr);
}
@ -986,13 +968,13 @@ CurrentBlockchainStatus::check_search_threads_for_exceptions()
OMERROR << "Error in search thread: " << e.what()
<< ". It will be cleared.";
}
OMINFO << "Ereasing a search thread";
searching_threads.erase(st.first);
}
}
return found_any_exception;
}
tuple<string, string, string>
CurrentBlockchainStatus::construct_output_rct_field(
const uint64_t global_amount_index,

@ -219,9 +219,6 @@ public:
virtual void
clean_search_thread_map();
virtual bool
check_search_threads_for_exceptions();
/*
* The frontend requires rct field to work
* the filed consisitct of rct_pk, mask, and amount.

@ -162,6 +162,8 @@ public:
j_transactions->push_back(nlohmann::json::parse(json_str));
}
MOCK_METHOD0(get_exception_ptr, std::exception_ptr());
};
@ -996,6 +998,9 @@ TEST_P(BCSTATUS_TEST, StartTxSearchThread)
EXPECT_CALL(*tx_search, operator_fcall()) // mock operator()
.WillOnce(MockSearchWhile());
EXPECT_CALL(*tx_search, get_exception_ptr())
.WillOnce(Return(nullptr));
EXPECT_TRUE(bcs->start_tx_search_thread(acc, std::move(tx_search)));
auto tx_search2 = std::make_unique<MockTxSearch>();
@ -1004,9 +1009,37 @@ TEST_P(BCSTATUS_TEST, StartTxSearchThread)
// should also return true as this is fine
EXPECT_TRUE(bcs->start_tx_search_thread(acc, std::move(tx_search2)));
auto tx_search3 = std::make_unique<MockTxSearch>();
// here we test what happens if TxSearchThread throws some
// has thrown an execption and std::exception_ptr is not null
xmreg::XmrAccount acc2; // empty, mock account
acc2.address = "mock address";
auto expt_ptr = std::make_exception_ptr(
std::runtime_error("some exception"));
EXPECT_CALL(*tx_search3, get_exception_ptr())
.WillOnce(Return(expt_ptr));
EXPECT_CALL(*tx_search3, operator_fcall()) // mock operator()
.WillOnce(MockSearchWhile());
EXPECT_TRUE(bcs->start_tx_search_thread(acc2, std::move(tx_search3)));
// wait a bit, just to give time to tx_search mock thread to finish
// its mocking action
std::this_thread::sleep_for(1s);
// cleaning up the search threads should detect that one
// thread has some exception
bcs->clean_search_thread_map();
EXPECT_FALSE(bcs->search_thread_exist(acc2.address));
}
ACTION(MockSearchWhile2)

Loading…
Cancel
Save