mu-index: add blocking start()

Useful for unit tests
This commit is contained in:
Dirk-Jan C. Binnema
2023-09-16 11:02:36 +03:00
parent c78dafd723
commit a2046dc2b1
3 changed files with 17 additions and 10 deletions

View File

@ -111,9 +111,11 @@ struct Indexer::Private {
bool add_message(const std::string& path); bool add_message(const std::string& path);
bool cleanup(); bool cleanup();
bool start(const Indexer::Config& conf); bool start(const Indexer::Config& conf, bool block);
bool stop(); bool stop();
bool is_running() const { return state_ != IndexState::Idle; }
Indexer::Config conf_; Indexer::Config conf_;
Store& store_; Store& store_;
Scanner scanner_; Scanner scanner_;
@ -369,7 +371,7 @@ Indexer::Private::scan_worker()
} }
bool bool
Indexer::Private::start(const Indexer::Config& conf) Indexer::Private::start(const Indexer::Config& conf, bool block)
{ {
stop(); stop();
@ -398,7 +400,13 @@ Indexer::Private::start(const Indexer::Config& conf)
/* kick the disk-scanner thread */ /* kick the disk-scanner thread */
scanner_worker_ = std::thread([this] { scan_worker(); }); 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; return true;
} }
@ -428,7 +436,7 @@ Indexer::Indexer(Store& store)
Indexer::~Indexer() = default; Indexer::~Indexer() = default;
bool bool
Indexer::start(const Indexer::Config& conf) Indexer::start(const Indexer::Config& conf, bool block)
{ {
const auto mdir{priv_->store_.root_maildir()}; const auto mdir{priv_->store_.root_maildir()};
if (G_UNLIKELY(access(mdir.c_str(), R_OK) != 0)) { if (G_UNLIKELY(access(mdir.c_str(), R_OK) != 0)) {
@ -440,7 +448,7 @@ Indexer::start(const Indexer::Config& conf)
if (is_running()) if (is_running())
return true; return true;
return priv_->start(conf); return priv_->start(conf, block);
} }
bool bool
@ -458,7 +466,7 @@ Indexer::stop()
bool bool
Indexer::is_running() const Indexer::is_running() const
{ {
return priv_->state_ != IndexState::Idle; return priv_->is_running();
} }
const Indexer::Progress& const Indexer::Progress&

View File

@ -61,7 +61,7 @@ public:
/** /**
* Start indexing. If already underway, do nothing. This returns * Start indexing. If already underway, do nothing. This returns
* immediately after starting, with the work being done in the * immediately after starting, with the work being done in the
* background. * background, unless blocking = true
* *
* @param conf a configuration object * @param conf a configuration object
* *
@ -69,7 +69,7 @@ public:
* underway; false otherwise. * underway; false otherwise.
* *
*/ */
bool start(const Config& conf); bool start(const Config& conf, bool block=false);
/** /**
* Stop indexing. If not indexing, do nothing. * Stop indexing. If not indexing, do nothing.

View File

@ -288,8 +288,7 @@ Scanner::Private::stop()
Scanner::Scanner(const std::string& root_dir, Scanner::Handler handler, Mode flavor) Scanner::Scanner(const std::string& root_dir, Scanner::Handler handler, Mode flavor)
: priv_{std::make_unique<Private>(root_dir, handler, flavor)} : priv_{std::make_unique<Private>(root_dir, handler, flavor)}
{ {}
}
Scanner::~Scanner() = default; Scanner::~Scanner() = default;