author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Thu, 02 Sep 2010 21:20:32 +0300 | |
changeset 34 | a33bf25e6f72 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
#include <qcoreapplication.h> |
|
46 |
#include <qdatetime.h> |
|
47 |
#include <qmutex.h> |
|
48 |
#include <qthread.h> |
|
49 |
#include <qtimer.h> |
|
50 |
#include <qwaitcondition.h> |
|
51 |
#include <qdebug.h> |
|
52 |
||
53 |
#ifdef Q_OS_UNIX |
|
54 |
#include <pthread.h> |
|
55 |
#endif |
|
56 |
#if defined(Q_OS_WINCE) |
|
57 |
#include <windows.h> |
|
58 |
#elif defined(Q_OS_WIN) |
|
59 |
#include <process.h> |
|
60 |
#include <windows.h> |
|
61 |
#endif |
|
62 |
||
63 |
//TESTED_CLASS= |
|
64 |
//TESTED_FILES= |
|
65 |
||
66 |
class tst_QThread : public QObject |
|
67 |
{ |
|
68 |
Q_OBJECT |
|
69 |
||
70 |
public: |
|
71 |
tst_QThread(); |
|
72 |
virtual ~tst_QThread(); |
|
73 |
||
74 |
private slots: |
|
75 |
void currentThreadId(); |
|
76 |
void currentThread(); |
|
77 |
void idealThreadCount(); |
|
78 |
void isFinished(); |
|
79 |
void isRunning(); |
|
80 |
void setPriority(); |
|
81 |
void priority(); |
|
82 |
void setStackSize(); |
|
83 |
void stackSize(); |
|
84 |
void exit(); |
|
85 |
void start(); |
|
86 |
void terminate(); |
|
87 |
void quit(); |
|
88 |
void wait(); |
|
89 |
void started(); |
|
90 |
void finished(); |
|
91 |
void terminated(); |
|
92 |
void run(); |
|
93 |
void exec(); |
|
94 |
void setTerminationEnabled(); |
|
95 |
void sleep(); |
|
96 |
void msleep(); |
|
97 |
void usleep(); |
|
98 |
||
99 |
void nativeThreadAdoption(); |
|
100 |
void adoptedThreadAffinity(); |
|
101 |
void adoptedThreadSetPriority(); |
|
102 |
void adoptedThreadExit(); |
|
103 |
void adoptedThreadExec(); |
|
104 |
void adoptedThreadFinished(); |
|
105 |
void adoptMultipleThreads(); |
|
106 |
||
107 |
void stressTest(); |
|
108 |
}; |
|
109 |
||
110 |
enum { one_minute = 60 * 1000, five_minutes = 5 * one_minute }; |
|
111 |
||
112 |
class SignalRecorder : public QObject |
|
113 |
{ |
|
114 |
Q_OBJECT |
|
115 |
public: |
|
116 |
QAtomicInt activationCount; |
|
117 |
||
118 |
inline SignalRecorder() |
|
119 |
{ activationCount = 0; } |
|
120 |
||
121 |
bool wasActivated() |
|
122 |
{ return activationCount > 0; } |
|
123 |
||
124 |
public slots: |
|
125 |
void slot(); |
|
126 |
}; |
|
127 |
||
128 |
void SignalRecorder::slot() |
|
129 |
{ activationCount.ref(); } |
|
130 |
||
131 |
class Current_Thread : public QThread |
|
132 |
{ |
|
133 |
public: |
|
134 |
Qt::HANDLE id; |
|
135 |
QThread *thread; |
|
136 |
||
137 |
void run() |
|
138 |
{ |
|
139 |
id = QThread::currentThreadId(); |
|
140 |
thread = QThread::currentThread(); |
|
141 |
} |
|
142 |
}; |
|
143 |
||
144 |
class Simple_Thread : public QThread |
|
145 |
{ |
|
146 |
public: |
|
147 |
QMutex mutex; |
|
148 |
QWaitCondition cond; |
|
149 |
||
150 |
void run() |
|
151 |
{ |
|
152 |
QMutexLocker locker(&mutex); |
|
153 |
cond.wakeOne(); |
|
154 |
} |
|
155 |
}; |
|
156 |
||
157 |
class Exit_Object : public QObject |
|
158 |
{ |
|
159 |
Q_OBJECT |
|
160 |
public: |
|
161 |
QThread *thread; |
|
162 |
int code; |
|
163 |
public slots: |
|
164 |
void slot() |
|
165 |
{ thread->exit(code); } |
|
166 |
}; |
|
167 |
||
168 |
class Exit_Thread : public Simple_Thread |
|
169 |
{ |
|
170 |
public: |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
171 |
Exit_Object *object; |
0 | 172 |
int code; |
173 |
int result; |
|
174 |
||
175 |
void run() |
|
176 |
{ |
|
177 |
Simple_Thread::run(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
178 |
if (object) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
179 |
object->thread = this; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
180 |
object->code = code; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
181 |
QTimer::singleShot(100, object, SLOT(slot())); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
182 |
} |
0 | 183 |
result = exec(); |
184 |
} |
|
185 |
}; |
|
186 |
||
187 |
class Terminate_Thread : public Simple_Thread |
|
188 |
{ |
|
189 |
public: |
|
190 |
void run() |
|
191 |
{ |
|
192 |
setTerminationEnabled(false); |
|
193 |
{ |
|
194 |
QMutexLocker locker(&mutex); |
|
195 |
cond.wakeOne(); |
|
196 |
cond.wait(&mutex, five_minutes); |
|
197 |
} |
|
198 |
setTerminationEnabled(true); |
|
199 |
Q_ASSERT_X(false, "tst_QThread", "test case hung"); |
|
200 |
} |
|
201 |
}; |
|
202 |
||
203 |
class Quit_Object : public QObject |
|
204 |
{ |
|
205 |
Q_OBJECT |
|
206 |
public: |
|
207 |
QThread *thread; |
|
208 |
public slots: |
|
209 |
void slot() |
|
210 |
{ thread->quit(); } |
|
211 |
}; |
|
212 |
||
213 |
class Quit_Thread : public Simple_Thread |
|
214 |
{ |
|
215 |
public: |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
216 |
Quit_Object *object; |
0 | 217 |
int result; |
218 |
||
219 |
void run() |
|
220 |
{ |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
221 |
Simple_Thread::run(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
222 |
if (object) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
223 |
object->thread = this; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
224 |
QTimer::singleShot(100, object, SLOT(slot())); |
0 | 225 |
} |
226 |
result = exec(); |
|
227 |
} |
|
228 |
}; |
|
229 |
||
230 |
class Sleep_Thread : public Simple_Thread |
|
231 |
{ |
|
232 |
public: |
|
233 |
enum SleepType { Second, Millisecond, Microsecond }; |
|
234 |
||
235 |
SleepType sleepType; |
|
236 |
int interval; |
|
237 |
||
238 |
int elapsed; // result, in *MILLISECONDS* |
|
239 |
||
240 |
void run() |
|
241 |
{ |
|
242 |
QMutexLocker locker(&mutex); |
|
243 |
||
244 |
elapsed = 0; |
|
245 |
QTime time; |
|
246 |
time.start(); |
|
247 |
switch (sleepType) { |
|
248 |
case Second: |
|
249 |
sleep(interval); |
|
250 |
break; |
|
251 |
case Millisecond: |
|
252 |
msleep(interval); |
|
253 |
break; |
|
254 |
case Microsecond: |
|
255 |
usleep(interval); |
|
256 |
break; |
|
257 |
} |
|
258 |
elapsed = time.elapsed(); |
|
259 |
||
260 |
cond.wakeOne(); |
|
261 |
} |
|
262 |
}; |
|
263 |
||
264 |
tst_QThread::tst_QThread() |
|
265 |
||
266 |
{ |
|
267 |
} |
|
268 |
||
269 |
tst_QThread::~tst_QThread() |
|
270 |
{ |
|
271 |
||
272 |
} |
|
273 |
||
274 |
void tst_QThread::currentThreadId() |
|
275 |
{ |
|
276 |
Current_Thread thread; |
|
277 |
thread.id = 0; |
|
278 |
thread.thread = 0; |
|
279 |
thread.start(); |
|
280 |
QVERIFY(thread.wait(five_minutes)); |
|
281 |
QVERIFY(thread.id != 0); |
|
282 |
QVERIFY(thread.id != QThread::currentThreadId()); |
|
283 |
} |
|
284 |
||
285 |
void tst_QThread::currentThread() |
|
286 |
{ |
|
287 |
QVERIFY(QThread::currentThread() != 0); |
|
288 |
QCOMPARE(QThread::currentThread(), thread()); |
|
289 |
||
290 |
Current_Thread thread; |
|
291 |
thread.id = 0; |
|
292 |
thread.thread = 0; |
|
293 |
thread.start(); |
|
294 |
QVERIFY(thread.wait(five_minutes)); |
|
295 |
QCOMPARE(thread.thread, (QThread *)&thread); |
|
296 |
} |
|
297 |
||
298 |
void tst_QThread::idealThreadCount() |
|
299 |
{ |
|
300 |
QVERIFY(QThread::idealThreadCount() > 0); |
|
301 |
qDebug() << "Available cpu cores:" << QThread::idealThreadCount(); |
|
302 |
} |
|
303 |
||
304 |
void tst_QThread::isFinished() |
|
305 |
{ |
|
306 |
Simple_Thread thread; |
|
307 |
QVERIFY(!thread.isFinished()); |
|
308 |
QMutexLocker locker(&thread.mutex); |
|
309 |
thread.start(); |
|
310 |
QVERIFY(!thread.isFinished()); |
|
311 |
thread.cond.wait(locker.mutex()); |
|
312 |
QVERIFY(thread.wait(five_minutes)); |
|
313 |
QVERIFY(thread.isFinished()); |
|
314 |
} |
|
315 |
||
316 |
void tst_QThread::isRunning() |
|
317 |
{ |
|
318 |
Simple_Thread thread; |
|
319 |
QVERIFY(!thread.isRunning()); |
|
320 |
QMutexLocker locker(&thread.mutex); |
|
321 |
thread.start(); |
|
322 |
QVERIFY(thread.isRunning()); |
|
323 |
thread.cond.wait(locker.mutex()); |
|
324 |
QVERIFY(thread.wait(five_minutes)); |
|
325 |
QVERIFY(!thread.isRunning()); |
|
326 |
} |
|
327 |
||
328 |
void tst_QThread::setPriority() |
|
329 |
{ |
|
330 |
Simple_Thread thread; |
|
331 |
||
332 |
// cannot change the priority, since the thread is not running |
|
333 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
334 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
335 |
thread.setPriority(QThread::IdlePriority); |
|
336 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
337 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
338 |
thread.setPriority(QThread::LowestPriority); |
|
339 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
340 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
341 |
thread.setPriority(QThread::LowPriority); |
|
342 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
343 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
344 |
thread.setPriority(QThread::NormalPriority); |
|
345 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
346 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
347 |
thread.setPriority(QThread::HighPriority); |
|
348 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
349 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
350 |
thread.setPriority(QThread::HighestPriority); |
|
351 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
352 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
353 |
thread.setPriority(QThread::TimeCriticalPriority); |
|
354 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
355 |
||
356 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
357 |
QMutexLocker locker(&thread.mutex); |
|
358 |
thread.start(); |
|
359 |
||
360 |
// change the priority of a running thread |
|
361 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
362 |
thread.setPriority(QThread::IdlePriority); |
|
363 |
QCOMPARE(thread.priority(), QThread::IdlePriority); |
|
364 |
thread.setPriority(QThread::LowestPriority); |
|
365 |
QCOMPARE(thread.priority(), QThread::LowestPriority); |
|
366 |
thread.setPriority(QThread::LowPriority); |
|
367 |
QCOMPARE(thread.priority(), QThread::LowPriority); |
|
368 |
thread.setPriority(QThread::NormalPriority); |
|
369 |
QCOMPARE(thread.priority(), QThread::NormalPriority); |
|
370 |
thread.setPriority(QThread::HighPriority); |
|
371 |
QCOMPARE(thread.priority(), QThread::HighPriority); |
|
372 |
thread.setPriority(QThread::HighestPriority); |
|
373 |
QCOMPARE(thread.priority(), QThread::HighestPriority); |
|
374 |
thread.setPriority(QThread::TimeCriticalPriority); |
|
375 |
QCOMPARE(thread.priority(), QThread::TimeCriticalPriority); |
|
376 |
thread.cond.wait(locker.mutex()); |
|
377 |
QVERIFY(thread.wait(five_minutes)); |
|
378 |
||
379 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
380 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
381 |
thread.setPriority(QThread::IdlePriority); |
|
382 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
383 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
384 |
thread.setPriority(QThread::LowestPriority); |
|
385 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
386 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
387 |
thread.setPriority(QThread::LowPriority); |
|
388 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
389 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
390 |
thread.setPriority(QThread::NormalPriority); |
|
391 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
392 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
393 |
thread.setPriority(QThread::HighPriority); |
|
394 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
395 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
396 |
thread.setPriority(QThread::HighestPriority); |
|
397 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
398 |
QTest::ignoreMessage(QtWarningMsg, "QThread::setPriority: Cannot set priority, thread is not running"); |
|
399 |
thread.setPriority(QThread::TimeCriticalPriority); |
|
400 |
QCOMPARE(thread.priority(), QThread::InheritPriority); |
|
401 |
} |
|
402 |
||
403 |
void tst_QThread::priority() |
|
404 |
{ DEPENDS_ON("setPriority"); } |
|
405 |
||
406 |
void tst_QThread::setStackSize() |
|
407 |
{ |
|
408 |
Simple_Thread thread; |
|
409 |
QCOMPARE(thread.stackSize(), 0u); |
|
410 |
thread.setStackSize(8192u); |
|
411 |
QCOMPARE(thread.stackSize(), 8192u); |
|
412 |
thread.setStackSize(0u); |
|
413 |
QCOMPARE(thread.stackSize(), 0u); |
|
414 |
} |
|
415 |
||
416 |
void tst_QThread::stackSize() |
|
417 |
{ |
|
418 |
DEPENDS_ON("setStackSize"); |
|
419 |
} |
|
420 |
||
421 |
void tst_QThread::exit() |
|
422 |
{ |
|
423 |
Exit_Thread thread; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
424 |
thread.object = new Exit_Object; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
425 |
thread.object->moveToThread(&thread); |
0 | 426 |
thread.code = 42; |
427 |
thread.result = 0; |
|
428 |
QVERIFY(!thread.isFinished()); |
|
429 |
QVERIFY(!thread.isRunning()); |
|
430 |
QMutexLocker locker(&thread.mutex); |
|
431 |
thread.start(); |
|
432 |
QVERIFY(thread.isRunning()); |
|
433 |
QVERIFY(!thread.isFinished()); |
|
434 |
thread.cond.wait(locker.mutex()); |
|
435 |
QVERIFY(thread.wait(five_minutes)); |
|
436 |
QVERIFY(thread.isFinished()); |
|
437 |
QVERIFY(!thread.isRunning()); |
|
438 |
QCOMPARE(thread.result, thread.code); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
439 |
delete thread.object; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
440 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
441 |
Exit_Thread thread2; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
442 |
thread2.object = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
443 |
thread2.code = 53; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
444 |
thread2.result = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
445 |
QMutexLocker locker2(&thread2.mutex); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
446 |
thread2.start(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
447 |
thread2.exit(thread2.code); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
448 |
thread2.cond.wait(locker2.mutex()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
449 |
QVERIFY(thread2.wait(five_minutes)); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
450 |
QCOMPARE(thread2.result, thread2.code); |
0 | 451 |
} |
452 |
||
453 |
void tst_QThread::start() |
|
454 |
{ |
|
455 |
QThread::Priority priorities[] = { |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
456 |
QThread::IdlePriority, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
457 |
QThread::LowestPriority, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
458 |
QThread::LowPriority, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
459 |
QThread::NormalPriority, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
460 |
QThread::HighPriority, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
461 |
QThread::HighestPriority, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
462 |
QThread::TimeCriticalPriority, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
463 |
QThread::InheritPriority |
0 | 464 |
}; |
465 |
const int prio_count = sizeof(priorities) / sizeof(QThread::Priority); |
|
466 |
||
467 |
for (int i = 0; i < prio_count; ++i) { |
|
468 |
Simple_Thread thread; |
|
469 |
QVERIFY(!thread.isFinished()); |
|
470 |
QVERIFY(!thread.isRunning()); |
|
471 |
QMutexLocker locker(&thread.mutex); |
|
472 |
thread.start(priorities[i]); |
|
473 |
QVERIFY(thread.isRunning()); |
|
474 |
QVERIFY(!thread.isFinished()); |
|
475 |
thread.cond.wait(locker.mutex()); |
|
476 |
QVERIFY(thread.wait(five_minutes)); |
|
477 |
QVERIFY(thread.isFinished()); |
|
478 |
QVERIFY(!thread.isRunning()); |
|
479 |
} |
|
480 |
} |
|
481 |
||
482 |
void tst_QThread::terminate() |
|
483 |
{ |
|
484 |
Terminate_Thread thread; |
|
485 |
{ |
|
486 |
QMutexLocker locker(&thread.mutex); |
|
487 |
thread.start(); |
|
488 |
QVERIFY(thread.cond.wait(locker.mutex(), five_minutes)); |
|
489 |
thread.terminate(); |
|
490 |
thread.cond.wakeOne(); |
|
491 |
} |
|
492 |
QVERIFY(thread.wait(five_minutes)); |
|
493 |
} |
|
494 |
||
495 |
void tst_QThread::quit() |
|
496 |
{ |
|
497 |
Quit_Thread thread; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
498 |
thread.object = new Quit_Object; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
499 |
thread.object->moveToThread(&thread); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
500 |
thread.result = -1; |
0 | 501 |
QVERIFY(!thread.isFinished()); |
502 |
QVERIFY(!thread.isRunning()); |
|
503 |
QMutexLocker locker(&thread.mutex); |
|
504 |
thread.start(); |
|
505 |
QVERIFY(thread.isRunning()); |
|
506 |
QVERIFY(!thread.isFinished()); |
|
507 |
thread.cond.wait(locker.mutex()); |
|
508 |
QVERIFY(thread.wait(five_minutes)); |
|
509 |
QVERIFY(thread.isFinished()); |
|
510 |
QVERIFY(!thread.isRunning()); |
|
511 |
QCOMPARE(thread.result, 0); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
512 |
delete thread.object; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
513 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
514 |
Quit_Thread thread2; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
515 |
thread2.object = 0; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
516 |
thread2.result = -1; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
517 |
QMutexLocker locker2(&thread2.mutex); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
518 |
thread2.start(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
519 |
thread2.quit(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
520 |
thread2.cond.wait(locker2.mutex()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
521 |
QVERIFY(thread2.wait(five_minutes)); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
522 |
QCOMPARE(thread2.result, 0); |
0 | 523 |
} |
524 |
||
525 |
void tst_QThread::wait() |
|
526 |
{ |
|
527 |
DEPENDS_ON("isRunning"); |
|
528 |
DEPENDS_ON("isFinished"); |
|
529 |
} |
|
530 |
||
531 |
void tst_QThread::started() |
|
532 |
{ |
|
533 |
SignalRecorder recorder; |
|
534 |
Simple_Thread thread; |
|
535 |
connect(&thread, SIGNAL(started()), &recorder, SLOT(slot()), Qt::DirectConnection); |
|
536 |
thread.start(); |
|
537 |
QVERIFY(thread.wait(five_minutes)); |
|
538 |
QVERIFY(recorder.wasActivated()); |
|
539 |
} |
|
540 |
||
541 |
void tst_QThread::finished() |
|
542 |
{ |
|
543 |
SignalRecorder recorder; |
|
544 |
Simple_Thread thread; |
|
545 |
connect(&thread, SIGNAL(finished()), &recorder, SLOT(slot()), Qt::DirectConnection); |
|
546 |
thread.start(); |
|
547 |
QVERIFY(thread.wait(five_minutes)); |
|
548 |
QVERIFY(recorder.wasActivated()); |
|
549 |
} |
|
550 |
||
551 |
void tst_QThread::terminated() |
|
552 |
{ |
|
553 |
SignalRecorder recorder; |
|
554 |
Terminate_Thread thread; |
|
555 |
connect(&thread, SIGNAL(terminated()), &recorder, SLOT(slot()), Qt::DirectConnection); |
|
556 |
{ |
|
557 |
QMutexLocker locker(&thread.mutex); |
|
558 |
thread.start(); |
|
559 |
thread.cond.wait(locker.mutex()); |
|
560 |
thread.terminate(); |
|
561 |
thread.cond.wakeOne(); |
|
562 |
} |
|
563 |
QVERIFY(thread.wait(five_minutes)); |
|
564 |
QVERIFY(recorder.wasActivated()); |
|
565 |
} |
|
566 |
||
567 |
void tst_QThread::run() |
|
568 |
{ DEPENDS_ON("wait()"); } |
|
569 |
||
570 |
void tst_QThread::exec() |
|
571 |
{ |
|
572 |
DEPENDS_ON("exit()"); |
|
573 |
DEPENDS_ON("quit()"); |
|
574 |
||
575 |
class MultipleExecThread : public QThread |
|
576 |
{ |
|
577 |
public: |
|
578 |
int res1, res2; |
|
579 |
||
580 |
MultipleExecThread() : res1(-2), res2(-2) { } |
|
581 |
||
582 |
void run() |
|
583 |
{ |
|
584 |
{ |
|
585 |
Exit_Object o; |
|
586 |
o.thread = this; |
|
587 |
o.code = 1; |
|
588 |
QTimer::singleShot(100, &o, SLOT(slot())); |
|
589 |
res1 = exec(); |
|
590 |
} |
|
591 |
{ |
|
592 |
Exit_Object o; |
|
593 |
o.thread = this; |
|
594 |
o.code = 2; |
|
595 |
QTimer::singleShot(100, &o, SLOT(slot())); |
|
596 |
res2 = exec(); |
|
597 |
} |
|
598 |
} |
|
599 |
}; |
|
600 |
||
601 |
MultipleExecThread thread; |
|
602 |
thread.start(); |
|
603 |
QVERIFY(thread.wait()); |
|
604 |
||
605 |
QCOMPARE(thread.res1, 1); |
|
606 |
QCOMPARE(thread.res2, 2); |
|
607 |
} |
|
608 |
||
609 |
void tst_QThread::setTerminationEnabled() |
|
610 |
{ DEPENDS_ON("terminate"); } |
|
611 |
||
612 |
void tst_QThread::sleep() |
|
613 |
{ |
|
614 |
Sleep_Thread thread; |
|
615 |
thread.sleepType = Sleep_Thread::Second; |
|
616 |
thread.interval = 2; |
|
617 |
thread.start(); |
|
618 |
QVERIFY(thread.wait(five_minutes)); |
|
619 |
QVERIFY(thread.elapsed >= 2000); |
|
620 |
} |
|
621 |
||
622 |
void tst_QThread::msleep() |
|
623 |
{ |
|
624 |
Sleep_Thread thread; |
|
625 |
thread.sleepType = Sleep_Thread::Millisecond; |
|
626 |
thread.interval = 120; |
|
627 |
thread.start(); |
|
628 |
QVERIFY(thread.wait(five_minutes)); |
|
629 |
#if defined (Q_OS_WIN) |
|
630 |
// Since the resolution of QTime is so coarse... |
|
631 |
QVERIFY(thread.elapsed >= 100); |
|
632 |
#else |
|
633 |
QVERIFY(thread.elapsed >= 120); |
|
634 |
#endif |
|
635 |
} |
|
636 |
||
637 |
void tst_QThread::usleep() |
|
638 |
{ |
|
639 |
Sleep_Thread thread; |
|
640 |
thread.sleepType = Sleep_Thread::Microsecond; |
|
641 |
thread.interval = 120000; |
|
642 |
thread.start(); |
|
643 |
QVERIFY(thread.wait(five_minutes)); |
|
644 |
#if defined (Q_OS_WIN) |
|
645 |
// Since the resolution of QTime is so coarse... |
|
646 |
QVERIFY(thread.elapsed >= 100); |
|
647 |
#else |
|
648 |
QVERIFY(thread.elapsed >= 120); |
|
649 |
#endif |
|
650 |
} |
|
651 |
||
652 |
typedef void (*FunctionPointer)(void *); |
|
653 |
void noop(void*) { } |
|
654 |
||
655 |
#ifdef Q_OS_UNIX |
|
656 |
typedef pthread_t ThreadHandle; |
|
657 |
#elif defined Q_OS_WIN |
|
658 |
typedef HANDLE ThreadHandle; |
|
659 |
#endif |
|
660 |
||
661 |
#ifdef Q_OS_WIN |
|
662 |
#define WIN_FIX_STDCALL __stdcall |
|
663 |
#else |
|
664 |
#define WIN_FIX_STDCALL |
|
665 |
#endif |
|
666 |
||
667 |
class NativeThreadWrapper |
|
668 |
{ |
|
669 |
public: |
|
670 |
NativeThreadWrapper() : qthread(0), waitForStop(false) {} |
|
671 |
void start(FunctionPointer functionPointer = noop, void *data = 0); |
|
672 |
void startAndWait(FunctionPointer functionPointer = noop, void *data = 0); |
|
673 |
void join(); |
|
674 |
void setWaitForStop() { waitForStop = true; } |
|
675 |
void stop(); |
|
676 |
||
677 |
ThreadHandle nativeThreadHandle; |
|
678 |
QThread *qthread; |
|
679 |
QWaitCondition startCondition; |
|
680 |
QMutex mutex; |
|
681 |
bool waitForStop; |
|
682 |
QWaitCondition stopCondition; |
|
683 |
protected: |
|
684 |
static void *runUnix(void *data); |
|
685 |
static unsigned WIN_FIX_STDCALL runWin(void *data); |
|
686 |
||
687 |
FunctionPointer functionPointer; |
|
688 |
void *data; |
|
689 |
}; |
|
690 |
||
691 |
void NativeThreadWrapper::start(FunctionPointer functionPointer, void *data) |
|
692 |
{ |
|
693 |
this->functionPointer = functionPointer; |
|
694 |
this->data = data; |
|
695 |
#ifdef Q_OS_UNIX |
|
696 |
const int state = pthread_create(&nativeThreadHandle, 0, NativeThreadWrapper::runUnix, this); |
|
697 |
Q_UNUSED(state); |
|
698 |
#elif defined(Q_OS_WINCE) |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
699 |
nativeThreadHandle = CreateThread(NULL, 0 , (LPTHREAD_START_ROUTINE)NativeThreadWrapper::runWin , this, 0, NULL); |
0 | 700 |
#elif defined Q_OS_WIN |
701 |
unsigned thrdid = 0; |
|
702 |
nativeThreadHandle = (Qt::HANDLE) _beginthreadex(NULL, 0, NativeThreadWrapper::runWin, this, 0, &thrdid); |
|
703 |
#endif |
|
704 |
} |
|
705 |
||
706 |
void NativeThreadWrapper::startAndWait(FunctionPointer functionPointer, void *data) |
|
707 |
{ |
|
708 |
QMutexLocker locker(&mutex); |
|
709 |
start(functionPointer, data); |
|
710 |
startCondition.wait(locker.mutex()); |
|
711 |
} |
|
712 |
||
713 |
void NativeThreadWrapper::join() |
|
714 |
{ |
|
715 |
#ifdef Q_OS_UNIX |
|
716 |
pthread_join(nativeThreadHandle, 0); |
|
717 |
#elif defined Q_OS_WIN |
|
718 |
WaitForSingleObject(nativeThreadHandle, INFINITE); |
|
719 |
CloseHandle(nativeThreadHandle); |
|
720 |
#endif |
|
721 |
} |
|
722 |
||
723 |
void *NativeThreadWrapper::runUnix(void *that) |
|
724 |
{ |
|
725 |
NativeThreadWrapper *nativeThreadWrapper = reinterpret_cast<NativeThreadWrapper*>(that); |
|
726 |
||
727 |
// Adopt thread, create QThread object. |
|
728 |
nativeThreadWrapper->qthread = QThread::currentThread(); |
|
729 |
||
730 |
// Release main thread. |
|
731 |
{ |
|
732 |
QMutexLocker lock(&nativeThreadWrapper->mutex); |
|
733 |
nativeThreadWrapper->startCondition.wakeOne(); |
|
734 |
} |
|
735 |
||
736 |
// Run function. |
|
737 |
nativeThreadWrapper->functionPointer(nativeThreadWrapper->data); |
|
738 |
||
739 |
// Wait for stop. |
|
740 |
{ |
|
741 |
QMutexLocker lock(&nativeThreadWrapper->mutex); |
|
742 |
if (nativeThreadWrapper->waitForStop) |
|
743 |
nativeThreadWrapper->stopCondition.wait(lock.mutex()); |
|
744 |
} |
|
745 |
||
746 |
return 0; |
|
747 |
} |
|
748 |
||
749 |
unsigned WIN_FIX_STDCALL NativeThreadWrapper::runWin(void *data) |
|
750 |
{ |
|
751 |
runUnix(data); |
|
752 |
return 0; |
|
753 |
} |
|
754 |
||
755 |
void NativeThreadWrapper::stop() |
|
756 |
{ |
|
757 |
QMutexLocker lock(&mutex); |
|
758 |
waitForStop = false; |
|
759 |
stopCondition.wakeOne(); |
|
760 |
} |
|
761 |
||
762 |
bool threadAdoptedOk = false; |
|
763 |
QThread *mainThread; |
|
764 |
void testNativeThreadAdoption(void *) |
|
765 |
{ |
|
766 |
threadAdoptedOk = (QThread::currentThreadId() != 0 |
|
767 |
&& QThread::currentThread() != 0 |
|
768 |
&& QThread::currentThread() != mainThread); |
|
769 |
} |
|
770 |
void tst_QThread::nativeThreadAdoption() |
|
771 |
{ |
|
772 |
threadAdoptedOk = false; |
|
773 |
mainThread = QThread::currentThread(); |
|
774 |
NativeThreadWrapper nativeThread; |
|
775 |
nativeThread.setWaitForStop(); |
|
776 |
nativeThread.startAndWait(testNativeThreadAdoption); |
|
777 |
QVERIFY(nativeThread.qthread); |
|
778 |
||
779 |
nativeThread.stop(); |
|
780 |
nativeThread.join(); |
|
781 |
||
782 |
QVERIFY(threadAdoptedOk); |
|
783 |
} |
|
784 |
||
785 |
void adoptedThreadAffinityFunction(void *arg) |
|
786 |
{ |
|
787 |
QThread **affinity = reinterpret_cast<QThread **>(arg); |
|
788 |
QThread *current = QThread::currentThread(); |
|
789 |
affinity[0] = current; |
|
790 |
affinity[1] = current->thread(); |
|
791 |
} |
|
792 |
||
793 |
void tst_QThread::adoptedThreadAffinity() |
|
794 |
{ |
|
795 |
QThread *affinity[2] = { 0, 0 }; |
|
796 |
||
797 |
NativeThreadWrapper thread; |
|
798 |
thread.startAndWait(adoptedThreadAffinityFunction, affinity); |
|
799 |
thread.join(); |
|
800 |
||
801 |
// adopted thread should have affinity to itself |
|
802 |
QCOMPARE(affinity[0], affinity[1]); |
|
803 |
} |
|
804 |
||
805 |
void tst_QThread::adoptedThreadSetPriority() |
|
806 |
{ |
|
807 |
||
808 |
NativeThreadWrapper nativeThread; |
|
809 |
nativeThread.setWaitForStop(); |
|
810 |
nativeThread.startAndWait(); |
|
811 |
||
812 |
// change the priority of a running thread |
|
813 |
QCOMPARE(nativeThread.qthread->priority(), QThread::InheritPriority); |
|
814 |
nativeThread.qthread->setPriority(QThread::IdlePriority); |
|
815 |
QCOMPARE(nativeThread.qthread->priority(), QThread::IdlePriority); |
|
816 |
nativeThread.qthread->setPriority(QThread::LowestPriority); |
|
817 |
QCOMPARE(nativeThread.qthread->priority(), QThread::LowestPriority); |
|
818 |
nativeThread.qthread->setPriority(QThread::LowPriority); |
|
819 |
QCOMPARE(nativeThread.qthread->priority(), QThread::LowPriority); |
|
820 |
nativeThread.qthread->setPriority(QThread::NormalPriority); |
|
821 |
QCOMPARE(nativeThread.qthread->priority(), QThread::NormalPriority); |
|
822 |
nativeThread.qthread->setPriority(QThread::HighPriority); |
|
823 |
QCOMPARE(nativeThread.qthread->priority(), QThread::HighPriority); |
|
824 |
nativeThread.qthread->setPriority(QThread::HighestPriority); |
|
825 |
QCOMPARE(nativeThread.qthread->priority(), QThread::HighestPriority); |
|
826 |
nativeThread.qthread->setPriority(QThread::TimeCriticalPriority); |
|
827 |
QCOMPARE(nativeThread.qthread->priority(), QThread::TimeCriticalPriority); |
|
828 |
||
829 |
nativeThread.stop(); |
|
830 |
nativeThread.join(); |
|
831 |
} |
|
832 |
||
833 |
void tst_QThread::adoptedThreadExit() |
|
834 |
{ |
|
835 |
NativeThreadWrapper nativeThread; |
|
836 |
nativeThread.setWaitForStop(); |
|
837 |
||
838 |
nativeThread.startAndWait(); |
|
839 |
QVERIFY(nativeThread.qthread); |
|
840 |
QVERIFY(nativeThread.qthread->isRunning()); |
|
841 |
QVERIFY(!nativeThread.qthread->isFinished()); |
|
842 |
||
843 |
nativeThread.stop(); |
|
844 |
nativeThread.join(); |
|
845 |
} |
|
846 |
||
847 |
void adoptedThreadExecFunction(void *) |
|
848 |
{ |
|
849 |
QThread * const adoptedThread = QThread::currentThread(); |
|
850 |
QEventLoop eventLoop(adoptedThread); |
|
851 |
||
852 |
const int code = 1; |
|
853 |
Exit_Object o; |
|
854 |
o.thread = adoptedThread; |
|
855 |
o.code = code; |
|
856 |
QTimer::singleShot(100, &o, SLOT(slot())); |
|
857 |
||
858 |
const int result = eventLoop.exec(); |
|
859 |
QCOMPARE(result, code); |
|
860 |
} |
|
861 |
||
862 |
void tst_QThread::adoptedThreadExec() |
|
863 |
{ |
|
864 |
NativeThreadWrapper nativeThread; |
|
865 |
nativeThread.start(adoptedThreadExecFunction); |
|
866 |
nativeThread.join(); |
|
867 |
} |
|
868 |
||
869 |
/* |
|
870 |
Test that you get the finished signal when an adopted thread exits. |
|
871 |
*/ |
|
872 |
void tst_QThread::adoptedThreadFinished() |
|
873 |
{ |
|
874 |
NativeThreadWrapper nativeThread; |
|
875 |
nativeThread.setWaitForStop(); |
|
876 |
nativeThread.startAndWait(); |
|
877 |
||
878 |
QObject::connect(nativeThread.qthread, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
879 |
||
880 |
nativeThread.stop(); |
|
881 |
nativeThread.join(); |
|
882 |
||
883 |
QTestEventLoop::instance().enterLoop(5); |
|
884 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
885 |
} |
|
886 |
||
887 |
void tst_QThread::adoptMultipleThreads() |
|
888 |
{ |
|
889 |
#if defined(Q_OS_WIN) |
|
890 |
// Windows CE is not capable of handling that many threads. On the emulator it is dead with 26 threads already. |
|
891 |
# if defined(Q_OS_WINCE) |
|
892 |
const int numThreads = 20; |
|
893 |
# else |
|
894 |
// need to test lots of threads, so that we exceed MAXIMUM_WAIT_OBJECTS in qt_adopted_thread_watcher() |
|
895 |
const int numThreads = 200; |
|
896 |
# endif |
|
897 |
#else |
|
898 |
const int numThreads = 5; |
|
899 |
#endif |
|
900 |
QVector<NativeThreadWrapper*> nativeThreads; |
|
901 |
||
902 |
SignalRecorder recorder; |
|
903 |
||
904 |
for (int i = 0; i < numThreads; ++i) { |
|
905 |
nativeThreads.append(new NativeThreadWrapper()); |
|
906 |
nativeThreads.at(i)->setWaitForStop(); |
|
907 |
nativeThreads.at(i)->startAndWait(); |
|
908 |
QObject::connect(nativeThreads.at(i)->qthread, SIGNAL(finished()), &recorder, SLOT(slot())); |
|
909 |
} |
|
910 |
||
911 |
QObject::connect(nativeThreads.at(numThreads - 1)->qthread, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
912 |
||
913 |
for (int i = 0; i < numThreads; ++i) { |
|
914 |
nativeThreads.at(i)->stop(); |
|
915 |
nativeThreads.at(i)->join(); |
|
916 |
} |
|
917 |
||
918 |
QTestEventLoop::instance().enterLoop(5); |
|
919 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
920 |
QCOMPARE(int(recorder.activationCount), numThreads); |
|
921 |
} |
|
922 |
||
923 |
void tst_QThread::stressTest() |
|
924 |
{ |
|
925 |
#if defined(Q_OS_WINCE) |
|
926 |
QSKIP("Disconnects on WinCE, skipping...", SkipAll); |
|
927 |
#endif |
|
928 |
QTime t; |
|
929 |
t.start(); |
|
930 |
while (t.elapsed() < one_minute) { |
|
931 |
Current_Thread t; |
|
932 |
t.start(); |
|
933 |
t.wait(one_minute); |
|
934 |
} |
|
935 |
} |
|
936 |
||
937 |
||
938 |
QTEST_MAIN(tst_QThread) |
|
939 |
#include "tst_qthread.moc" |