/[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 210 by schoenebeck, Tue May 4 18:52:24 2004 UTC revision 211 by schoenebeck, Sun Jul 25 23:27:41 2004 UTC
# Line 4  Line 4 
4    
5  CPPUNIT_TEST_SUITE_REGISTRATION(ThreadTest);  CPPUNIT_TEST_SUITE_REGISTRATION(ThreadTest);
6    
7    using namespace std;
8    
9    
10  // DummyThread  // DummyThread
11    
# Line 13  ThreadTest::DummyThread::DummyThread() : Line 15  ThreadTest::DummyThread::DummyThread() :
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    
# Line 26  ThreadTest::HelperThread::HelperThread(D Line 28  ThreadTest::HelperThread::HelperThread(D
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    
# Line 42  int ThreadTest::WaitingThread::Main() { Line 49  int ThreadTest::WaitingThread::Main() {
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();
# Line 72  void ThreadTest::testRelaunchThread() { Line 83  void ThreadTest::testRelaunchThread() {
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  }  }

Legend:
Removed from v.210  
changed lines
  Added in v.211

  ViewVC Help
Powered by ViewVC