Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit 3b97a6d

Browse files
committed
added setup tests and improved integration test
1 parent 8506072 commit 3b97a6d

File tree

15 files changed

+340
-33
lines changed

15 files changed

+340
-33
lines changed

src/datasync/defaults.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ bool Defaults::isValid() const
4242
return d;
4343
}
4444

45+
void Defaults::drop()
46+
{
47+
d.clear();
48+
}
49+
4550
Logger *Defaults::createLogger(const QByteArray &subCategory, QObject *parent) const
4651
{
4752
return new Logger(subCategory, d->setupName, parent);
@@ -189,6 +194,11 @@ QSqlDatabase *DatabaseRef::operator->() const
189194
return &(d->db());
190195
}
191196

197+
void DatabaseRef::drop()
198+
{
199+
d.reset();
200+
}
201+
192202
// ------------- PRIVATE IMPLEMENTATION Defaults -------------
193203

194204
#undef QTDATASYNC_LOG

src/datasync/defaults.h

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class Q_DATASYNC_EXPORT DatabaseRef
4949
//! Arrow operator to access the database
5050
QSqlDatabase *operator->() const;
5151

52+
void drop();
53+
5254
private:
5355
QScopedPointer<DatabaseRefPrivate> d;
5456
};
@@ -94,6 +96,7 @@ class Q_DATASYNC_EXPORT Defaults
9496
~Defaults();
9597

9698
bool isValid() const;
99+
void drop();
97100

98101
//! Create a new logger instance
99102
Logger *createLogger(const QByteArray &subCategory, QObject *parent = nullptr) const;

