1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
// SPDX-FileCopyrightText: 2020 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <QObject>
#include <gtest/gtest.h>
#include <QTest>
#include <QtConcurrent>
#include <QSignalSpy>
#include <DThreadUtils>
#include <string>
#include <thread>
#include <chrono>
DCORE_USE_NAMESPACE
#if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
class ThreadUtils : public QObject
{
Q_OBJECT
public Q_SLOTS:
void testCallInMainThread();
};
void ThreadUtils::testCallInMainThread()
{
DThreadUtil::runInMainThread([]() {
bool result = QThread::currentThread() == QCoreApplication::instance()->thread();
ASSERT_TRUE(result);
});
auto fe = QtConcurrent::run([] {
ASSERT_TRUE(DThreadUtil::runInMainThread([](QThread *thread) -> bool {
return QThread::currentThread() == QCoreApplication::instance()->thread() && QThread::currentThread() != thread;
}, QThread::currentThread()));
});
ASSERT_TRUE(QTest::qWaitFor([&] {
return fe.isFinished();
}));
}
class ut_DThreadUtils : public testing::Test
{
public:
virtual void SetUp()
{
m_threadutil = new ThreadUtils();
}
virtual void TearDown()
{
delete m_threadutil;
}
protected:
ThreadUtils *m_threadutil = nullptr;
};
TEST_F(ut_DThreadUtils, CallInMainThread)
{
ASSERT_TRUE(m_threadutil);
m_threadutil->testCallInMainThread();
}
#else
class ut_DThreadUtils :public testing::Test{
public:
virtual void SetUp()
{
t = new QThread();
t->start();
m_threadutil = new DThreadUtils(t);
}
virtual void TearDown()
{
t->exit();
t->wait();
delete t;
delete m_threadutil;
}
protected:
QThread *t{nullptr};
DThreadUtils *m_threadutil{nullptr};
};
class QWorker : public QObject {
Q_OBJECT
public:
explicit QWorker(QObject *parent = nullptr):QObject(parent){}
~QWorker() = default;
public Q_SLOTS:
int testFunc(int i, double j) {
int r =qFloor(i + j);
wait();
emit testFuncTrigger(r);
return r;
}
void setWaitForSecs(int sec){
m_waitSecs = sec;
}
void wait() {
std::unique_lock<std::mutex> lock(m_mutex);
m_cv.wait_for(lock, std::chrono::seconds(m_waitSecs));
}
void notifyOne() {
m_cv.notify_one();
}
Q_SIGNALS:
void testFuncTrigger(int v);
private:
std::mutex m_mutex;
std::condition_variable m_cv;
int m_waitSecs = 2;
};
class CallableObject{
public:
CallableObject() = default;
~CallableObject() = default;
QString operator()(const QString &str){
s += str;
std::this_thread::sleep_for(std::chrono::microseconds(100));
return s;
}
QString testFunc(const QString &){
return s;
}
private:
QString s{"CallableObject: "};
};
TEST_F(ut_DThreadUtils, testThread)
{
auto tmp = m_threadutil->thread();
EXPECT_EQ(tmp, t);
}
TEST_F(ut_DThreadUtils, testRunWithQObj)
{
QWorker w;
QSignalSpy spy(&w, &QWorker::testFuncTrigger);
auto result = m_threadutil->run(&w, &QWorker::testFunc, 10, 24.6);
std::this_thread::sleep_for(std::chrono::microseconds(100));
EXPECT_TRUE(result.isStarted());
EXPECT_TRUE(result.isRunning());
w.notifyOne();
result.waitForFinished();
EXPECT_TRUE(result.isFinished());
auto raw = result.result();
EXPECT_EQ(raw, 34);
EXPECT_EQ(spy.count(), 1);
}
TEST_F(ut_DThreadUtils,testRunWithLambda)
{
auto threadId1 = std::this_thread::get_id();
auto result = m_threadutil->run([](decltype(threadId1) id){
EXPECT_NE(std::this_thread::get_id(), id);
return true;
}, threadId1);
result.waitForFinished();
auto raw = result.result();
EXPECT_TRUE(raw);
}
TEST_F(ut_DThreadUtils,testRunWithCallableObj)
{
CallableObject obj;
QString tmp{"Hello"};
auto result1 = m_threadutil->run(obj, tmp);
result1.waitForFinished();
auto raw1 = result1.result();
EXPECT_EQ(raw1, QString{"CallableObject: Hello"});
auto result2 = m_threadutil->run(&obj, &CallableObject::testFunc, tmp);
result2.waitForFinished();
auto raw2 = result2.result();
EXPECT_EQ(raw2, QString{"CallableObject: "});
}
TEST_F(ut_DThreadUtils, testExecWithQObj)
{
QWorker w;
w.setWaitForSecs(0);
QSignalSpy spy(&w, SIGNAL(testFuncTrigger(int)));
auto result = m_threadutil->exec(&w, &QWorker::testFunc, 10, 24.6);
EXPECT_EQ(result, 34);
EXPECT_EQ(spy.count(), 1);
}
TEST_F(ut_DThreadUtils, testExecWithLambda)
{
auto threadId1 = std::this_thread::get_id();
auto result = m_threadutil->exec (
[](decltype(threadId1) id) {
EXPECT_NE(std::this_thread::get_id(), id);
return true;
}, threadId1);
EXPECT_TRUE(result);
}
TEST_F(ut_DThreadUtils, testExecWithCallableObj)
{
CallableObject obj;
QString tmp{"Hello"};
auto result1 = m_threadutil->exec(obj, tmp);
EXPECT_EQ(result1, QString{"CallableObject: Hello"});
auto result2 = m_threadutil->exec(&obj, &CallableObject::testFunc, tmp);
EXPECT_EQ(result2, QString{"CallableObject: "});
}
TEST_F(ut_DThreadUtils, testDirectlyInvoke)
{
DThreadUtils tu(QThread::currentThread());
QWorker w;
w.setWaitForSecs(0);
QSignalSpy spy(&w, SIGNAL(testFuncTrigger(int)));
auto result = tu.run(&w, &QWorker::testFunc, 10, 24.6);
auto raw = result.result(); // no wait
EXPECT_EQ(raw, 34);
EXPECT_EQ(spy.count(), 1);
}
TEST_F(ut_DThreadUtils, testCancel)
{
CallableObject obj;
QString tmp{"Hello"};
int cancelCounter{0};
auto result1 = m_threadutil->run(obj, tmp);
auto cancelResult = result1.onCanceled([&cancelCounter]() {
cancelCounter += 1;
return QString{"failed"};
});
result1.cancel();
EXPECT_FALSE(result1.isFinished());
EXPECT_FALSE(result1.isValid());
EXPECT_TRUE(result1.isCanceled());
cancelResult.waitForFinished();
EXPECT_EQ(cancelCounter, 1);
EXPECT_EQ(cancelResult.result(), QString{"failed"});
}
TEST_F(ut_DThreadUtils, testDestructCancel)
{
auto w = new QWorker{};
auto failedCounter{0};
auto result = m_threadutil->run(w, &QWorker::testFunc, 10, 24.6);
delete w;
auto failedResult = result.onFailed([&failedCounter]() {
failedCounter += 1;
return -1;
});
EXPECT_FALSE(result.isValid());
failedResult.waitForFinished();
EXPECT_EQ(failedCounter, 1);
EXPECT_EQ(failedResult.result(), -1);
}
#endif
#include "ut_dthreadutils.moc"
|