/[svn]/linuxsampler/trunk/src/testcases/ThreadTest.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/testcases/ThreadTest.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3552 - (show annotations) (download)
Tue Aug 6 13:18:59 2019 UTC (4 years, 8 months ago) by schoenebeck
File size: 3819 byte(s)
* Test cases: Fixed thread tests segfaulting on Linux.

1 #include "ThreadTest.h"
2
3 #include <iostream>
4
5 CPPUNIT_TEST_SUITE_REGISTRATION(ThreadTest);
6
7 using namespace std;
8
9 static ThreadTest::DummyThread dummythread;
10
11
12 // DummyThread
13
14 ThreadTest::DummyThread::DummyThread() : LinuxSampler::Thread(false, false, 0, -4) {
15 wasRunning = false;
16 }
17
18 int ThreadTest::DummyThread::Main() {
19 wasRunning = true;
20 while (true) {
21 someVariable = -1;
22 #if CONFIG_PTHREAD_TESTCANCEL
23 TestCancel();
24 #endif
25 }
26 return 0;
27 }
28
29
30 // HelperThread
31
32 ThreadTest::HelperThread::HelperThread(DummyThread* pDummyThread) : LinuxSampler::Thread(false, false, 0, -4) {
33 returnedFromDummyStop = false;
34 this->pDummyThread = pDummyThread;
35 }
36
37 int ThreadTest::HelperThread::Main() {
38 pDummyThread->StopThread();
39 pDummyThread->someVariable = 0; // we set this to another value than -1 so we can check if the DummyThread was still running
40 returnedFromDummyStop = true;
41 return 0;
42 }
43
44 bool ThreadTest::HelperThread::dummyThreadWasNotRunningAnymoreAfter_StopThread_call() {
45 return (pDummyThread->someVariable == 0);
46 }
47
48
49 // WaitingThread
50
51 ThreadTest::WaitingThread::WaitingThread() : LinuxSampler::Thread(false, false, 0, -4) {
52 }
53
54 int ThreadTest::WaitingThread::Main() {
55 while (true) condition.WaitIf(false);
56 }
57
58
59 // ThreadTest
60
61 void ThreadTest::printTestSuiteName() {
62 cout << "\b \nRunning Thread Tests: " << flush;
63 }
64
65 // Check if Thread class actually deploys a thread
66 void ThreadTest::testThreadRunning() {
67 dummythread.StartThread();
68 usleep(25000); // wait 25ms
69 CPPUNIT_ASSERT(dummythread.wasRunning);
70 CPPUNIT_ASSERT(dummythread.IsRunning());
71 }
72
73 // Check if SignalStopThread() method actually stops the thread
74 void ThreadTest::testSignalStopThread() {
75 CPPUNIT_ASSERT(dummythread.wasRunning);
76 CPPUNIT_ASSERT(dummythread.IsRunning());
77 dummythread.SignalStopThread();
78 usleep(80000); // wait 40ms
79 CPPUNIT_ASSERT(!dummythread.IsRunning());
80 }
81
82 // Check if the thread can be relaunched
83 void ThreadTest::testRelaunchThread() {
84 dummythread.StartThread();
85 usleep(25000); // wait 25ms
86 CPPUNIT_ASSERT(dummythread.wasRunning);
87 CPPUNIT_ASSERT(dummythread.IsRunning());
88 dummythread.StopThread();
89 usleep(25000); // wait 25ms
90 dummythread.wasRunning = false;
91 dummythread.StartThread();
92 usleep(25000); // wait 25ms
93 CPPUNIT_ASSERT(dummythread.wasRunning);
94 CPPUNIT_ASSERT(dummythread.IsRunning());
95 }
96
97 // 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.
98 void ThreadTest::testStopThread() {
99 HelperThread* phelper = new HelperThread(&dummythread);
100 phelper->StartThread(); // let the helper thread kill the dummy thread
101 usleep(25000); // wait 25ms
102 CPPUNIT_ASSERT(!dummythread.IsRunning());
103 CPPUNIT_ASSERT(phelper->returnedFromDummyStop);
104 bool wasnotrunning = phelper->dummyThreadWasNotRunningAnymoreAfter_StopThread_call();
105 if (wasnotrunning && phelper) delete phelper;
106 CPPUNIT_ASSERT(wasnotrunning);
107 }
108
109 // Check if the thread can be stopped even when it's waiting for a condition
110 void ThreadTest::testThreadKillableWhenWaiting() {
111 WaitingThread* pwaitingthread = new WaitingThread;
112 pwaitingthread->SignalStartThread();
113 usleep(150000); // wait 150ms
114 CPPUNIT_ASSERT(pwaitingthread->IsRunning());
115 pwaitingthread->SignalStopThread();
116 for (uint trials = 400; trials; trials--) {
117 bool success = !pwaitingthread->IsRunning();
118 if (success) {
119 usleep(15000); // wait 15ms
120 delete pwaitingthread;
121 CPPUNIT_ASSERT(true); // success
122 return;
123 }
124 else usleep(150000); // wait 150ms and try again
125 }
126 CPPUNIT_ASSERT(false); // failure
127 }

  ViewVC Help
Powered by ViewVC