src/datasync/exchangeengine.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ void EngineThread::waitAndTerminate(unsigned long timeout)
389389
{
390390
if(isFinished())
391391
return;
392+
qCDebug(qdssetup) << "Waiting for workerthread of setup" << _name << "to exit normally...";
392393
if(!wait(timeout)) {
393394
qCWarning(qdssetup) << "Workerthread of setup" << _name << "did not finish before the timout. Terminating...";
394395
terminate();

src/datasync/qpmx.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"package": "de.skycoder42.qpluginfactory",
55
"provider": "qpm",
6-
"version": "1.3.0"
6+
"version": "1.3.1"
77
},
88
{
99
"package": "de.skycoder42.cryptoqq",

src/datasync/remoteconfig.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ RemoteConfig &RemoteConfig::operator=(const RemoteConfig &other) = default;
1717

1818
RemoteConfig &RemoteConfig::operator=(RemoteConfig &&other) noexcept = default;
1919

20+
bool RemoteConfig::operator==(const RemoteConfig &other) const
21+
{
22+
return d == other.d || (
23+
d->url == other.d->url &&
24+
d->accessKey == other.d->accessKey &&
25+
d->headers == other.d->headers &&
26+
d->keepaliveTimeout == other.d->keepaliveTimeout);
27+
}
28+
29+
bool RemoteConfig::operator!=(const RemoteConfig &other) const
30+
{
31+
return d != other.d && (
32+
d->url != other.d->url ||
33+
d->accessKey != other.d->accessKey ||
34+
d->headers != other.d->headers ||
35+
d->keepaliveTimeout != other.d->keepaliveTimeout);
36+
}
37+
38+
2039
QUrl RemoteConfig::url() const
2140
{
2241
return d->url;

src/datasync/remoteconfig.h

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class Q_DATASYNC_EXPORT RemoteConfig
4848
//! Move-Assignment operator
4949
RemoteConfig &operator=(RemoteConfig &&other) noexcept;
5050

51+
bool operator==(const RemoteConfig &other) const;
52+
bool operator!=(const RemoteConfig &other) const;
53+
5154
//! @readAcFn{RemoteConfig::url}
5255
QUrl url() const;
5356
//! @readAcFn{RemoteConfig::accessKey}

src/datasync/setup.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void Setup::setCleanupTimeout(unsigned long timeout)
2929
SetupPrivate::timeout = timeout;
3030
}
3131

32-
void Setup::removeSetup(const QString &name, bool waitForFinished) //TODO add awaitable version
32+
void Setup::removeSetup(const QString &name, bool waitForFinished)
3333
{
3434
QMutexLocker _(&SetupPrivate::setupMutex);
3535
if(!SetupPrivate::engines.contains(name))

src/datasync/setup.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ namespace literals {
3535

3636
//! literal for kilobytes
3737
Q_DECL_CONSTEXPR inline int operator "" _kb(unsigned long long x) {
38-
return KB(static_cast<int>(x));
38+
return KB(static_cast<intmax_t>(x));
3939
}
4040

4141
//! literal for megabytes
4242
Q_DECL_CONSTEXPR inline int operator "" _mb(unsigned long long x) {
43-
return MB(static_cast<int>(x));
43+
return MB(static_cast<intmax_t>(x));
4444
}
4545

4646
//! literal for gigabytes
4747
Q_DECL_CONSTEXPR inline int operator "" _gb(unsigned long long x) {
48-
return GB(static_cast<int>(x));
48+
return GB(static_cast<intmax_t>(x));
4949
}
5050

5151
}

tests/auto/datasync/IntegrationTest/IntegrationTest.pro

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
include(../tests.pri)
22

3+
QT += service
4+
35
TARGET = tst_integration
46

57
SOURCES += \

tests/auto/datasync/IntegrationTest/tst_integration.cpp

+106-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <QString>
22
#include <QtTest>
33
#include <QCoreApplication>
4+
#include <QtService/ServiceControl>
45
#include <testlib.h>
56
#include <testdata.h>
67
using namespace QtDataSync;
@@ -20,11 +21,13 @@ private Q_SLOTS:
2021
void testRemoveAndResetAccount();
2122
void testAddAccountTrusted();
2223

24+
void testAddAccountOnCreate();
25+
2326
Q_SIGNALS:
2427
void unlock();
2528

2629
private:
27-
QProcess *server;
30+
QtService::ServiceControl *server;
2831

2932
AccountManager *acc1;
3033
SyncManager *sync1;
@@ -45,21 +48,24 @@ void IntegrationTest::initTestCase()
4548
QVERIFY(QFile::exists(QString::fromUtf8(confPath)));
4649
qputenv("QDSAPP_CONFIG_FILE", confPath);
4750

51+
const QString launchPath { QStringLiteral(BUILD_BIN_DIR "qdsapp") };
4852
#ifdef Q_OS_UNIX
49-
QString binPath { QStringLiteral(BUILD_BIN_DIR "qdsappd") };
53+
const QString binPath { QStringLiteral(BUILD_BIN_DIR "qdsappd") };
5054
#elif Q_OS_WIN
51-
QString binPath { QStringLiteral(BUILD_BIN_DIR "qdsappsvc") };
55+
const QString binPath { QStringLiteral(BUILD_BIN_DIR "qdsappsvc") };
5256
#else
53-
QString binPath { QStringLiteral(BUILD_BIN_DIR "qdsapp") };
57+
const auto binPath = launchPath;
5458
#endif
5559
QVERIFY(QFile::exists(binPath));
60+
if(!QFile::exists(launchPath))
61+
QVERIFY(QFile::link(binPath, launchPath));
62+
QVERIFY(QFile::exists(launchPath));
5663

57-
server = new QProcess(this);
58-
server->setProgram(binPath);
59-
server->setProcessChannelMode(QProcess::ForwardedErrorChannel);
60-
server->start();
61-
QVERIFY(server->waitForStarted(5000));
62-
QVERIFY(!server->waitForFinished(5000));
64+
server = QtService::ServiceControl::create(QStringLiteral("debug"), launchPath, this);
65+
QVERIFY(server);
66+
QVERIFY(server->serviceExists());
67+
server->setBlocking(true);
68+
QVERIFY(server->start());
6369

6470
try {
6571
TestLib::init();
@@ -117,16 +123,9 @@ void IntegrationTest::cleanupTestCase()
117123
acc2 = nullptr;
118124
Setup::removeSetup(QStringLiteral("setup2"), true);
119125

120-
//send a signal to stop
121-
#ifdef Q_OS_UNIX
122-
server->terminate(); //same as kill(SIGTERM)
123-
#elif Q_OS_WIN
124-
GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, server->processId());
125-
#endif
126-
QVERIFY(server->waitForFinished(5000));
127-
QCOMPARE(server->exitStatus(), QProcess::NormalExit);
128-
QCOMPARE(server->exitCode(), 0);
129-
server->close();
126+
QVERIFY(server->stop());
127+
QTRY_COMPARE(server->status(), QtService::ServiceControl::ServiceStopped);
128+
server->deleteLater();
130129
}
131130

132131
void IntegrationTest::testPrepareData()
@@ -241,12 +240,12 @@ void IntegrationTest::testAddAccount()
241240
emit unlock();
242241
QCOMPARE(s, SyncManager::Synchronized);
243242
}, true);
244-
unlockSpy.wait();
243+
QVERIFY(unlockSpy.wait());
245244
sync2->runOnSynchronized([this](SyncManager::SyncState s) {
246245
emit unlock();
247246
QCOMPARE(s, SyncManager::Synchronized);
248247
});
249-
unlockSpy.wait();
248+
QVERIFY(unlockSpy.wait());
250249
} while(oldCnt1 != store1->count() || oldCnt2 != store2->count());
251250

252251
QCOMPARE(store1->count(), 20);
@@ -598,12 +597,12 @@ void IntegrationTest::testAddAccountTrusted()
598597
emit unlock();
599598
QCOMPARE(s, SyncManager::Synchronized);
600599
}, true);
601-
unlockSpy.wait();
600+
QVERIFY(unlockSpy.wait());
602601
sync2->runOnSynchronized([this](SyncManager::SyncState s) {
603602
emit unlock();
604603
QCOMPARE(s, SyncManager::Synchronized);
605604
});
606-
unlockSpy.wait();
605+
QVERIFY(unlockSpy.wait());
607606

608607
QCOMPARE(store1->count(), 0);
609608
QCOMPARE(store2->count(), 0);
@@ -615,6 +614,89 @@ void IntegrationTest::testAddAccountTrusted()
615614
}
616615
}
617616

617+
void IntegrationTest::testAddAccountOnCreate()
618+
{
619+
try {
620+
QSignalSpy error1Spy(acc1, &AccountManager::lastErrorChanged);
621+
QSignalSpy grantSpy(acc1, &AccountManager::accountAccessGranted);
622+
QSignalSpy unlockSpy(this, &IntegrationTest::unlock);
623+
624+
//export from acc1
625+
auto password = QStringLiteral("password");
626+
QJsonObject exportData;
627+
acc1->exportAccountTrusted(false, password, [&](QJsonObject exp) {
628+
QVERIFY(AccountManager::isTrustedImport(exp));
629+
exportData = exp;
630+
emit unlock();
631+
}, [](QString e) {
632+
QFAIL(qUtf8Printable(e));
633+
});
634+
QVERIFY(unlockSpy.wait());
635+
QVERIFY(!exportData.isEmpty());
636+
637+
// create acc3 with account from setup
638+
AccountManager *acc3;
639+
SyncManager *sync3;
640+
DataTypeStore<TestData> *store3;
641+
QString setupName3 = QStringLiteral("setup3");
642+
{
643+
Setup setup3;
644+
TestLib::setup(setup3);
645+
setup3.setLocalDir(setup3.localDir() + QLatin1Char('/') + setupName3)
646+
.setRemoteConfiguration(QUrl(QStringLiteral("ws://localhost:14242")))
647+
.setAccountTrusted(exportData, password, false, false);
648+
setup3.create(setupName3);
649+
650+
acc3 = new AccountManager(setupName3, this);
651+
QVERIFY(acc3->replica()->waitForSource(5000));
652+
sync3 = new SyncManager(setupName3, this);
653+
QVERIFY(sync3->replica()->waitForSource(5000));
654+
store3 = new DataTypeStore<TestData>(setupName3, this);
655+
}
656+
657+
QSignalSpy error3Spy(acc3, &AccountManager::lastErrorChanged);
658+
QSignalSpy acceptSpy(acc3, &AccountManager::importAccepted);
659+
660+
//wait for accept
661+
if(acceptSpy.isEmpty())
662+
QVERIFY(acceptSpy.wait());
663+
QCOMPARE(acceptSpy.size(), 1);
664+
665+
//wait for grant
666+
if(grantSpy.isEmpty())
667+
QVERIFY(grantSpy.wait());
668+
QCOMPARE(grantSpy.size(), 1);
669+
670+
//wait for sync
671+
sync1->runOnSynchronized([this](SyncManager::SyncState s) {
672+
emit unlock();
673+
QCOMPARE(s, SyncManager::Synchronized);
674+
}, true);
675+
QVERIFY(unlockSpy.wait());
676+
sync3->runOnSynchronized([this](SyncManager::SyncState s) {
677+
emit unlock();
678+
QCOMPARE(s, SyncManager::Synchronized);
679+
});
680+
QVERIFY(unlockSpy.wait());
681+
682+
QCOMPARE(store1->count(), 0);
683+
QCOMPARE(store3->count(), 0);
684+
685+
QVERIFY(error1Spy.isEmpty());
686+
QVERIFY(error3Spy.isEmpty());
687+
688+
delete store3;
689+
store3 = nullptr;
690+
delete sync3;
691+
sync3 = nullptr;
692+
delete acc3;
693+
acc3 = nullptr;
694+
Setup::removeSetup(setupName3, true);
695+
} catch(std::exception &e) {
696+
QFAIL(e.what());
697+
}
698+
}
699+
618700
QTEST_MAIN(IntegrationTest)
619701

620702
#include "tst_integration.moc"

tests/auto/datasync/TestAppServer/tst_appserver.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ void TestAppServer::cleanupTestCase()
163163
clean();
164164
clean(partner);
165165
if(server->status() == QtService::ServiceControl::ServiceRunning) {
166-
server->stop();
166+
QVERIFY(server->stop());
167167
QTRY_COMPARE(server->status(), QtService::ServiceControl::ServiceStopped);
168168
}
169169
server->deleteLater();
170170
}
171171

172172
void TestAppServer::testStart()
173173
{
174-
server->start();
174+
QVERIFY(server->start());
175175
QTRY_COMPARE(server->status(), QtService::ServiceControl::ServiceRunning);
176176
QEXPECT_FAIL("", "Server should not stop easily", Continue);
177177
QTRY_COMPARE(server->status(), QtService::ServiceControl::ServiceStopped);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include(../tests.pri)
2+
3+
TARGET = tst_setup
4+
5+
SOURCES += \
6+
tst_setup.cpp
7+

0 commit comments

Comments
 (0)