Debian Codegraph @

tests/ut_dthreadutils.cpp

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