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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 57 by schoenebeck, Sun May 2 17:45:43 2004 UTC revision 1221 by schoenebeck, Wed Jun 6 18:50:03 2007 UTC
# Line 4  Line 4 
4    
5  CPPUNIT_TEST_SUITE_REGISTRATION(ThreadTest);  CPPUNIT_TEST_SUITE_REGISTRATION(ThreadTest);
6    
7    using namespace std;
8    
9    static ThreadTest::DummyThread dummythread;
10    
11    
12  // DummyThread  // DummyThread
13    
14  ThreadTest::DummyThread::DummyThread() : Thread(false, 0, -4) {  ThreadTest::DummyThread::DummyThread() : LinuxSampler::Thread(false, false, 0, -4) {
15      wasRunning = false;      wasRunning = false;
16  }  }
17    
18  int ThreadTest::DummyThread::Main() {  int ThreadTest::DummyThread::Main() {
19      wasRunning = true;      wasRunning = true;
20      while (true) sleep (1000);      while (true) someVariable = -1;
21  }  }
22    
23    
24  // HelperThread  // HelperThread
25    
26  ThreadTest::HelperThread::HelperThread(DummyThread* pDummyThread) : Thread(false, 0, -4) {  ThreadTest::HelperThread::HelperThread(DummyThread* pDummyThread) : LinuxSampler::Thread(false, false, 0, -4) {
27      returnedFromDummyStop = false;      returnedFromDummyStop = false;
28      this->pDummyThread = pDummyThread;      this->pDummyThread = pDummyThread;
29  }  }
30    
31  int ThreadTest::HelperThread::Main() {  int ThreadTest::HelperThread::Main() {
32      pDummyThread->StopThread();      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;      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  // 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() {  void ThreadTest::testThreadRunning() {
60      dummythread.StartThread();      dummythread.StartThread();
61      usleep(25000); // wait 25ms      usleep(25000); // wait 25ms
# Line 39  void ThreadTest::testThreadRunning() { Line 63  void ThreadTest::testThreadRunning() {
63      CPPUNIT_ASSERT(dummythread.IsRunning());      CPPUNIT_ASSERT(dummythread.IsRunning());
64  }  }
65    
66    // Check if SignalStopThread() method actually stops the thread
67  void ThreadTest::testSignalStopThread() {  void ThreadTest::testSignalStopThread() {
68        CPPUNIT_ASSERT(dummythread.wasRunning);
69        CPPUNIT_ASSERT(dummythread.IsRunning());
70      dummythread.SignalStopThread();      dummythread.SignalStopThread();
71      usleep(40000); // wait 40ms      usleep(80000); // wait 40ms
72      CPPUNIT_ASSERT(!dummythread.IsRunning());      CPPUNIT_ASSERT(!dummythread.IsRunning());
73  }  }
74    
75    // Check if the thread can be relaunched
76  void ThreadTest::testRelaunchThread() {  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;      dummythread.wasRunning = false;
84      dummythread.StartThread();      dummythread.StartThread();
85      usleep(25000); // wait 25ms      usleep(25000); // wait 25ms
# Line 53  void ThreadTest::testRelaunchThread() { Line 87  void ThreadTest::testRelaunchThread() {
87      CPPUNIT_ASSERT(dummythread.IsRunning());      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() {  void ThreadTest::testStopThread() {
92      HelperThread* phelper = new HelperThread(&dummythread);      HelperThread* phelper = new HelperThread(&dummythread);
93      phelper->StartThread(); // let the helper thread kill the dummy thread      phelper->StartThread(); // let the helper thread kill the dummy thread
94      usleep(25000); // wait 25ms      usleep(25000); // wait 25ms
95      CPPUNIT_ASSERT(!dummythread.IsRunning());      CPPUNIT_ASSERT(!dummythread.IsRunning());
96      CPPUNIT_ASSERT(phelper->returnedFromDummyStop);      CPPUNIT_ASSERT(phelper->returnedFromDummyStop);
97      if (phelper) delete phelper;      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  }  }

Legend:
Removed from v.57  
changed lines
  Added in v.1221

  ViewVC Help
Powered by ViewVC