Commit 26cee193 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

IMediaLibrary: Improve onReload* callbacks

Don't invoke the callback with an empty entry point and a global success
state.
Instead, let the discoverer invoke the callback with the actual entry
point being reloaded, and its associated success state
The deleteFolder test was removed because the RemoveDirectory test
already covers folder deletion tests, but also because the folder was
directly removed from database, which makes the reload operation not
reload anything, so no callback was ever sent, and waitForReload was
timing out.
parent 79e02b7e
......@@ -163,7 +163,7 @@ public:
* @param entryPoint Will be an empty string is the reload was a global reload, or the specific
* entry point that has been reloaded
*/
virtual void onReloadCompleted( const std::string& entryPoint ) = 0;
virtual void onReloadCompleted( const std::string& entryPoint, bool success ) = 0;
/**
* @brief onEntryPointRemoved will be invoked when an entrypoint removal request gets processsed
* by the appropriate worker thread.
......
......@@ -165,15 +165,21 @@ void DiscovererWorker::run()
void DiscovererWorker::runReload( const std::string& entryPoint )
{
m_ml->getCb()->onReloadStarted( entryPoint );
for ( auto& d : m_discoverers )
{
try
{
if ( entryPoint.empty() == true )
{
// Let the discoverer invoke the callbacks for all its known folders
d->reload();
}
else
d->reload( entryPoint );
{
m_ml->getCb()->onReloadStarted( entryPoint );
auto res = d->reload( entryPoint );
m_ml->getCb()->onReloadCompleted( entryPoint, res );
}
}
catch(std::exception& ex)
{
......@@ -182,7 +188,6 @@ void DiscovererWorker::runReload( const std::string& entryPoint )
if ( m_run == false )
break;
}
m_ml->getCb()->onReloadCompleted( entryPoint );
}
void DiscovererWorker::runRemove( const std::string& ep )
......
......@@ -108,7 +108,7 @@ bool FsDiscoverer::discover( const std::string& entryPoint )
return true;
}
void FsDiscoverer::reloadFolder( std::shared_ptr<Folder> f )
bool FsDiscoverer::reloadFolder( std::shared_ptr<Folder> f )
{
assert( f->isPresent() );
auto mrl = f->mrl();
......@@ -119,7 +119,7 @@ void FsDiscoverer::reloadFolder( std::shared_ptr<Folder> f )
directory = m_fsFactory->createDirectory( mrl );
assert( directory->device() != nullptr );
if ( directory->device() == nullptr )
return;
return false;
}
catch ( const std::system_error& ex )
{
......@@ -134,7 +134,7 @@ void FsDiscoverer::reloadFolder( std::shared_ptr<Folder> f )
LOG_INFO( "Failed to find folder matching entrypoint ", mrl, ". "
"Removing that folder" );
m_ml->deleteFolder( *f );
return;
return false;
}
}
try
......@@ -144,8 +144,9 @@ void FsDiscoverer::reloadFolder( std::shared_ptr<Folder> f )
catch ( DeviceRemovedException& )
{
LOG_INFO( "Reloading of ", mrl, " was stopped after the device was removed" );
return false;
}
return true;
}
bool FsDiscoverer::reload()
......@@ -156,7 +157,10 @@ bool FsDiscoverer::reload()
{
// fetchRootFolders only returns present folders
assert( f->isPresent() == true );
reloadFolder( f );
auto mrl = f->mrl();
m_cb->onReloadStarted( mrl );
auto res = reloadFolder( f );
m_cb->onReloadCompleted( mrl, res );
}
return true;
}
......
......@@ -57,7 +57,7 @@ private:
std::shared_ptr<Folder> parentFolder ) const;
bool addFolder( std::shared_ptr<fs::IDirectory> folder,
Folder* parentFolder ) const;
void reloadFolder( std::shared_ptr<Folder> folder );
bool reloadFolder( std::shared_ptr<Folder> folder );
private:
MediaLibrary* m_ml;
......
......@@ -36,6 +36,7 @@ class WaitForDiscoveryComplete : public mock::NoopCallback
public:
WaitForDiscoveryComplete()
: m_discoveryDone( false )
, m_initialDiscoveryDone( false )
, m_reloadDone( false )
, m_banFolderDone( false )
, m_unbanFolderDone( false )
......@@ -50,7 +51,7 @@ public:
m_cond.notify_all();
}
virtual void onReloadCompleted( const std::string& ) override
virtual void onReloadCompleted( const std::string&, bool ) override
{
m_reloadDone = true;
m_cond.notify_all();
......@@ -81,11 +82,16 @@ public:
return m_discoveryDone.load();
} );
m_discoveryDone = false;
m_initialDiscoveryDone = true;
return res;
}
bool waitReload()
{
// A reload() request when no discovery has run will never reload anything
// and therefor won't invoke any callback
if ( m_initialDiscoveryDone == false )
return true;
std::unique_lock<compat::Mutex> lock( m_mutex );
auto res = m_cond.wait_for( lock, std::chrono::seconds( 5 ), [this]() {
return m_reloadDone.load();
......@@ -126,6 +132,7 @@ public:
private:
std::atomic_bool m_discoveryDone;
std::atomic_bool m_initialDiscoveryDone;
std::atomic_bool m_reloadDone;
std::atomic_bool m_banFolderDone;
std::atomic_bool m_unbanFolderDone;
......
......@@ -50,7 +50,7 @@ class NoopCallback : public IMediaLibraryCb
virtual void onPlaylistsModified( std::vector<PlaylistPtr> ) override {}
virtual void onPlaylistsDeleted( std::vector<int64_t> ) override {}
virtual void onReloadStarted( const std::string& ) override {}
virtual void onReloadCompleted( const std::string& ) override {}
virtual void onReloadCompleted( const std::string&, bool ) override {}
virtual void onEntryPointRemoved( const std::string&, bool ) override {}
virtual void onEntryPointBanned( const std::string&, bool ) override {}
virtual void onEntryPointUnbanned( const std::string&, bool ) override {}
......
......@@ -84,29 +84,6 @@ TEST_F( Folders, Add )
ASSERT_EQ( files.size(), 3u );
}
TEST_F( Folders, Delete )
{
auto f = ml->folder( mock::FileSystemFactory::Root );
auto folderPath = f->mrl();
auto files = ml->files();
ASSERT_EQ( files.size(), 3u );
ml->deleteFolder( *f );
f = ml->folder( folderPath );
ASSERT_EQ( nullptr, f );
files = ml->files();
ASSERT_EQ( files.size(), 0u );
Reload();
// Recheck folder deletion from DB:
f = ml->folder( folderPath );
ASSERT_EQ( nullptr, f );
}
TEST_F( Folders, Load )
{
Reload();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment