Commit 5fea91ac authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

tests: Add a fast_discoverer_cancel test

Which discovers/remove/add/ban/unban/reload folders in a loop
parent e82baeca
......@@ -382,7 +382,7 @@ samples_LDADD = \
$(SQLITE_LIBS) \
$(NULL)
check_PROGRAMS += test_discoverer sqlite_load test_fast_teardown
check_PROGRAMS += test_discoverer sqlite_load test_fast_teardown test_fast_discover
test_discoverer_SOURCES = test/discoverer/main.cpp
test_discoverer_CXXFLAGS = $(MEDIALIB_CPPFLAGS)
......@@ -398,6 +398,10 @@ test_fast_teardown_CPPFLAGS = -I$(top_srcdir)/test
test_fast_teardown_CXXFLAGS = $(MEDIALIB_CPPFLAGS)
test_fast_teardown_LDADD = libmedialibrary.la $(SQLITE_LIBS)
test_fast_discover_SOURCES = test/fast_discover_cancel/main.cpp
test_fast_discover_CPPFLAGS = -I$(top_srcdir)/test
test_fast_discover_CXXFLAGS = $(MEDIALIB_CPPFLAGS)
test_fast_discover_LDADD = libmedialibrary.la $(SQLITE_LIBS)
endif
pkgconfigdir = $(libdir)/pkgconfig
......
......@@ -105,6 +105,7 @@ asan-ubsan:
- ./unittest
- ./samples
- ./test_fast_teardown $CI_PROJECT_DIR/samples/samples
- ./test_fast_discover $CI_PROJECT_DIR/samples/samples
tsan:
image: registry.videolan.org/vlc-debian-unstable:20191204094457
......@@ -129,3 +130,5 @@ tsan:
- ./unittest
- ./samples
- ./test_fast_teardown $CI_PROJECT_DIR/samples/samples
- ./test_fast_discover $CI_PROJECT_DIR/samples/samples
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2020 Hugo Beauzée-Luyssen, Videolabs, VideoLAN
*
* Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "MediaLibrary.h"
#include "mocks/NoopCallback.h"
#include "utils/Filename.h"
#include "medialibrary/filesystem/IDirectory.h"
#include "compat/Mutex.h"
#include "compat/ConditionVariable.h"
#include <iostream>
#include <condition_variable>
#include <set>
#include <cassert>
#include <unistd.h>
class FastDiscoverCancelCb : public mock::NoopCallback
{
public:
FastDiscoverCancelCb()
: m_doneQueuing( false )
, m_discovererRunning( true )
{
}
virtual void onDiscoveryStarted( const std::string& ep ) override
{
std::lock_guard<compat::Mutex> lock( m_mutex );
assert( m_discoveryStarted.find( ep ) == cend( m_discoveryStarted ) );
m_discoveryStarted.insert( ep );
}
virtual void onDiscoveryCompleted( const std::string& ep, bool ) override
{
std::lock_guard<compat::Mutex> lock( m_mutex );
auto it = m_discoveryStarted.find( ep );
assert( it != cend( m_discoveryStarted ) );
m_discoveryStarted.erase( it );
if ( m_discoveryStarted.empty() == true )
m_cond.notify_all();
}
virtual void onReloadStarted( const std::string& ep ) override
{
std::lock_guard<compat::Mutex> lock( m_mutex );
assert( m_reloadStarted.find( ep ) == cend( m_reloadStarted ) );
m_reloadStarted.insert( ep );
}
virtual void onReloadCompleted( const std::string& ep, bool ) override
{
std::lock_guard<compat::Mutex> lock( m_mutex );
auto it = m_reloadStarted.find( ep );
assert( it != cend( m_reloadStarted ) );
m_reloadStarted.erase( it );
}
virtual void onBackgroundTasksIdleChanged( bool idle ) override
{
std::lock_guard<compat::Mutex> lock( m_mutex );
m_discovererRunning = !idle;
m_cond.notify_all();
}
void check()
{
std::lock_guard<compat::Mutex> lock( m_mutex );
assert( m_discoveryStarted.empty() == true );
assert( m_reloadStarted.empty() == true );
}
void markDoneQueuing()
{
std::unique_lock<compat::Mutex> lock( m_mutex );
m_doneQueuing = true;
}
void waitForDiscoveryCompleted()
{
std::unique_lock<compat::Mutex> lock( m_mutex );
auto res = m_cond.wait_for( lock, std::chrono::minutes{ 10 }, [this](){
return m_doneQueuing == true && m_discovererRunning == false;
});
assert( res == true );
}
private:
std::set<std::string> m_discoveryStarted;
std::set<std::string> m_reloadStarted;
compat::Mutex m_mutex;
compat::ConditionVariable m_cond;
bool m_doneQueuing;
bool m_discovererRunning;
};
int main( int argc, char** argv )
{
if ( argc < 2 )
{
std::cerr << "usage: " << argv[0] << " <entrypoint path>" << std::endl;
return 1;
}
auto entrypoint = utils::file::toMrl( argv[1] );
auto testCb = std::make_unique<FastDiscoverCancelCb>();
auto ml = std::make_unique<medialibrary::MediaLibrary>();
// ml->setVerbosity( LogLevel::Debug );
unlink( "/tmp/test.db" );
ml->initialize( "/tmp/test.db", "/tmp/ml_folder", testCb.get() );
ml->start();
ml->discover( entrypoint );
for ( auto i = 0; i < 1000; ++i )
{
auto fsFactory = ml->fsFactoryForMrl( entrypoint );
auto fsDir = fsFactory->createDirectory( entrypoint );
auto j = 0u;
for ( const auto& d : fsDir->dirs() )
{
switch ( j % 2 )
{
case 0:
ml->banFolder( d->mrl() );
ml->unbanFolder( d->mrl() );
break;
case 1:
ml->removeEntryPoint( d->mrl() );
ml->discover( d->mrl() );
break;
}
++j;
}
}
ml->reload( entrypoint );
testCb->markDoneQueuing();
testCb->waitForDiscoveryCompleted();
return 0;
}
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