/[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 503 by schoenebeck, Fri Apr 29 20:43:07 2005 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    
12  ThreadTest::DummyThread::DummyThread() : Thread(false, 0, -4) {  ThreadTest::DummyThread::DummyThread() : Thread(false, false, 0, -4) {
13      wasRunning = false;      wasRunning = false;
14  }  }
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    
22  // HelperThread  // HelperThread
23    
24  ThreadTest::HelperThread::HelperThread(DummyThread* pDummyThread) : Thread(false, 0, -4) {  ThreadTest::HelperThread::HelperThread(DummyThread* pDummyThread) : Thread(false, false, 0, -4) {
25      returnedFromDummyStop = false;      returnedFromDummyStop = false;
26      this->pDummyThread = pDummyThread;      this->pDummyThread = pDummyThread;
27  }  }
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
41    
42    ThreadTest::WaitingThread::WaitingThread() : Thread(false, false, 0, -4) {
43    }
44    
45    int ThreadTest::WaitingThread::Main() {
46        while (true) condition.WaitIf(false);
47    }
48    
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
57  void ThreadTest::testThreadRunning() {  void ThreadTest::testThreadRunning() {
58      dummythread.StartThread();      dummythread.StartThread();
59      usleep(25000); // wait 25ms      usleep(25000); // wait 25ms
# Line 39  void ThreadTest::testThreadRunning() { Line 61  void ThreadTest::testThreadRunning() {
61      CPPUNIT_ASSERT(dummythread.IsRunning());      CPPUNIT_ASSERT(dummythread.IsRunning());
62  }  }
63    
64    // Check if SignalStopThread() method actually stops the thread
65  void ThreadTest::testSignalStopThread() {  void ThreadTest::testSignalStopThread() {
66      dummythread.SignalStopThread();      dummythread.SignalStopThread();
67      usleep(40000); // wait 40ms      usleep(40000); // wait 40ms
68      CPPUNIT_ASSERT(!dummythread.IsRunning());      CPPUNIT_ASSERT(!dummythread.IsRunning());
69  }  }
70    
71    // Check if the thread can be relaunched
72  void ThreadTest::testRelaunchThread() {  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;      dummythread.wasRunning = false;
80      dummythread.StartThread();      dummythread.StartThread();
81      usleep(25000); // wait 25ms      usleep(25000); // wait 25ms
# Line 53  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 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
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  }  }

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

  ViewVC Help
Powered by ViewVC