LeechCraft
0.6.70-19424-g027e980fd1
Modular cross-platform feature rich live environment.
Toggle main menu visibility
Loading...
Searching...
No Matches
workerthreadtest.cpp
Go to the documentation of this file.
1
/**********************************************************************
2
* LeechCraft - modular cross-platform feature rich internet client.
3
* Copyright (C) 2006-2014 Georg Rudoy
4
*
5
* Distributed under the Boost Software License, Version 1.0.
6
* (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7
**********************************************************************/
8
9
#include "
workerthreadtest.h
"
10
#include <QTest>
11
#include <QTimer>
12
#include <
workerthreadbase.h
>
13
14
QTEST_GUILESS_MAIN (
LC::Util::WorkerThreadTest
)
15
16
namespace
LC::Util
17
{
18
struct
Worker
19
{
20
QThread *
const
Main_
;
21
int
&
Val_
;
22
23
explicit
Worker
(
int
*val, QThread *main)
24
:
Main_
{ main }
25
,
Val_
{ *val }
26
{
27
}
28
29
void
Increment
(
int
n)
30
{
31
qDebug () << QThread::currentThread ();
32
QVERIFY (QThread::currentThread () !=
Main_
);
33
34
Val_
+= n;
35
}
36
37
void
Quit
(QEventLoop& loop)
38
{
39
QTimer::singleShot (0, &loop, &QEventLoop::quit);
40
}
41
};
42
43
void
WorkerThreadTest::testWorkerThread ()
44
{
45
QEventLoop loop;
46
auto
spin = [&loop]
47
{
48
QTimer::singleShot (100, &loop, &QEventLoop::quit);
49
loop.exec ();
50
};
51
52
qDebug () << Q_FUNC_INFO << QThread::currentThread ();
53
54
int
val = 0;
55
WorkerThread<Worker> worker { &val, QThread::currentThread () };
56
worker.SetQuitWait (1000);
57
worker.start ();
58
59
worker.SetPaused (
true
);
60
worker.ScheduleImpl (&
Worker::Increment
, 1);
61
worker.ScheduleImpl (&
Worker::Increment
, 2);
62
worker.ScheduleImpl (&
Worker::Increment
, 3);
63
64
spin ();
65
QCOMPARE (val, 0);
66
67
worker.SetPaused (
false
);
68
69
spin ();
70
QCOMPARE (val, 6);
71
72
worker.ScheduleImpl (&
Worker::Increment
, 10);
73
74
spin ();
75
QCOMPARE (val, 16);
76
}
77
}
LC::Util::WorkerThreadTest
Definition
workerthreadtest.h:16
LC::Util
Definition
icoreproxy.h:34
LC::Util::Worker::Worker
Worker(int *val, QThread *main)
Definition
workerthreadtest.cpp:23
LC::Util::Worker::Increment
void Increment(int n)
Definition
workerthreadtest.cpp:29
LC::Util::Worker::Main_
QThread *const Main_
Definition
workerthreadtest.cpp:20
LC::Util::Worker::Quit
void Quit(QEventLoop &loop)
Definition
workerthreadtest.cpp:37
LC::Util::Worker::Val_
int & Val_
Definition
workerthreadtest.cpp:21
workerthreadbase.h
workerthreadtest.h
src
util
threads
tests
workerthreadtest.cpp
Generated by
1.18.0