4 |
|
|
5 |
CPPUNIT_TEST_SUITE_REGISTRATION(ThreadTest); |
CPPUNIT_TEST_SUITE_REGISTRATION(ThreadTest); |
6 |
|
|
7 |
|
using namespace std; |
8 |
|
|
9 |
|
|
10 |
// DummyThread |
// DummyThread |
11 |
|
|
15 |
|
|
16 |
int ThreadTest::DummyThread::Main() { |
int ThreadTest::DummyThread::Main() { |
17 |
wasRunning = true; |
wasRunning = true; |
18 |
while (true) sleep (1000); |
while (true) someVariable = -1; |
19 |
} |
} |
20 |
|
|
21 |
|
|
28 |
|
|
29 |
int ThreadTest::HelperThread::Main() { |
int ThreadTest::HelperThread::Main() { |
30 |
pDummyThread->StopThread(); |
pDummyThread->StopThread(); |
31 |
|
pDummyThread->someVariable = 0; // we set this to another value than -1 so we can check if the DummyThread was still running |
32 |
returnedFromDummyStop = true; |
returnedFromDummyStop = true; |
33 |
} |
} |
34 |
|
|
35 |
|
bool ThreadTest::HelperThread::dummyThreadWasNotRunningAnymoreAfter_StopThread_call() { |
36 |
|
return (pDummyThread->someVariable == 0); |
37 |
|
} |
38 |
|
|
39 |
|
|
40 |
// WaitingThread |
// WaitingThread |
41 |
|
|
49 |
|
|
50 |
// ThreadTest |
// ThreadTest |
51 |
|
|
52 |
|
void ThreadTest::printTestSuiteName() { |
53 |
|
cout << "\b \nRunning Thread Tests: " << flush; |
54 |
|
} |
55 |
|
|
56 |
// Check if Thread class actually deploys a thread |
// Check if Thread class actually deploys a thread |
57 |
void ThreadTest::testThreadRunning() { |
void ThreadTest::testThreadRunning() { |
58 |
dummythread.StartThread(); |
dummythread.StartThread(); |
83 |
CPPUNIT_ASSERT(dummythread.IsRunning()); |
CPPUNIT_ASSERT(dummythread.IsRunning()); |
84 |
} |
} |
85 |
|
|
86 |
// Check if the StopThread() method actually stops the thread and doesn't freeze the calling thread which wants to stop it |
// Check if the StopThread() method actually stops the thread and doesn't freeze the calling thread which wants to stop it and also check if the thread was still running after the StopThread() method was called. |
87 |
void ThreadTest::testStopThread() { |
void ThreadTest::testStopThread() { |
88 |
HelperThread* phelper = new HelperThread(&dummythread); |
HelperThread* phelper = new HelperThread(&dummythread); |
89 |
phelper->StartThread(); // let the helper thread kill the dummy thread |
phelper->StartThread(); // let the helper thread kill the dummy thread |
90 |
usleep(25000); // wait 25ms |
usleep(25000); // wait 25ms |
91 |
CPPUNIT_ASSERT(!dummythread.IsRunning()); |
CPPUNIT_ASSERT(!dummythread.IsRunning()); |
92 |
CPPUNIT_ASSERT(phelper->returnedFromDummyStop); |
CPPUNIT_ASSERT(phelper->returnedFromDummyStop); |
93 |
if (phelper) delete phelper; |
bool wasnotrunning = phelper->dummyThreadWasNotRunningAnymoreAfter_StopThread_call(); |
94 |
|
if (wasnotrunning && phelper) delete phelper; |
95 |
|
CPPUNIT_ASSERT(wasnotrunning); |
96 |
} |
} |
97 |
|
|
98 |
// Check if the thread can be stopped even when it's waiting for a condition |
// Check if the thread can be stopped even when it's waiting for a condition |
99 |
void ThreadTest::testThreadKillableWhenWaiting() { |
void ThreadTest::testThreadKillableWhenWaiting() { |
100 |
WaitingThread waitingthread; |
WaitingThread* pwaitingthread = new WaitingThread; |
101 |
waitingthread.SignalStartThread(); |
pwaitingthread->SignalStartThread(); |
102 |
usleep(50000); // wait 50ms |
usleep(150000); // wait 150ms |
103 |
CPPUNIT_ASSERT(waitingthread.IsRunning()); |
CPPUNIT_ASSERT(pwaitingthread->IsRunning()); |
104 |
waitingthread.SignalStopThread(); |
pwaitingthread->SignalStopThread(); |
105 |
usleep(40000); // wait 40ms |
for (uint trials = 400; trials; trials--) { |
106 |
CPPUNIT_ASSERT(!waitingthread.IsRunning()); |
bool success = !pwaitingthread->IsRunning(); |
107 |
|
if (success) { |
108 |
|
usleep(15000); // wait 15ms |
109 |
|
delete pwaitingthread; |
110 |
|
CPPUNIT_ASSERT(true); // success |
111 |
|
return; |
112 |
|
} |
113 |
|
else usleep(150000); // wait 150ms and try again |
114 |
|
} |
115 |
|
CPPUNIT_ASSERT(false); // failure |
116 |
} |
} |