Browse Source

test: Fix issues with core_online_test

* Move static function variables from core to be class member variables,
  allowing two Core instances to run at once
* Replace deprecated QLatin1Literal with QLatin1String
* qvariant_cast slot argument to ToxPk, rather than trying to convert variant
  to bytes directly which is invalid
* Fix "wait for both to come online" accidentally waiting for Bob twice
* Move all sleeps to QTRY_VERIFY_WITH_TIMEOUT to speed up test
* Update settings* -> settings& based on Core API change
* Update Mock to match API change of IBootstrapListGenerator
* Register metatype of ToxPk, uint32_t, Status::Status
* Correct Alice's spy looking for Bob's pk
reviewable/pr6496/r11
Anthony Bilinski 3 years ago
parent
commit
e426994e5f
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
  1. 11
      src/core/core.cpp
  2. 10
      src/core/core.h
  3. 39
      test/core/core_online_test.cpp
  4. 1
      test/core/core_test.cpp

11
src/core/core.cpp

@ -740,15 +740,6 @@ CoreExt* Core::getExt() @@ -740,15 +740,6 @@ CoreExt* Core::getExt()
return ext.get();
}
/* Using the now commented out statements in checkConnection(), I watched how
* many ticks disconnects-after-initial-connect lasted. Out of roughly 15 trials,
* 5 disconnected; 4 were DCd for less than 20 ticks, while the 5th was ~50 ticks.
* So I set the tolerance here at 25, and initial DCs should be very rare now.
* This should be able to go to 50 or 100 without affecting legitimate disconnects'
* downtime, but lets be conservative for now. Edit: now ~~40~~ 30.
*/
#define CORE_DISCONNECT_TOLERANCE 30
/**
* @brief Processes toxcore events and ensure we stay connected, called by its own timer
*/
@ -758,7 +749,6 @@ void Core::process() @@ -758,7 +749,6 @@ void Core::process()
ASSERT_CORE_THREAD;
static int tolerance = CORE_DISCONNECT_TOLERANCE;
tox_iterate(tox.get(), this);
ext->process();
@ -783,7 +773,6 @@ void Core::process() @@ -783,7 +773,6 @@ void Core::process()
bool Core::checkConnection()
{
ASSERT_CORE_THREAD;
static bool isConnected = false;
auto selfConnection = tox_self_get_connection_status(tox.get());
QString connectionName;
bool toxConnected = false;

10
src/core/core.h

@ -251,6 +251,14 @@ private: @@ -251,6 +251,14 @@ private:
tox_kill(tox);
}
};
/* Using the now commented out statements in checkConnection(), I watched how
* many ticks disconnects-after-initial-connect lasted. Out of roughly 15 trials,
* 5 disconnected; 4 were DCd for less than 20 ticks, while the 5th was ~50 ticks.
* So I set the tolerance here at 25, and initial DCs should be very rare now.
* This should be able to go to 50 or 100 without affecting legitimate disconnects'
* downtime, but lets be conservative for now. Edit: now ~~40~~ 30.
*/
#define CORE_DISCONNECT_TOLERANCE 30
using ToxPtr = std::unique_ptr<Tox, ToxDeleter>;
ToxPtr tox;
@ -265,4 +273,6 @@ private: @@ -265,4 +273,6 @@ private:
std::unique_ptr<QThread> coreThread;
const IBootstrapListGenerator& bootstrapListGenerator;
const ICoreSettings& settings;
bool isConnected = false;
int tolerance = CORE_DISCONNECT_TOLERANCE;
};

39
test/core/core_online_test.cpp

