Commit 81531a8e authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

Get rid of AkonadiServer singleton

parent 32884f46
......@@ -41,8 +41,8 @@ bool SetupTest::startAkonadiDaemon()
Q_ASSERT(Akonadi::ServerManager::hasInstanceIdentifier());
if (!mAkonadiDaemonProcess) {
mAkonadiDaemonProcess = new KProcess(this);
connect(mAkonadiDaemonProcess, QOverload<int, QProcess::ExitStatus>::of(&KProcess::finished),
mAkonadiDaemonProcess = std::make_unique<KProcess>();
connect(mAkonadiDaemonProcess.get(), QOverload<int, QProcess::ExitStatus>::of(&KProcess::finished),
this, &SetupTest::slotAkonadiDaemonProcessFinished);
}
......@@ -59,15 +59,13 @@ void SetupTest::stopAkonadiDaemon()
if (!mAkonadiDaemonProcess) {
return;
}
disconnect(mAkonadiDaemonProcess, SIGNAL(finished(int)), this, nullptr);
disconnect(mAkonadiDaemonProcess.get(), SIGNAL(finished(int)), this, nullptr);
mAkonadiDaemonProcess->terminate();
const bool finished = mAkonadiDaemonProcess->waitForFinished(5000);
if (!finished) {
qCDebug(AKONADITEST_LOG) << "Problem finishing process.";
}
mAkonadiDaemonProcess->close();
mAkonadiDaemonProcess->deleteLater();
mAkonadiDaemonProcess = nullptr;
mAkonadiDaemonProcess.reset();
}
void SetupTest::setupAgents()
......@@ -360,7 +358,7 @@ void SetupTest::shutdownHarder()
void SetupTest::restartAkonadiServer()
{
qCDebug(AKONADITEST_LOG) << "Restarting Akonadi";
disconnect(mAkonadiDaemonProcess, SIGNAL(finished(int)), this, nullptr);
disconnect(mAkonadiDaemonProcess.get(), SIGNAL(finished(int)), this, nullptr);
Akonadi::ServerManager::self()->stop();
const bool shutdownResult = mAkonadiDaemonProcess->waitForFinished();
if (!shutdownResult) {
......@@ -371,7 +369,7 @@ void SetupTest::restartAkonadiServer()
// it forcefully, if necessary, and know the pid
startAkonadiDaemon();
// from here on, the server exiting is an error again
connect(mAkonadiDaemonProcess, SIGNAL(finished(int)),
connect(mAkonadiDaemonProcess.get(), SIGNAL(finished(int)),
this, SLOT(slotAkonadiDaemonProcessFinished(int)));
}
......@@ -408,7 +406,7 @@ void SetupTest::slotAkonadiDaemonProcessFinished(int exitCode)
qCWarning(AKONADITEST_LOG) << "Akonadi server process was terminated externally!";
Q_EMIT serverExited(exitCode);
}
mAkonadiDaemonProcess = nullptr;
mAkonadiDaemonProcess.reset();
}
void SetupTest::trackAkonadiProcess(bool track)
......
......@@ -24,6 +24,8 @@
#include <QVector>
#include <QPair>
#include <memory>
class KProcess;
class KJob;
......@@ -83,7 +85,7 @@ private:
void checkSetupDone();
private:
KProcess *mAkonadiDaemonProcess = nullptr;
std::unique_ptr<KProcess> mAkonadiDaemonProcess;
bool mShuttingDown;
bool mAgentsCreated;
bool mTrackAkonadiProcess;
......
......@@ -36,15 +36,11 @@ class CollectionCreateHandlerTest : public QObject
{
Q_OBJECT
FakeAkonadiServer mAkonadi;
public:
CollectionCreateHandlerTest()
{
FakeAkonadiServer::instance()->init();
}
~CollectionCreateHandlerTest()
{
FakeAkonadiServer::instance()->quit();
mAkonadi.init();
}
private Q_SLOTS:
......@@ -162,10 +158,10 @@ private Q_SLOTS:
QFETCH(TestScenario::List, scenarios);
QFETCH(Protocol::CollectionChangeNotificationPtr, notification);
FakeAkonadiServer::instance()->setScenarios(scenarios);
FakeAkonadiServer::instance()->runTest();
mAkonadi.setScenarios(scenarios);
mAkonadi.runTest();
auto notificationSpy = FakeAkonadiServer::instance()->notificationSpy();
auto notificationSpy = mAkonadi.notificationSpy();
if (notification->operation() != Protocol::CollectionChangeNotification::InvalidOp) {
QCOMPARE(notificationSpy->count(), 1);
const auto notifications = notificationSpy->takeFirst().first().value<Protocol::ChangeNotificationList>();
......
......@@ -36,12 +36,13 @@ class CollectionFetchHandlerTest : public QObject
{
Q_OBJECT
FakeAkonadiServer mAkonadi;
public:
CollectionFetchHandlerTest()
: QObject()
{
FakeAkonadiServer::instance()->setPopulateDb(false);
FakeAkonadiServer::instance()->init();
mAkonadi.setPopulateDb(false);
mAkonadi.init();
{
MimeType mt(QStringLiteral("mimetype1"));
mt.insert();
......@@ -60,11 +61,6 @@ public:
}
}
~CollectionFetchHandlerTest()
{
FakeAkonadiServer::instance()->quit();
}
Protocol::FetchCollectionsCommandPtr createCommand(const Scope &scope,
Protocol::FetchCollectionsCommand::Depth depth = Akonadi::Protocol::FetchCollectionsCommand::BaseCollection,
Protocol::Ancestor::Depth ancDepth = Protocol::Ancestor::NoAncestor,
......@@ -174,8 +170,8 @@ private Q_SLOTS:
{
QFETCH(TestScenario::List, scenarios);
FakeAkonadiServer::instance()->setScenarios(scenarios);
FakeAkonadiServer::instance()->runTest();
mAkonadi.setScenarios(scenarios);
mAkonadi.runTest();
}
void testListFiltered_data()
......@@ -211,8 +207,8 @@ private Q_SLOTS:
{
QFETCH(TestScenario::List, scenarios);
FakeAkonadiServer::instance()->setScenarios(scenarios);
FakeAkonadiServer::instance()->runTest();
mAkonadi.setScenarios(scenarios);
mAkonadi.runTest();
}
void testListFilterByResource()
......@@ -240,8 +236,8 @@ private Q_SLOTS:
<< TestScenario::create(5, TestScenario::ServerCmd, initializer->listResponse(col1))
<< TestScenario::create(5, TestScenario::ServerCmd, Protocol::FetchCollectionsResponsePtr::create());
FakeAkonadiServer::instance()->setScenarios(scenarios);
FakeAkonadiServer::instance()->runTest();
mAkonadi.setScenarios(scenarios);
mAkonadi.runTest();
col2.remove();
res2.remove();
......@@ -338,8 +334,8 @@ private Q_SLOTS:
{
QFETCH(TestScenario::List, scenarios);
FakeAkonadiServer::instance()->setScenarios(scenarios);
FakeAkonadiServer::instance()->runTest();
mAkonadi.setScenarios(scenarios);
mAkonadi.runTest();
}
void testListAttribute_data()
......@@ -386,8 +382,8 @@ private Q_SLOTS:
{
QFETCH(TestScenario::List, scenarios);
FakeAkonadiServer::instance()->setScenarios(scenarios);
FakeAkonadiServer::instance()->runTest();
mAkonadi.setScenarios(scenarios);
mAkonadi.runTest();
}
void testListAncestorAttributes_data()
......@@ -423,8 +419,8 @@ private Q_SLOTS:
{
QFETCH(TestScenario::List, scenarios);
FakeAkonadiServer::instance()->setScenarios(scenarios);
FakeAkonadiServer::instance()->runTest();
mAkonadi.setScenarios(scenarios);
mAkonadi.runTest();
}
void testIncludeAncestors_data()
......@@ -477,8 +473,8 @@ private Q_SLOTS:
{
QFETCH(TestScenario::List, scenarios);
FakeAkonadiServer::instance()->setScenarios(scenarios);
FakeAkonadiServer::instance()->runTest();
mAkonadi.setScenarios(scenarios);
mAkonadi.runTest();
}
//No point in running the benchmark every time
......@@ -569,8 +565,8 @@ private Q_SLOTS:
// StorageDebugger::instance()->writeToFile(QLatin1String("sqllog.txt"));
QBENCHMARK {
FakeAkonadiServer::instance()->setScenarios(scenarios);
FakeAkonadiServer::instance()->runTest();
mAkonadi.setScenarios(scenarios);
mAkonadi.runTest();
}
}
......
......@@ -36,15 +36,11 @@ class CollectionModifyHandlerTest : public QObject
{
Q_OBJECT
FakeAkonadiServer mAkonadi;
public:
CollectionModifyHandlerTest()
{
FakeAkonadiServer::instance()->init();
}
~CollectionModifyHandlerTest()
{
FakeAkonadiServer::instance()->quit();
mAkonadi.init();
}
private Q_SLOTS:
......@@ -154,10 +150,10 @@ private Q_SLOTS:
QFETCH(Protocol::ChangeNotificationList, expectedNotifications);
QFETCH(QVariant, newValue);
FakeAkonadiServer::instance()->setScenarios(scenarios);
FakeAkonadiServer::instance()->runTest();
mAkonadi.setScenarios(scenarios);
mAkonadi.runTest();
auto notificationSpy = FakeAkonadiServer::instance()->notificationSpy();
auto notificationSpy = mAkonadi.notificationSpy();
if (expectedNotifications.isEmpty()) {
QVERIFY(notificationSpy->isEmpty() || notificationSpy->takeFirst().first().value<Protocol::ChangeNotificationList>().isEmpty());
return;
......
......@@ -36,10 +36,11 @@ class CollectionSchedulerTest : public QObject
{
Q_OBJECT
FakeAkonadiServer mAkonadi;
private Q_SLOTS:
void initTestCase()
{
FakeAkonadiServer::instance()->init();
mAkonadi.init();
}
void shouldInitializeSyncIntervals()
......
......@@ -59,22 +59,17 @@ class CollectionStatisticsTest : public QObject
{
Q_OBJECT
DbInitializer *dbInitializer = nullptr;
std::unique_ptr<DbInitializer> dbInitializer;
FakeAkonadiServer mAkonadi;
public:
CollectionStatisticsTest()
{
qRegisterMetaType<Collection>();
FakeAkonadiServer::instance()->setPopulateDb(false);
FakeAkonadiServer::instance()->init();
mAkonadi.setPopulateDb(false);
mAkonadi.init();
dbInitializer = new DbInitializer;
}
~CollectionStatisticsTest()
{
delete dbInitializer;
FakeAkonadiServer::instance()->quit();
dbInitializer = std::make_unique<DbInitializer>();
}
private Q_SLOTS:
......
......@@ -90,17 +90,6 @@ TestScenario TestScenario::create(qint64 tag, TestScenario::Action action,
}
FakeAkonadiServer *FakeAkonadiServer::instance()
{
if (!AkonadiServer::s_instance) {
AkonadiServer::s_instance = new FakeAkonadiServer;
}
Q_ASSERT(qobject_cast<FakeAkonadiServer *>(AkonadiServer::s_instance));
return qobject_cast<FakeAkonadiServer *>(AkonadiServer::s_instance);
}
FakeAkonadiServer::FakeAkonadiServer()
: AkonadiServer()
{
......@@ -112,10 +101,13 @@ FakeAkonadiServer::FakeAkonadiServer()
mClient = std::make_unique<FakeClient>();
FakeDataStore::registerFactory();
DataStore::setFactory(std::make_unique<FakeDataStoreFactory>(*this));
}
FakeAkonadiServer::~FakeAkonadiServer() = default;
FakeAkonadiServer::~FakeAkonadiServer()
{
quit();
}
QString FakeAkonadiServer::basePath()
{
......@@ -277,7 +269,7 @@ void FakeAkonadiServer::setScenarios(const TestScenario::List &scenarios)
void FakeAkonadiServer::newCmdConnection(quintptr socketDescriptor)
{
mConnection = std::make_unique<FakeConnection>(socketDescriptor);
mConnection = std::make_unique<FakeConnection>(socketDescriptor, *this);
// Connection is its own thread, so we have to make sure we get collector
// from DataStore of the Connection's thread, not ours
......
......@@ -83,16 +83,12 @@ class FakeAkonadiServer : public AkonadiServer
Q_OBJECT
public:
static FakeAkonadiServer *instance();
explicit FakeAkonadiServer();
~FakeAkonadiServer() override;
/* Reimpl */
bool init() override;
/* Reimpl */
bool quit() override;
static QString basePath();
static QString socketFile();
static QString instanceName();
......@@ -114,7 +110,7 @@ protected:
void newCmdConnection(quintptr socketDescriptor) override;
private:
explicit FakeAkonadiServer();
bool quit() override;
void initFake();
FakeDataStore *mDataStore = nullptr;
......@@ -129,8 +125,6 @@ private:
bool mPopulateDb = true;
bool mDisableItemRetrievalManager = false;
static FakeAkonadiServer *sInstance;
};
AKONADI_EXCEPTION_MAKE_INSTANCE(FakeAkonadiServerException);
......
......@@ -25,13 +25,13 @@
using namespace Akonadi::Server;
FakeConnection::FakeConnection(quintptr socketDescriptor, QObject *parent)
: Connection(socketDescriptor, parent)
FakeConnection::FakeConnection(quintptr socketDescriptor, FakeAkonadiServer &akonadi)
: Connection(socketDescriptor, akonadi)
{
}
FakeConnection::FakeConnection(QObject *parent)
: Connection(parent)
FakeConnection::FakeConnection(AkonadiServer &akonadi)
: Connection(akonadi)
{
}
......
......@@ -26,14 +26,15 @@ namespace Akonadi {
namespace Server {
class NotificationCollector;
class FakeAkonadiServer;
class FakeConnection : public Connection
{
Q_OBJECT
public:
explicit FakeConnection(quintptr socketDescriptor, QObject *parent = nullptr);
explicit FakeConnection(QObject *parent = nullptr);
explicit FakeConnection(quintptr socketDescriptor, FakeAkonadiServer &akonadi);
explicit FakeConnection(AkonadiServer &akonadi);
~FakeConnection() override;
public Q_SLOTS:
......
......@@ -18,6 +18,7 @@
*/
#include "fakedatastore.h"
#include "fakeakonadiserver.h"
#include "dbpopulator.h"
#include "storage/dbconfig.h"
#include "inspectablenotificationcollector.h"
......@@ -38,38 +39,23 @@ Q_DECLARE_METATYPE(QVector<Tag>)
Q_DECLARE_METATYPE(MimeType)
Q_DECLARE_METATYPE(QList<QByteArray>)
namespace Akonadi {
namespace Server {
FakeDataStoreFactory::FakeDataStoreFactory(FakeAkonadiServer &akonadi)
: m_akonadi(akonadi)
{}
class FakeDataStoreFactory : public DataStoreFactory
DataStore * FakeDataStoreFactory::createStore()
{
public:
FakeDataStoreFactory() = default;
~FakeDataStoreFactory() override = default;
DataStore * createStore() override
{
return new FakeDataStore();
}
};
}
return new FakeDataStore(m_akonadi);
}
Akonadi::Server::FakeDataStore::FakeDataStore()
: DataStore()
FakeDataStore::FakeDataStore(FakeAkonadiServer &akonadi)
: DataStore(akonadi)
, mPopulateDb(true)
{
mNotificationCollector = std::make_unique<InspectableNotificationCollector>(this);
mNotificationCollector = std::make_unique<InspectableNotificationCollector>(m_akonadi, this);
}
FakeDataStore::~FakeDataStore()
{
}
void FakeDataStore::registerFactory()
{
sFactory.reset(new FakeDataStoreFactory);
}
FakeDataStore::~FakeDataStore() = default;
bool FakeDataStore::init()
{
......@@ -320,3 +306,4 @@ void FakeDataStore::setPopulateDb(bool populate)
{
mPopulateDb = populate;
}
......@@ -25,7 +25,17 @@
namespace Akonadi {
namespace Server {
class FakeDataStoreFactory;
class FakeAkonadiServer;
class FakeDataStoreFactory : public DataStoreFactory
{
public:
FakeDataStoreFactory(FakeAkonadiServer &akonadi);
DataStore *createStore() override;
private:
FakeAkonadiServer &m_akonadi;
};
class FakeDataStore : public DataStore
{
Q_OBJECT
......@@ -33,8 +43,6 @@ class FakeDataStore : public DataStore
public:
~FakeDataStore() override;
static void registerFactory();
bool init() override;
QMap<QString, QVariantList> changes() const
......@@ -123,7 +131,7 @@ public:
void setPopulateDb(bool populate);
protected:
FakeDataStore();
FakeDataStore(FakeAkonadiServer &akonadi);
QMap<QString, QVariantList> mChanges;
......
......@@ -40,19 +40,15 @@ class FetchHandlerTest : public QObject
{
Q_OBJECT
FakeAkonadiServer mAkonadi;
public:
FetchHandlerTest()
: QObject()
{
qRegisterMetaType<Akonadi::Server::Tag::List>();
FakeAkonadiServer::instance()->setPopulateDb(false);
FakeAkonadiServer::instance()->init();
}
~FetchHandlerTest()
{
FakeAkonadiServer::instance()->quit();
mAkonadi.setPopulateDb(false);
mAkonadi.init();
}
Protocol::FetchItemsCommandPtr createCommand(const Scope &scope, const Protocol::ScopeContext &ctx = Protocol::ScopeContext())
......@@ -88,7 +84,7 @@ private Q_SLOTS:
{
TestScenario::List scenarios;
scenarios << FakeAkonadiServer::loginScenario()
scenarios << mAkonadi.loginScenario()
<< TestScenario::create(5, TestScenario::ClientCmd, createCommand(item1.id()))
<< TestScenario::create(5, TestScenario::ServerCmd, createResponse(item1))
<< TestScenario::create(5, TestScenario::ServerCmd, Protocol::FetchItemsResponsePtr::create());
......@@ -97,7 +93,7 @@ private Q_SLOTS:
}
{
TestScenario::List scenarios;
scenarios << FakeAkonadiServer::loginScenario()
scenarios << mAkonadi.loginScenario()
<< TestScenario::create(5, TestScenario::ClientCmd, createCommand(ImapSet::all(), Protocol::ScopeContext(Protocol::ScopeContext::Collection, col.id())))
<< TestScenario::create(5, TestScenario::ServerCmd, createResponse(item2))
<< TestScenario::create(5, TestScenario::ServerCmd, createResponse(item1))
......@@ -110,8 +106,8 @@ private Q_SLOTS:
{
QFETCH(TestScenario::List, scenarios);
FakeAkonadiServer::instance()->setScenarios(scenarios);
FakeAkonadiServer::instance()->runTest();
mAkonadi.setScenarios(scenarios);
mAkonadi.runTest();
}
void testFetchByTag_data()
......@@ -137,7 +133,7 @@ private Q_SLOTS:
{
TestScenario::List scenarios;
scenarios << FakeAkonadiServer::loginScenario()
scenarios << mAkonadi.loginScenario()
<< TestScenario::create(5, TestScenario::ClientCmd, createCommand(ImapSet::all(), Protocol::ScopeContext(Protocol::ScopeContext::Tag, tag.id())))
<< TestScenario::create(5, TestScenario::ServerCmd, createResponse(item1))
<< TestScenario::create(5, TestScenario::ServerCmd, Protocol::FetchItemsResponsePtr::create());
......@@ -146,8 +142,8 @@ private Q_SLOTS:
}
{
TestScenario::List scenarios;
scenarios << FakeAkonadiServer::loginScenario()
<< FakeAkonadiServer::selectResourceScenario(QStringLiteral("testresource"))
scenarios << mAkonadi.loginScenario()
<< mAkonadi.selectResourceScenario(QStringLiteral("testresource"))
<< TestScenario::create(5, TestScenario::ClientCmd, createCommand(ImapSet::all(), Protocol::ScopeContext(Protocol::ScopeContext::Tag, tag.id())))
<< TestScenario::create(5, TestScenario::ServerCmd, createResponse(item1))
<< TestScenario::create(5, TestScenario::ServerCmd, Protocol::FetchItemsResponsePtr::create());
......@@ -156,8 +152,8 @@ private Q_SLOTS:
}
{
TestScenario::List scenarios;
scenarios << FakeAkonadiServer::loginScenario()
<< FakeAkonadiServer::selectResourceScenario(QStringLiteral("testresource"))
scenarios << mAkonadi.loginScenario()
<< mAkonadi.<