/[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 63 - (show 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 #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 // 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 // ThreadTest
44
45 // Check if Thread class actually deploys a thread
46 void ThreadTest::testThreadRunning() {
47 dummythread.StartThread();
48 usleep(25000); // wait 25ms
49 CPPUNIT_ASSERT(dummythread.wasRunning);
50 CPPUNIT_ASSERT(dummythread.IsRunning());
51 }
52
53 // Check if SignalStopThread() method actually stops the thread
54 void ThreadTest::testSignalStopThread() {
55 dummythread.SignalStopThread();
56 usleep(40000); // wait 40ms
57 CPPUNIT_ASSERT(!dummythread.IsRunning());
58 }
59
60 // Check if the thread can be relaunched
61 void ThreadTest::testRelaunchThread() {
62 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 dummythread.wasRunning = false;
69 dummythread.StartThread();
70 usleep(25000); // wait 25ms
71 CPPUNIT_ASSERT(dummythread.wasRunning);
72 CPPUNIT_ASSERT(dummythread.IsRunning());
73 }
74
75 // Check if the StopThread() method actually stops the thread and doesn't freeze the calling thread which wants to stop it
76 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
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