@ -18,21 +18,25 @@ @@ -18,21 +18,25 @@
*/
#include "src/core/core.h"
#include "src/core/toxoptions.h"
#include "src/core/icoresettings.h"
#include "src/net/bootstrapnodeupdater.h"
#include "src/core/toxoptions.h"
#include "src/model/ibootstraplistgenerator.h"
#include "src/net/bootstrapnodeupdater.h"
#include "src/persistence/settings.h"
#include <QtTest/QtTest>
#include <QSignalSpy>
#include <QtGlobal>
#include <QtTest/QtTest>
#include <limits>
#include <QSignalSpy>
#include <QTest>
#include <iostream>
#include <memory>
Q_DECLARE_METATYPE(QList<DhtServer>)
Q_DECLARE_METATYPE(ToxPk)
Q_DECLARE_METATYPE(uint32_t)
Q_DECLARE_METATYPE(Status::Status)
class MockSettings : public QObject, public ICoreSettings
{
@ -41,6 +45,9 @@ public: @@ -41,6 +45,9 @@ public:
MockSettings() {
Q_INIT_RESOURCE(res);
qRegisterMetaType<QList<DhtServer>>("QList<DhtServer>");
qRegisterMetaType<ToxPk>("ToxPk");
qRegisterMetaType<uint32_t>("uint32_t");
qRegisterMetaType<Status::Status>("Status::Status");
}
bool getEnableIPv6() const override { return false; }
@ -79,10 +86,10 @@ private: @@ -79,10 +86,10 @@ private:
class MockNodeListGenerator : public IBootstrapListGenerator
{
QList<DhtServer> getBootstrapnodes();
QList<DhtServer> getBootstrapnodes() const override;
};
QList<DhtServer> MockNodeListGenerator::getBootstrapnodes() {
QList<DhtServer> MockNodeListGenerator::getBootstrapnodes() const {
return BootstrapNodeUpdater::loadDefaultBootstrapNodes();
}
@ -112,9 +119,9 @@ void TestCoreOnline::init() @@ -112,9 +119,9 @@ void TestCoreOnline::init()
MockNodeListGenerator nodesGenerator{};
alice = Core::makeToxCore(QByteArray{}, nullptr, nodesGenerator, nullptr);
alice = Core::makeToxCore(QByteArray{}, *settings, nodesGenerator, nullptr);
QVERIFY2(alice != nullptr, "alice initialization failed");
bob = Core::makeToxCore(QByteArray{}, nullptr, nodesGenerator, nullptr);
bob = Core::makeToxCore(QByteArray{}, *settings, nodesGenerator, nullptr);
QVERIFY2(bob != nullptr, "bob initialization failed");
QSignalSpy spyAliceOnline(alice.get(), &Core::connected);
@ -123,28 +130,24 @@ void TestCoreOnline::init() @@ -123,28 +130,24 @@ void TestCoreOnline::init()
alice->start();
bob->start();
QTest::qSleep(60000);
// Wait for both instances coming online
QTRY_VERIFY(spyAliceOnline.count() >= 1 && spyBobOnline.count() >= 1);
QTRY_VERIFY_WITH_TIMEOUT(spyAliceOnline.count() >= 1 && spyBobOnline.count() >= 1, timeout);
// Make a friend request from alice to bob
const QLatin1Literal friendMsg{"Test Invite Message"};
const QLatin1String friendMsg{"Test Invite Message"};
QSignalSpy spyBobFriendMsg(bob.get(), &Core::friendRequestReceived);
QSignalSpy spyAliceFriendMsg(alice.get(), &Core::requestSent);
alice->requestFriendship(bob->getSelfId(), friendMsg);
QTest::qSleep(30000);
// Wait for both instances coming online
QTRY_VERIFY(spyBobFriendMsg.count() == 1 && spyBobFriendMsg.count() == 1);
QTRY_VERIFY_WITH_TIMEOUT(spyBobFriendMsg.count() == 1 && spyAliceFriendMsg.count() == 1, timeout);
// Check for expected signal content
QVERIFY(spyBobFriendMsg[0][0].toByteArray() == alice->getSelfPublicKey().getByteArray());
QVERIFY(qvariant_cast<ToxPk>(spyBobFriendMsg[0][0]) == alice->getSelfPublicKey());
QVERIFY(spyBobFriendMsg[0][1].toString() == friendMsg);
QVERIFY(spyAliceFriendMsg[0][0].toByteArray() == alice->getSelfPublicKey().getByteArray());
QVERIFY(qvariant_cast<ToxPk>(spyAliceFriendMsg[0][0]) == bob->getSelfPublicKey());
QVERIFY(spyAliceFriendMsg[0][1].toString() == friendMsg);
// Let Bob accept the friend request from Alice
@ -172,7 +175,7 @@ void TestCoreOnline::deinit() @@ -172,7 +175,7 @@ void TestCoreOnline::deinit()
void TestCoreOnline::change_name()
{
// Change the name of Alice to "Alice"
const QLatin1Literal aliceName{"Alice"};
const QLatin1String aliceName{"Alice"};
QSignalSpy aliceSaveRequest(alice.get(), &Core::saveRequest);
QSignalSpy aliceUsernameChanged(alice.get(), &Core::usernameSet);

1
test/core/core_test.cpp

@ -28,7 +28,6 @@ @@ -28,7 +28,6 @@
#include <QtGlobal>
#include <limits>
#include <QSignalSpy>
#include <QStringLiteral>
#include <iostream>
#include <memory>

Loading…
Cancel
Save