Commit 6c429c9a authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

unittest: Simplify async call handling

parent 48a877ad
......@@ -35,7 +35,8 @@ class WaitForDiscoveryComplete : public mock::NoopCallback
{
public:
WaitForDiscoveryComplete()
: m_waitingReload( false )
: m_discoveryDone( false )
, m_reloadDone( false )
, m_banFolderDone( false )
, m_entryPointRemoved( false )
{
......@@ -44,15 +45,13 @@ public:
virtual void onDiscoveryCompleted( const std::string& entryPoint ) override
{
assert( entryPoint.empty() == false );
m_done = true;
m_discoveryDone = true;
m_cond.notify_all();
}
virtual void onReloadCompleted( const std::string& entryPoint ) override
virtual void onReloadCompleted( const std::string& ) override
{
if ( m_waitingReload == false )
return;
m_done = true;
m_reloadDone = true;
m_cond.notify_all();
}
......@@ -68,12 +67,24 @@ public:
m_cond.notify_all();
}
bool wait()
bool waitDiscovery()
{
std::unique_lock<compat::Mutex> lock( m_mutex );
return m_cond.wait_for( lock, std::chrono::seconds( 5 ), [this]() {
return m_done.load();
auto res = m_cond.wait_for( lock, std::chrono::seconds( 5 ), [this]() {
return m_discoveryDone.load();
} );
m_discoveryDone = false;
return res;
}
bool waitReload()
{
std::unique_lock<compat::Mutex> lock( m_mutex );
auto res = m_cond.wait_for( lock, std::chrono::seconds( 5 ), [this]() {
return m_reloadDone.load();
} );
m_reloadDone = false;
return res;
}
bool waitBanFolder()
......@@ -96,26 +107,9 @@ public:
return res;
}
// We don't synchronously trigger the discovery, so we can't rely on started/completed being called
// Instead, we manually tell this mock how much discovery we expect. This sucks, but the alternative
// would probably be to have an extra IMediaLibraryCb member to signal that a discovery has been queue.
// however, in practice, this is a callback that says "yep, you've called IMediaLibrary::discover()"
// which is probably lame.
void prepareForWait()
{
m_done = false;
m_waitingReload = false;
}
void prepareForReload()
{
m_done = false;
m_waitingReload = true;
}
private:
std::atomic_bool m_done;
std::atomic_bool m_waitingReload;
std::atomic_bool m_discoveryDone;
std::atomic_bool m_reloadDone;
std::atomic_bool m_banFolderDone;
std::atomic_bool m_entryPointRemoved;
compat::ConditionVariable m_cond;
......
......@@ -68,6 +68,8 @@ protected:
virtual void Reload()
{
Tests::Reload( fsMock, cbMock.get() );
auto res = cbMock->waitReload();
ASSERT_TRUE( res );
}
};
......@@ -112,9 +114,8 @@ TEST_F( DeviceEntity, SetPresent )
TEST_F( DeviceFs, RemoveDisk )
{
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
auto files = ml->files();
......@@ -125,10 +126,7 @@ TEST_F( DeviceFs, RemoveDisk )
auto device = fsMock->removeDevice( RemovableDeviceUuid );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
ASSERT_EQ( 3u, files.size() );
......@@ -139,9 +137,8 @@ TEST_F( DeviceFs, RemoveDisk )
TEST_F( DeviceFs, UnmountDisk )
{
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
auto files = ml->files();
......@@ -152,10 +149,7 @@ TEST_F( DeviceFs, UnmountDisk )
fsMock->unmountDevice( RemovableDeviceUuid );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
ASSERT_EQ( 3u, files.size() );
......@@ -165,10 +159,7 @@ TEST_F( DeviceFs, UnmountDisk )
fsMock->remountDevice( RemovableDeviceUuid );
cbMock->prepareForReload();
Reload();
reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
ASSERT_EQ( 5u, files.size() );
......@@ -179,9 +170,8 @@ TEST_F( DeviceFs, UnmountDisk )
TEST_F( DeviceFs, ReplugDisk )
{
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
auto files = ml->files();
......@@ -192,10 +182,7 @@ TEST_F( DeviceFs, ReplugDisk )
auto device = fsMock->removeDevice( RemovableDeviceUuid );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
ASSERT_EQ( 3u, files.size() );
......@@ -204,10 +191,7 @@ TEST_F( DeviceFs, ReplugDisk )
ASSERT_EQ( nullptr, media );
fsMock->addDevice( device );
cbMock->prepareForReload();
Reload();
reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
ASSERT_EQ( 5u, files.size() );
......@@ -218,9 +202,8 @@ TEST_F( DeviceFs, ReplugDisk )
TEST_F( DeviceFs, ReplugDiskWithExtraFiles )
{
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
auto files = ml->files();
......@@ -228,10 +211,7 @@ TEST_F( DeviceFs, ReplugDiskWithExtraFiles )
auto device = fsMock->removeDevice( RemovableDeviceUuid );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
ASSERT_EQ( 3u, files.size() );
......@@ -239,10 +219,7 @@ TEST_F( DeviceFs, ReplugDiskWithExtraFiles )
fsMock->addDevice( device );
fsMock->addFile( RemovableDeviceMountpoint + "newfile.mkv" );
cbMock->prepareForReload();
Reload();
reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
ASSERT_EQ( 6u, files.size() );
......@@ -250,9 +227,8 @@ TEST_F( DeviceFs, ReplugDiskWithExtraFiles )
TEST_F( DeviceFs, RemoveAlbum )
{
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
// Create an album on a non-removable device
......@@ -281,10 +257,7 @@ TEST_F( DeviceFs, RemoveAlbum )
auto device = fsMock->removeDevice( RemovableDeviceUuid );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
albums = ml->albums( SortingCriteria::Default, false );
ASSERT_EQ( 1u, albums.size() );
......@@ -294,9 +267,8 @@ TEST_F( DeviceFs, RemoveAlbum )
TEST_F( DeviceFs, PartialAlbumRemoval )
{
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
{
......@@ -319,10 +291,7 @@ TEST_F( DeviceFs, PartialAlbumRemoval )
ASSERT_EQ( 2u, artist->media( SortingCriteria::Default, false ).size() );
auto device = fsMock->removeDevice( RemovableDeviceUuid );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
albums = ml->albums( SortingCriteria::Default, false );
ASSERT_EQ( 1u, albums.size() );
......@@ -334,9 +303,8 @@ TEST_F( DeviceFs, PartialAlbumRemoval )
TEST_F( DeviceFs, ChangeDevice )
{
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
// Fetch a removable media's ID
......@@ -354,10 +322,7 @@ TEST_F( DeviceFs, ChangeDevice )
fsMock->addDevice( RemovableDeviceMountpoint, "{another-removable-device}" );
fsMock->addFile( RemovableDeviceMountpoint + "removablefile.mp3" );
cbMock->prepareForReload();
Reload();
auto reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
// Check that new files with the same name have different IDs
// but the same "full path"
......@@ -371,10 +336,7 @@ TEST_F( DeviceFs, ChangeDevice )
auto device = fsMock->removeDevice( "{another-removable-device}" );
fsMock->addDevice( oldRemovableDevice );
cbMock->prepareForReload();
Reload();
reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
f = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
ASSERT_NE( nullptr, f );
......
......@@ -60,6 +60,8 @@ public:
virtual void Reload()
{
Tests::Reload( fsMock, cbMock.get() );
auto res = cbMock->waitReload();
ASSERT_TRUE( res );
}
};
......@@ -69,9 +71,8 @@ class Folders : public FoldersNoDiscover
virtual void SetUp() override
{
FoldersNoDiscover::SetUp();
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
}
};
......@@ -99,10 +100,7 @@ TEST_F( Folders, Delete )
files = ml->files();
ASSERT_EQ( files.size(), 0u );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
// Recheck folder deletion from DB:
f = ml->folder( folderPath );
......@@ -111,10 +109,7 @@ TEST_F( Folders, Delete )
TEST_F( Folders, Load )
{
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
auto files = ml->files();
ASSERT_EQ( files.size(), 3u );
......@@ -122,9 +117,8 @@ TEST_F( Folders, Load )
TEST_F( FoldersNoDiscover, InvalidPath )
{
cbMock->prepareForWait();
ml->discover( "/invalid/path" );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
auto files = ml->files();
......@@ -138,10 +132,7 @@ TEST_F( Folders, List )
auto files = f->files();
ASSERT_EQ( files.size(), 2u );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
f = ml->folder( f->mrl() );
files = f->files();
......@@ -185,10 +176,7 @@ TEST_F( Folders, NewFolderWithFile )
fsMock->addFile( newFolder + "newfile.avi" );
// This will trigger a reload
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
ASSERT_EQ( 4u, ml->files().size() );
auto f = ml->media( newFolder + "newfile.avi" );
......@@ -206,10 +194,7 @@ TEST_F( Folders, NewFileInSubFolder )
ml.reset();
fsMock->addFile( mock::FileSystemFactory::SubFolder + "newfile.avi" );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
ASSERT_EQ( 4u, ml->files().size() );
auto media = ml->media( mock::FileSystemFactory::SubFolder + "newfile.avi" );
......@@ -225,10 +210,7 @@ TEST_F( Folders, RemoveFileFromDirectory )
ml.reset();
fsMock->removeFile( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
ASSERT_EQ( 2u, ml->files().size() );
auto media = ml->media( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
......@@ -244,10 +226,7 @@ TEST_F( Folders, RemoveDirectory )
ml.reset();
fsMock->removeFolder( mock::FileSystemFactory::SubFolder );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
ASSERT_EQ( 2u, ml->files().size() );
auto media = ml->media( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
......@@ -266,10 +245,7 @@ TEST_F( Folders, UpdateFile )
ml.reset();
fsMock->file( filePath )->markAsModified();
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
f = ml->media( filePath );
ASSERT_NE( nullptr, f );
......@@ -280,11 +256,10 @@ TEST_F( Folders, UpdateFile )
TEST_F( FoldersNoDiscover, Blacklist )
{
cbMock->prepareForWait();
ml->banFolder( mock::FileSystemFactory::SubFolder );
cbMock->waitBanFolder();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
auto f = ml->folder( mock::FileSystemFactory::SubFolder );
......@@ -293,11 +268,10 @@ TEST_F( FoldersNoDiscover, Blacklist )
TEST_F( FoldersNoDiscover, DiscoverBlacklisted )
{
cbMock->prepareForWait();
ml->banFolder( mock::FileSystemFactory::Root );
cbMock->waitBanFolder();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
auto f = ml->folder( mock::FileSystemFactory::Root );
......@@ -319,11 +293,10 @@ TEST_F( Folders, BlacklistAfterDiscovery )
TEST_F( FoldersNoDiscover, RemoveFromBlacklist )
{
cbMock->prepareForWait();
ml->banFolder( mock::FileSystemFactory::SubFolder );
cbMock->waitBanFolder();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
auto files = ml->files();
ASSERT_EQ( 2u, files.size() );
......@@ -331,11 +304,8 @@ TEST_F( FoldersNoDiscover, RemoveFromBlacklist )
auto f = ml->folder( mock::FileSystemFactory::SubFolder );
ASSERT_EQ( nullptr, f );
cbMock->prepareForReload();
ml->unbanFolder( mock::FileSystemFactory::SubFolder );
cbMock->waitBanFolder();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
ASSERT_EQ( 3u, files.size() );
f = ml->folder( mock::FileSystemFactory::SubFolder );
......@@ -344,7 +314,6 @@ TEST_F( FoldersNoDiscover, RemoveFromBlacklist )
TEST_F( FoldersNoDiscover, BlacklistTwice )
{
cbMock->prepareForWait();
ml->banFolder( mock::FileSystemFactory::SubFolder );
cbMock->waitBanFolder();
ml->banFolder( mock::FileSystemFactory::SubFolder );
......@@ -353,7 +322,6 @@ TEST_F( FoldersNoDiscover, BlacklistTwice )
TEST_F( FoldersNoDiscover, BlacklistNonExistant )
{
cbMock->prepareForWait();
ml->banFolder( "foo/bar/otters" );
cbMock->waitBanFolder();
ml->banFolder( "/foo/bar/otters" );
......@@ -370,9 +338,8 @@ TEST_F( FoldersNoDiscover, NoMediaBeforeDiscovery )
fsMock->addFile( newFolder + "newfile.avi" );
fsMock->addFile( newFolder + ".nomedia" );
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
auto files = ml->files();
......@@ -386,10 +353,7 @@ TEST_F( Folders, InsertNoMedia )
ASSERT_EQ( 3u, files.size() );
fsMock->addFile( mock::FileSystemFactory::SubFolder + ".nomedia" );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
files = ml->files();
ASSERT_EQ( 2u, files.size() );
......@@ -399,10 +363,7 @@ TEST_F( Folders, InsertNoMediaInRoot )
{
fsMock->addFile( mock::FileSystemFactory::Root + ".nomedia" );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
auto files = ml->files();
ASSERT_EQ( 0u, files.size() );
......@@ -414,18 +375,16 @@ TEST_F( Folders, ReloadSubDir )
ASSERT_EQ( 3u, files.size() );
fsMock->addFile( mock::FileSystemFactory::Root + "newmedia.mkv" );
cbMock->prepareForReload();
ml->reload( mock::FileSystemFactory::SubFolder );
bool reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
auto res = cbMock->waitReload();
ASSERT_TRUE( res );
files = ml->files();
ASSERT_EQ( 3u, files.size() );
cbMock->prepareForReload();
ml->reload();
reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
res = cbMock->waitReload();
ASSERT_TRUE( res );
files = ml->files();
ASSERT_EQ( 4u, files.size() );
......@@ -476,10 +435,7 @@ TEST_F( Folders, RemoveEntryPoint )
auto eps = ml->entryPoints();
ASSERT_EQ( 1u, eps.size() );
cbMock->prepareForReload();
ml->reload();
auto reloaded = cbMock->wait();
ASSERT_TRUE( reloaded );
// Ensure it wasn't re-discovered, ie. that it was properly blacklisted
auto media2 = ml->files();
......
......@@ -568,9 +568,8 @@ const std::string FetchMedia::RemovableDeviceMountpoint = "file:///a/mnt/fake-de
TEST_F( FetchMedia, FetchNonRemovable )
{
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
auto m = ml->media( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
......@@ -579,9 +578,8 @@ TEST_F( FetchMedia, FetchNonRemovable )
TEST_F( FetchMedia, FetchRemovable )
{
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
auto m = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
......@@ -590,16 +588,14 @@ TEST_F( FetchMedia, FetchRemovable )
TEST_F( FetchMedia, FetchRemovableUnplugged )
{
cbMock->prepareForWait();
ml->discover( mock::FileSystemFactory::Root );
bool discovered = cbMock->wait();
bool discovered = cbMock->waitDiscovery();
ASSERT_TRUE( discovered );
fsMock->unmountDevice( RemovableDeviceUuid );
cbMock->prepareForReload();
Reload();
bool reloaded = cbMock->wait();
bool reloaded = cbMock->waitReload();
ASSERT_TRUE( reloaded );
auto m = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
......
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