/[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 211 - (show annotations) (download)
Sun Jul 25 23:27:41 2004 UTC (19 years, 9 months ago) by schoenebeck
File size: 3537 byte(s)
* src/linuxsampler.cpp: tidied up a bit, "initialization completed"
  message shown only after the server is actually running
* src/testcases/: print the name of each test suite before running the
  tests of the suite, added first tests against the LSCP server using a
  socket connection to the LSCP server (tests for the following LSCP
  commands: "ADD CHANNEL", "GET CHANNELS", "REMOVE CHANNEL")

1 #include "ThreadTest.h"
2
3 #include <iostream>
4
5 CPPUNIT_TEST_SUITE_REGISTRATION(ThreadTest);
6
7 using namespace std;
8
9
10 // DummyThread
11
12 ThreadTest::DummyThread::DummyThread() : Thread(false, 0, -4) {
13 wasRunning = false;
14 }
15
16 int ThreadTest::DummyThread::Main() {
17 wasRunning = true;
18 while (true) someVariable = -1;
19 }
20
21
22 // HelperThread
23
24 ThreadTest::HelperThread::HelperThread(DummyThread* pDummyThread) : Thread(false, 0, -4) {
25 returnedFromDummyStop = false;
26 this->pDummyThread = pDummyThread;
27 }
28
29 int ThreadTest::HelperThread::Main() {
30 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;
33 }
34
35 bool ThreadTest::HelperThread::dummyThreadWasNotRunningAnymoreAfter_StopThread_call() {
36 return (pDummyThread->someVariable == 0);
37 }
38
39
40 // WaitingThread
41
42 ThreadTest::WaitingThread::WaitingThread() : Thread(false, 0, -4) {
43 }
44
45 int ThreadTest::WaitingThread::Main() {
46 while (true) condition.WaitIf(false);
47 }
48
49
50 // ThreadTest
51
52 void ThreadTest::printTestSuiteName() {
53 cout << "\b \nRunning Thread Tests: " << flush;
54 }
55
56 // Check if Thread class actually deploys a thread
57 void ThreadTest::testThreadRunning() {
58 dummythread.StartThread();
59 usleep(25000); // wait 25ms
60 CPPUNIT_ASSERT(dummythread.wasRunning);
61 CPPUNIT_ASSERT(dummythread.IsRunning());
62 }
63
64 // Check if SignalStopThread() method actually stops the thread
65 void ThreadTest::testSignalStopThread() {
66 dummythread.SignalStopThread();
67 usleep(40000); // wait 40ms
68 CPPUNIT_ASSERT(!dummythread.IsRunning());
69 }
70
71 // Check if the thread can be relaunched
72 void ThreadTest::testRelaunchThread() {
73 dummythread.StartThread();
74 usleep(25000); // wait 25ms
75 CPPUNIT_ASSERT(dummythread.wasRunning);
76 CPPUNIT_ASSERT(dummythread.IsRunning());
77 dummythread.StopThread();
78 usleep(25000); // wait 25ms
79 dummythread.wasRunning = false;
80 dummythread.StartThread();
81 usleep(25000); // wait 25ms
82 CPPUNIT_ASSERT(dummythread.wasRunning);
83 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 and also check if the thread was still running after the StopThread() method was called.
87 void ThreadTest::testStopThread() {
88 HelperThread* phelper = new HelperThread(&dummythread);
89 phelper->StartThread(); // let the helper thread kill the dummy thread
90 usleep(25000); // wait 25ms
91 CPPUNIT_ASSERT(!dummythread.IsRunning());
92 CPPUNIT_ASSERT(phelper->returnedFromDummyStop);
93 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
99 void ThreadTest::testThreadKillableWhenWaiting() {
100 WaitingThread* pwaitingthread = new WaitingThread;
101 pwaitingthread->SignalStartThread();
102 usleep(150000); // wait 150ms
103 CPPUNIT_ASSERT(pwaitingthread->IsRunning());
104 pwaitingthread->SignalStopThread();
105 for (uint trials = 400; trials; trials--) {
106 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 }

  ViewVC Help
Powered by ViewVC