Heart/HeartTests/AbstractSpace/bigdatatest.cpp

125 lines
2.7 KiB
C++
Raw Normal View History

2021-10-02 22:12:39 +03:00
/*
2022-03-03 19:01:19 +03:00
* Copyright (C) 2018-2022 QuasarApp.
2021-10-02 22:12:39 +03:00
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "bigdatatest.h"
#include <abstractnode.h>
#define LOCAL_TEST_PORT TEST_PORT + 4
class BigPackage: public QH::PKG::AbstractData {
2021-10-06 12:29:06 +03:00
QH_PACKAGE(BigPackage, "BigPackage")
2021-10-02 22:12:39 +03:00
public:
BigPackage(){
data = {};
};
QByteArray data;
// StreamBase interface
protected:
2021-10-06 12:29:06 +03:00
QDataStream &fromStream(QDataStream &stream) override{
2021-10-02 22:12:39 +03:00
stream >> data;
return stream;
};
2021-10-06 12:29:06 +03:00
QDataStream &toStream(QDataStream &stream) const override{
2021-10-02 22:12:39 +03:00
stream << data;
return stream;
};
};
class TestingClientBigData: public QH::AbstractNode {
// AbstractNode interface
public:
TestingClientBigData(int i) {
Q_UNUSED(i);
registerPackageType<BigPackage>();
data = new BigPackage();
}
bool sendRequest(const QByteArray& data) {
BigPackage pkg;
pkg.data = data;
return sendData(&pkg, QH::HostAddress(TEST_LOCAL_HOST, LOCAL_TEST_PORT));
}
const BigPackage* getData() const {
2021-10-07 22:37:17 +03:00
QMutexLocker locker(&_mData);
2021-10-02 22:12:39 +03:00
return data;
}
protected:
void incomingData(const QH::PKG::AbstractData *pkg, const QH::AbstractNodeInfo *sender) override {
Q_UNUSED(sender);
2021-10-06 12:29:06 +03:00
if (pkg->cmd() == BigPackage::command()) {
2021-10-02 22:12:39 +03:00
2021-10-07 22:37:17 +03:00
_mData.lock();
2021-10-02 22:12:39 +03:00
data->copy<BigPackage>(*pkg);
2021-10-07 22:37:17 +03:00
_mData.unlock();
2021-10-02 22:12:39 +03:00
sendData(data, sender);
}
}
private:
2021-10-07 22:37:17 +03:00
mutable QMutex _mData;
2021-10-02 22:12:39 +03:00
BigPackage *data = nullptr;
};
BigDataTest::BigDataTest() {
_nodeA = new TestingClientBigData(0);
_nodeB = new TestingClientBigData(0);
}
BigDataTest::~BigDataTest() {
_nodeA->softDelete();
_nodeB->softDelete();
}
void BigDataTest::test() {
2021-10-07 22:37:41 +03:00
//#ifdef Q_OS_LINUX
2021-10-04 17:22:39 +03:00
2021-10-02 22:12:39 +03:00
QVERIFY(connectTest());
QVERIFY(sendDataTest());
2021-10-07 22:37:41 +03:00
//#endif
2021-10-02 22:12:39 +03:00
}
bool BigDataTest::connectTest() {
if (!_nodeA->run(TEST_LOCAL_HOST, LOCAL_TEST_PORT)) {
return false;
}
return connectFunc(_nodeB, TEST_LOCAL_HOST, LOCAL_TEST_PORT);
}
bool BigDataTest::sendDataTest() {
2021-10-04 17:22:39 +03:00
2021-10-02 22:12:39 +03:00
QByteArray testData;
for (int i = 0; i < 1024 * 1024 * 10; i++) {
2021-10-02 22:12:39 +03:00
testData.push_back(rand());
}
auto request = [this, testData](){
return static_cast<TestingClientBigData*>(_nodeB)->sendRequest(testData);
};
auto client = dynamic_cast<TestingClientBigData*>(_nodeB);
auto check = [client, testData](){
return client->getData() && client->getData()->data == testData;
};
return funcPrivateConnect(request, check);
}