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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 63 - (hide annotations) (download)
Tue May 4 18:52:24 2004 UTC (19 years, 11 months ago) by schoenebeck
File size: 2593 byte(s)
* src/common/Thread.cpp: threads are now stoppable even if they are
  waiting for a condition
* src/common/Condition.cpp: fixed little misbehavior of Set() method,
  which locked the Condition object on return
* src/testcases: added a couple of new unit tests (against classes
  'Mutex', 'Condition' and 'Thread')

1 schoenebeck 57 #include "ThreadTest.h"
2    
3     #include <iostream>
4    
5     CPPUNIT_TEST_SUITE_REGISTRATION(ThreadTest);
6    
7    
8     // DummyThread
9    
10     ThreadTest::DummyThread::DummyThread() : Thread(false, 0, -4) {
11     wasRunning = false;
12     }
13    
14     int ThreadTest::DummyThread::Main() {
15     wasRunning = true;
16     while (true) sleep (1000);
17     }
18    
19    
20     // HelperThread
21    
22     ThreadTest::HelperThread::HelperThread(DummyThread* pDummyThread) : Thread(false, 0, -4) {
23     returnedFromDummyStop = false;
24     this->pDummyThread = pDummyThread;
25     }
26    
27     int ThreadTest::HelperThread::Main() {
28     pDummyThread->StopThread();
29     returnedFromDummyStop = true;
30     }
31    
32    
33 schoenebeck 63 // WaitingThread
34    
35     ThreadTest::WaitingThread::WaitingThread() : Thread(false, 0, -4) {
36     }
37    
38     int ThreadTest::WaitingThread::Main() {
39     while (true) condition.WaitIf(false);
40     }
41    
42    
43 schoenebeck 57 // ThreadTest
44    
45 schoenebeck 63 // Check if Thread class actually deploys a thread
46 schoenebeck 57 void ThreadTest::testThreadRunning() {
47     dummythread.StartThread();
48     usleep(25000); // wait 25ms
49     CPPUNIT_ASSERT(dummythread.wasRunning);
50     CPPUNIT_ASSERT(dummythread.IsRunning());
51     }
52    
53 schoenebeck 63 // Check if SignalStopThread() method actually stops the thread
54 schoenebeck 57 void ThreadTest::testSignalStopThread() {
55     dummythread.SignalStopThread();
56     usleep(40000); // wait 40ms
57     CPPUNIT_ASSERT(!dummythread.IsRunning());
58     }
59    
60 schoenebeck 63 // Check if the thread can be relaunched
61 schoenebeck 57 void ThreadTest::testRelaunchThread() {
62 schoenebeck 63 dummythread.StartThread();
63     usleep(25000); // wait 25ms
64     CPPUNIT_ASSERT(dummythread.wasRunning);
65     CPPUNIT_ASSERT(dummythread.IsRunning());
66     dummythread.StopThread();
67     usleep(25000); // wait 25ms
68 schoenebeck 57 dummythread.wasRunning = false;
69     dummythread.StartThread();
70     usleep(25000); // wait 25ms
71     CPPUNIT_ASSERT(dummythread.wasRunning);
72     CPPUNIT_ASSERT(dummythread.IsRunning());
73     }
74    
75 schoenebeck 63 // Check if the StopThread() method actually stops the thread and doesn't freeze the calling thread which wants to stop it
76 schoenebeck 57 void ThreadTest::testStopThread() {
77     HelperThread* phelper = new HelperThread(&dummythread);
78     phelper->StartThread(); // let the helper thread kill the dummy thread
79     usleep(25000); // wait 25ms
80     CPPUNIT_ASSERT(!dummythread.IsRunning());
81     CPPUNIT_ASSERT(phelper->returnedFromDummyStop);
82     if (phelper) delete phelper;
83     }
84 schoenebeck 63
85     // Check if the thread can be stopped even when it's waiting for a condition
86     void ThreadTest::testThreadKillableWhenWaiting() {
87     WaitingThread waitingthread;
88     waitingthread.SignalStartThread();
89     usleep(50000); // wait 50ms
90     CPPUNIT_ASSERT(waitingthread.IsRunning());
91     waitingthread.SignalStopThread();
92     usleep(40000); // wait 40ms
93     CPPUNIT_ASSERT(!waitingthread.IsRunning());
94     }

  ViewVC Help
Powered by ViewVC