Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / qa / osl / process / osl_Thread.cxx
blob3e394aa57efd7bbbf5478ec0df378524f8459ca0
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 #ifdef WNT
21 #include <windows.h>
22 #else
23 #include <unistd.h>
24 #include <time.h>
25 #endif
27 //------------------------------------------------------------------------
28 // include files
29 //------------------------------------------------------------------------
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 // -----------------------------------------------------------------------------
54 // Small stopwatch
55 class StopWatch {
56 TimeValue t1,t2; // Start and stoptime
58 protected:
59 sal_Int32 m_nNanoSec;
60 sal_Int32 m_nSeconds;
62 bool m_bIsValid; // TRUE, when started and stopped
63 bool m_bIsRunning; // TRUE, when started
65 public:
66 StopWatch();
67 ~StopWatch() {}
69 void start(); // Starts time
70 void stop(); // Stops time
72 double getSeconds() const;
73 double getTenthSec() const;
76 // ================================= Stop Watch =================================
78 // A small stopwatch for internal use
79 // (c) Lars Langhans 29.12.1996 22:10
81 StopWatch::StopWatch():m_bIsValid(false),m_bIsRunning(false) {}
83 void StopWatch::start()
85 // pre: %
86 // post: Start Timer
88 m_bIsValid = false;
89 m_bIsRunning = true;
90 osl_getSystemTime( &t1 );
91 t_print("# %u %u nsecs\n", (unsigned)t1.Seconds, (unsigned)t1.Nanosec);
92 // gettimeofday(&t1, 0);
95 void StopWatch::stop()
97 // pre: Timer should be started
98 // post: Timer will stopped
100 // gettimeofday(&t2, 0); // Ask timer
101 osl_getSystemTime( &t2 );
102 t_print("# %u %u nsecs\n", (unsigned) t2.Seconds, (unsigned) t2.Nanosec);
104 if (m_bIsRunning)
105 { // check if started.
106 m_nSeconds = static_cast<sal_Int32>(t2.Seconds) - static_cast<sal_Int32>(t1.Seconds);
107 if ( t2.Nanosec > t1.Nanosec )
108 m_nNanoSec = static_cast<sal_Int32>(t2.Nanosec) - static_cast<sal_Int32>(t1.Nanosec);
109 else
111 m_nNanoSec = 1000000000 + static_cast<sal_Int32>(t2.Nanosec) - static_cast<sal_Int32>(t1.Nanosec);
112 m_nSeconds -= 1;
114 t_print("# %u %u nsecs\n", (unsigned) m_nSeconds, (unsigned) m_nNanoSec );
115 //if (m_nNanoSec < 0)
117 //m_nNanoSec += 1000000000;
118 //m_nSeconds -= 1;
120 m_bIsValid = true;
121 m_bIsRunning = false;
125 double StopWatch::getSeconds() const
127 // pre: valid = TRUE
128 // BACK: time in seconds
130 double nValue = 0.0;
131 if (m_bIsValid)
133 nValue = double(m_nNanoSec) / 1000000000.0 + m_nSeconds; // milli micro nano
135 return nValue;
138 double StopWatch::getTenthSec() const
140 double nValue = 0.0;
141 if (m_bIsValid)
143 nValue = double(m_nNanoSec) / 100000000.0 + m_nSeconds * 10;
145 return nValue ;
148 // -----------------------------------------------------------------------------
149 template <class T>
150 class ThreadSafeValue
152 T m_nFlag;
153 Mutex m_aMutex;
154 public:
155 ThreadSafeValue(T n = 0): m_nFlag(n) {}
156 T getValue()
158 //block if already acquired by another thread.
159 osl::MutexGuard g(m_aMutex);
160 return m_nFlag;
162 void addValue(T n)
164 //only one thread operate on the flag.
165 osl::MutexGuard g(m_aMutex);
166 m_nFlag += n;
168 void acquire() {m_aMutex.acquire();}
169 void release() {m_aMutex.release();}
172 // -----------------------------------------------------------------------------
173 namespace ThreadHelper
175 void thread_sleep_tenth_sec(sal_Int32 _nTenthSec)
177 #ifdef WNT
178 Sleep(_nTenthSec * 100 );
179 #else
180 TimeValue nTV;
181 nTV.Seconds = static_cast<sal_uInt32>( _nTenthSec/10 );
182 nTV.Nanosec = ( (_nTenthSec%10 ) * 100000000 );
183 osl_waitThread(&nTV);
184 #endif
187 void outputPriority(oslThreadPriority const& _aPriority)
189 // LLA: output the priority
190 if (_aPriority == osl_Thread_PriorityHighest)
192 t_print("Prio is High\n");
194 else if (_aPriority == osl_Thread_PriorityAboveNormal)
196 t_print("Prio is above normal\n");
198 else if (_aPriority == osl_Thread_PriorityNormal)
200 t_print("Prio is normal\n");
202 else if (_aPriority == osl_Thread_PriorityBelowNormal)
204 t_print("Prio is below normal\n");
206 else if (_aPriority == osl_Thread_PriorityLowest)
208 t_print("Prio is lowest\n");
210 else
212 t_print("Prio is unknown\n");
217 /** Simple thread for testing Thread-create.
219 Just add 1 of value 0, and after running, result is 1.
221 class myThread : public Thread
223 ThreadSafeValue<sal_Int32> m_aFlag;
224 public:
225 sal_Int32 getValue() { return m_aFlag.getValue(); }
226 protected:
227 /** guarded value which initialized 0
229 @see ThreadSafeValue
231 void SAL_CALL run()
233 while(schedule())
235 m_aFlag.addValue(1);
236 ThreadHelper::thread_sleep_tenth_sec(1);
240 public:
242 virtual void SAL_CALL suspend()
244 m_aFlag.acquire();
245 ::osl::Thread::suspend();
246 m_aFlag.release();
249 ~myThread()
251 if (isRunning())
253 t_print("error: not terminated.\n");
259 // -----------------------------------------------------------------------------
260 /** Thread which has a flag add 1 every second until 20
262 class OCountThread : public Thread
264 ThreadSafeValue<sal_Int32> m_aFlag;
265 public:
266 OCountThread()
268 m_nWaitSec = 0;
269 t_print("new OCountThread thread %u!\n", (unsigned) getIdentifier());
271 sal_Int32 getValue() { return m_aFlag.getValue(); }
273 void setWait(sal_Int32 nSec)
275 m_nWaitSec = nSec;
276 //m_bWait = sal_True;
279 virtual void SAL_CALL suspend()
281 m_aFlag.acquire();
282 ::osl::Thread::suspend();
283 m_aFlag.release();
286 protected:
287 //sal_Bool m_bWait;
288 sal_Int32 m_nWaitSec;
290 void SAL_CALL run()
292 /// if the thread should terminate, schedule return false
293 while (m_aFlag.getValue() < 20 && schedule() == sal_True)
295 m_aFlag.addValue(1);
296 ThreadHelper::thread_sleep_tenth_sec(1);
298 if (m_nWaitSec != 0)
300 TimeValue nTV;
301 nTV.Seconds = m_nWaitSec / 10 ;
302 nTV.Nanosec = ( m_nWaitSec%10 ) * 100000000 ;
303 wait( nTV );
304 m_nWaitSec = 0;
308 void SAL_CALL onTerminated()
310 t_print("normally terminate this thread %u!\n", (unsigned) getIdentifier());
312 public:
314 ~OCountThread()
316 if (isRunning())
318 t_print("error: not terminated.\n");
324 /** call suspend in the run method
326 class OSuspendThread : public Thread
328 ThreadSafeValue<sal_Int32> m_aFlag;
329 public:
330 OSuspendThread(){ m_bSuspend = sal_False; }
331 sal_Int32 getValue() { return m_aFlag.getValue(); }
332 void setSuspend()
334 m_bSuspend = sal_True;
336 virtual void SAL_CALL suspend()
338 m_aFlag.acquire();
339 ::osl::Thread::suspend();
340 m_aFlag.release();
342 protected:
343 sal_Bool m_bSuspend;
344 void SAL_CALL run()
346 //if the thread should terminate, schedule return false
347 while (schedule() == sal_True)
349 m_aFlag.addValue(1);
351 ThreadHelper::thread_sleep_tenth_sec(1);
352 if (m_bSuspend == sal_True)
354 suspend();
355 m_bSuspend = sal_False;
359 public:
361 ~OSuspendThread()
363 if (isRunning())
365 t_print("error: not terminated.\n");
371 /** no call schedule in the run method
373 class ONoScheduleThread : public Thread
375 ThreadSafeValue<sal_Int32> m_aFlag;
376 public:
377 sal_Int32 getValue() { return m_aFlag.getValue(); }
379 virtual void SAL_CALL suspend()
381 m_aFlag.acquire();
382 ::osl::Thread::suspend();
383 m_aFlag.release();
385 protected:
386 void SAL_CALL run()
388 while (m_aFlag.getValue() < 10)
390 m_aFlag.addValue(1);
391 ThreadHelper::thread_sleep_tenth_sec(1);
394 void SAL_CALL onTerminated()
396 t_print("normally terminate this thread %u!\n", (unsigned) getIdentifier());
398 public:
399 ONoScheduleThread()
401 t_print("new thread id %u!\n", (unsigned) getIdentifier());
403 ~ONoScheduleThread()
405 if (isRunning())
407 t_print("error: not terminated.\n");
415 class OAddThread : public Thread
417 ThreadSafeValue<sal_Int32> m_aFlag;
418 public:
419 //oslThreadIdentifier m_id, m_CurId;
420 OAddThread(){}
421 sal_Int32 getValue() { return m_aFlag.getValue(); }
423 virtual void SAL_CALL suspend()
425 m_aFlag.acquire();
426 ::osl::Thread::suspend();
427 m_aFlag.release();
429 protected:
430 void SAL_CALL run()
432 //if the thread should terminate, schedule return false
433 while (schedule() == sal_True)
435 m_aFlag.addValue(1);
438 void SAL_CALL onTerminated()
440 // t_print("normally terminate this thread %d!\n", getIdentifier());
442 public:
444 ~OAddThread()
446 if (isRunning())
448 // t_print("error: not terminated.\n");
454 namespace osl_Thread
457 void resumeAndWaitThread(Thread* _pThread)
459 // This functions starts a thread, wait a second and suspends the thread
460 // Due to the fact, that a suspend and never run thread never really exists.
462 // Note: on UNX, after createSuspended, and then terminate the thread, it performs well;
463 // while on Windows, after createSuspended, the thread can not terminate, wait endlessly,
464 // so here call resume at first, then call terminate.
465 #ifdef WNT
466 t_print("resumeAndWaitThread\n");
467 _pThread->resume();
468 ThreadHelper::thread_sleep_tenth_sec(1);
469 #else
470 _pThread->resume();
471 #endif
474 // kill a running thread and join it, if it has terminated, do nothing
475 void termAndJoinThread(Thread* _pThread)
477 _pThread->terminate();
479 // LLA: Windows feature???, a suspended thread can not terminated, so we have to weak it up
480 #ifdef WNT
481 _pThread->resume();
482 ThreadHelper::thread_sleep_tenth_sec(1);
483 #endif
484 t_print("#wait for join.\n");
485 _pThread->join();
487 /** Test of the osl::Thread::create method
490 class create : public CppUnit::TestFixture
492 public:
494 // initialise your test code values here.
495 void setUp()
499 void tearDown()
503 /** Simple create a thread.
505 Create a simple thread, it just does add 1 to value(which initialized 0),
506 if the thread run, the value should be 1.
508 void create_001()
510 myThread* newthread = new myThread();
511 sal_Bool bRes = newthread->create();
512 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!\n", bRes == sal_True );
514 ThreadHelper::thread_sleep_tenth_sec(1); // wait short
515 sal_Bool isRunning = newthread->isRunning(); // check if thread is running
516 /// wait for the new thread to assure it has run
517 ThreadHelper::thread_sleep_tenth_sec(3);
518 sal_Int32 nValue = newthread->getValue();
519 /// to assure the new thread has terminated
520 termAndJoinThread(newthread);
521 delete newthread;
523 t_print(" nValue = %d\n", (int) nValue);
524 t_print("isRunning = %s\n", isRunning == sal_True ? "true" : "false");
526 CPPUNIT_ASSERT_MESSAGE(
527 "Creates a new thread",
528 nValue >= 1 && isRunning == sal_True
533 /** only one running thread per instance, return false if create secondly
535 void create_002()
537 myThread* newthread = new myThread();
538 sal_Bool res1 = newthread->create();
539 sal_Bool res2 = newthread->create();
540 t_print("In non pro, an assertion should occurred. This behaviour is right.\n");
541 termAndJoinThread(newthread);
542 delete newthread;
544 CPPUNIT_ASSERT_MESSAGE(
545 "Creates a new thread: can not create two threads per instance",
546 res1 && !res2
551 CPPUNIT_TEST_SUITE(create);
552 CPPUNIT_TEST(create_001);
553 CPPUNIT_TEST(create_002);
554 CPPUNIT_TEST_SUITE_END();
555 }; // class create
559 /** Test of the osl::Thread::createSuspended method
561 class createSuspended : public CppUnit::TestFixture
563 public:
564 // initialise your test code values here.
565 void setUp()
569 void tearDown()
573 /** Create a suspended thread, use the same class as create_001
575 after create, wait enough time, check the value, if it's still the initial value, pass
577 void createSuspended_001()
579 myThread* newthread = new myThread();
580 sal_Bool bRes = newthread->createSuspended();
581 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!", bRes == sal_True );
583 ThreadHelper::thread_sleep_tenth_sec(1);
584 sal_Bool isRunning = newthread->isRunning();
585 ThreadHelper::thread_sleep_tenth_sec(3);
586 sal_Int32 nValue = newthread->getValue();
588 resumeAndWaitThread(newthread);
590 termAndJoinThread(newthread);
591 delete newthread;
593 CPPUNIT_ASSERT_MESSAGE(
594 "Creates a new suspended thread",
595 nValue == 0 && isRunning
599 void createSuspended_002()
601 myThread* newthread = new myThread();
602 sal_Bool res1 = newthread->createSuspended();
603 sal_Bool res2 = newthread->createSuspended();
605 resumeAndWaitThread(newthread);
607 termAndJoinThread(newthread);
609 delete newthread;
611 CPPUNIT_ASSERT_MESSAGE(
612 "Creates a new thread: can not create two threads per instance",
613 res1 && !res2
617 CPPUNIT_TEST_SUITE(createSuspended);
618 CPPUNIT_TEST(createSuspended_001);
619 // LLA: Deadlocked!!!
620 CPPUNIT_TEST(createSuspended_002);
621 CPPUNIT_TEST_SUITE_END();
622 }; // class createSuspended
624 /** when the count value equal to or more than 3, suspend the thread.
626 void suspendCountThread(OCountThread* _pCountThread)
628 sal_Int32 nValue = 0;
629 while (1)
631 nValue = _pCountThread->getValue();
632 if (nValue >= 3)
634 _pCountThread->suspend();
635 break;
640 /** Test of the osl::Thread::suspend method
642 class suspend : public CppUnit::TestFixture
644 public:
645 // initialise your test code values here.
646 void setUp()
650 void tearDown()
654 /** Use a thread which has a flag added 1 every second
656 ALGORITHM:
657 create the thread, after running special time, record value of flag, then suspend it,
658 wait a long time, check the flag, if it remains unchanged during suspending
660 void suspend_001()
662 OCountThread* aCountThread = new OCountThread();
663 sal_Bool bRes = aCountThread->create();
664 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
665 // the thread run for some seconds, but not terminate
666 suspendCountThread( aCountThread );
668 // the value just after calling suspend
669 sal_Int32 nValue = aCountThread->getValue(); // (2)
671 ThreadHelper::thread_sleep_tenth_sec(3);
673 // the value after waiting 3 seconds
674 sal_Int32 nLaterValue = aCountThread->getValue(); // (3)
676 resumeAndWaitThread(aCountThread);
677 termAndJoinThread(aCountThread);
678 delete aCountThread;
680 CPPUNIT_ASSERT_MESSAGE(
681 "Suspend the thread",
682 bRes == sal_True && nValue == nLaterValue
686 /** suspend a thread in it's worker-function, the ALGORITHM is same as suspend_001
687 reason of deadlocked I think: no schedule can schedule other threads to go on excuting
689 void suspend_002()
691 OSuspendThread* aThread = new OSuspendThread();
692 sal_Bool bRes = aThread->create();
693 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
694 // first the thread run for some seconds, but not terminate
695 sal_Int32 nValue = 0;
696 //while (1)
698 ThreadHelper::thread_sleep_tenth_sec(3);
699 nValue = aThread->getValue(); // (1)
700 t_print(" getValue is %d !", (int) nValue );
701 if (nValue >= 2)
703 aThread->setSuspend();
704 //break;
707 t_print(" after while!");
708 // the value just after calling suspend
709 nValue = aThread->getValue(); // (2)
711 ThreadHelper::thread_sleep_tenth_sec(3);
712 t_print(" after sleep!");
713 // the value after waiting 3 seconds
714 sal_Int32 nLaterValue = aThread->getValue(); // (3)
716 //resumeAndWaitThread(aThread);
717 aThread->resume();
718 termAndJoinThread(aThread);
719 delete aThread;
721 CPPUNIT_ASSERT_MESSAGE(
722 "Suspend the thread",
723 bRes == sal_True && nValue == nLaterValue
727 CPPUNIT_TEST_SUITE(suspend);
728 CPPUNIT_TEST(suspend_001);
729 // LLA: Deadlocked!!!
730 // CPPUNIT_TEST(createSuspended_002);
731 CPPUNIT_TEST_SUITE_END();
732 }; // class suspend
734 /** Test of the osl::Thread::resume method
736 class resume : public CppUnit::TestFixture
738 public:
739 // initialise your test code values here.
740 void setUp()
744 void tearDown()
748 /** check if the thread run samely as usual after suspend and resume
750 ALGORITHM:
751 compare the values before and after suspend, they should be same,
752 then compare values before and after resume, the difference should be same as the sleep seconds number
754 void resume_001()
756 OCountThread* pCountThread = new OCountThread();
757 sal_Bool bRes = pCountThread->create();
758 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
760 suspendCountThread(pCountThread);
762 sal_Int32 nSuspendValue = pCountThread->getValue(); // (2)
763 // suspend for 3 seconds
764 ThreadHelper::thread_sleep_tenth_sec(3);
765 pCountThread->resume();
767 ThreadHelper::thread_sleep_tenth_sec(3);
768 sal_Int32 nResumeValue = pCountThread->getValue();
770 ThreadHelper::thread_sleep_tenth_sec(3);
771 sal_Int32 nLaterValue = pCountThread->getValue();
773 termAndJoinThread(pCountThread);
774 delete pCountThread;
776 t_print("SuspendValue: %d\n", (int) nSuspendValue);
777 t_print("ResumeValue: %d\n", (int) nResumeValue);
778 t_print("LaterValue: %d\n", (int) nLaterValue);
780 /* LLA: this assumption is no longer relevant: nResumeValue == nSuspendValue && */
781 CPPUNIT_ASSERT_MESSAGE(
782 "Suspend then resume the thread",
783 nLaterValue >= 9 &&
784 nResumeValue > nSuspendValue &&
785 nLaterValue > nResumeValue
790 /** Create a suspended thread then resume, check if the thread has run
792 void resume_002()
794 myThread* newthread = new myThread();
795 sal_Bool bRes = newthread->createSuspended();
796 CPPUNIT_ASSERT_MESSAGE ( "Can't create thread!", bRes == sal_True );
798 newthread->resume();
799 ThreadHelper::thread_sleep_tenth_sec(2);
800 sal_Int32 nValue = newthread->getValue();
802 termAndJoinThread(newthread);
803 delete newthread;
805 t_print(" nValue = %d\n", (int) nValue);
807 CPPUNIT_ASSERT_MESSAGE(
808 "Creates a suspended thread, then resume",
809 nValue >= 1
813 CPPUNIT_TEST_SUITE(resume);
814 CPPUNIT_TEST(resume_001);
815 CPPUNIT_TEST(resume_002);
816 CPPUNIT_TEST_SUITE_END();
817 }; // class resume
819 /** Test of the osl::Thread::terminate method
821 class terminate : public CppUnit::TestFixture
823 public:
824 // initialise your test code values here.
825 void setUp()
829 void tearDown()
833 /** Check after call terminate if the running thread running go on executing
835 ALGORITHM:
836 before and after call terminate, the values should be the same
838 void terminate_001()
840 OCountThread* aCountThread = new OCountThread();
841 sal_Bool bRes = aCountThread->create();
842 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
844 ThreadHelper::thread_sleep_tenth_sec(2);
845 sal_Int32 nValue = aCountThread->getValue();
846 aCountThread->terminate();
847 ThreadHelper::thread_sleep_tenth_sec(2);
848 sal_Int32 nLaterValue = aCountThread->getValue();
850 // isRunning should be false after terminate
851 sal_Bool isRunning = aCountThread->isRunning();
852 aCountThread->join();
853 delete aCountThread;
855 t_print(" nValue = %d\n", (int) nValue);
856 t_print("nLaterValue = %d\n", (int) nLaterValue);
858 CPPUNIT_ASSERT_MESSAGE(
859 "Terminate the thread",
860 isRunning == sal_False && nLaterValue >= nValue
863 /** Check if a suspended thread will terminate after call terminate, different on w32 and on UNX
865 void terminate_002()
867 OCountThread* aCountThread = new OCountThread();
868 sal_Bool bRes = aCountThread->create();
869 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
871 ThreadHelper::thread_sleep_tenth_sec(1);
872 suspendCountThread(aCountThread);
873 sal_Int32 nValue = aCountThread->getValue();
875 // seems a suspended thread can not be terminated on W32, while on Solaris can
876 resumeAndWaitThread(aCountThread);
878 ThreadHelper::thread_sleep_tenth_sec(2);
880 termAndJoinThread(aCountThread);
881 sal_Int32 nLaterValue = aCountThread->getValue();
882 delete aCountThread;
884 t_print(" nValue = %d\n", (int) nValue);
885 t_print("nLaterValue = %d\n", (int) nLaterValue);
887 CPPUNIT_ASSERT_MESSAGE(
888 "Suspend then resume the thread",
889 nLaterValue > nValue );
892 CPPUNIT_TEST_SUITE(terminate);
893 CPPUNIT_TEST(terminate_001);
894 CPPUNIT_TEST(terminate_002);
895 CPPUNIT_TEST_SUITE_END();
896 }; // class terminate
898 /** Test of the osl::Thread::join method
900 class join : public CppUnit::TestFixture
902 public:
903 // initialise your test code values here.
904 void setUp()
908 void tearDown()
912 /** Check after call terminate if the thread running function will not go on executing
914 the next statement after join will not exec before the thread terminate
915 ALGORITHM:
916 recode system time at the beginning of the thread run, call join, then record system time again,
917 the difference of the two time should be equal or more than 20 seconds, the CountThead normally terminate
919 void join_001()
921 OCountThread *aCountThread = new OCountThread();
922 sal_Bool bRes = aCountThread->create();
923 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
925 StopWatch aStopWatch;
926 aStopWatch.start();
927 // TimeValue aTimeVal_befor;
928 // osl_getSystemTime( &aTimeVal_befor );
929 //t_print("#join:the system time is %d,%d\n", pTimeVal_befor->Seconds,pTimeVal_befor->Nanosec);
931 aCountThread->join();
933 //the below line will be executed after aCountThread terminate
934 // TimeValue aTimeVal_after;
935 // osl_getSystemTime( &aTimeVal_after );
936 aStopWatch.stop();
937 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
938 double nSec = aStopWatch.getSeconds();
939 t_print("join_001 nSec=%f\n", nSec);
940 delete aCountThread;
942 CPPUNIT_ASSERT_MESSAGE(
943 "Join the thread: after the thread terminate",
944 nSec >= 2
948 /** after terminated by another thread, join exited immediately
950 ALGORITHM:
951 terminate the thread when value>=3, call join, check the beginning time and time after join,
952 the difference should be 3 seconds, join costs little time
954 void join_002()
956 OCountThread *aCountThread = new OCountThread();
957 sal_Bool bRes = aCountThread->create();
958 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
960 //record the time when the running begin
961 // TimeValue aTimeVal_befor;
962 // osl_getSystemTime( &aTimeVal_befor );
963 StopWatch aStopWatch;
964 aStopWatch.start();
966 ThreadHelper::thread_sleep_tenth_sec(10);
967 termAndJoinThread(aCountThread);
969 //the below line will be executed after aCountThread terminate
970 // TimeValue aTimeVal_after;
971 // osl_getSystemTime( &aTimeVal_after );
972 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
973 aStopWatch.stop();
974 double nSec = aStopWatch.getSeconds();
975 t_print("join_002 nSec=%f\n", nSec);
977 delete aCountThread;
978 CPPUNIT_ASSERT_MESSAGE(
979 "Join the thread: after thread terminate by another thread",
980 nSec >= 1
984 CPPUNIT_TEST_SUITE(join);
985 CPPUNIT_TEST(join_001);
986 CPPUNIT_TEST(join_002);
987 CPPUNIT_TEST_SUITE_END();
988 }; // class join
990 /** Test of the osl::Thread::isRunning method
992 class isRunning : public CppUnit::TestFixture
994 public:
995 // initialise your test code values here.
996 void setUp()
1000 void tearDown()
1006 void isRunning_001()
1008 OCountThread *aCountThread = new OCountThread();
1009 sal_Bool bRes = aCountThread->create();
1010 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
1012 sal_Bool bRun = aCountThread->isRunning();
1014 ThreadHelper::thread_sleep_tenth_sec(2);
1015 termAndJoinThread(aCountThread);
1016 sal_Bool bTer = aCountThread->isRunning();
1017 delete aCountThread;
1019 CPPUNIT_ASSERT_MESSAGE(
1020 "Test isRunning",
1021 bRun == sal_True && bTer == sal_False
1024 /** check the value of isRunning when suspending and after resume
1026 void isRunning_002()
1028 OCountThread *aCountThread = new OCountThread();
1029 sal_Bool bRes = aCountThread->create();
1030 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
1032 // sal_Bool bRunning = aCountThread->isRunning();
1033 // sal_Int32 nValue = 0;
1034 suspendCountThread(aCountThread);
1036 sal_Bool bRunning_sup = aCountThread->isRunning();
1037 ThreadHelper::thread_sleep_tenth_sec(2);
1038 aCountThread->resume();
1039 ThreadHelper::thread_sleep_tenth_sec(2);
1040 sal_Bool bRunning_res = aCountThread->isRunning();
1041 termAndJoinThread(aCountThread);
1042 sal_Bool bRunning_ter = aCountThread->isRunning();
1043 delete aCountThread;
1045 CPPUNIT_ASSERT_MESSAGE(
1046 "Test isRunning",
1047 bRes == sal_True &&
1048 bRunning_sup == sal_True &&
1049 bRunning_res == sal_True &&
1050 bRunning_ter == sal_False
1055 CPPUNIT_TEST_SUITE(isRunning);
1056 CPPUNIT_TEST(isRunning_001);
1057 CPPUNIT_TEST(isRunning_002);
1058 CPPUNIT_TEST_SUITE_END();
1059 }; // class isRunning
1062 /// check osl::Thread::setPriority
1063 class setPriority : public CppUnit::TestFixture
1065 public:
1066 // initialise your test code values here.
1067 void setUp()
1071 void tearDown()
1075 // insert your test code here.
1076 rtl::OString getPrioName(oslThreadPriority _aPriority)
1078 rtl::OString sPrioStr;
1079 switch (_aPriority)
1081 case osl_Thread_PriorityHighest:
1082 sPrioStr = "Highest";
1083 break;
1085 case osl_Thread_PriorityAboveNormal:
1086 sPrioStr = "AboveNormal";
1087 break;
1089 case osl_Thread_PriorityNormal:
1090 sPrioStr = "Normal";
1091 break;
1093 case osl_Thread_PriorityBelowNormal:
1094 sPrioStr = "BelowNormal";
1095 break;
1097 case osl_Thread_PriorityLowest:
1098 sPrioStr = "Lowest";
1099 break;
1100 default:
1101 sPrioStr = "unknown";
1103 return sPrioStr;
1107 /** check 2 threads.
1109 ALGORITHM:
1110 Here the function should show, that 2 different threads,
1111 which only increase a value, should run at the same time with same prio.
1112 The test fails, if the difference between the two values is more than 5%
1113 but IMHO this isn't a failure, it's only a feature of the OS.
1116 void check2Threads(oslThreadPriority _aPriority)
1118 // initial 5 threads with different priorities
1119 OAddThread* pThread = new OAddThread();
1120 OAddThread* p2Thread = new OAddThread();
1122 //Create them and start running at the same time
1123 pThread->create();
1124 pThread->setPriority(_aPriority);
1125 p2Thread->create();
1126 p2Thread->setPriority(_aPriority);
1128 ThreadHelper::thread_sleep_tenth_sec(5);
1130 pThread->terminate();
1131 p2Thread->terminate();
1133 sal_Int32 nValueNormal = 0;
1134 nValueNormal = pThread->getValue();
1136 sal_Int32 nValueNormal2 = 0;
1137 nValueNormal2 = p2Thread->getValue();
1139 rtl::OString sPrio = getPrioName(_aPriority);
1140 t_print("After 10 tenth seconds\n");
1142 t_print("nValue in %s Prio Thread is %d\n",sPrio.getStr(), (int) nValueNormal);
1143 t_print("nValue in %s Prio Thread is %d\n", sPrio.getStr(), (int) nValueNormal2);
1145 // ThreadHelper::thread_sleep_tenth_sec(1);
1146 pThread->join();
1147 p2Thread->join();
1149 delete pThread;
1150 delete p2Thread;
1152 sal_Int32 nDelta = abs(nValueNormal - nValueNormal2);
1153 double nQuotient = std::max(nValueNormal, nValueNormal2);
1154 CPPUNIT_ASSERT_MESSAGE(
1155 "Quotient is zero, which means, there exist no right values.",
1156 nQuotient != 0
1158 double nDeltaPercent = nDelta / nQuotient * 100;
1160 t_print("Delta value %d, percent %f\n", (int) nDelta, nDeltaPercent);
1162 // LLA: it's not a bug if the current OS is not able to handle thread scheduling right and good.
1163 // like Windows XP
1164 // LLA: CPPUNIT_ASSERT_MESSAGE(
1165 // LLA: "Run 2 normal threads, the count diff more than 5 percent.",
1166 // LLA: nDeltaPercent <= 5
1167 // LLA: );
1170 void setPriority_001_1()
1172 check2Threads(osl_Thread_PriorityHighest);
1174 void setPriority_001_2()
1176 check2Threads(osl_Thread_PriorityAboveNormal);
1178 void setPriority_001_3()
1180 check2Threads(osl_Thread_PriorityNormal);
1182 void setPriority_001_4()
1184 check2Threads(osl_Thread_PriorityBelowNormal);
1186 void setPriority_001_5()
1188 check2Threads(osl_Thread_PriorityLowest);
1191 void setPriority_002()
1193 // initial 5 threads with different priorities
1195 OAddThread aHighestThread;
1196 OAddThread aAboveNormalThread;
1197 OAddThread aNormalThread;
1198 //OAddThread *aBelowNormalThread = new OAddThread();
1199 //OAddThread *aLowestThread = new OAddThread();
1201 //Create them and start running at the same time
1202 aHighestThread.createSuspended();
1203 aHighestThread.setPriority(osl_Thread_PriorityHighest);
1205 aAboveNormalThread.createSuspended();
1206 aAboveNormalThread.setPriority(osl_Thread_PriorityAboveNormal);
1208 aNormalThread.createSuspended();
1209 aNormalThread.setPriority(osl_Thread_PriorityNormal);
1210 /*aBelowNormalThread->create();
1211 aBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1212 aLowestThread->create();
1213 aLowestThread->setPriority(osl_Thread_PriorityLowest);
1216 aHighestThread.resume();
1217 aAboveNormalThread.resume();
1218 aNormalThread.resume();
1220 ThreadHelper::thread_sleep_tenth_sec(5);
1222 aHighestThread.suspend();
1223 aAboveNormalThread.suspend();
1224 aNormalThread.suspend();
1226 termAndJoinThread(&aNormalThread);
1227 termAndJoinThread(&aAboveNormalThread);
1228 termAndJoinThread(&aHighestThread);
1229 //aBelowNormalThread->terminate();
1230 //aLowestThread->terminate();
1232 sal_Int32 nValueHighest = 0;
1233 nValueHighest = aHighestThread.getValue();
1235 sal_Int32 nValueAboveNormal = 0;
1236 nValueAboveNormal = aAboveNormalThread.getValue();
1238 sal_Int32 nValueNormal = 0;
1239 nValueNormal = aNormalThread.getValue();
1241 t_print("After 10 tenth seconds\n");
1242 t_print("nValue in Highest Prio Thread is %d\n", (int) nValueHighest);
1243 t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal);
1244 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
1246 #ifndef WNT
1247 CPPUNIT_ASSERT_MESSAGE(
1248 "SetPriority",
1249 nValueHighest > 0 &&
1250 nValueAboveNormal > 0 &&
1251 nValueNormal > 0
1253 #endif
1256 void setPriority_003()
1258 // initial 5 threads with different priorities
1259 OAddThread *pHighestThread = new OAddThread();
1260 OAddThread *pAboveNormalThread = new OAddThread();
1261 OAddThread *pNormalThread = new OAddThread();
1262 OAddThread *pBelowNormalThread = new OAddThread();
1263 OAddThread *pLowestThread = new OAddThread();
1265 //Create them and start running at the same time
1266 pHighestThread->createSuspended();
1267 pHighestThread->setPriority(osl_Thread_PriorityHighest);
1269 pAboveNormalThread->createSuspended();
1270 pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1272 pNormalThread->createSuspended();
1273 pNormalThread->setPriority(osl_Thread_PriorityNormal);
1275 pBelowNormalThread->createSuspended();
1276 pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1278 pLowestThread->createSuspended();
1279 pLowestThread->setPriority(osl_Thread_PriorityLowest);
1281 pHighestThread->resume();
1282 pAboveNormalThread->resume();
1283 pNormalThread->resume();
1284 pBelowNormalThread->resume();
1285 pLowestThread->resume();
1287 ThreadHelper::thread_sleep_tenth_sec(5);
1289 pHighestThread->suspend();
1290 pAboveNormalThread->suspend();
1291 pNormalThread->suspend();
1292 pBelowNormalThread->suspend();
1293 pLowestThread->suspend();
1295 termAndJoinThread(pHighestThread);
1296 termAndJoinThread(pAboveNormalThread);
1297 termAndJoinThread(pNormalThread);
1298 termAndJoinThread(pBelowNormalThread);
1299 termAndJoinThread(pLowestThread);
1301 sal_Int32 nValueHighest = 0;
1302 nValueHighest = pHighestThread->getValue();
1304 sal_Int32 nValueAboveNormal = 0;
1305 nValueAboveNormal = pAboveNormalThread->getValue();
1307 sal_Int32 nValueNormal = 0;
1308 nValueNormal = pNormalThread->getValue();
1310 sal_Int32 nValueBelowNormal = 0;
1311 nValueBelowNormal = pBelowNormalThread->getValue();
1313 sal_Int32 nValueLowest = 0;
1314 nValueLowest = pLowestThread->getValue();
1316 t_print("After 10 tenth seconds\n");
1317 t_print("nValue in Highest Prio Thread is %d\n", (int) nValueHighest);
1318 t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal);
1319 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
1320 t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal);
1321 t_print("nValue in Lowest Prio Thread is %d\n", (int) nValueLowest);
1323 delete pHighestThread;
1324 delete pAboveNormalThread;
1325 delete pNormalThread;
1326 delete pBelowNormalThread;
1327 delete pLowestThread;
1329 #ifndef WNT
1330 CPPUNIT_ASSERT_MESSAGE(
1331 "SetPriority",
1332 nValueHighest > 0 &&
1333 nValueAboveNormal > 0 &&
1334 nValueNormal > 0 &&
1335 nValueBelowNormal > 0 &&
1336 nValueLowest > 0
1338 #endif
1341 void setPriority_004()
1343 // initial 5 threads with different priorities
1344 // OAddThread *pHighestThread = new OAddThread();
1345 OAddThread *pAboveNormalThread = new OAddThread();
1346 OAddThread *pNormalThread = new OAddThread();
1347 OAddThread *pBelowNormalThread = new OAddThread();
1348 OAddThread *pLowestThread = new OAddThread();
1350 //Create them and start running at the same time
1351 // pHighestThread->createSuspended();
1352 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1354 pAboveNormalThread->createSuspended();
1355 pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1357 pNormalThread->createSuspended();
1358 pNormalThread->setPriority(osl_Thread_PriorityNormal);
1360 pBelowNormalThread->createSuspended();
1361 pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1363 pLowestThread->createSuspended();
1364 pLowestThread->setPriority(osl_Thread_PriorityLowest);
1366 // pHighestThread->resume();
1367 pAboveNormalThread->resume();
1368 pNormalThread->resume();
1369 pBelowNormalThread->resume();
1370 pLowestThread->resume();
1372 ThreadHelper::thread_sleep_tenth_sec(5);
1374 // pHighestThread->suspend();
1375 pAboveNormalThread->suspend();
1376 pNormalThread->suspend();
1377 pBelowNormalThread->suspend();
1378 pLowestThread->suspend();
1380 // termAndJoinThread(pHighestThread);
1381 termAndJoinThread(pAboveNormalThread);
1382 termAndJoinThread(pNormalThread);
1383 termAndJoinThread(pBelowNormalThread);
1384 termAndJoinThread(pLowestThread);
1386 // sal_Int32 nValueHighest = 0;
1387 // nValueHighest = pHighestThread->getValue();
1389 sal_Int32 nValueAboveNormal = 0;
1390 nValueAboveNormal = pAboveNormalThread->getValue();
1392 sal_Int32 nValueNormal = 0;
1393 nValueNormal = pNormalThread->getValue();
1395 sal_Int32 nValueBelowNormal = 0;
1396 nValueBelowNormal = pBelowNormalThread->getValue();
1398 sal_Int32 nValueLowest = 0;
1399 nValueLowest = pLowestThread->getValue();
1401 t_print("After 5 tenth seconds\n");
1402 t_print("nValue in AboveNormal Prio Thread is %d\n", (int) nValueAboveNormal);
1403 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
1404 t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal);
1405 t_print("nValue in Lowest Prio Thread is %d\n", (int) nValueLowest);
1407 // delete pHighestThread;
1408 delete pAboveNormalThread;
1409 delete pNormalThread;
1410 delete pBelowNormalThread;
1411 delete pLowestThread;
1413 #ifndef WNT
1414 CPPUNIT_ASSERT_MESSAGE(
1415 "SetPriority",
1416 /* nValueHighest > 0 && */
1417 nValueAboveNormal > 0 &&
1418 nValueNormal > 0 &&
1419 nValueBelowNormal > 0 &&
1420 nValueLowest > 0
1422 #endif
1424 void setPriority_005()
1426 // initial 5 threads with different priorities
1427 // OAddThread *pHighestThread = new OAddThread();
1428 // OAddThread *pAboveNormalThread = new OAddThread();
1429 OAddThread *pNormalThread = new OAddThread();
1430 OAddThread *pBelowNormalThread = new OAddThread();
1431 OAddThread *pLowestThread = new OAddThread();
1433 //Create them and start running at the same time
1434 // pHighestThread->createSuspended();
1435 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1437 // pAboveNormalThread->createSuspended();
1438 // pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1440 pNormalThread->createSuspended();
1441 pNormalThread->setPriority(osl_Thread_PriorityNormal);
1443 pBelowNormalThread->createSuspended();
1444 pBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1446 pLowestThread->createSuspended();
1447 pLowestThread->setPriority(osl_Thread_PriorityLowest);
1449 // pHighestThread->resume();
1450 // pAboveNormalThread->resume();
1451 pNormalThread->resume();
1452 pBelowNormalThread->resume();
1453 pLowestThread->resume();
1455 ThreadHelper::thread_sleep_tenth_sec(5);
1457 // pHighestThread->suspend();
1458 // pAboveNormalThread->suspend();
1459 pNormalThread->suspend();
1460 pBelowNormalThread->suspend();
1461 pLowestThread->suspend();
1463 // termAndJoinThread(pHighestThread);
1464 // termAndJoinThread(pAboveNormalThread);
1465 termAndJoinThread(pNormalThread);
1466 termAndJoinThread(pBelowNormalThread);
1467 termAndJoinThread(pLowestThread);
1469 // sal_Int32 nValueHighest = 0;
1470 // nValueHighest = pHighestThread->getValue();
1472 // sal_Int32 nValueAboveNormal = 0;
1473 // nValueAboveNormal = pAboveNormalThread->getValue();
1475 sal_Int32 nValueNormal = 0;
1476 nValueNormal = pNormalThread->getValue();
1478 sal_Int32 nValueBelowNormal = 0;
1479 nValueBelowNormal = pBelowNormalThread->getValue();
1481 sal_Int32 nValueLowest = 0;
1482 nValueLowest = pLowestThread->getValue();
1484 t_print("After 5 tenth seconds\n");
1485 t_print("nValue in Normal Prio Thread is %d\n", (int) nValueNormal);
1486 t_print("nValue in BelowNormal Prio Thread is %d\n", (int) nValueBelowNormal);
1487 t_print("nValue in Lowest Prio Thread is %d\n", (int) nValueLowest);
1489 delete pNormalThread;
1490 delete pBelowNormalThread;
1491 delete pLowestThread;
1493 #ifndef WNT
1494 CPPUNIT_ASSERT_MESSAGE(
1495 "SetPriority",
1496 /* nValueHighest > 0 && */
1497 /* nValueAboveNormal > 0 && */
1498 nValueNormal > 0 &&
1499 nValueBelowNormal > 0 &&
1500 nValueLowest > 0
1502 #endif
1506 CPPUNIT_TEST_SUITE(setPriority);
1507 #ifndef SOLARIS
1508 CPPUNIT_TEST(setPriority_002);
1509 CPPUNIT_TEST(setPriority_003);
1510 CPPUNIT_TEST(setPriority_004);
1511 CPPUNIT_TEST(setPriority_005);
1512 #endif
1513 CPPUNIT_TEST(setPriority_001_1);
1514 CPPUNIT_TEST(setPriority_001_2);
1515 CPPUNIT_TEST(setPriority_001_3);
1516 CPPUNIT_TEST(setPriority_001_4);
1517 CPPUNIT_TEST(setPriority_001_5);
1518 CPPUNIT_TEST_SUITE_END();
1519 }; // class setPriority
1521 /** Test of the osl::Thread::getPriority method
1523 class getPriority : public CppUnit::TestFixture
1525 public:
1526 // initialise your test code values here.
1527 void setUp()
1531 void tearDown()
1535 // insert your test code here.
1536 void getPriority_001()
1538 OAddThread *pHighestThread = new OAddThread();
1540 //Create them and start running at the same time
1541 pHighestThread->create();
1542 pHighestThread->setPriority(osl_Thread_PriorityHighest);
1544 oslThreadPriority aPriority = pHighestThread->getPriority();
1545 termAndJoinThread(pHighestThread);
1546 delete pHighestThread;
1548 ThreadHelper::outputPriority(aPriority);
1550 // LLA: Priority settings may not work within some OS versions.
1551 #if ( defined WNT ) || ( defined SOLARIS )
1552 CPPUNIT_ASSERT_MESSAGE(
1553 "getPriority",
1554 aPriority == osl_Thread_PriorityHighest
1556 #else
1557 // LLA: Linux
1558 // NO_PTHREAD_PRIORITY ???
1559 CPPUNIT_ASSERT_MESSAGE(
1560 "getPriority",
1561 aPriority == osl_Thread_PriorityNormal
1563 #endif
1566 void getPriority_002()
1571 CPPUNIT_TEST_SUITE(getPriority);
1572 CPPUNIT_TEST(getPriority_001);
1573 CPPUNIT_TEST(getPriority_002);
1574 CPPUNIT_TEST_SUITE_END();
1575 }; // class getPriority
1578 class getIdentifier : public CppUnit::TestFixture
1580 public:
1581 // initialise your test code values here.
1582 void setUp()
1586 void tearDown()
1590 // insert your test code here.
1591 void getIdentifier_001()
1596 void getIdentifier_002()
1601 CPPUNIT_TEST_SUITE(getIdentifier);
1602 CPPUNIT_TEST(getIdentifier_001);
1603 CPPUNIT_TEST(getIdentifier_002);
1604 CPPUNIT_TEST_SUITE_END();
1605 }; // class getIdentifier
1607 /** Test of the osl::Thread::getCurrentIdentifier method
1609 class getCurrentIdentifier : public CppUnit::TestFixture
1611 public:
1612 // initialise your test code values here.
1613 void setUp()
1617 void tearDown()
1621 // insert your test code here.
1622 void getCurrentIdentifier_001()
1624 oslThreadIdentifier oId;
1625 OCountThread* pCountThread = new OCountThread;
1626 pCountThread->create();
1627 pCountThread->setWait(3);
1628 oId = Thread::getCurrentIdentifier();
1629 oslThreadIdentifier oIdChild = pCountThread->getIdentifier();
1630 termAndJoinThread(pCountThread);
1631 delete pCountThread;
1633 CPPUNIT_ASSERT_MESSAGE(
1634 "Get the identifier for the current active thread.",
1635 oId != oIdChild
1640 void getCurrentIdentifier_002()
1644 CPPUNIT_TEST_SUITE(getCurrentIdentifier);
1645 CPPUNIT_TEST(getCurrentIdentifier_001);
1646 //CPPUNIT_TEST(getCurrentIdentifier_002);
1647 CPPUNIT_TEST_SUITE_END();
1648 }; // class getCurrentIdentifier
1650 /** Test of the osl::Thread::wait method
1652 class wait : public CppUnit::TestFixture
1654 public:
1655 // initialise your test code values here.
1656 void setUp()
1660 void tearDown()
1664 /** call wait in the run method
1666 ALGORITHM:
1667 tested thread wait nWaitSec seconds, main thread sleep (2) seconds,
1668 then terminate the tested thread, due to the fact that the thread do a sleep(1) + wait(5)
1669 it's finish after 6 seconds.
1671 void wait_001()
1673 OCountThread *aCountThread = new OCountThread();
1674 sal_Int32 nWaitSec = 5;
1675 aCountThread->setWait(nWaitSec);
1676 // thread runs at least 5 seconds.
1677 sal_Bool bRes = aCountThread->create();
1678 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
1680 //record the time when the running begin
1681 StopWatch aStopWatch;
1682 aStopWatch.start();
1684 // wait a little bit, to let the thread the time, to start
1685 ThreadHelper::thread_sleep_tenth_sec( 4 );
1687 // if wait works,
1688 // this function returns, after 4 sec. later
1689 termAndJoinThread(aCountThread);
1691 // value should be one.
1692 sal_Int32 nValue = aCountThread->getValue();
1694 aStopWatch.stop();
1696 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
1697 double nTenthSec = aStopWatch.getTenthSec();
1698 double nSec = aStopWatch.getSeconds();
1699 delete aCountThread;
1700 t_print("nTenthSec = %f \n", nTenthSec);
1701 t_print("nSec = %f \n", nSec);
1702 t_print("nValue = %d \n", (int) nValue);
1704 CPPUNIT_ASSERT_MESSAGE(
1705 "Wait: Blocks the calling thread for the given number of time.",
1706 nTenthSec >= 5 && nValue == 1
1711 CPPUNIT_TEST_SUITE(wait);
1712 CPPUNIT_TEST(wait_001);
1713 CPPUNIT_TEST_SUITE_END();
1714 }; // class wait
1716 /** osl::Thread::yield method: can not design good test scenario to test up to now
1718 class yield : public CppUnit::TestFixture
1720 public:
1721 void setUp()
1725 void tearDown()
1729 // insert your test code here.
1730 void yield_001()
1735 CPPUNIT_TEST_SUITE(yield);
1736 CPPUNIT_TEST(yield_001);
1737 CPPUNIT_TEST_SUITE_END();
1738 }; // class yield
1740 /** Test of the osl::Thread::schedule method
1742 class schedule : public CppUnit::TestFixture
1744 public:
1745 // initialise your test code values here.
1746 void setUp()
1750 void tearDown()
1754 /** The requested thread will get terminate the next time schedule() is called.
1756 Note: on UNX, if call suspend thread is not the to be suspended thread, the to be
1757 suspended thread will get suspended the next time schedule() is called,
1758 while on w32, it's nothing with schedule.
1760 check if suspend and terminate work well via schedule
1762 void schedule_001()
1764 OAddThread* aThread = new OAddThread();
1765 sal_Bool bRes = aThread->create();
1766 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
1768 ThreadHelper::thread_sleep_tenth_sec(2);
1769 aThread->suspend();
1770 ThreadHelper::thread_sleep_tenth_sec(1);
1771 sal_Int32 nValue = aThread->getValue();
1772 ThreadHelper::thread_sleep_tenth_sec(3);
1773 sal_Int32 nLaterValue = aThread->getValue();
1774 // resumeAndWaitThread(aThread);
1775 t_print(" value = %d\n", (int) nValue);
1776 t_print("later value = %d\n", (int) nLaterValue);
1777 // if value and latervalue not equal, than the thread would not suspended
1779 CPPUNIT_ASSERT_MESSAGE(
1780 "Schedule: suspend works.",
1781 nLaterValue == nValue
1784 aThread->resume();
1785 ThreadHelper::thread_sleep_tenth_sec(2);
1787 aThread->terminate();
1788 sal_Int32 nValue_term = aThread->getValue();
1790 aThread->join();
1791 sal_Int32 nValue_join = aThread->getValue();
1793 t_print("value after term = %d\n", (int) nValue_term);
1794 t_print("value after join = %d\n", (int) nValue_join);
1796 // nValue_term and nValue_join should be the same
1797 // but should be differ from nValue
1799 delete aThread;
1800 //check if thread really terminate after call terminate, if join immediatlly return
1801 CPPUNIT_ASSERT_MESSAGE(
1802 "Schedule: Returns False if the thread should terminate.",
1803 nValue_join - nValue_term <= 1 && nValue_join - nValue_term >= 0
1808 /** design a thread that has not call schedule in the workfunction--run method
1810 void schedule_002()
1812 ONoScheduleThread aThread; // this thread runs 10 sec. (no schedule() used)
1813 sal_Bool bRes = aThread.create();
1814 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes == sal_True );
1816 ThreadHelper::thread_sleep_tenth_sec(2);
1817 aThread.suspend();
1818 sal_Int32 nValue = aThread.getValue();
1820 ThreadHelper::thread_sleep_tenth_sec(3);
1821 sal_Int32 nLaterValue = aThread.getValue();
1822 ThreadHelper::thread_sleep_tenth_sec(5);
1824 resumeAndWaitThread(&aThread);
1826 t_print(" value = %d\n", (int) nValue);
1827 t_print("later value = %d\n", (int) nLaterValue);
1829 //On windows, suspend works, so the values are same
1830 #ifdef WNT
1831 CPPUNIT_ASSERT_MESSAGE(
1832 "Schedule: don't schedule in thread run method, suspend works.",
1833 nLaterValue == nValue
1835 #endif
1837 //On UNX, suspend does not work, so the difference of the values equals to sleep seconds number
1838 #ifdef UNX
1839 aThread.resume();
1840 CPPUNIT_ASSERT_MESSAGE(
1841 "Schedule: don't schedule in thread run method, suspend does not work too.",
1842 nLaterValue > nValue
1844 #endif
1846 // terminate will not work if no schedule in thread's work function
1847 termAndJoinThread(&aThread);
1848 sal_Int32 nValue_term = aThread.getValue();
1850 t_print(" value term = %d\n", (int) nValue_term);
1852 CPPUNIT_ASSERT_MESSAGE(
1853 "Schedule: don't schedule in thread run method, terminate failed.",
1854 nValue_term == 10
1858 CPPUNIT_TEST_SUITE(schedule);
1859 CPPUNIT_TEST(schedule_001);
1860 CPPUNIT_TEST(schedule_002);
1861 CPPUNIT_TEST_SUITE_END();
1862 }; // class schedule
1864 // -----------------------------------------------------------------------------
1865 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::create, "osl_Thread");
1866 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::createSuspended, "osl_Thread");
1867 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::suspend, "osl_Thread");
1868 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::resume, "osl_Thread");
1869 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::terminate, "osl_Thread");
1870 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::join, "osl_Thread");
1871 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::isRunning, "osl_Thread");
1872 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::setPriority, "osl_Thread");
1873 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getPriority, "osl_Thread");
1874 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getIdentifier, "osl_Thread");
1875 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getCurrentIdentifier, "osl_Thread");
1876 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::wait, "osl_Thread");
1877 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::yield, "osl_Thread");
1878 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::schedule, "osl_Thread");
1879 } // namespace osl_Thread
1882 // -----------------------------------------------------------------------------
1883 // destroy function when the binding thread terminate
1884 void SAL_CALL destroyCallback(void * data)
1886 delete[] (char *) data;
1889 static ThreadData myThreadData(destroyCallback);
1893 class myKeyThread : public Thread
1895 public:
1896 // a public char member for test result checking
1897 char m_Char_Test;
1898 // for pass thread-special data to thread
1899 myKeyThread(const char cData)
1901 m_nData = cData;
1903 private:
1904 char m_nData;
1906 void SAL_CALL run()
1908 char * pc = new char[2];
1909 // strcpy(pc, &m_nData);
1910 memcpy(pc, &m_nData, 1);
1911 pc[1] = '\0';
1913 myThreadData.setData(pc);
1914 char* pData = (char*)myThreadData.getData();
1915 m_Char_Test = *pData;
1916 // wait for long time to check the data value in main thread
1917 ThreadHelper::thread_sleep_tenth_sec(3);
1919 public:
1920 ~myKeyThread()
1922 if (isRunning())
1924 t_print("error: not terminated.\n");
1929 static ThreadData idData;
1931 class idThread: public Thread
1933 public:
1934 oslThreadIdentifier m_Id;
1935 private:
1936 void SAL_CALL run()
1938 oslThreadIdentifier* pId = new oslThreadIdentifier;
1939 *pId = getIdentifier();
1940 idData.setData(pId);
1941 oslThreadIdentifier* pIdData = (oslThreadIdentifier*)idData.getData();
1942 //t_print("Thread %d has Data %d\n", getIdentifier(), *pIdData);
1943 m_Id = *pIdData;
1944 delete pId;
1947 public:
1948 ~idThread()
1950 if (isRunning())
1952 t_print("error: not terminated.\n");
1957 namespace osl_ThreadData
1960 class ctors : public CppUnit::TestFixture
1962 public:
1963 // initialise your test code values here.
1964 void setUp()
1968 void tearDown()
1972 // insert your test code here.
1973 void ctor_001()
1978 CPPUNIT_TEST_SUITE(ctors);
1979 CPPUNIT_TEST(ctor_001);
1980 CPPUNIT_TEST_SUITE_END();
1981 }; // class ctors
1984 class setData : public CppUnit::TestFixture
1986 public:
1987 // initialise your test code values here.
1988 void setUp()
1992 void tearDown()
1996 /** the same instance of the class can have different values in different threads
1998 void setData_001()
2000 idThread aThread1;
2001 aThread1.create();
2002 idThread aThread2;
2003 aThread2.create();
2005 aThread1.join();
2006 aThread2.join();
2008 oslThreadIdentifier aThreadId1 = aThread1.getIdentifier();
2009 oslThreadIdentifier aThreadId2 = aThread2.getIdentifier();
2011 CPPUNIT_ASSERT_MESSAGE(
2012 "ThreadData setData: ",
2013 aThread1.m_Id == aThreadId1 && aThread2.m_Id == aThreadId2
2018 void setData_002()
2020 // at first, set the data a value
2021 char* pc = new char[2];
2022 char m_nData = 'm';
2023 // LLA: this is a copy functions only and really only for \0 terminated strings
2024 // m_nData is not a string, it's a character
2025 // strcpy(pc, &m_nData);
2026 memcpy(pc, &m_nData, 1);
2027 pc[1] = '\0';
2029 myThreadData.setData(pc);
2031 myKeyThread aThread1('a');
2032 aThread1.create();
2033 myKeyThread aThread2('b');
2034 aThread2.create();
2035 // aThread1 and aThread2 should have not terminated yet, check current data, not 'a' 'b'
2036 char* pChar = (char*)myThreadData.getData();
2037 char aChar = *pChar;
2039 aThread1.join();
2040 aThread2.join();
2042 // the saved thread data of aThread1 & aThread2, different
2043 char cData1 = aThread1.m_Char_Test;
2044 char cData2 = aThread2.m_Char_Test;
2046 CPPUNIT_ASSERT_MESSAGE(
2047 "ThreadData setData: ",
2048 cData1 == 'a' && cData2 == 'b' && aChar == 'm'
2052 /** setData the second time, and then getData
2054 void setData_003()
2056 // at first, set the data a value
2057 char* pc = new char[2];
2058 char m_nData = 'm';
2059 memcpy(pc, &m_nData, 1);
2060 pc[1] = '\0';
2061 myThreadData.setData(pc);
2063 myKeyThread aThread1('a');
2064 aThread1.create();
2065 myKeyThread aThread2('b');
2066 aThread2.create();
2067 // aThread1 and aThread2 should have not terminated yet
2068 // setData the second time
2069 char* pc2 = new char[2];
2070 m_nData = 'o';
2071 memcpy(pc2, &m_nData, 1);
2072 pc2[1] = '\0';
2074 myThreadData.setData(pc2);
2075 char* pChar = (char*)myThreadData.getData();
2076 char aChar = *pChar;
2078 aThread1.join();
2079 aThread2.join();
2081 // the saved thread data of aThread1 & aThread2, different
2082 char cData1 = aThread1.m_Char_Test;
2083 char cData2 = aThread2.m_Char_Test;
2085 CPPUNIT_ASSERT_MESSAGE(
2086 "ThreadData setData: ",
2087 cData1 == 'a' && cData2 == 'b' && aChar == 'o'
2091 CPPUNIT_TEST_SUITE(setData);
2092 CPPUNIT_TEST(setData_001);
2093 CPPUNIT_TEST(setData_002);
2094 CPPUNIT_TEST(setData_003);
2095 CPPUNIT_TEST_SUITE_END();
2096 }; // class setData
2098 class getData : public CppUnit::TestFixture
2100 public:
2101 // initialise your test code values here.
2102 void setUp()
2106 void tearDown()
2110 // After setData in child threads, get Data in the main thread, should be independent
2111 void getData_001()
2113 char* pc = new char[2];
2114 char m_nData[] = "i";
2115 strcpy(pc, m_nData);
2116 myThreadData.setData(pc);
2118 myKeyThread aThread1('c');
2119 aThread1.create();
2120 myKeyThread aThread2('d');
2121 aThread2.create();
2123 aThread1.join();
2124 aThread2.join();
2126 char cData1 = aThread1.m_Char_Test;
2127 char cData2 = aThread2.m_Char_Test;
2129 char* pChar = (char*)myThreadData.getData();
2130 char aChar = *pChar;
2132 CPPUNIT_ASSERT_MESSAGE(
2133 "ThreadData setData: ",
2134 cData1 == 'c' && cData2 == 'd' && aChar == 'i'
2138 // setData then change the value in the address data pointer points,
2139 // and then getData, should get the new value
2140 void getData_002()
2142 char* pc = new char[2];
2143 char m_nData = 'i';
2144 memcpy(pc, &m_nData, 1);
2145 pc[1] = '\0';
2147 myThreadData.setData(pc);
2149 myKeyThread aThread1('a');
2150 aThread1.create();
2151 myKeyThread aThread2('b');
2152 aThread2.create();
2154 // change the value which pc points
2155 char m_nData2 = 'j';
2156 memcpy(pc, &m_nData2, 1);
2157 pc[1] = '\0';
2159 void* pChar = myThreadData.getData();
2160 char aChar = *(char*)pChar;
2162 aThread1.join();
2163 aThread2.join();
2165 char cData1 = aThread1.m_Char_Test;
2166 char cData2 = aThread2.m_Char_Test;
2168 CPPUNIT_ASSERT_MESSAGE(
2169 "ThreadData setData: ",
2170 cData1 == 'a' && cData2 == 'b' && aChar == 'j'
2175 CPPUNIT_TEST_SUITE(getData);
2176 CPPUNIT_TEST(getData_001);
2177 CPPUNIT_TEST(getData_002);
2178 CPPUNIT_TEST_SUITE_END();
2179 }; // class getData
2181 // -----------------------------------------------------------------------------
2182 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::ctors);
2183 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::setData);
2184 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::getData);
2185 } // namespace osl_ThreadData
2187 // this macro creates an empty function, which will called by the RegisterAllFunctions()
2188 // to let the user the possibility to also register some functions by hand.
2189 CPPUNIT_PLUGIN_IMPLEMENT();
2191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */