bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / osl / process / osl_Thread.cxx
blob403490d66a150519cdd66e88f6f5b1bc3e3ecf27
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <algorithm>
21 #ifdef WNT
22 #include <windows.h>
23 #else
24 #include <unistd.h>
25 #include <time.h>
26 #endif
28 // include files
30 #include <sal/types.h>
32 #include <rtl/string.hxx>
34 #include <rtl/strbuf.hxx>
36 #include <osl/thread.hxx>
38 #include <osl/mutex.hxx>
39 #include <osl/time.h>
41 #include <string.h>
43 #include <cppunit/TestFixture.h>
44 #include <cppunit/extensions/HelperMacros.h>
45 #include <cppunit/plugin/TestPlugIn.h>
47 #define t_print printf
49 using namespace osl;
51 using ::rtl::OString;
53 // Small stopwatch
54 class StopWatch {
55 TimeValue t1,t2; // Start and stoptime
57 protected:
58 sal_Int32 m_nNanoSec;
59 sal_Int32 m_nSeconds;
61 bool m_bIsValid; // TRUE, when started and stopped
62 bool m_bIsRunning; // TRUE, when started
64 public:
65 StopWatch();
66 ~StopWatch() {}
68 void start(); // Starts time
69 void stop(); // Stops time
71 double getSeconds() const;
72 double getTenthSec() const;
75 // ================================= Stop Watch =================================
77 // A small stopwatch for internal use
78 // (c) Lars Langhans 29.12.1996 22:10
80 StopWatch::StopWatch()
81 : m_nNanoSec(0)
82 , m_nSeconds(0)
83 , m_bIsValid(false)
84 , m_bIsRunning(false)
86 t1.Seconds = 0;
87 t1.Nanosec = 0;
88 t2.Seconds = 0;
89 t2.Nanosec = 0;
92 void StopWatch::start()
94 // pre: %
95 // post: Start Timer
97 m_bIsValid = false;
98 m_bIsRunning = true;
99 osl_getSystemTime( &t1 );
100 t_print("# %u %u nsecs\n", (unsigned)t1.Seconds, (unsigned)t1.Nanosec);
101 // gettimeofday(&t1, 0);
104 void StopWatch::stop()
106 // pre: Timer should be started
107 // post: Timer will stopped
109 // gettimeofday(&t2, 0); // Ask timer
110 osl_getSystemTime( &t2 );
111 t_print("# %u %u nsecs\n", (unsigned) t2.Seconds, (unsigned) t2.Nanosec);
113 if (m_bIsRunning)
114 { // check if started.
115 m_nSeconds = static_cast<sal_Int32>(t2.Seconds) - static_cast<sal_Int32>(t1.Seconds);
116 if ( t2.Nanosec > t1.Nanosec )
117 m_nNanoSec = static_cast<sal_Int32>(t2.Nanosec) - static_cast<sal_Int32>(t1.Nanosec);
118 else
120 m_nNanoSec = 1000000000 + static_cast<sal_Int32>(t2.Nanosec) - static_cast<sal_Int32>(t1.Nanosec);
121 m_nSeconds -= 1;
123 t_print("# %u %u nsecs\n", (unsigned) m_nSeconds, (unsigned) m_nNanoSec );
124 //if (m_nNanoSec < 0)
126 //m_nNanoSec += 1000000000;
127 //m_nSeconds -= 1;
129 m_bIsValid = true;
130 m_bIsRunning = false;
134 double StopWatch::getSeconds() const
136 // pre: valid = TRUE
137 // BACK: time in seconds
139 double nValue = 0.0;
140 if (m_bIsValid)
142 nValue = double(m_nNanoSec) / 1000000000.0 + m_nSeconds; // milli micro nano
144 return nValue;
147 double StopWatch::getTenthSec() const
149 double nValue = 0.0;
150 if (m_bIsValid)
152 nValue = double(m_nNanoSec) / 100000000.0 + m_nSeconds * 10;
154 return nValue ;
157 template <class T>
158 class ThreadSafeValue
160 T m_nFlag;
161 Mutex m_aMutex;
162 public:
163 ThreadSafeValue(T n = 0): m_nFlag(n) {}
164 T getValue()
166 //block if already acquired by another thread.
167 osl::MutexGuard g(m_aMutex);
168 return m_nFlag;
170 void addValue(T n)
172 //only one thread operate on the flag.
173 osl::MutexGuard g(m_aMutex);
174 m_nFlag += n;
176 void acquire() {m_aMutex.acquire();}
177 void release() {m_aMutex.release();}
180 namespace ThreadHelper
182 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec)
184 #ifdef WNT
185 Sleep(_nTenthSec * 100 );
186 #else
187 TimeValue nTV;
188 nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
189 nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
190 osl_waitThread(&nTV);
191 #endif
194 void outputPriority(oslThreadPriority const& _aPriority)
196 // LLA: output the priority
197 if (_aPriority == osl_Thread_PriorityHighest)
199 t_print("Prio is High\n");
201 else if (_aPriority == osl_Thread_PriorityAboveNormal)
203 t_print("Prio is above normal\n");
205 else if (_aPriority == osl_Thread_PriorityNormal)
207 t_print("Prio is normal\n");
209 else if (_aPriority == osl_Thread_PriorityBelowNormal)
211 t_print("Prio is below normal\n");
213 else if (_aPriority == osl_Thread_PriorityLowest)
215 t_print("Prio is lowest\n");
217 else
219 t_print("Prio is unknown\n");
224 /** Simple thread for testing Thread-create.
226 Just add 1 of value 0, and after running, result is 1.
228 class myThread : public Thread
230 ThreadSafeValue<sal_Int32> m_aFlag;
231 public:
232 sal_Int32 getValue() { return m_aFlag.getValue(); }
233 protected:
234 /** guarded value which initialized 0
236 @see ThreadSafeValue
238 void SAL_CALL run() SAL_OVERRIDE
240 while(schedule())
242 m_aFlag.addValue(1);
243 ThreadHelper::thread_sleep_tenth_sec(1);
247 public:
249 virtual void SAL_CALL suspend() SAL_OVERRIDE
251 m_aFlag.acquire();
252 ::osl::Thread::suspend();
253 m_aFlag.release();
256 virtual ~myThread()
258 if (isRunning())
260 t_print("error: not terminated.\n");
266 /** Thread which has a flag add 1 every second until 20
268 class OCountThread : public Thread
270 ThreadSafeValue<sal_Int32> m_aFlag;
271 public:
272 OCountThread()
274 m_nWaitSec = 0;
275 t_print("new OCountThread thread %u!\n", (unsigned) getIdentifier());
277 sal_Int32 getValue() { return m_aFlag.getValue(); }
279 void setWait(sal_Int32 nSec)
281 m_nWaitSec = nSec;
282 //m_bWait = sal_True;
285 virtual void SAL_CALL suspend() SAL_OVERRIDE
287 m_aFlag.acquire();
288 ::osl::Thread::suspend();
289 m_aFlag.release();
292 protected:
293 //sal_Bool m_bWait;
294 sal_Int32 m_nWaitSec;
296 void SAL_CALL run() SAL_OVERRIDE
298 /// if the thread should terminate, schedule return false
299 while (m_aFlag.getValue() < 20 && schedule())
301 m_aFlag.addValue(1);
302 ThreadHelper::thread_sleep_tenth_sec(1);
304 if (m_nWaitSec != 0)
306 TimeValue nTV;
307 nTV.Seconds = m_nWaitSec / 10 ;
308 nTV.Nanosec = ( m_nWaitSec%10 ) * 100000000 ;
309 wait( nTV );
310 m_nWaitSec = 0;
314 void SAL_CALL onTerminated() SAL_OVERRIDE
316 t_print("normally terminate this thread %u!\n", (unsigned) getIdentifier());
318 public:
320 virtual ~OCountThread()
322 if (isRunning())
324 t_print("error: not terminated.\n");
330 /** no call schedule in the run method
332 class ONoScheduleThread : public Thread
334 ThreadSafeValue<sal_Int32> m_aFlag;
335 public:
336 sal_Int32 getValue() { return m_aFlag.getValue(); }
338 virtual void SAL_CALL suspend() SAL_OVERRIDE
340 m_aFlag.acquire();
341 ::osl::Thread::suspend();
342 m_aFlag.release();
344 protected:
345 void SAL_CALL run() SAL_OVERRIDE
347 while (m_aFlag.getValue() < 10)
349 m_aFlag.addValue(1);
350 ThreadHelper::thread_sleep_tenth_sec(1);
353 void SAL_CALL onTerminated() SAL_OVERRIDE
355 t_print("normally terminate this thread %u!\n", (unsigned) getIdentifier());
357 public:
358 ONoScheduleThread()
360 t_print("new thread id %u!\n", (unsigned) getIdentifier());
362 virtual ~ONoScheduleThread()
364 if (isRunning())
366 t_print("error: not terminated.\n");
374 class OAddThread : public Thread
376 ThreadSafeValue<sal_Int32> m_aFlag;
377 public:
378 //oslThreadIdentifier m_id, m_CurId;
379 OAddThread(){}
380 sal_Int32 getValue() { return m_aFlag.getValue(); }
382 virtual void SAL_CALL suspend() SAL_OVERRIDE
384 m_aFlag.acquire();
385 ::osl::Thread::suspend();
386 m_aFlag.release();
388 protected:
389 void SAL_CALL run() SAL_OVERRIDE
391 //if the thread should terminate, schedule return false
392 while (schedule())
394 m_aFlag.addValue(1);
397 void SAL_CALL onTerminated() SAL_OVERRIDE
399 // t_print("normally terminate this thread %d!\n", getIdentifier());
401 public:
403 virtual ~OAddThread()
405 if (isRunning())
407 // t_print("error: not terminated.\n");
413 namespace osl_Thread
416 void resumeAndWaitThread(Thread* _pThread)
418 // This function starts a thread, wait a second and suspends the thread
419 // Due to the fact, that a suspend and never run thread never really exists.
421 // Note: on UNX, after createSuspended, and then terminate the thread, it performs well;
422 // while on Windows, after createSuspended, the thread can not terminate, wait endlessly,
423 // so here call resume at first, then call terminate.
424 #ifdef WNT
425 t_print("resumeAndWaitThread\n");
426 _pThread->resume();
427 ThreadHelper::thread_sleep_tenth_sec(1);
428 #else
429 _pThread->resume();
430 #endif
433 // kill a running thread and join it, if it has terminated, do nothing
434 void termAndJoinThread(Thread* _pThread)
436 _pThread->terminate();
438 // LLA: Windows feature???, a suspended thread can not terminated, so we have to weak it up
439 #ifdef WNT
440 _pThread->resume();
441 ThreadHelper::thread_sleep_tenth_sec(1);
442 #endif
443 t_print("#wait for join.\n");
444 _pThread->join();
446 /** Test of the osl::Thread::create method
449 class create : public CppUnit::TestFixture
451 public:
453 // initialise your test code values here.
454 void setUp() SAL_OVERRIDE
458 void tearDown() SAL_OVERRIDE
462 /** Simple create a thread.
464 Create a simple thread, it just does add 1 to value(which initialized 0),
465 if the thread run, the value should be 1.
467 void create_001()
469 myThread* newthread = new myThread();
470 bool bRes = newthread->create();
471 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!\n", bRes);
473 ThreadHelper::thread_sleep_tenth_sec(1); // wait short
474 bool isRunning = newthread->isRunning(); // check if thread is running
475 /// wait for the new thread to assure it has run
476 ThreadHelper::thread_sleep_tenth_sec(3);
477 sal_Int32 nValue = newthread->getValue();
478 /// to assure the new thread has terminated
479 termAndJoinThread(newthread);
480 delete newthread;
482 t_print(" nValue = %d\n", (int) nValue);
483 t_print("isRunning = %s\n", isRunning ? "true" : "false");
485 CPPUNIT_ASSERT_MESSAGE(
486 "Creates a new thread",
487 nValue >= 1 && isRunning
492 /** only one running thread per instance, return false if create secondly
494 void create_002()
496 myThread* newthread = new myThread();
497 bool res1 = newthread->create();
498 bool res2 = newthread->create();
499 t_print("In non pro, an assertion should occurred. This behaviour is right.\n");
500 termAndJoinThread(newthread);
501 delete newthread;
503 CPPUNIT_ASSERT_MESSAGE(
504 "Creates a new thread: can not create two threads per instance",
505 res1 && !res2
510 CPPUNIT_TEST_SUITE(create);
511 CPPUNIT_TEST(create_001);
512 CPPUNIT_TEST(create_002);
513 CPPUNIT_TEST_SUITE_END();
514 }; // class create
516 /** Test of the osl::Thread::createSuspended method
518 class createSuspended : public CppUnit::TestFixture
520 public:
521 // initialise your test code values here.
522 void setUp() SAL_OVERRIDE
526 void tearDown() SAL_OVERRIDE
530 /** Create a suspended thread, use the same class as create_001
532 after create, wait enough time, check the value, if it's still the initial value, pass
534 void createSuspended_001()
536 myThread* newthread = new myThread();
537 bool bRes = newthread->createSuspended();
538 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!", bRes);
540 ThreadHelper::thread_sleep_tenth_sec(1);
541 bool isRunning = newthread->isRunning();
542 ThreadHelper::thread_sleep_tenth_sec(3);
543 sal_Int32 nValue = newthread->getValue();
545 resumeAndWaitThread(newthread);
547 termAndJoinThread(newthread);
548 delete newthread;
550 CPPUNIT_ASSERT_MESSAGE(
551 "Creates a new suspended thread",
552 nValue == 0 && isRunning
556 void createSuspended_002()
558 myThread* newthread = new myThread();
559 bool res1 = newthread->createSuspended();
560 bool res2 = newthread->createSuspended();
562 resumeAndWaitThread(newthread);
564 termAndJoinThread(newthread);
566 delete newthread;
568 CPPUNIT_ASSERT_MESSAGE(
569 "Creates a new thread: can not create two threads per instance",
570 res1 && !res2
574 CPPUNIT_TEST_SUITE(createSuspended);
575 CPPUNIT_TEST(createSuspended_001);
576 // LLA: Deadlocked!!!
577 CPPUNIT_TEST(createSuspended_002);
578 CPPUNIT_TEST_SUITE_END();
579 }; // class createSuspended
581 /** when the count value equal to or more than 3, suspend the thread.
583 void suspendCountThread(OCountThread* _pCountThread)
585 sal_Int32 nValue = 0;
586 while (true)
588 nValue = _pCountThread->getValue();
589 if (nValue >= 3)
591 _pCountThread->suspend();
592 break;
597 /** Test of the osl::Thread::suspend method
599 class suspend : public CppUnit::TestFixture
601 public:
602 // initialise your test code values here.
603 void setUp() SAL_OVERRIDE
607 void tearDown() SAL_OVERRIDE
611 /** Use a thread which has a flag added 1 every second
613 ALGORITHM:
614 create the thread, after running special time, record value of flag, then suspend it,
615 wait a long time, check the flag, if it remains unchanged during suspending
617 void suspend_001()
619 OCountThread* aCountThread = new OCountThread();
620 bool bRes = aCountThread->create();
621 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
622 // the thread run for some seconds, but not terminate
623 suspendCountThread( aCountThread );
625 // the value just after calling suspend
626 sal_Int32 nValue = aCountThread->getValue(); // (2)
628 ThreadHelper::thread_sleep_tenth_sec(3);
630 // the value after waiting 3 seconds
631 sal_Int32 nLaterValue = aCountThread->getValue(); // (3)
633 resumeAndWaitThread(aCountThread);
634 termAndJoinThread(aCountThread);
635 delete aCountThread;
637 CPPUNIT_ASSERT_MESSAGE(
638 "Suspend the thread",
639 bRes && nValue == nLaterValue
644 CPPUNIT_TEST_SUITE(suspend);
645 CPPUNIT_TEST(suspend_001);
646 // LLA: Deadlocked!!!
647 // CPPUNIT_TEST(createSuspended_002);
648 CPPUNIT_TEST_SUITE_END();
649 }; // class suspend
651 /** Test of the osl::Thread::resume method
653 class resume : public CppUnit::TestFixture
655 public:
656 // initialise your test code values here.
657 void setUp() SAL_OVERRIDE
661 void tearDown() SAL_OVERRIDE
665 /** check if the thread run samely as usual after suspend and resume
667 ALGORITHM:
668 compare the values before and after suspend, they should be same,
669 then compare values before and after resume, the difference should be same as the sleep seconds number
671 void resume_001()
673 OCountThread* pCountThread = new OCountThread();
674 bool bRes = pCountThread->create();
675 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
677 suspendCountThread(pCountThread);
679 sal_Int32 nSuspendValue = pCountThread->getValue(); // (2)
680 // suspend for 3 seconds
681 ThreadHelper::thread_sleep_tenth_sec(3);
682 pCountThread->resume();
684 ThreadHelper::thread_sleep_tenth_sec(3);
685 sal_Int32 nResumeValue = pCountThread->getValue();
687 ThreadHelper::thread_sleep_tenth_sec(3);
688 sal_Int32 nLaterValue = pCountThread->getValue();
690 termAndJoinThread(pCountThread);
691 delete pCountThread;
693 t_print("SuspendValue: %d\n", (int) nSuspendValue);
694 t_print("ResumeValue: %d\n", (int) nResumeValue);
695 t_print("LaterValue: %d\n", (int) nLaterValue);
697 /* LLA: this assumption is no longer relevant: nResumeValue == nSuspendValue && */
698 CPPUNIT_ASSERT_MESSAGE(
699 "Suspend then resume the thread",
700 nLaterValue >= 9 &&
701 nResumeValue > nSuspendValue &&
702 nLaterValue > nResumeValue
707 /** Create a suspended thread then resume, check if the thread has run
709 void resume_002()
711 myThread* newthread = new myThread();
712 bool bRes = newthread->createSuspended();
713 CPPUNIT_ASSERT_MESSAGE ( "Can't create thread!", bRes );
715 newthread->resume();
716 ThreadHelper::thread_sleep_tenth_sec(2);
717 sal_Int32 nValue = newthread->getValue();
719 termAndJoinThread(newthread);
720 delete newthread;
722 t_print(" nValue = %d\n", (int) nValue);
724 CPPUNIT_ASSERT_MESSAGE(
725 "Creates a suspended thread, then resume",
726 nValue >= 1
730 CPPUNIT_TEST_SUITE(resume);
731 CPPUNIT_TEST(resume_001);
732 CPPUNIT_TEST(resume_002);
733 CPPUNIT_TEST_SUITE_END();
734 }; // class resume
736 /** Test of the osl::Thread::terminate method
738 class terminate : public CppUnit::TestFixture
740 public:
741 // initialise your test code values here.
742 void setUp() SAL_OVERRIDE
746 void tearDown() SAL_OVERRIDE
750 /** Check after call terminate if the running thread running go on executing
752 ALGORITHM:
753 before and after call terminate, the values should be the same
755 void terminate_001()
757 OCountThread* aCountThread = new OCountThread();
758 bool bRes = aCountThread->create();
759 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
761 ThreadHelper::thread_sleep_tenth_sec(2);
762 sal_Int32 nValue = aCountThread->getValue();
763 aCountThread->terminate();
764 ThreadHelper::thread_sleep_tenth_sec(2);
765 sal_Int32 nLaterValue = aCountThread->getValue();
767 // isRunning should be false after terminate
768 bool isRunning = aCountThread->isRunning();
769 aCountThread->join();
770 delete aCountThread;
772 t_print(" nValue = %d\n", (int) nValue);
773 t_print("nLaterValue = %d\n", (int) nLaterValue);
775 CPPUNIT_ASSERT_MESSAGE(
776 "Terminate the thread",
777 !isRunning && nLaterValue >= nValue
780 /** Check if a suspended thread will terminate after call terminate, different on w32 and on UNX
782 void terminate_002()
784 OCountThread* aCountThread = new OCountThread();
785 bool bRes = aCountThread->create();
786 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
788 ThreadHelper::thread_sleep_tenth_sec(1);
789 suspendCountThread(aCountThread);
790 sal_Int32 nValue = aCountThread->getValue();
792 // seems a suspended thread can not be terminated on W32, while on Solaris can
793 resumeAndWaitThread(aCountThread);
795 ThreadHelper::thread_sleep_tenth_sec(2);
797 termAndJoinThread(aCountThread);
798 sal_Int32 nLaterValue = aCountThread->getValue();
799 delete aCountThread;
801 t_print(" nValue = %d\n", (int) nValue);
802 t_print("nLaterValue = %d\n", (int) nLaterValue);
804 CPPUNIT_ASSERT_MESSAGE(
805 "Suspend then resume the thread",
806 nLaterValue > nValue );
809 CPPUNIT_TEST_SUITE(terminate);
810 CPPUNIT_TEST(terminate_001);
811 CPPUNIT_TEST(terminate_002);
812 CPPUNIT_TEST_SUITE_END();
813 }; // class terminate
815 /** Test of the osl::Thread::join method
817 class join : public CppUnit::TestFixture
819 public:
820 // initialise your test code values here.
821 void setUp() SAL_OVERRIDE
825 void tearDown() SAL_OVERRIDE
829 /** Check after call terminate if the thread running function will not go on executing
831 the next statement after join will not exec before the thread terminate
832 ALGORITHM:
833 recode system time at the beginning of the thread run, call join, then record system time again,
834 the difference of the two time should be equal or more than 20 seconds, the CountThead normally terminate
836 void join_001()
838 OCountThread *aCountThread = new OCountThread();
839 bool bRes = aCountThread->create();
840 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
842 StopWatch aStopWatch;
843 aStopWatch.start();
844 // TimeValue aTimeVal_befor;
845 // osl_getSystemTime( &aTimeVal_befor );
846 //t_print("#join:the system time is %d,%d\n", pTimeVal_befor->Seconds,pTimeVal_befor->Nanosec);
848 aCountThread->join();
850 //the below line will be executed after aCountThread terminate
851 // TimeValue aTimeVal_after;
852 // osl_getSystemTime( &aTimeVal_after );
853 aStopWatch.stop();
854 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
855 double nSec = aStopWatch.getSeconds();
856 t_print("join_001 nSec=%f\n", nSec);
857 delete aCountThread;
859 CPPUNIT_ASSERT_MESSAGE(
860 "Join the thread: after the thread terminate",
861 nSec >= 2
865 /** after terminated by another thread, join exited immediately
867 ALGORITHM:
868 terminate the thread when value>=3, call join, check the beginning time and time after join,
869 the difference should be 3 seconds, join costs little time
871 void join_002()
873 OCountThread *aCountThread = new OCountThread();
874 bool bRes = aCountThread->create();
875 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
877 //record the time when the running begin
878 // TimeValue aTimeVal_befor;
879 // osl_getSystemTime( &aTimeVal_befor );
880 StopWatch aStopWatch;
881 aStopWatch.start();
883 ThreadHelper::thread_sleep_tenth_sec(10);
884 termAndJoinThread(aCountThread);
886 //the below line will be executed after aCountThread terminate
887 // TimeValue aTimeVal_after;
888 // osl_getSystemTime( &aTimeVal_after );
889 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
890 aStopWatch.stop();
891 double nSec = aStopWatch.getSeconds();
892 t_print("join_002 nSec=%f\n", nSec);
894 delete aCountThread;
895 CPPUNIT_ASSERT_MESSAGE(
896 "Join the thread: after thread terminate by another thread",
897 nSec >= 1
901 CPPUNIT_TEST_SUITE(join);
902 CPPUNIT_TEST(join_001);
903 CPPUNIT_TEST(join_002);
904 CPPUNIT_TEST_SUITE_END();
905 }; // class join
907 /** Test of the osl::Thread::isRunning method
909 class isRunning : public CppUnit::TestFixture
911 public:
912 // initialise your test code values here.
913 void setUp() SAL_OVERRIDE
917 void tearDown() SAL_OVERRIDE
923 void isRunning_001()
925 OCountThread *aCountThread = new OCountThread();
926 bool bRes = aCountThread->create();
927 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
929 bool bRun = aCountThread->isRunning();
931 ThreadHelper::thread_sleep_tenth_sec(2);
932 termAndJoinThread(aCountThread);
933 bool bTer = aCountThread->isRunning();
934 delete aCountThread;
936 CPPUNIT_ASSERT_MESSAGE(
937 "Test isRunning",
938 bRun && !bTer
941 /** check the value of isRunning when suspending and after resume
943 void isRunning_002()
945 OCountThread *aCountThread = new OCountThread();
946 bool bRes = aCountThread->create();
947 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
949 // sal_Bool bRunning = aCountThread->isRunning();
950 // sal_Int32 nValue = 0;
951 suspendCountThread(aCountThread);
953 bool bRunning_sup = aCountThread->isRunning();
954 ThreadHelper::thread_sleep_tenth_sec(2);
955 aCountThread->resume();
956 ThreadHelper::thread_sleep_tenth_sec(2);
957 bool bRunning_res = aCountThread->isRunning();
958 termAndJoinThread(aCountThread);
959 bool bRunning_ter = aCountThread->isRunning();
960 delete aCountThread;
962 CPPUNIT_ASSERT_MESSAGE(
963 "Test isRunning",
964 bRes && bRunning_sup && bRunning_res && !bRunning_ter );
968 CPPUNIT_TEST_SUITE(isRunning);
969 CPPUNIT_TEST(isRunning_001);
970 CPPUNIT_TEST(isRunning_002);
971 CPPUNIT_TEST_SUITE_END();
972 }; // class isRunning
974 /// check osl::Thread::setPriority
975 class setPriority : public CppUnit::TestFixture
977 public:
978 // initialise your test code values here.
979 void setUp() SAL_OVERRIDE
983 void tearDown() SAL_OVERRIDE
987 // insert your test code here.
988 rtl::OString getPrioName(oslThreadPriority _aPriority)
990 rtl::OString sPrioStr;
991 switch (_aPriority)
993 case osl_Thread_PriorityHighest:
994 sPrioStr = "Highest";
995 break;
997 case osl_Thread_PriorityAboveNormal:
998 sPrioStr = "AboveNormal";
999 break;
1001 case osl_Thread_PriorityNormal:
1002 sPrioStr = "Normal";
1003 break;
1005 case osl_Thread_PriorityBelowNormal:
1006 sPrioStr = "BelowNormal";
1007 break;
1009 case osl_Thread_PriorityLowest:
1010 sPrioStr = "Lowest";
1011 break;
1012 default:
1013 sPrioStr = "unknown";
1015 return sPrioStr;
1018 /** check 2 threads.
1020 ALGORITHM:
1021 Here the function should show, that 2 different threads,
1022 which only increase a value, should run at the same time with same prio.
1023 The test fails, if the difference between the two values is more than 5%
1024 but IMHO this isn't a failure, it's only a feature of the OS.
1027 void check2Threads(oslThreadPriority _aPriority)
1029 // initial 5 threads with different priorities
1030 OAddThread* pThread = new OAddThread();
1031 OAddThread* p2Thread = new OAddThread();
1033 //Create them and start running at the same time
1034 pThread->create();
1035 pThread->setPriority(_aPriority);
1036 p2Thread->create();
1037 p2Thread->setPriority(_aPriority);
1039 ThreadHelper::thread_sleep_tenth_sec(5);
1041 pThread->terminate();
1042 p2Thread->terminate();
1044 sal_Int32 nValueNormal = 0;
1045 nValueNormal = pThread->getValue();
1047 sal_Int32 nValueNormal2 = 0;
1048 nValueNormal2 = p2Thread->getValue();
1050 rtl::OString sPrio = getPrioName(_aPriority);
1051 t_print("After 10 tenth seconds\n");
1053 t_print("nValue in %s Prio Thread is %d\n",sPrio.getStr(), (int) nValueNormal);
1054 t_print("nValue in %s Prio Thread is %d\n", sPrio.getStr(), (int) nValueNormal2);
1056 // ThreadHelper::thread_sleep_tenth_sec(1);
1057 pThread->join();
1058 p2Thread->join();
1060 delete pThread;
1061 delete p2Thread;
1063 sal_Int32 nDelta = abs(nValueNormal - nValueNormal2);
1064 double nQuotient = std::max(nValueNormal, nValueNormal2);
1065 CPPUNIT_ASSERT_MESSAGE(
1066 "Quotient is zero, which means, there exist no right values.",
1067 nQuotient != 0
1069 double nDeltaPercent = nDelta / nQuotient * 100;
1071 t_print("Delta value %d, percent %f\n", (int) nDelta, nDeltaPercent);
1073 // LLA: it's not a bug if the current OS is not able to handle thread scheduling right and good.
1074 // like Windows XP
1075 // LLA: CPPUNIT_ASSERT_MESSAGE(
1076 // LLA: "Run 2 normal threads, the count diff more than 5 percent.",
1077 // LLA: nDeltaPercent <= 5
1078 // LLA: );
1081 void setPriority_001_1()
1083 check2Threads(osl_Thread_PriorityHighest);
1085 void setPriority_001_2()
1087 check2Threads(osl_Thread_PriorityAboveNormal);
1089 void setPriority_001_3()
1091 check2Threads(osl_Thread_PriorityNormal);
1093 void setPriority_001_4()
1095 check2Threads(osl_Thread_PriorityBelowNormal);
1097 void setPriority_001_5()
1099 check2Threads(osl_Thread_PriorityLowest);
1102 void setPriority_002()
1104 // initial 5 threads with different priorities
1106 OAddThread aHighestThread;
1107 OAddThread aAboveNormalThread;
1108 OAddThread aNormalThread;
1109 //OAddThread *aBelowNormalThread = new OAddThread();
1110 //OAddThread *aLowestThread = new OAddThread();
1112 //Create them and start running at the same time
1113 aHighestThread.createSuspended();
1114 aHighestThread.setPriority(osl_Thread_PriorityHighest);
1116 aAboveNormalThread.createSuspended();
1117 aAboveNormalThread.setPriority(osl_Thread_PriorityAboveNormal);
1119 aNormalThread.createSuspended();
1120 aNormalThread.setPriority(osl_Thread_PriorityNormal);
1121 /*aBelowNormalThread->create();
1122 aBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1123 aLowestThread->create();
1124 aLowestThread->setPriority(osl_Thread_PriorityLowest);
1127 aHighestThread.resume();
1128 aAboveNormalThread.resume();
1129 aNormalThread.resume();
1131 ThreadHelper::thread_sleep_tenth_sec(5);
1133 aHighestThread.suspend();
1134 aAboveNormalThread.suspend();
1135 aNormalThread.suspend();
1137 termAndJoinThread(&aNormalThread);
1138 termAndJoinThread(&aAboveNormalThread);
1139 termAndJoinThread(&aHighestThread);
1140 //aBelowNormalThread->terminate();
1141 //aLowestThread->terminate();
1143 sal_Int32 nValueHighest = 0;
1144 nValueHighest = aHighestThread.getValue();
1146 sal_Int32 nValueAboveNormal = 0;
1147 nValueAboveNormal = aAboveNormalThread.getValue();
1149 sal_Int32 nValueNormal = 0;
1150 nValueNormal = aNormalThread.getValue();
1152 t_print("After 10 tenth seconds\n");
1153 t_print("nValue in Highest Prio Thread is %d\n", (int) nValueHighest);
1154 t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal);
1155 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
1157 #ifndef WNT
1158 CPPUNIT_ASSERT_MESSAGE(
1159 "SetPriority",
1160 nValueHighest > 0 &&
1161 nValueAboveNormal > 0 &&
1162 nValueNormal > 0
1164 #endif
1167 void setPriority_003()
1169 // initial 5 threads with different priorities
1170 OAddThread *pHighestThread = new OAddThread();
1171 OAddThread *pAboveNormalThread = new OAddThread();
1172 OAddThread *pNormalThread = new OAddThread();
1173 OAddThread *pBelowNormalThread = new OAddThread();
1174 OAddThread *pLowestThread = new OAddThread();
1176 //Create them and start running at the same time
1177 pHighestThread->createSuspended();
1178 pHighestThread->setPriority(osl_Thread_PriorityHighest);
1180 pAboveNormalThread->createSuspended();
1181 pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1183 pNormalThread->createSuspended();
1184 pNormalThread->setPriority(osl_Thread_PriorityNormal);
1186 pBelowNormalThread->createSuspended();
1187 pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1189 pLowestThread->createSuspended();
1190 pLowestThread->setPriority(osl_Thread_PriorityLowest);
1192 pHighestThread->resume();
1193 pAboveNormalThread->resume();
1194 pNormalThread->resume();
1195 pBelowNormalThread->resume();
1196 pLowestThread->resume();
1198 ThreadHelper::thread_sleep_tenth_sec(5);
1200 pHighestThread->suspend();
1201 pAboveNormalThread->suspend();
1202 pNormalThread->suspend();
1203 pBelowNormalThread->suspend();
1204 pLowestThread->suspend();
1206 termAndJoinThread(pHighestThread);
1207 termAndJoinThread(pAboveNormalThread);
1208 termAndJoinThread(pNormalThread);
1209 termAndJoinThread(pBelowNormalThread);
1210 termAndJoinThread(pLowestThread);
1212 sal_Int32 nValueHighest = 0;
1213 nValueHighest = pHighestThread->getValue();
1215 sal_Int32 nValueAboveNormal = 0;
1216 nValueAboveNormal = pAboveNormalThread->getValue();
1218 sal_Int32 nValueNormal = 0;
1219 nValueNormal = pNormalThread->getValue();
1221 sal_Int32 nValueBelowNormal = 0;
1222 nValueBelowNormal = pBelowNormalThread->getValue();
1224 sal_Int32 nValueLowest = 0;
1225 nValueLowest = pLowestThread->getValue();
1227 t_print("After 10 tenth seconds\n");
1228 t_print("nValue in Highest Prio Thread is %d\n", (int) nValueHighest);
1229 t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal);
1230 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
1231 t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal);
1232 t_print("nValue in Lowest Prio Thread is %d\n", (int) nValueLowest);
1234 delete pHighestThread;
1235 delete pAboveNormalThread;
1236 delete pNormalThread;
1237 delete pBelowNormalThread;
1238 delete pLowestThread;
1240 #ifndef WNT
1241 CPPUNIT_ASSERT_MESSAGE(
1242 "SetPriority",
1243 nValueHighest > 0 &&
1244 nValueAboveNormal > 0 &&
1245 nValueNormal > 0 &&
1246 nValueBelowNormal > 0 &&
1247 nValueLowest > 0
1249 #endif
1252 void setPriority_004()
1254 // initial 5 threads with different priorities
1255 // OAddThread *pHighestThread = new OAddThread();
1256 OAddThread *pAboveNormalThread = new OAddThread();
1257 OAddThread *pNormalThread = new OAddThread();
1258 OAddThread *pBelowNormalThread = new OAddThread();
1259 OAddThread *pLowestThread = new OAddThread();
1261 //Create them and start running at the same time
1262 // pHighestThread->createSuspended();
1263 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1265 pAboveNormalThread->createSuspended();
1266 pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1268 pNormalThread->createSuspended();
1269 pNormalThread->setPriority(osl_Thread_PriorityNormal);
1271 pBelowNormalThread->createSuspended();
1272 pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1274 pLowestThread->createSuspended();
1275 pLowestThread->setPriority(osl_Thread_PriorityLowest);
1277 // pHighestThread->resume();
1278 pAboveNormalThread->resume();
1279 pNormalThread->resume();
1280 pBelowNormalThread->resume();
1281 pLowestThread->resume();
1283 ThreadHelper::thread_sleep_tenth_sec(5);
1285 // pHighestThread->suspend();
1286 pAboveNormalThread->suspend();
1287 pNormalThread->suspend();
1288 pBelowNormalThread->suspend();
1289 pLowestThread->suspend();
1291 // termAndJoinThread(pHighestThread);
1292 termAndJoinThread(pAboveNormalThread);
1293 termAndJoinThread(pNormalThread);
1294 termAndJoinThread(pBelowNormalThread);
1295 termAndJoinThread(pLowestThread);
1297 // sal_Int32 nValueHighest = 0;
1298 // nValueHighest = pHighestThread->getValue();
1300 sal_Int32 nValueAboveNormal = 0;
1301 nValueAboveNormal = pAboveNormalThread->getValue();
1303 sal_Int32 nValueNormal = 0;
1304 nValueNormal = pNormalThread->getValue();
1306 sal_Int32 nValueBelowNormal = 0;
1307 nValueBelowNormal = pBelowNormalThread->getValue();
1309 sal_Int32 nValueLowest = 0;
1310 nValueLowest = pLowestThread->getValue();
1312 t_print("After 5 tenth seconds\n");
1313 t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal);
1314 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
1315 t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal);
1316 t_print("nValue in Lowest Prio Thread is %d\n", (int) nValueLowest);
1318 // delete pHighestThread;
1319 delete pAboveNormalThread;
1320 delete pNormalThread;
1321 delete pBelowNormalThread;
1322 delete pLowestThread;
1324 #ifndef WNT
1325 CPPUNIT_ASSERT_MESSAGE(
1326 "SetPriority",
1327 /* nValueHighest > 0 && */
1328 nValueAboveNormal > 0 &&
1329 nValueNormal > 0 &&
1330 nValueBelowNormal > 0 &&
1331 nValueLowest > 0
1333 #endif
1335 void setPriority_005()
1337 // initial 5 threads with different priorities
1338 // OAddThread *pHighestThread = new OAddThread();
1339 // OAddThread *pAboveNormalThread = new OAddThread();
1340 OAddThread *pNormalThread = new OAddThread();
1341 OAddThread *pBelowNormalThread = new OAddThread();
1342 OAddThread *pLowestThread = new OAddThread();
1344 //Create them and start running at the same time
1345 // pHighestThread->createSuspended();
1346 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1348 // pAboveNormalThread->createSuspended();
1349 // pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1351 pNormalThread->createSuspended();
1352 pNormalThread->setPriority(osl_Thread_PriorityNormal);
1354 pBelowNormalThread->createSuspended();
1355 pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1357 pLowestThread->createSuspended();
1358 pLowestThread->setPriority(osl_Thread_PriorityLowest);
1360 // pHighestThread->resume();
1361 // pAboveNormalThread->resume();
1362 pNormalThread->resume();
1363 pBelowNormalThread->resume();
1364 pLowestThread->resume();
1366 ThreadHelper::thread_sleep_tenth_sec(5);
1368 // pHighestThread->suspend();
1369 // pAboveNormalThread->suspend();
1370 pNormalThread->suspend();
1371 pBelowNormalThread->suspend();
1372 pLowestThread->suspend();
1374 // termAndJoinThread(pHighestThread);
1375 // termAndJoinThread(pAboveNormalThread);
1376 termAndJoinThread(pNormalThread);
1377 termAndJoinThread(pBelowNormalThread);
1378 termAndJoinThread(pLowestThread);
1380 // sal_Int32 nValueHighest = 0;
1381 // nValueHighest = pHighestThread->getValue();
1383 // sal_Int32 nValueAboveNormal = 0;
1384 // nValueAboveNormal = pAboveNormalThread->getValue();
1386 sal_Int32 nValueNormal = 0;
1387 nValueNormal = pNormalThread->getValue();
1389 sal_Int32 nValueBelowNormal = 0;
1390 nValueBelowNormal = pBelowNormalThread->getValue();
1392 sal_Int32 nValueLowest = 0;
1393 nValueLowest = pLowestThread->getValue();
1395 t_print("After 5 tenth seconds\n");
1396 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
1397 t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal);
1398 t_print("nValue in Lowest Prio Thread is %d\n", (int) nValueLowest);
1400 delete pNormalThread;
1401 delete pBelowNormalThread;
1402 delete pLowestThread;
1404 #ifndef WNT
1405 CPPUNIT_ASSERT_MESSAGE(
1406 "SetPriority",
1407 /* nValueHighest > 0 && */
1408 /* nValueAboveNormal > 0 && */
1409 nValueNormal > 0 &&
1410 nValueBelowNormal > 0 &&
1411 nValueLowest > 0
1413 #endif
1416 CPPUNIT_TEST_SUITE(setPriority);
1417 #ifndef SOLARIS
1418 CPPUNIT_TEST(setPriority_002);
1419 CPPUNIT_TEST(setPriority_003);
1420 CPPUNIT_TEST(setPriority_004);
1421 CPPUNIT_TEST(setPriority_005);
1422 #endif
1423 CPPUNIT_TEST(setPriority_001_1);
1424 CPPUNIT_TEST(setPriority_001_2);
1425 CPPUNIT_TEST(setPriority_001_3);
1426 CPPUNIT_TEST(setPriority_001_4);
1427 CPPUNIT_TEST(setPriority_001_5);
1428 CPPUNIT_TEST_SUITE_END();
1429 }; // class setPriority
1431 /** Test of the osl::Thread::getPriority method
1433 class getPriority : public CppUnit::TestFixture
1435 public:
1436 // initialise your test code values here.
1437 void setUp() SAL_OVERRIDE {}
1439 void tearDown() SAL_OVERRIDE {}
1441 // insert your test code here.
1442 void getPriority_001()
1444 OAddThread *pHighestThread = new OAddThread();
1446 //Create them and start running at the same time
1447 pHighestThread->create();
1448 pHighestThread->setPriority(osl_Thread_PriorityHighest);
1450 oslThreadPriority aPriority = pHighestThread->getPriority();
1451 termAndJoinThread(pHighestThread);
1452 delete pHighestThread;
1454 ThreadHelper::outputPriority(aPriority);
1456 // LLA: Priority settings may not work within some OS versions.
1457 #if ( defined WNT ) || ( defined SOLARIS )
1458 CPPUNIT_ASSERT_MESSAGE(
1459 "getPriority",
1460 aPriority == osl_Thread_PriorityHighest
1462 #else
1463 // LLA: Linux
1464 // NO_PTHREAD_PRIORITY ???
1465 CPPUNIT_ASSERT_MESSAGE(
1466 "getPriority",
1467 aPriority == osl_Thread_PriorityNormal
1469 #endif
1472 CPPUNIT_TEST_SUITE(getPriority);
1473 CPPUNIT_TEST(getPriority_001);
1474 CPPUNIT_TEST_SUITE_END();
1475 }; // class getPriority
1477 class getIdentifier : public CppUnit::TestFixture
1479 public:
1480 // initialise your test code values here.
1481 void setUp() SAL_OVERRIDE {}
1483 void tearDown() SAL_OVERRIDE {}
1485 void getIdentifier_001()
1487 // insert your test code here.
1490 CPPUNIT_TEST_SUITE(getIdentifier);
1491 CPPUNIT_TEST(getIdentifier_001);
1492 CPPUNIT_TEST_SUITE_END();
1493 }; // class getIdentifier
1495 /** Test of the osl::Thread::getCurrentIdentifier method
1497 class getCurrentIdentifier : public CppUnit::TestFixture
1499 public:
1500 void setUp() SAL_OVERRIDE {}
1501 void tearDown() SAL_OVERRIDE {}
1503 void getCurrentIdentifier_001()
1505 oslThreadIdentifier oId;
1506 OCountThread* pCountThread = new OCountThread;
1507 pCountThread->create();
1508 pCountThread->setWait(3);
1509 oId = Thread::getCurrentIdentifier();
1510 oslThreadIdentifier oIdChild = pCountThread->getIdentifier();
1511 termAndJoinThread(pCountThread);
1512 delete pCountThread;
1514 CPPUNIT_ASSERT_MESSAGE(
1515 "Get the identifier for the current active thread.",
1516 oId != oIdChild);
1519 CPPUNIT_TEST_SUITE(getCurrentIdentifier);
1520 CPPUNIT_TEST(getCurrentIdentifier_001);
1521 CPPUNIT_TEST_SUITE_END();
1522 }; // class getCurrentIdentifier
1524 /** Test of the osl::Thread::wait method
1526 class waittest : public CppUnit::TestFixture
1528 public:
1529 void setUp() SAL_OVERRIDE {}
1530 void tearDown() SAL_OVERRIDE {}
1532 /** call wait in the run method
1534 ALGORITHM:
1535 tested thread wait nWaitSec seconds, main thread sleep (2) seconds,
1536 then terminate the tested thread, due to the fact that the thread do a sleep(1) + wait(5)
1537 it's finish after 6 seconds.
1539 void wait_001()
1541 OCountThread *aCountThread = new OCountThread();
1542 sal_Int32 nWaitSec = 5;
1543 aCountThread->setWait(nWaitSec);
1544 // thread runs at least 5 seconds.
1545 bool bRes = aCountThread->create();
1546 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
1548 //record the time when the running begin
1549 StopWatch aStopWatch;
1550 aStopWatch.start();
1552 // wait a little bit, to let the thread the time, to start
1553 ThreadHelper::thread_sleep_tenth_sec( 4 );
1555 // if wait works,
1556 // this function returns, after 4 sec. later
1557 termAndJoinThread(aCountThread);
1559 // value should be one.
1560 sal_Int32 nValue = aCountThread->getValue();
1562 aStopWatch.stop();
1564 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
1565 double nTenthSec = aStopWatch.getTenthSec();
1566 double nSec = aStopWatch.getSeconds();
1567 delete aCountThread;
1568 t_print("nTenthSec = %f \n", nTenthSec);
1569 t_print("nSec = %f \n", nSec);
1570 t_print("nValue = %d \n", (int) nValue);
1572 CPPUNIT_ASSERT_MESSAGE(
1573 "Wait: Blocks the calling thread for the given number of time.",
1574 nTenthSec >= 5 && nValue == 1
1579 CPPUNIT_TEST_SUITE(waittest);
1580 CPPUNIT_TEST(wait_001);
1581 CPPUNIT_TEST_SUITE_END();
1582 }; // class waittest
1584 /** osl::Thread::yield method: can not design good test scenario to test up to now
1586 class yield : public CppUnit::TestFixture
1588 public:
1589 void setUp() SAL_OVERRIDE {}
1590 void tearDown() SAL_OVERRIDE {}
1592 void yield_001()
1594 // insert your test code here.
1597 CPPUNIT_TEST_SUITE(yield);
1598 CPPUNIT_TEST(yield_001);
1599 CPPUNIT_TEST_SUITE_END();
1600 }; // class yield
1602 /** Test of the osl::Thread::schedule method
1604 class schedule : public CppUnit::TestFixture
1606 public:
1607 void setUp() SAL_OVERRIDE {}
1608 void tearDown() SAL_OVERRIDE {}
1610 /** The requested thread will get terminate the next time schedule() is called.
1612 Note: on UNX, if call suspend thread is not the to be suspended thread, the to be
1613 suspended thread will get suspended the next time schedule() is called,
1614 while on w32, it's nothing with schedule.
1616 check if suspend and terminate work well via schedule
1618 void schedule_001()
1620 OAddThread* aThread = new OAddThread();
1621 bool bRes = aThread->create();
1622 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
1624 ThreadHelper::thread_sleep_tenth_sec(2);
1625 aThread->suspend();
1626 ThreadHelper::thread_sleep_tenth_sec(1);
1627 sal_Int32 nValue = aThread->getValue();
1628 ThreadHelper::thread_sleep_tenth_sec(3);
1629 sal_Int32 nLaterValue = aThread->getValue();
1630 // resumeAndWaitThread(aThread);
1631 t_print(" value = %d\n", (int) nValue);
1632 t_print("later value = %d\n", (int) nLaterValue);
1633 // if value and latervalue not equal, than the thread would not suspended
1635 CPPUNIT_ASSERT_MESSAGE(
1636 "Schedule: suspend works.",
1637 nLaterValue == nValue
1640 aThread->resume();
1641 ThreadHelper::thread_sleep_tenth_sec(2);
1643 aThread->terminate();
1644 sal_Int32 nValue_term = aThread->getValue();
1646 aThread->join();
1647 sal_Int32 nValue_join = aThread->getValue();
1649 t_print("value after term = %d\n", (int) nValue_term);
1650 t_print("value after join = %d\n", (int) nValue_join);
1652 // nValue_term and nValue_join should be the same
1653 // but should be differ from nValue
1655 delete aThread;
1656 //check if thread really terminate after call terminate, if join immediatlly return
1657 CPPUNIT_ASSERT_MESSAGE(
1658 "Schedule: Returns False if the thread should terminate.",
1659 nValue_join - nValue_term <= 1 && nValue_join - nValue_term >= 0
1664 /** design a thread that has not call schedule in the workfunction--run method
1666 void schedule_002()
1668 ONoScheduleThread aThread; // this thread runs 10 sec. (no schedule() used)
1669 bool bRes = aThread.create();
1670 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
1672 ThreadHelper::thread_sleep_tenth_sec(2);
1673 aThread.suspend();
1674 sal_Int32 nValue = aThread.getValue();
1676 ThreadHelper::thread_sleep_tenth_sec(3);
1677 sal_Int32 nLaterValue = aThread.getValue();
1678 ThreadHelper::thread_sleep_tenth_sec(5);
1680 resumeAndWaitThread(&aThread);
1682 t_print(" value = %d\n", (int) nValue);
1683 t_print("later value = %d\n", (int) nLaterValue);
1685 //On windows, suspend works, so the values are same
1686 #ifdef WNT
1687 CPPUNIT_ASSERT_MESSAGE(
1688 "Schedule: don't schedule in thread run method, suspend works.",
1689 nLaterValue == nValue
1691 #endif
1693 //On UNX, suspend does not work, so the difference of the values equals to sleep seconds number
1694 #ifdef UNX
1695 aThread.resume();
1696 CPPUNIT_ASSERT_MESSAGE(
1697 "Schedule: don't schedule in thread run method, suspend does not work too.",
1698 nLaterValue > nValue
1700 #endif
1702 // terminate will not work if no schedule in thread's work function
1703 termAndJoinThread(&aThread);
1704 sal_Int32 nValue_term = aThread.getValue();
1706 t_print(" value term = %d\n", (int) nValue_term);
1708 CPPUNIT_ASSERT_MESSAGE(
1709 "Schedule: don't schedule in thread run method, terminate failed.",
1710 nValue_term == 10
1714 CPPUNIT_TEST_SUITE(schedule);
1715 CPPUNIT_TEST(schedule_001);
1716 CPPUNIT_TEST(schedule_002);
1717 CPPUNIT_TEST_SUITE_END();
1718 }; // class schedule
1720 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::create, "osl_Thread");
1721 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::createSuspended, "osl_Thread");
1722 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::suspend, "osl_Thread");
1723 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::resume, "osl_Thread");
1724 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::terminate, "osl_Thread");
1725 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::join, "osl_Thread");
1726 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::isRunning, "osl_Thread");
1727 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::setPriority, "osl_Thread");
1728 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getPriority, "osl_Thread");
1729 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getIdentifier, "osl_Thread");
1730 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getCurrentIdentifier, "osl_Thread");
1731 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::waittest, "osl_Thread");
1732 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::yield, "osl_Thread");
1733 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::schedule, "osl_Thread");
1734 } // namespace osl_Thread
1736 // destroy function when the binding thread terminate
1737 void SAL_CALL destroyCallback(void * data)
1739 delete[] static_cast<char *>(data);
1742 static ThreadData myThreadData(destroyCallback);
1746 class myKeyThread : public Thread
1748 public:
1749 // a public char member for test result checking
1750 char m_Char_Test;
1751 // for pass thread-special data to thread
1752 myKeyThread(const char cData)
1753 : m_Char_Test(0)
1755 m_nData = cData;
1757 private:
1758 char m_nData;
1760 void SAL_CALL run() SAL_OVERRIDE
1762 char * pc = new char[2];
1763 // strcpy(pc, &m_nData);
1764 memcpy(pc, &m_nData, 1);
1765 pc[1] = '\0';
1767 myThreadData.setData(pc);
1768 char* pData = static_cast<char*>(myThreadData.getData());
1769 m_Char_Test = *pData;
1770 // wait for long time to check the data value in main thread
1771 ThreadHelper::thread_sleep_tenth_sec(3);
1773 public:
1774 virtual ~myKeyThread()
1776 if (isRunning())
1778 t_print("error: not terminated.\n");
1783 static ThreadData idData;
1785 class idThread: public Thread
1787 public:
1788 oslThreadIdentifier m_Id;
1789 private:
1790 void SAL_CALL run() SAL_OVERRIDE
1792 oslThreadIdentifier* pId = new oslThreadIdentifier;
1793 *pId = getIdentifier();
1794 idData.setData(pId);
1795 oslThreadIdentifier* pIdData = static_cast<oslThreadIdentifier*>(idData.getData());
1796 //t_print("Thread %d has Data %d\n", getIdentifier(), *pIdData);
1797 m_Id = *pIdData;
1798 delete pId;
1801 public:
1802 virtual ~idThread()
1804 if (isRunning())
1806 t_print("error: not terminated.\n");
1811 namespace osl_ThreadData
1814 class ctors : public CppUnit::TestFixture
1816 public:
1817 // initialise your test code values here.
1818 void setUp() SAL_OVERRIDE
1822 void tearDown() SAL_OVERRIDE
1826 // insert your test code here.
1827 void ctor_001()
1832 CPPUNIT_TEST_SUITE(ctors);
1833 CPPUNIT_TEST(ctor_001);
1834 CPPUNIT_TEST_SUITE_END();
1835 }; // class ctors
1837 class setData : public CppUnit::TestFixture
1839 public:
1840 // initialise your test code values here.
1841 void setUp() SAL_OVERRIDE
1845 void tearDown() SAL_OVERRIDE
1849 /** the same instance of the class can have different values in different threads
1851 void setData_001()
1853 idThread aThread1;
1854 aThread1.create();
1855 idThread aThread2;
1856 aThread2.create();
1858 aThread1.join();
1859 aThread2.join();
1861 oslThreadIdentifier aThreadId1 = aThread1.getIdentifier();
1862 oslThreadIdentifier aThreadId2 = aThread2.getIdentifier();
1864 CPPUNIT_ASSERT_MESSAGE(
1865 "ThreadData setData: ",
1866 aThread1.m_Id == aThreadId1 && aThread2.m_Id == aThreadId2
1871 void setData_002()
1873 // at first, set the data a value
1874 char* pc = new char[2];
1875 char m_nData = 'm';
1876 pc[0] = m_nData;
1877 pc[1] = '\0';
1879 myThreadData.setData(pc);
1881 myKeyThread aThread1('a');
1882 aThread1.create();
1883 myKeyThread aThread2('b');
1884 aThread2.create();
1885 // aThread1 and aThread2 should have not terminated yet, check current data, not 'a' 'b'
1886 char* pChar = static_cast<char*>(myThreadData.getData());
1887 char aChar = *pChar;
1889 aThread1.join();
1890 aThread2.join();
1892 // the saved thread data of aThread1 & aThread2, different
1893 char cData1 = aThread1.m_Char_Test;
1894 char cData2 = aThread2.m_Char_Test;
1896 CPPUNIT_ASSERT_MESSAGE(
1897 "ThreadData setData: ",
1898 cData1 == 'a' && cData2 == 'b' && aChar == 'm'
1902 /** setData the second time, and then getData
1904 void setData_003()
1906 // at first, set the data a value
1907 char* pc = new char[2];
1908 char m_nData = 'm';
1909 memcpy(pc, &m_nData, 1);
1910 pc[1] = '\0';
1911 myThreadData.setData(pc);
1913 myKeyThread aThread1('a');
1914 aThread1.create();
1915 myKeyThread aThread2('b');
1916 aThread2.create();
1917 // aThread1 and aThread2 should have not terminated yet
1918 // setData the second time
1919 char* pc2 = new char[2];
1920 m_nData = 'o';
1921 memcpy(pc2, &m_nData, 1);
1922 pc2[1] = '\0';
1924 myThreadData.setData(pc2);
1925 char* pChar = static_cast<char*>(myThreadData.getData());
1926 char aChar = *pChar;
1928 aThread1.join();
1929 aThread2.join();
1931 // the saved thread data of aThread1 & aThread2, different
1932 char cData1 = aThread1.m_Char_Test;
1933 char cData2 = aThread2.m_Char_Test;
1935 CPPUNIT_ASSERT_MESSAGE(
1936 "ThreadData setData: ",
1937 cData1 == 'a' && cData2 == 'b' && aChar == 'o'
1941 CPPUNIT_TEST_SUITE(setData);
1942 CPPUNIT_TEST(setData_001);
1943 CPPUNIT_TEST(setData_002);
1944 CPPUNIT_TEST(setData_003);
1945 CPPUNIT_TEST_SUITE_END();
1946 }; // class setData
1948 class getData : public CppUnit::TestFixture
1950 public:
1951 // initialise your test code values here.
1952 void setUp() SAL_OVERRIDE
1956 void tearDown() SAL_OVERRIDE
1960 // After setData in child threads, get Data in the main thread, should be independent
1961 void getData_001()
1963 char* pc = new char[2];
1964 char m_nData[] = "i";
1965 strcpy(pc, m_nData);
1966 myThreadData.setData(pc);
1968 myKeyThread aThread1('c');
1969 aThread1.create();
1970 myKeyThread aThread2('d');
1971 aThread2.create();
1973 aThread1.join();
1974 aThread2.join();
1976 char cData1 = aThread1.m_Char_Test;
1977 char cData2 = aThread2.m_Char_Test;
1979 char* pChar = static_cast<char*>(myThreadData.getData());
1980 char aChar = *pChar;
1982 CPPUNIT_ASSERT_MESSAGE(
1983 "ThreadData setData: ",
1984 cData1 == 'c' && cData2 == 'd' && aChar == 'i'
1988 // setData then change the value in the address data pointer points,
1989 // and then getData, should get the new value
1990 void getData_002()
1992 char* pc = new char[2];
1993 char m_nData = 'i';
1994 memcpy(pc, &m_nData, 1);
1995 pc[1] = '\0';
1997 myThreadData.setData(pc);
1999 myKeyThread aThread1('a');
2000 aThread1.create();
2001 myKeyThread aThread2('b');
2002 aThread2.create();
2004 // change the value which pc points
2005 char m_nData2 = 'j';
2006 memcpy(pc, &m_nData2, 1);
2007 pc[1] = '\0';
2009 void* pChar = myThreadData.getData();
2010 char aChar = *static_cast<char*>(pChar);
2012 aThread1.join();
2013 aThread2.join();
2015 char cData1 = aThread1.m_Char_Test;
2016 char cData2 = aThread2.m_Char_Test;
2018 CPPUNIT_ASSERT_MESSAGE(
2019 "ThreadData setData: ",
2020 cData1 == 'a' && cData2 == 'b' && aChar == 'j'
2025 CPPUNIT_TEST_SUITE(getData);
2026 CPPUNIT_TEST(getData_001);
2027 CPPUNIT_TEST(getData_002);
2028 CPPUNIT_TEST_SUITE_END();
2029 }; // class getData
2031 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::ctors);
2032 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::setData);
2033 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::getData);
2034 } // namespace osl_ThreadData
2036 // this macro creates an empty function, which will called by the RegisterAllFunctions()
2037 // to let the user the possibility to also register some functions by hand.
2038 CPPUNIT_PLUGIN_IMPLEMENT();
2040 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */