Как тестировать QThread в gtest?
Пытаюсь запускаю в потоке функцию doWork()
#include <QObject>
#include <QTextEdit>
#include <QThread>
#include <QVBoxLayout>
#include <QWidget>
#include <QtWidgets>
#include <QWaitCondition>
#include <QApplication>
#include <QDebug>
class Worker : public QObject {
Q_OBJECT
public slots:
void doWork() {
QThread::sleep(2); // имитируем долгий процесс вычислений в потоке
emit resultReady("Workers are the best");
}
signals:
void resultReady(const QString &data);
};
class Client : public QObject {
Q_OBJECT
public:
Client() {}
virtual ~Client() {}
QString data() const
{
return m_data;
}
public slots:
void append(const QString& data) {
m_data = data;
qDebug() << data;
}
private:
QString m_data;
};
int main()
{
int argc1 = 0;
QCoreApplication app(argc1, (char**)0);
/* Setup thread */
Client wgt;
auto thread = new QThread(&wgt);
auto worker = new Worker();
worker->moveToThread(thread);
QObject::connect(thread, &QThread::finished, worker, &QObject::deleteLater);
QObject::connect(thread, &QThread::started, worker, &Worker::doWork);
QObject::connect(worker, &Worker::resultReady, &wgt, &Client::append);
/* Start working*/
thread->start();
// Очищаем ресурсы
thread->quit();
thread->wait();
// Q_ASSERT(!wgt.data().isEmpty());
return app.exec();
}
#include "main.moc"
Пытаюсь написать для него тест:
#include <QDebug>
#include <QThread>
#include <QMutex>
#include <QCoreApplication>
#include <gtest/gtest.h>
class Worker : public QObject {
Q_OBJECT
public slots:
void doWork() {
QThread::sleep(2); // имитируем долгий процесс вычислений в потоке
emit resultReady("Workers are the best");
}
signals:
void resultReady(const QString &data);
};
class Client : public QObject {
Q_OBJECT
public:
Client() {}
virtual ~Client() {}
QString data() const;
public slots:
void append(const QString& data) {
m_data = data;
qDebug() << " {" << data << "} ";
std::cout << " [" << data.toStdString() << "] ";
}
private:
QString m_data;
};
QString Client::data() const {
return m_data;
}
TEST(TimeOperatingTest, WorkIsHandledCorrectly) {
int argc = 0;
QCoreApplication app(argc, (char**)0);
/* Setup thread */
Client wgt;
auto thread = new QThread(&wgt);
auto worker = new Worker();
worker->moveToThread(thread);
QObject::connect(thread, &QThread::finished, worker, &QObject::deleteLater);
QObject::connect(thread, &QThread::started, worker, &Worker::doWork);
QObject::connect(worker, &Worker::resultReady, &wgt, &Client::append);
/* Start working*/
thread->start();
// Очищаем ресурсы
thread->quit();
thread->wait();
qDebug() << " wgt data " << wgt.data(); // здесь пусто
//EXPECT_FALSE(wgt.data().isEmpty());
app.exec();
qDebug() << " wgt data " << wgt.data(); // суда естественно не дойдет
}
#include "OperatingThreadTests.moc"
Подскажите как правильно тестировать QThread?