From a2046dc2b1bb2500a76500713466715ec669baa0 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 16 Sep 2023 11:02:36 +0300 Subject: [PATCH] mu-index: add blocking start() Useful for unit tests --- lib/mu-indexer.cc | 20 ++++++++++++++------ lib/mu-indexer.hh | 4 ++-- lib/mu-scanner.cc | 3 +-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/mu-indexer.cc b/lib/mu-indexer.cc index 52cfdedf..36f3e155 100644 --- a/lib/mu-indexer.cc +++ b/lib/mu-indexer.cc @@ -111,9 +111,11 @@ struct Indexer::Private { bool add_message(const std::string& path); bool cleanup(); - bool start(const Indexer::Config& conf); + bool start(const Indexer::Config& conf, bool block); bool stop(); + bool is_running() const { return state_ != IndexState::Idle; } + Indexer::Config conf_; Store& store_; Scanner scanner_; @@ -369,7 +371,7 @@ Indexer::Private::scan_worker() } bool -Indexer::Private::start(const Indexer::Config& conf) +Indexer::Private::start(const Indexer::Config& conf, bool block) { stop(); @@ -398,7 +400,13 @@ Indexer::Private::start(const Indexer::Config& conf) /* kick the disk-scanner thread */ scanner_worker_ = std::thread([this] { scan_worker(); }); - mu_debug("started indexer"); + mu_debug("started indexer in {}-mode", block ? "blocking" : "non-blocking"); + if (block) { + while(is_running()) { + using namespace std::chrono_literals; + std::this_thread::sleep_for(100ms); + } + } return true; } @@ -428,7 +436,7 @@ Indexer::Indexer(Store& store) Indexer::~Indexer() = default; bool -Indexer::start(const Indexer::Config& conf) +Indexer::start(const Indexer::Config& conf, bool block) { const auto mdir{priv_->store_.root_maildir()}; if (G_UNLIKELY(access(mdir.c_str(), R_OK) != 0)) { @@ -440,7 +448,7 @@ Indexer::start(const Indexer::Config& conf) if (is_running()) return true; - return priv_->start(conf); + return priv_->start(conf, block); } bool @@ -458,7 +466,7 @@ Indexer::stop() bool Indexer::is_running() const { - return priv_->state_ != IndexState::Idle; + return priv_->is_running(); } const Indexer::Progress& diff --git a/lib/mu-indexer.hh b/lib/mu-indexer.hh index d65f3bcc..3ea1fb63 100644 --- a/lib/mu-indexer.hh +++ b/lib/mu-indexer.hh @@ -61,7 +61,7 @@ public: /** * Start indexing. If already underway, do nothing. This returns * immediately after starting, with the work being done in the - * background. + * background, unless blocking = true * * @param conf a configuration object * @@ -69,7 +69,7 @@ public: * underway; false otherwise. * */ - bool start(const Config& conf); + bool start(const Config& conf, bool block=false); /** * Stop indexing. If not indexing, do nothing. diff --git a/lib/mu-scanner.cc b/lib/mu-scanner.cc index 3f006462..ddda1598 100644 --- a/lib/mu-scanner.cc +++ b/lib/mu-scanner.cc @@ -288,8 +288,7 @@ Scanner::Private::stop() Scanner::Scanner(const std::string& root_dir, Scanner::Handler handler, Mode flavor) : priv_{std::make_unique(root_dir, handler, flavor)} -{ -} +{} Scanner::~Scanner() = default;