/[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 1221 - (show annotations) (download)
Wed Jun 6 18:50:03 2007 UTC (16 years, 10 months ago) by schoenebeck
File size: 3734 byte(s)
* fixed several issues in fundamental "Thread" class: set scheduling
  policy and priority on thread level, set a minimum stack size for
  thread (TODO: a reasonable value yet to be tested), bugfix: non-RT
  threads simply inherited properties of starting thread instead of
  setting their own policy and priority
* updated and fixed test cases (haven't been touched in a while, but
  are now all running successfully through all cases)

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

  ViewVC Help
Powered by ViewVC