From a1a44af6ae5f46c52cd4ae6524e1b04afa6af5fe Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Wed, 15 Aug 2018 14:35:01 +0800 Subject: [PATCH] test throwing excetpion get_exception_ptr() --- src/CurrentBlockchainStatus.cpp | 28 +++++----------------------- src/CurrentBlockchainStatus.h | 3 --- tests/bcstatus_tests.cpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/CurrentBlockchainStatus.cpp b/src/CurrentBlockchainStatus.cpp index 4faa62a..83ea062 100755 --- a/src/CurrentBlockchainStatus.cpp +++ b/src/CurrentBlockchainStatus.cpp @@ -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 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 CurrentBlockchainStatus::construct_output_rct_field( const uint64_t global_amount_index, diff --git a/src/CurrentBlockchainStatus.h b/src/CurrentBlockchainStatus.h index fc1ac6c..a2bbcff 100755 --- a/src/CurrentBlockchainStatus.h +++ b/src/CurrentBlockchainStatus.h @@ -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. diff --git a/tests/bcstatus_tests.cpp b/tests/bcstatus_tests.cpp index 71f23a1..1140b31 100644 --- a/tests/bcstatus_tests.cpp +++ b/tests/bcstatus_tests.cpp @@ -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(); @@ -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(); + + // 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)