LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sal / qa / osl / process / osl_Thread.cxx
blob3f62c6ea3e067264a53afbee97631a967dcebc8c
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 _WIN32
22 #if !defined WIN32_LEAN_AND_MEAN
23 # define WIN32_LEAN_AND_MEAN
24 #endif
25 #include <windows.h>
26 #else
27 #include <unistd.h>
28 #include <time.h>
29 #endif
31 // include files
33 #include <sal/types.h>
34 #include <rtl/string.hxx>
35 #include <rtl/strbuf.hxx>
36 #include <osl/thread.hxx>
37 #include <osl/mutex.hxx>
38 #include <osl/time.h>
40 #include <string.h>
41 #include <memory>
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 namespace {
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();
67 void start(); // Starts time
68 void stop(); // Stops time
70 double getSeconds() const;
71 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()
82 : m_nNanoSec(0)
83 , m_nSeconds(0)
84 , m_bIsValid(false)
85 , m_bIsRunning(false)
87 t1.Seconds = 0;
88 t1.Nanosec = 0;
89 t2.Seconds = 0;
90 t2.Nanosec = 0;
93 void StopWatch::start()
95 // pre: %
96 // post: Start Timer
98 m_bIsValid = false;
99 m_bIsRunning = true;
100 osl_getSystemTime( &t1 );
101 t_print("# %u %u nsecs\n", static_cast<unsigned>(t1.Seconds), static_cast<unsigned>(t1.Nanosec));
102 // gettimeofday(&t1, 0);
105 void StopWatch::stop()
107 // pre: Timer should be started
108 // post: Timer will stopped
110 osl_getSystemTime( &t2 );
111 t_print("# %u %u nsecs\n", static_cast<unsigned>(t2.Seconds), static_cast<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", static_cast<unsigned>(m_nSeconds), static_cast<unsigned>(m_nNanoSec) );
124 m_bIsValid = true;
125 m_bIsRunning = false;
129 double StopWatch::getSeconds() const
131 // pre: valid = TRUE
132 // BACK: time in seconds
134 double nValue = 0.0;
135 if (m_bIsValid)
137 nValue = double(m_nNanoSec) / 1000000000.0 + m_nSeconds; // milli micro nano
139 return nValue;
142 double StopWatch::getTenthSec() const
144 double nValue = 0.0;
145 if (m_bIsValid)
147 nValue = double(m_nNanoSec) / 100000000.0 + m_nSeconds * 10;
149 return nValue ;
152 namespace {
154 template <class T>
155 class ThreadSafeValue
157 T m_nFlag;
158 Mutex m_aMutex;
159 public:
160 explicit ThreadSafeValue(T n = 0): m_nFlag(n) {}
161 T getValue()
163 //block if already acquired by another thread.
164 osl::MutexGuard g(m_aMutex);
165 return m_nFlag;
167 void incValue()
169 //only one thread operate on the flag.
170 osl::MutexGuard g(m_aMutex);
171 m_nFlag++;
173 void acquire() {m_aMutex.acquire();}
174 void release() {m_aMutex.release();}
179 namespace ThreadHelper
181 static void thread_sleep_tenth_sec(sal_Int32 _nTenthSec)
183 osl::Thread::wait(std::chrono::milliseconds(_nTenthSec * 100));
186 static void outputPriority(oslThreadPriority const& _aPriority)
188 // LLA: output the priority
189 if (_aPriority == osl_Thread_PriorityHighest)
191 t_print("Prio is High\n");
193 else if (_aPriority == osl_Thread_PriorityAboveNormal)
195 t_print("Prio is above normal\n");
197 else if (_aPriority == osl_Thread_PriorityNormal)
199 t_print("Prio is normal\n");
201 else if (_aPriority == osl_Thread_PriorityBelowNormal)
203 t_print("Prio is below normal\n");
205 else if (_aPriority == osl_Thread_PriorityLowest)
207 t_print("Prio is lowest\n");
209 else
211 t_print("Prio is unknown\n");
216 namespace {
218 /** Simple thread for testing Thread-create.
220 Just add 1 of value 0, and after running, result is 1.
222 class myThread : public Thread
224 ThreadSafeValue<sal_Int32> m_aFlag;
225 public:
226 sal_Int32 getValue() { return m_aFlag.getValue(); }
227 protected:
228 /** guarded value which initialized 0
230 @see ThreadSafeValue
232 void SAL_CALL run() override
234 while(schedule())
236 m_aFlag.incValue();
237 ThreadHelper::thread_sleep_tenth_sec(1);
241 public:
243 virtual void SAL_CALL suspend() override
245 m_aFlag.acquire();
246 ::osl::Thread::suspend();
247 m_aFlag.release();
250 virtual ~myThread() override
252 if (isRunning())
254 t_print("error: not terminated.\n");
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() : m_nWaitSec(0)
268 t_print("new OCountThread thread %u!\n", static_cast<unsigned>(getIdentifier()));
270 sal_Int32 getValue() { return m_aFlag.getValue(); }
272 void setWait(sal_Int32 nSec)
274 m_nWaitSec = nSec;
275 //m_bWait = sal_True;
278 virtual void SAL_CALL suspend() override
280 m_aFlag.acquire();
281 ::osl::Thread::suspend();
282 m_aFlag.release();
285 protected:
286 //sal_Bool m_bWait;
287 sal_Int32 m_nWaitSec;
289 void SAL_CALL run() override
291 /// if the thread should terminate, schedule return false
292 while (m_aFlag.getValue() < 20 && schedule())
294 m_aFlag.incValue();
295 ThreadHelper::thread_sleep_tenth_sec(1);
297 if (m_nWaitSec != 0)
299 TimeValue nTV;
300 nTV.Seconds = m_nWaitSec / 10 ;
301 nTV.Nanosec = ( m_nWaitSec%10 ) * 100000000 ;
302 wait( nTV );
303 m_nWaitSec = 0;
307 void SAL_CALL onTerminated() override
309 t_print("normally terminate this thread %u!\n", static_cast<unsigned>(getIdentifier()));
311 public:
313 virtual ~OCountThread() override
315 if (isRunning())
317 t_print("error: not terminated.\n");
323 /** no call schedule in the run method
325 class ONoScheduleThread : public Thread
327 ThreadSafeValue<sal_Int32> m_aFlag;
328 public:
329 sal_Int32 getValue() { return m_aFlag.getValue(); }
331 virtual void SAL_CALL suspend() override
333 m_aFlag.acquire();
334 ::osl::Thread::suspend();
335 m_aFlag.release();
337 protected:
338 void SAL_CALL run() override
340 while (m_aFlag.getValue() < 10)
342 m_aFlag.incValue();
343 ThreadHelper::thread_sleep_tenth_sec(1);
346 void SAL_CALL onTerminated() override
348 t_print("normally terminate this thread %u!\n", static_cast<unsigned>(getIdentifier()));
350 public:
351 ONoScheduleThread()
353 t_print("new thread id %u!\n", static_cast<unsigned>(getIdentifier()));
355 virtual ~ONoScheduleThread() override
357 if (isRunning())
359 t_print("error: not terminated.\n");
366 class OAddThread : public Thread
368 ThreadSafeValue<sal_Int32> m_aFlag;
369 public:
370 //oslThreadIdentifier m_id, m_CurId;
371 OAddThread(){}
372 sal_Int32 getValue() { return m_aFlag.getValue(); }
374 virtual void SAL_CALL suspend() override
376 m_aFlag.acquire();
377 ::osl::Thread::suspend();
378 m_aFlag.release();
380 protected:
381 void SAL_CALL run() override
383 //if the thread should terminate, schedule return false
384 while (schedule())
386 m_aFlag.incValue();
389 void SAL_CALL onTerminated() override
391 // t_print("normally terminate this thread %d!\n", getIdentifier());
393 public:
395 virtual ~OAddThread() override
397 if (isRunning())
399 // t_print("error: not terminated.\n");
407 namespace osl_Thread
410 static void resumeAndWaitThread(Thread* _pThread)
412 // This function starts a thread, wait a second and suspends the thread
413 // Due to the fact, that a suspend and never run thread never really exists.
415 // Note: on UNX, after createSuspended, and then terminate the thread, it performs well;
416 // while on Windows, after createSuspended, the thread can not terminate, wait endlessly,
417 // so here call resume at first, then call terminate.
418 #ifdef _WIN32
419 t_print("resumeAndWaitThread\n");
420 _pThread->resume();
421 ThreadHelper::thread_sleep_tenth_sec(1);
422 #else
423 _pThread->resume();
424 #endif
427 // kill a running thread and join it, if it has terminated, do nothing
428 static void termAndJoinThread(Thread* _pThread)
430 _pThread->terminate();
432 // LLA: Windows feature???, a suspended thread can not terminated, so we have to weak it up
433 #ifdef _WIN32
434 _pThread->resume();
435 ThreadHelper::thread_sleep_tenth_sec(1);
436 #endif
437 t_print("#wait for join.\n");
438 _pThread->join();
440 /** Test of the osl::Thread::create method
443 class create : public CppUnit::TestFixture
445 public:
446 /** Simple create a thread.
448 Create a simple thread, it just does add 1 to value(which initialized 0),
449 if the thread run, the value should be 1.
451 void create_001()
453 myThread* newthread = new myThread;
454 bool bRes = newthread->create();
455 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!\n", bRes);
457 ThreadHelper::thread_sleep_tenth_sec(1); // wait short
458 bool isRunning = newthread->isRunning(); // check if thread is running
459 /// wait for the new thread to assure it has run
460 ThreadHelper::thread_sleep_tenth_sec(3);
461 sal_Int32 nValue = newthread->getValue();
462 /// to assure the new thread has terminated
463 termAndJoinThread(newthread);
464 delete newthread;
466 t_print(" nValue = %d\n", static_cast<int>(nValue));
467 t_print("isRunning = %s\n", isRunning ? "true" : "false");
469 CPPUNIT_ASSERT_MESSAGE(
470 "Creates a new thread",
471 nValue >= 1
473 CPPUNIT_ASSERT_MESSAGE(
474 "Creates a new thread",
475 isRunning
480 /** only one running thread per instance, return false if create secondly
482 void create_002()
484 myThread* newthread = new myThread;
485 bool res1 = newthread->create();
486 bool res2 = newthread->create();
487 t_print("In non pro, an assertion should occurred. This behaviour is right.\n");
488 termAndJoinThread(newthread);
489 delete newthread;
491 CPPUNIT_ASSERT_MESSAGE(
492 "Creates a new thread: can not create two threads per instance",
493 res1
495 CPPUNIT_ASSERT_MESSAGE(
496 "Creates a new thread: can not create two threads per instance",
497 !res2
502 CPPUNIT_TEST_SUITE(create);
503 CPPUNIT_TEST(create_001);
504 CPPUNIT_TEST(create_002);
505 CPPUNIT_TEST_SUITE_END();
506 }; // class create
508 /** Test of the osl::Thread::createSuspended method
510 class createSuspended : public CppUnit::TestFixture
512 public:
513 /** Create a suspended thread, use the same class as create_001
515 after create, wait enough time, check the value, if it's still the initial value, pass
517 void createSuspended_001()
519 myThread* newthread = new myThread;
520 bool bRes = newthread->createSuspended();
521 CPPUNIT_ASSERT_MESSAGE("Can not creates a new thread!", bRes);
523 ThreadHelper::thread_sleep_tenth_sec(1);
524 bool isRunning = newthread->isRunning();
525 ThreadHelper::thread_sleep_tenth_sec(3);
526 sal_Int32 nValue = newthread->getValue();
528 resumeAndWaitThread(newthread);
530 termAndJoinThread(newthread);
531 delete newthread;
533 CPPUNIT_ASSERT_EQUAL_MESSAGE(
534 "Creates a new suspended thread",
535 sal_Int32(0), nValue
537 CPPUNIT_ASSERT_MESSAGE(
538 "Creates a new suspended thread",
539 isRunning
543 void createSuspended_002()
545 myThread* newthread = new myThread;
546 bool res1 = newthread->createSuspended();
547 bool res2 = newthread->createSuspended();
549 resumeAndWaitThread(newthread);
551 termAndJoinThread(newthread);
553 delete newthread;
555 CPPUNIT_ASSERT_MESSAGE(
556 "Creates a new thread: can not create two threads per instance",
557 res1
559 CPPUNIT_ASSERT_MESSAGE(
560 "Creates a new thread: can not create two threads per instance",
561 !res2
565 CPPUNIT_TEST_SUITE(createSuspended);
566 CPPUNIT_TEST(createSuspended_001);
567 // LLA: Deadlocked!!!
568 CPPUNIT_TEST(createSuspended_002);
569 CPPUNIT_TEST_SUITE_END();
570 }; // class createSuspended
572 /** when the count value equal to or more than 3, suspend the thread.
574 static void suspendCountThread(OCountThread* _pCountThread)
576 sal_Int32 nValue = 0;
577 while (true)
579 nValue = _pCountThread->getValue();
580 if (nValue >= 3)
582 _pCountThread->suspend();
583 break;
588 /** Test of the osl::Thread::suspend method
590 class suspend : public CppUnit::TestFixture
592 public:
593 /** Use a thread which has a flag added 1 every second
595 ALGORITHM:
596 create the thread, after running special time, record value of flag, then suspend it,
597 wait a long time, check the flag, if it remains unchanged during suspending
599 void suspend_001()
601 OCountThread* aCountThread = new OCountThread();
602 bool bRes = aCountThread->create();
603 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
604 // the thread run for some seconds, but not terminate
605 suspendCountThread( aCountThread );
607 // the value just after calling suspend
608 sal_Int32 nValue = aCountThread->getValue(); // (2)
610 ThreadHelper::thread_sleep_tenth_sec(3);
612 // the value after waiting 3 seconds
613 sal_Int32 nLaterValue = aCountThread->getValue(); // (3)
615 resumeAndWaitThread(aCountThread);
616 termAndJoinThread(aCountThread);
617 delete aCountThread;
619 CPPUNIT_ASSERT_MESSAGE(
620 "Suspend the thread",
621 bRes
623 CPPUNIT_ASSERT_EQUAL_MESSAGE(
624 "Suspend the thread",
625 nValue, nLaterValue
630 CPPUNIT_TEST_SUITE(suspend);
631 CPPUNIT_TEST(suspend_001);
632 // LLA: Deadlocked!!!
633 // CPPUNIT_TEST(createSuspended_002);
634 CPPUNIT_TEST_SUITE_END();
635 }; // class suspend
637 /** Test of the osl::Thread::resume method
639 class resume : public CppUnit::TestFixture
641 public:
642 /** check if the thread run samely as usual after suspend and resume
644 ALGORITHM:
645 compare the values before and after suspend, they should be same,
646 then compare values before and after resume, the difference should be same as the sleep seconds number
648 void resume_001()
650 OCountThread* pCountThread = new OCountThread();
651 bool bRes = pCountThread->create();
652 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
654 suspendCountThread(pCountThread);
656 sal_Int32 nSuspendValue = pCountThread->getValue(); // (2)
657 // suspend for 3 seconds
658 ThreadHelper::thread_sleep_tenth_sec(3);
659 pCountThread->resume();
661 ThreadHelper::thread_sleep_tenth_sec(3);
662 sal_Int32 nResumeValue = pCountThread->getValue();
664 ThreadHelper::thread_sleep_tenth_sec(3);
665 sal_Int32 nLaterValue = pCountThread->getValue();
667 termAndJoinThread(pCountThread);
668 delete pCountThread;
670 t_print("SuspendValue: %d\n", static_cast<int>(nSuspendValue));
671 t_print("ResumeValue: %d\n", static_cast<int>(nResumeValue));
672 t_print("LaterValue: %d\n", static_cast<int>(nLaterValue));
674 /* LLA: this assumption is no longer relevant: nResumeValue == nSuspendValue && */
675 CPPUNIT_ASSERT_MESSAGE(
676 "Suspend then resume the thread",
677 nLaterValue >= 9
679 CPPUNIT_ASSERT_MESSAGE(
680 "Suspend then resume the thread",
681 nResumeValue > nSuspendValue
683 CPPUNIT_ASSERT_MESSAGE(
684 "Suspend then resume the thread",
685 nLaterValue > nResumeValue
690 /** Create a suspended thread then resume, check if the thread has run
692 void resume_002()
694 myThread* newthread = new myThread;
695 bool bRes = newthread->createSuspended();
696 CPPUNIT_ASSERT_MESSAGE ( "Can't create thread!", bRes );
698 newthread->resume();
699 ThreadHelper::thread_sleep_tenth_sec(2);
700 sal_Int32 nValue = newthread->getValue();
702 termAndJoinThread(newthread);
703 delete newthread;
705 t_print(" nValue = %d\n", static_cast<int>(nValue));
707 CPPUNIT_ASSERT_MESSAGE(
708 "Creates a suspended thread, then resume",
709 nValue >= 1
713 CPPUNIT_TEST_SUITE(resume);
714 CPPUNIT_TEST(resume_001);
715 CPPUNIT_TEST(resume_002);
716 CPPUNIT_TEST_SUITE_END();
717 }; // class resume
719 /** Test of the osl::Thread::terminate method
721 class terminate : public CppUnit::TestFixture
723 public:
724 /** Check after call terminate if the running thread running go on executing
726 ALGORITHM:
727 before and after call terminate, the values should be the same
729 void terminate_001()
731 OCountThread* aCountThread = new OCountThread();
732 bool bRes = aCountThread->create();
733 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
735 ThreadHelper::thread_sleep_tenth_sec(2);
736 sal_Int32 nValue = aCountThread->getValue();
737 aCountThread->terminate();
738 ThreadHelper::thread_sleep_tenth_sec(2);
739 sal_Int32 nLaterValue = aCountThread->getValue();
741 // isRunning should be false after terminate
742 bool isRunning = aCountThread->isRunning();
743 aCountThread->join();
744 delete aCountThread;
746 t_print(" nValue = %d\n", static_cast<int>(nValue));
747 t_print("nLaterValue = %d\n", static_cast<int>(nLaterValue));
749 CPPUNIT_ASSERT_MESSAGE(
750 "Terminate the thread",
751 !isRunning
753 CPPUNIT_ASSERT_MESSAGE(
754 "Terminate the thread",
755 nLaterValue >= nValue
758 /** Check if a suspended thread will terminate after call terminate, different on w32 and on UNX
760 void terminate_002()
762 OCountThread* aCountThread = new OCountThread();
763 bool bRes = aCountThread->create();
764 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
766 ThreadHelper::thread_sleep_tenth_sec(1);
767 suspendCountThread(aCountThread);
768 sal_Int32 nValue = aCountThread->getValue();
770 // seems a suspended thread can not be terminated on W32, while on Solaris can
771 resumeAndWaitThread(aCountThread);
773 ThreadHelper::thread_sleep_tenth_sec(2);
775 termAndJoinThread(aCountThread);
776 sal_Int32 nLaterValue = aCountThread->getValue();
777 delete aCountThread;
779 t_print(" nValue = %d\n", static_cast<int>(nValue));
780 t_print("nLaterValue = %d\n", static_cast<int>(nLaterValue));
782 CPPUNIT_ASSERT_MESSAGE(
783 "Suspend then resume the thread",
784 nLaterValue > nValue );
787 CPPUNIT_TEST_SUITE(terminate);
788 CPPUNIT_TEST(terminate_001);
789 CPPUNIT_TEST(terminate_002);
790 CPPUNIT_TEST_SUITE_END();
791 }; // class terminate
793 /** Test of the osl::Thread::join method
795 class join : public CppUnit::TestFixture
797 public:
798 /** Check after call terminate if the thread running function will not go on executing
800 the next statement after join will not exec before the thread terminates
801 ALGORITHM:
802 recode system time at the beginning of the thread run, call join, then record system time again,
803 the difference of the two times should be equal or more than 20 seconds, the CountThread normally terminates
805 void join_001()
807 OCountThread *aCountThread = new OCountThread();
808 bool bRes = aCountThread->create();
809 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
811 StopWatch aStopWatch;
812 aStopWatch.start();
813 // TimeValue aTimeVal_befor;
814 // osl_getSystemTime( &aTimeVal_befor );
815 //t_print("#join:the system time is %d,%d\n", pTimeVal_befor->Seconds,pTimeVal_befor->Nanosec);
817 aCountThread->join();
819 //the below line will be executed after aCountThread terminate
820 // TimeValue aTimeVal_after;
821 // osl_getSystemTime( &aTimeVal_after );
822 aStopWatch.stop();
823 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
824 double nSec = aStopWatch.getSeconds();
825 t_print("join_001 nSec=%f\n", nSec);
826 delete aCountThread;
828 CPPUNIT_ASSERT_MESSAGE(
829 "Join the thread: after the thread terminate",
830 nSec >= 2
834 /** after terminated by another thread, join exited immediately
836 ALGORITHM:
837 terminate the thread when value>=3, call join, check the beginning time and time after join,
838 the difference should be 3 seconds, join costs little time
840 void join_002()
842 OCountThread *aCountThread = new OCountThread();
843 bool bRes = aCountThread->create();
844 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
846 //record the time when the running begin
847 // TimeValue aTimeVal_befor;
848 // osl_getSystemTime( &aTimeVal_befor );
849 StopWatch aStopWatch;
850 aStopWatch.start();
852 ThreadHelper::thread_sleep_tenth_sec(10);
853 termAndJoinThread(aCountThread);
855 //the below line will be executed after aCountThread terminate
856 // TimeValue aTimeVal_after;
857 // osl_getSystemTime( &aTimeVal_after );
858 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
859 aStopWatch.stop();
860 double nSec = aStopWatch.getSeconds();
861 t_print("join_002 nSec=%f\n", nSec);
863 delete aCountThread;
864 CPPUNIT_ASSERT_MESSAGE(
865 "Join the thread: after thread terminate by another thread",
866 nSec >= 1
870 CPPUNIT_TEST_SUITE(join);
871 CPPUNIT_TEST(join_001);
872 CPPUNIT_TEST(join_002);
873 CPPUNIT_TEST_SUITE_END();
874 }; // class join
876 /** Test of the osl::Thread::isRunning method
878 class isRunning : public CppUnit::TestFixture
880 public:
881 void isRunning_001()
883 OCountThread *aCountThread = new OCountThread();
884 bool bRes = aCountThread->create();
885 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
887 bool bRun = aCountThread->isRunning();
889 ThreadHelper::thread_sleep_tenth_sec(2);
890 termAndJoinThread(aCountThread);
891 bool bTer = aCountThread->isRunning();
892 delete aCountThread;
894 CPPUNIT_ASSERT_MESSAGE(
895 "Test isRunning",
896 bRun
898 CPPUNIT_ASSERT_MESSAGE(
899 "Test isRunning",
900 !bTer
903 /** check the value of isRunning when suspending and after resume
905 void isRunning_002()
907 OCountThread *aCountThread = new OCountThread();
908 bool bRes = aCountThread->create();
909 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
911 // sal_Bool bRunning = aCountThread->isRunning();
912 // sal_Int32 nValue = 0;
913 suspendCountThread(aCountThread);
915 bool bRunning_sup = aCountThread->isRunning();
916 ThreadHelper::thread_sleep_tenth_sec(2);
917 aCountThread->resume();
918 ThreadHelper::thread_sleep_tenth_sec(2);
919 bool bRunning_res = aCountThread->isRunning();
920 termAndJoinThread(aCountThread);
921 bool bRunning_ter = aCountThread->isRunning();
922 delete aCountThread;
924 CPPUNIT_ASSERT_MESSAGE(
925 "Test isRunning",
926 bRes );
927 CPPUNIT_ASSERT_MESSAGE(
928 "Test isRunning",
929 bRunning_sup );
930 CPPUNIT_ASSERT_MESSAGE(
931 "Test isRunning",
932 bRunning_res );
933 CPPUNIT_ASSERT_MESSAGE(
934 "Test isRunning",
935 !bRunning_ter );
939 CPPUNIT_TEST_SUITE(isRunning);
940 CPPUNIT_TEST(isRunning_001);
941 CPPUNIT_TEST(isRunning_002);
942 CPPUNIT_TEST_SUITE_END();
943 }; // class isRunning
945 /// check osl::Thread::setPriority
946 class setPriority : public CppUnit::TestFixture
948 public:
949 // insert your test code here.
950 OString getPrioName(oslThreadPriority _aPriority)
952 OString sPrioStr;
953 switch (_aPriority)
955 case osl_Thread_PriorityHighest:
956 sPrioStr = "Highest";
957 break;
959 case osl_Thread_PriorityAboveNormal:
960 sPrioStr = "AboveNormal";
961 break;
963 case osl_Thread_PriorityNormal:
964 sPrioStr = "Normal";
965 break;
967 case osl_Thread_PriorityBelowNormal:
968 sPrioStr = "BelowNormal";
969 break;
971 case osl_Thread_PriorityLowest:
972 sPrioStr = "Lowest";
973 break;
974 default:
975 sPrioStr = "unknown";
977 return sPrioStr;
980 /** check 2 threads.
982 ALGORITHM:
983 Here the function should show, that 2 different threads,
984 which only increase a value, should run at the same time with same prio.
985 The test fails, if the difference between the two values is more than 5%
986 but IMHO this isn't a failure, it's only a feature of the OS.
989 void check2Threads(oslThreadPriority _aPriority)
991 // initial 5 threads with different priorities
992 OAddThread* pThread = new OAddThread();
993 OAddThread* p2Thread = new OAddThread();
995 //Create them and start running at the same time
996 pThread->create();
997 pThread->setPriority(_aPriority);
998 p2Thread->create();
999 p2Thread->setPriority(_aPriority);
1001 ThreadHelper::thread_sleep_tenth_sec(5);
1003 pThread->terminate();
1004 p2Thread->terminate();
1006 sal_Int32 nValueNormal = pThread->getValue();
1008 sal_Int32 nValueNormal2 = p2Thread->getValue();
1010 OString sPrio = getPrioName(_aPriority);
1011 t_print("After 10 tenth seconds\n");
1013 t_print("nValue in %s Prio Thread is %d\n",sPrio.getStr(), static_cast<int>(nValueNormal));
1014 t_print("nValue in %s Prio Thread is %d\n", sPrio.getStr(), static_cast<int>(nValueNormal2));
1016 // ThreadHelper::thread_sleep_tenth_sec(1);
1017 pThread->join();
1018 p2Thread->join();
1020 delete pThread;
1021 delete p2Thread;
1023 sal_Int32 nDelta = abs(nValueNormal - nValueNormal2);
1024 double nQuotient = std::max(nValueNormal, nValueNormal2);
1025 CPPUNIT_ASSERT_MESSAGE(
1026 "Quotient is zero, which means, there exist no right values.",
1027 nQuotient != 0
1029 double nDeltaPercent = nDelta / nQuotient * 100;
1031 t_print("Delta value %d, percent %f\n", static_cast<int>(nDelta), nDeltaPercent);
1033 // LLA: it's not a bug if the current OS is not able to handle thread scheduling right and good.
1034 // like Windows XP
1035 // LLA: CPPUNIT_ASSERT_MESSAGE(
1036 // LLA: "Run 2 normal threads, the count diff more than 5 percent.",
1037 // LLA: nDeltaPercent <= 5
1038 // LLA: );
1041 void setPriority_001_1()
1043 check2Threads(osl_Thread_PriorityHighest);
1045 void setPriority_001_2()
1047 check2Threads(osl_Thread_PriorityAboveNormal);
1049 void setPriority_001_3()
1051 check2Threads(osl_Thread_PriorityNormal);
1053 void setPriority_001_4()
1055 check2Threads(osl_Thread_PriorityBelowNormal);
1057 void setPriority_001_5()
1059 check2Threads(osl_Thread_PriorityLowest);
1062 void setPriority_002()
1064 // initial 5 threads with different priorities
1066 OAddThread aHighestThread;
1067 OAddThread aAboveNormalThread;
1068 OAddThread aNormalThread;
1069 //OAddThread *aBelowNormalThread = new OAddThread();
1070 //OAddThread *aLowestThread = new OAddThread();
1072 //Create them and start running at the same time
1073 aHighestThread.createSuspended();
1074 aHighestThread.setPriority(osl_Thread_PriorityHighest);
1076 aAboveNormalThread.createSuspended();
1077 aAboveNormalThread.setPriority(osl_Thread_PriorityAboveNormal);
1079 aNormalThread.createSuspended();
1080 aNormalThread.setPriority(osl_Thread_PriorityNormal);
1081 /*aBelowNormalThread->create();
1082 aBelowNormalThread->setPriority(osl_Thread_PriorityBelowNormal);
1083 aLowestThread->create();
1084 aLowestThread->setPriority(osl_Thread_PriorityLowest);
1087 aHighestThread.resume();
1088 aAboveNormalThread.resume();
1089 aNormalThread.resume();
1091 ThreadHelper::thread_sleep_tenth_sec(5);
1093 aHighestThread.suspend();
1094 aAboveNormalThread.suspend();
1095 aNormalThread.suspend();
1097 termAndJoinThread(&aNormalThread);
1098 termAndJoinThread(&aAboveNormalThread);
1099 termAndJoinThread(&aHighestThread);
1100 //aBelowNormalThread->terminate();
1101 //aLowestThread->terminate();
1103 sal_Int32 nValueHighest = aHighestThread.getValue();
1105 sal_Int32 nValueAboveNormal = aAboveNormalThread.getValue();
1107 sal_Int32 nValueNormal = aNormalThread.getValue();
1109 t_print("After 10 tenth seconds\n");
1110 t_print("nValue in Highest Prio Thread is %d\n", static_cast<int>(nValueHighest));
1111 t_print("nValue in AboveNormal Prio Thread is %d\n", static_cast<int>(nValueAboveNormal));
1112 t_print("nValue in Normal Prio Thread is %d\n", static_cast<int>(nValueNormal));
1114 #ifndef _WIN32
1115 CPPUNIT_ASSERT_MESSAGE(
1116 "SetPriority",
1117 nValueHighest > 0
1119 CPPUNIT_ASSERT_MESSAGE(
1120 "SetPriority",
1121 nValueAboveNormal > 0
1123 CPPUNIT_ASSERT_MESSAGE(
1124 "SetPriority",
1125 nValueNormal > 0
1127 #endif
1130 void setPriority_003()
1132 // initial 5 threads with different priorities
1133 OAddThread pHighestThread;
1134 OAddThread pAboveNormalThread;
1135 OAddThread pNormalThread;
1136 OAddThread pBelowNormalThread;
1137 OAddThread pLowestThread;
1139 //Create them and start running at the same time
1140 pHighestThread.createSuspended();
1141 pHighestThread.setPriority(osl_Thread_PriorityHighest);
1143 pAboveNormalThread.createSuspended();
1144 pAboveNormalThread.setPriority(osl_Thread_PriorityAboveNormal);
1146 pNormalThread.createSuspended();
1147 pNormalThread.setPriority(osl_Thread_PriorityNormal);
1149 pBelowNormalThread.createSuspended();
1150 pBelowNormalThread.setPriority(osl_Thread_PriorityBelowNormal);
1152 pLowestThread.createSuspended();
1153 pLowestThread.setPriority(osl_Thread_PriorityLowest);
1155 pHighestThread.resume();
1156 pAboveNormalThread.resume();
1157 pNormalThread.resume();
1158 pBelowNormalThread.resume();
1159 pLowestThread.resume();
1161 ThreadHelper::thread_sleep_tenth_sec(5);
1163 pHighestThread.suspend();
1164 pAboveNormalThread.suspend();
1165 pNormalThread.suspend();
1166 pBelowNormalThread.suspend();
1167 pLowestThread.suspend();
1169 termAndJoinThread(&pHighestThread);
1170 termAndJoinThread(&pAboveNormalThread);
1171 termAndJoinThread(&pNormalThread);
1172 termAndJoinThread(&pBelowNormalThread);
1173 termAndJoinThread(&pLowestThread);
1175 sal_Int32 nValueHighest = pHighestThread.getValue();
1177 sal_Int32 nValueAboveNormal = pAboveNormalThread.getValue();
1179 sal_Int32 nValueNormal = pNormalThread.getValue();
1181 sal_Int32 nValueBelowNormal = pBelowNormalThread.getValue();
1183 sal_Int32 nValueLowest = pLowestThread.getValue();
1185 t_print("After 10 tenth seconds\n");
1186 t_print("nValue in Highest Prio Thread is %d\n", static_cast<int>(nValueHighest));
1187 t_print("nValue in AboveNormal Prio Thread is %d\n", static_cast<int>(nValueAboveNormal));
1188 t_print("nValue in Normal Prio Thread is %d\n", static_cast<int>(nValueNormal));
1189 t_print("nValue in BelowNormal Prio Thread is %d\n", static_cast<int>(nValueBelowNormal));
1190 t_print("nValue in Lowest Prio Thread is %d\n", static_cast<int>(nValueLowest));
1192 #ifndef _WIN32
1193 CPPUNIT_ASSERT_MESSAGE(
1194 "SetPriority",
1195 nValueHighest > 0
1197 CPPUNIT_ASSERT_MESSAGE(
1198 "SetPriority",
1199 nValueAboveNormal > 0
1201 CPPUNIT_ASSERT_MESSAGE(
1202 "SetPriority",
1203 nValueNormal > 0
1205 CPPUNIT_ASSERT_MESSAGE(
1206 "SetPriority",
1207 nValueBelowNormal > 0
1209 CPPUNIT_ASSERT_MESSAGE(
1210 "SetPriority",
1211 nValueLowest > 0
1213 #endif
1216 void setPriority_004()
1218 // initial 5 threads with different priorities
1219 // OAddThread *pHighestThread = new OAddThread();
1220 OAddThread pAboveNormalThread;
1221 OAddThread pNormalThread;
1222 OAddThread pBelowNormalThread;
1223 OAddThread pLowestThread;
1225 //Create them and start running at the same time
1226 // pHighestThread->createSuspended();
1227 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1229 pAboveNormalThread.createSuspended();
1230 pAboveNormalThread.setPriority(osl_Thread_PriorityAboveNormal);
1232 pNormalThread.createSuspended();
1233 pNormalThread.setPriority(osl_Thread_PriorityNormal);
1235 pBelowNormalThread.createSuspended();
1236 pBelowNormalThread.setPriority(osl_Thread_PriorityBelowNormal);
1238 pLowestThread.createSuspended();
1239 pLowestThread.setPriority(osl_Thread_PriorityLowest);
1241 // pHighestThread->resume();
1242 pAboveNormalThread.resume();
1243 pNormalThread.resume();
1244 pBelowNormalThread.resume();
1245 pLowestThread.resume();
1247 ThreadHelper::thread_sleep_tenth_sec(5);
1249 // pHighestThread->suspend();
1250 pAboveNormalThread.suspend();
1251 pNormalThread.suspend();
1252 pBelowNormalThread.suspend();
1253 pLowestThread.suspend();
1255 // termAndJoinThread(pHighestThread);
1256 termAndJoinThread(&pAboveNormalThread);
1257 termAndJoinThread(&pNormalThread);
1258 termAndJoinThread(&pBelowNormalThread);
1259 termAndJoinThread(&pLowestThread);
1261 // sal_Int32 nValueHighest = 0;
1262 // nValueHighest = pHighestThread->getValue();
1264 sal_Int32 nValueAboveNormal = pAboveNormalThread.getValue();
1266 sal_Int32 nValueNormal = pNormalThread.getValue();
1268 sal_Int32 nValueBelowNormal = pBelowNormalThread.getValue();
1270 sal_Int32 nValueLowest = pLowestThread.getValue();
1272 t_print("After 5 tenth seconds\n");
1273 t_print("nValue in AboveNormal Prio Thread is %d\n", static_cast<int>(nValueAboveNormal));
1274 t_print("nValue in Normal Prio Thread is %d\n", static_cast<int>(nValueNormal));
1275 t_print("nValue in BelowNormal Prio Thread is %d\n", static_cast<int>(nValueBelowNormal));
1276 t_print("nValue in Lowest Prio Thread is %d\n", static_cast<int>(nValueLowest));
1278 // delete pHighestThread;
1280 #ifndef _WIN32
1281 CPPUNIT_ASSERT_MESSAGE(
1282 "SetPriority",
1283 /* nValueHighest > 0 && */
1284 nValueAboveNormal > 0
1286 CPPUNIT_ASSERT_MESSAGE(
1287 "SetPriority",
1288 nValueNormal > 0
1290 CPPUNIT_ASSERT_MESSAGE(
1291 "SetPriority",
1292 nValueBelowNormal > 0
1294 CPPUNIT_ASSERT_MESSAGE(
1295 "SetPriority",
1296 nValueLowest > 0
1298 #endif
1300 void setPriority_005()
1302 // initial 5 threads with different priorities
1303 // OAddThread *pHighestThread = new OAddThread();
1304 // OAddThread *pAboveNormalThread = new OAddThread();
1305 OAddThread pNormalThread;
1306 OAddThread pBelowNormalThread;
1307 OAddThread pLowestThread;
1309 //Create them and start running at the same time
1310 // pHighestThread->createSuspended();
1311 // pHighestThread->setPriority(osl_Thread_PriorityHighest);
1313 // pAboveNormalThread->createSuspended();
1314 // pAboveNormalThread->setPriority(osl_Thread_PriorityAboveNormal);
1316 pNormalThread.createSuspended();
1317 pNormalThread.setPriority(osl_Thread_PriorityNormal);
1319 pBelowNormalThread.createSuspended();
1320 pBelowNormalThread.setPriority(osl_Thread_PriorityBelowNormal);
1322 pLowestThread.createSuspended();
1323 pLowestThread.setPriority(osl_Thread_PriorityLowest);
1325 // pHighestThread->resume();
1326 // pAboveNormalThread->resume();
1327 pNormalThread.resume();
1328 pBelowNormalThread.resume();
1329 pLowestThread.resume();
1331 ThreadHelper::thread_sleep_tenth_sec(5);
1333 // pHighestThread->suspend();
1334 // pAboveNormalThread->suspend();
1335 pNormalThread.suspend();
1336 pBelowNormalThread.suspend();
1337 pLowestThread.suspend();
1339 // termAndJoinThread(pHighestThread);
1340 // termAndJoinThread(pAboveNormalThread);
1341 termAndJoinThread(&pNormalThread);
1342 termAndJoinThread(&pBelowNormalThread);
1343 termAndJoinThread(&pLowestThread);
1345 // sal_Int32 nValueHighest = 0;
1346 // nValueHighest = pHighestThread->getValue();
1348 // sal_Int32 nValueAboveNormal = 0;
1349 // nValueAboveNormal = pAboveNormalThread->getValue();
1351 sal_Int32 nValueNormal = pNormalThread.getValue();
1353 sal_Int32 nValueBelowNormal = pBelowNormalThread.getValue();
1355 sal_Int32 nValueLowest = pLowestThread.getValue();
1357 t_print("After 5 tenth seconds\n");
1358 t_print("nValue in Normal Prio Thread is %d\n", static_cast<int>(nValueNormal));
1359 t_print("nValue in BelowNormal Prio Thread is %d\n", static_cast<int>(nValueBelowNormal));
1360 t_print("nValue in Lowest Prio Thread is %d\n", static_cast<int>(nValueLowest));
1362 #ifndef _WIN32
1363 CPPUNIT_ASSERT_MESSAGE(
1364 "SetPriority",
1365 /* nValueHighest > 0 && */
1366 /* nValueAboveNormal > 0 && */
1367 nValueNormal > 0
1369 CPPUNIT_ASSERT_MESSAGE(
1370 "SetPriority",
1371 nValueBelowNormal > 0
1373 CPPUNIT_ASSERT_MESSAGE(
1374 "SetPriority",
1375 nValueLowest > 0
1377 #endif
1380 CPPUNIT_TEST_SUITE(setPriority);
1381 #ifndef __sun
1382 CPPUNIT_TEST(setPriority_002);
1383 CPPUNIT_TEST(setPriority_003);
1384 CPPUNIT_TEST(setPriority_004);
1385 CPPUNIT_TEST(setPriority_005);
1386 #endif
1387 CPPUNIT_TEST(setPriority_001_1);
1388 CPPUNIT_TEST(setPriority_001_2);
1389 CPPUNIT_TEST(setPriority_001_3);
1390 CPPUNIT_TEST(setPriority_001_4);
1391 CPPUNIT_TEST(setPriority_001_5);
1392 CPPUNIT_TEST_SUITE_END();
1393 }; // class setPriority
1395 /** Test of the osl::Thread::getPriority method
1397 class getPriority : public CppUnit::TestFixture
1399 public:
1400 // insert your test code here.
1401 void getPriority_001()
1403 OAddThread *pHighestThread = new OAddThread();
1405 //Create them and start running at the same time
1406 pHighestThread->create();
1407 pHighestThread->setPriority(osl_Thread_PriorityHighest);
1409 oslThreadPriority aPriority = pHighestThread->getPriority();
1410 termAndJoinThread(pHighestThread);
1411 delete pHighestThread;
1413 ThreadHelper::outputPriority(aPriority);
1415 // LLA: Priority settings may not work within some OS versions.
1416 #if defined(_WIN32) || defined(__sun)
1417 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1418 "getPriority",
1419 osl_Thread_PriorityHighest, aPriority
1421 #else
1422 // LLA: Linux
1423 // NO_PTHREAD_PRIORITY ???
1424 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1425 "getPriority",
1426 osl_Thread_PriorityNormal, aPriority
1428 #endif
1431 CPPUNIT_TEST_SUITE(getPriority);
1432 CPPUNIT_TEST(getPriority_001);
1433 CPPUNIT_TEST_SUITE_END();
1434 }; // class getPriority
1436 class getIdentifier : public CppUnit::TestFixture
1438 public:
1439 // initialise your test code values here.
1441 void getIdentifier_001()
1443 // insert your test code here.
1446 CPPUNIT_TEST_SUITE(getIdentifier);
1447 CPPUNIT_TEST(getIdentifier_001);
1448 CPPUNIT_TEST_SUITE_END();
1449 }; // class getIdentifier
1451 /** Test of the osl::Thread::getCurrentIdentifier method
1453 class getCurrentIdentifier : public CppUnit::TestFixture
1455 public:
1456 void getCurrentIdentifier_001()
1458 oslThreadIdentifier oId;
1459 OCountThread* pCountThread = new OCountThread;
1460 pCountThread->create();
1461 pCountThread->setWait(3);
1462 oId = Thread::getCurrentIdentifier();
1463 oslThreadIdentifier oIdChild = pCountThread->getIdentifier();
1464 termAndJoinThread(pCountThread);
1465 delete pCountThread;
1467 CPPUNIT_ASSERT_MESSAGE(
1468 "Get the identifier for the current active thread.",
1469 oId != oIdChild);
1472 CPPUNIT_TEST_SUITE(getCurrentIdentifier);
1473 CPPUNIT_TEST(getCurrentIdentifier_001);
1474 CPPUNIT_TEST_SUITE_END();
1475 }; // class getCurrentIdentifier
1477 /** Test of the osl::Thread::wait method
1479 class waittest : public CppUnit::TestFixture
1481 public:
1482 /** call wait in the run method
1484 ALGORITHM:
1485 tested thread wait nWaitSec seconds, main thread sleep (2) seconds,
1486 then terminate the tested thread, due to the fact that the thread do a sleep(1) + wait(5)
1487 it's finish after 6 seconds.
1489 void wait_001()
1491 OCountThread *aCountThread = new OCountThread();
1492 sal_Int32 nWaitSec = 5;
1493 aCountThread->setWait(nWaitSec);
1494 // thread runs at least 5 seconds.
1495 bool bRes = aCountThread->create();
1496 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
1498 //record the time when the running begin
1499 StopWatch aStopWatch;
1500 aStopWatch.start();
1502 // wait a little bit, to let the thread the time, to start
1503 ThreadHelper::thread_sleep_tenth_sec( 4 );
1505 // if wait works,
1506 // this function returns, after 4 sec. later
1507 termAndJoinThread(aCountThread);
1509 // value should be one.
1510 sal_Int32 nValue = aCountThread->getValue();
1512 aStopWatch.stop();
1514 // sal_uInt32 nSec = aTimeVal_after.Seconds - aTimeVal_befor.Seconds;
1515 double nTenthSec = aStopWatch.getTenthSec();
1516 double nSec = aStopWatch.getSeconds();
1517 delete aCountThread;
1518 t_print("nTenthSec = %f \n", nTenthSec);
1519 t_print("nSec = %f \n", nSec);
1520 t_print("nValue = %d \n", static_cast<int>(nValue));
1522 CPPUNIT_ASSERT_MESSAGE(
1523 "Wait: Blocks the calling thread for the given number of time.",
1524 nTenthSec >= 5
1526 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1527 "Wait: Blocks the calling thread for the given number of time.",
1528 sal_Int32(1), nValue
1533 CPPUNIT_TEST_SUITE(waittest);
1534 CPPUNIT_TEST(wait_001);
1535 CPPUNIT_TEST_SUITE_END();
1536 }; // class waittest
1538 /** osl::Thread::yield method: can not design good test scenario to test up to now
1540 class yield : public CppUnit::TestFixture
1542 public:
1543 void yield_001()
1545 // insert your test code here.
1548 CPPUNIT_TEST_SUITE(yield);
1549 CPPUNIT_TEST(yield_001);
1550 CPPUNIT_TEST_SUITE_END();
1551 }; // class yield
1553 /** Test of the osl::Thread::schedule method
1555 class schedule : public CppUnit::TestFixture
1557 public:
1559 /** The requested thread will get terminate the next time schedule() is called.
1561 Note: on UNX, if call suspend thread is not the to be suspended thread, the to be
1562 suspended thread will get suspended the next time schedule() is called,
1563 while on w32, it's nothing with schedule.
1565 check if suspend and terminate work well via schedule
1567 void schedule_001()
1569 OAddThread* aThread = new OAddThread();
1570 bool bRes = aThread->create();
1571 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
1573 ThreadHelper::thread_sleep_tenth_sec(2);
1574 aThread->suspend();
1575 ThreadHelper::thread_sleep_tenth_sec(1);
1576 sal_Int32 nValue = aThread->getValue();
1577 ThreadHelper::thread_sleep_tenth_sec(3);
1578 sal_Int32 nLaterValue = aThread->getValue();
1579 // resumeAndWaitThread(aThread);
1580 t_print(" value = %d\n", static_cast<int>(nValue));
1581 t_print("later value = %d\n", static_cast<int>(nLaterValue));
1582 // if value and latervalue not equal, then the thread would not suspended
1584 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1585 "Schedule: suspend works.",
1586 nValue, nLaterValue
1589 aThread->resume();
1590 ThreadHelper::thread_sleep_tenth_sec(2);
1592 aThread->terminate();
1593 sal_Int32 nValue_term = aThread->getValue();
1595 aThread->join();
1596 sal_Int32 nValue_join = aThread->getValue();
1598 t_print("value after term = %d\n", static_cast<int>(nValue_term));
1599 t_print("value after join = %d\n", static_cast<int>(nValue_join));
1601 // nValue_term and nValue_join should be the same
1602 // but should be differ from nValue
1604 delete aThread;
1605 //check if thread really terminate after call terminate, if join immediately return
1606 CPPUNIT_ASSERT_MESSAGE(
1607 "Schedule: Returns False if the thread should terminate.",
1608 nValue_join - nValue_term <= 1
1610 CPPUNIT_ASSERT_MESSAGE(
1611 "Schedule: Returns False if the thread should terminate.",
1612 nValue_join - nValue_term >= 0
1617 /** design a thread that has not call schedule in the workfunction--run method
1619 void schedule_002()
1621 ONoScheduleThread aThread; // this thread runs 10 sec. (no schedule() used)
1622 bool bRes = aThread.create();
1623 CPPUNIT_ASSERT_MESSAGE ( "Can't start thread!", bRes );
1625 ThreadHelper::thread_sleep_tenth_sec(2);
1626 aThread.suspend();
1627 sal_Int32 nValue = aThread.getValue();
1629 ThreadHelper::thread_sleep_tenth_sec(3);
1630 sal_Int32 nLaterValue = aThread.getValue();
1631 ThreadHelper::thread_sleep_tenth_sec(5);
1633 resumeAndWaitThread(&aThread);
1635 t_print(" value = %d\n", static_cast<int>(nValue));
1636 t_print("later value = %d\n", static_cast<int>(nLaterValue));
1638 //On windows, suspend works, so the values are same
1639 #ifdef _WIN32
1640 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1641 "Schedule: don't schedule in thread run method, suspend works.",
1642 nValue, nLaterValue
1644 #endif
1646 //On UNX, suspend does not work, so the difference of the values equals to sleep seconds number
1647 #ifdef UNX
1648 aThread.resume();
1649 CPPUNIT_ASSERT_MESSAGE(
1650 "Schedule: don't schedule in thread run method, suspend does not work too.",
1651 nLaterValue > nValue
1653 #endif
1655 // terminate will not work if no schedule in thread's work function
1656 termAndJoinThread(&aThread);
1657 sal_Int32 nValue_term = aThread.getValue();
1659 t_print(" value term = %d\n", static_cast<int>(nValue_term));
1661 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1662 "Schedule: don't schedule in thread run method, terminate failed.",
1663 static_cast<sal_Int32>(10), nValue_term
1667 CPPUNIT_TEST_SUITE(schedule);
1668 CPPUNIT_TEST(schedule_001);
1669 CPPUNIT_TEST(schedule_002);
1670 CPPUNIT_TEST_SUITE_END();
1671 }; // class schedule
1673 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::create, "osl_Thread");
1674 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::createSuspended, "osl_Thread");
1675 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::suspend, "osl_Thread");
1676 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::resume, "osl_Thread");
1677 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::terminate, "osl_Thread");
1678 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::join, "osl_Thread");
1679 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::isRunning, "osl_Thread");
1680 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::setPriority, "osl_Thread");
1681 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getPriority, "osl_Thread");
1682 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getIdentifier, "osl_Thread");
1683 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::getCurrentIdentifier, "osl_Thread");
1684 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::waittest, "osl_Thread");
1685 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::yield, "osl_Thread");
1686 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Thread::schedule, "osl_Thread");
1687 } // namespace osl_Thread
1689 // destroy function when the binding thread terminate
1690 static void destroyCallback(void * data)
1692 delete[] static_cast<char *>(data);
1695 static ThreadData myThreadData(destroyCallback);
1697 namespace {
1699 class myKeyThread : public Thread
1701 public:
1702 // a public char member for test result checking
1703 char m_Char_Test;
1704 // for pass thread-special data to thread
1705 explicit myKeyThread(const char cData)
1706 : m_Char_Test(0)
1708 m_nData = cData;
1710 private:
1711 char m_nData;
1713 void SAL_CALL run() override
1715 char * pc = new char[2];
1716 // strcpy(pc, &m_nData);
1717 memcpy(pc, &m_nData, 1);
1718 pc[1] = '\0';
1720 myThreadData.setData(pc);
1721 char* pData = static_cast<char*>(myThreadData.getData());
1722 m_Char_Test = *pData;
1723 // wait for long time to check the data value in main thread
1724 ThreadHelper::thread_sleep_tenth_sec(3);
1726 public:
1727 virtual ~myKeyThread() override
1729 if (isRunning())
1731 t_print("error: not terminated.\n");
1738 static ThreadData idData;
1740 namespace {
1742 class idThread: public Thread
1744 public:
1745 oslThreadIdentifier m_Id;
1746 private:
1747 void SAL_CALL run() override
1749 std::unique_ptr<oslThreadIdentifier> pId( new oslThreadIdentifier );
1750 *pId = getIdentifier();
1751 idData.setData(pId.get());
1752 oslThreadIdentifier* pIdData = static_cast<oslThreadIdentifier*>(idData.getData());
1753 //t_print("Thread %d has Data %d\n", getIdentifier(), *pIdData);
1754 m_Id = *pIdData;
1757 public:
1758 virtual ~idThread() override
1760 if (isRunning())
1762 t_print("error: not terminated.\n");
1769 namespace osl_ThreadData
1772 class ctors : public CppUnit::TestFixture
1774 public:
1776 // insert your test code here.
1777 void ctor_001()
1782 CPPUNIT_TEST_SUITE(ctors);
1783 CPPUNIT_TEST(ctor_001);
1784 CPPUNIT_TEST_SUITE_END();
1785 }; // class ctors
1787 class setData : public CppUnit::TestFixture
1789 public:
1791 /** the same instance of the class can have different values in different threads
1793 void setData_001()
1795 idThread aThread1;
1796 aThread1.create();
1797 idThread aThread2;
1798 aThread2.create();
1800 aThread1.join();
1801 aThread2.join();
1803 oslThreadIdentifier aThreadId1 = aThread1.getIdentifier();
1804 oslThreadIdentifier aThreadId2 = aThread2.getIdentifier();
1806 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1807 "ThreadData setData: ",
1808 aThread1.m_Id, aThreadId1
1810 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1811 "ThreadData setData: ",
1812 aThread2.m_Id, aThreadId2
1817 void setData_002()
1819 // at first, set the data a value
1820 char* pc = new char[2];
1821 char nData = 'm';
1822 pc[0] = nData;
1823 pc[1] = '\0';
1825 myThreadData.setData(pc);
1827 myKeyThread aThread1('a');
1828 aThread1.create();
1829 myKeyThread aThread2('b');
1830 aThread2.create();
1831 // aThread1 and aThread2 should have not terminated yet, check current data, not 'a' 'b'
1832 char* pChar = static_cast<char*>(myThreadData.getData());
1833 char aChar = *pChar;
1835 aThread1.join();
1836 aThread2.join();
1838 // the saved thread data of aThread1 & aThread2, different
1839 char cData1 = aThread1.m_Char_Test;
1840 char cData2 = aThread2.m_Char_Test;
1842 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1843 "ThreadData setData: ",
1844 'a', cData1
1846 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1847 "ThreadData setData: ",
1848 'b', cData2
1850 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1851 "ThreadData setData: ",
1852 'm', aChar
1856 /** setData the second time, and then getData
1858 void setData_003()
1860 // at first, set the data a value
1861 char* pc = new char[2];
1862 char nData = 'm';
1863 memcpy(pc, &nData, 1);
1864 pc[1] = '\0';
1865 myThreadData.setData(pc);
1867 myKeyThread aThread1('a');
1868 aThread1.create();
1869 myKeyThread aThread2('b');
1870 aThread2.create();
1871 // aThread1 and aThread2 should have not terminated yet
1872 // setData the second time
1873 char* pc2 = new char[2];
1874 nData = 'o';
1875 memcpy(pc2, &nData, 1);
1876 pc2[1] = '\0';
1878 myThreadData.setData(pc2);
1879 char* pChar = static_cast<char*>(myThreadData.getData());
1880 char aChar = *pChar;
1882 aThread1.join();
1883 aThread2.join();
1885 // the saved thread data of aThread1 & aThread2, different
1886 char cData1 = aThread1.m_Char_Test;
1887 char cData2 = aThread2.m_Char_Test;
1889 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1890 "ThreadData setData: ",
1891 'a', cData1
1893 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1894 "ThreadData setData: ",
1895 'b', cData2
1897 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1898 "ThreadData setData: ",
1899 'o', aChar
1903 CPPUNIT_TEST_SUITE(setData);
1904 CPPUNIT_TEST(setData_001);
1905 CPPUNIT_TEST(setData_002);
1906 CPPUNIT_TEST(setData_003);
1907 CPPUNIT_TEST_SUITE_END();
1908 }; // class setData
1910 class getData : public CppUnit::TestFixture
1912 public:
1914 // After setData in child threads, get Data in the main thread, should be independent
1915 void getData_001()
1917 char* pc = new char[2];
1918 strcpy(pc, "i");
1919 myThreadData.setData(pc);
1921 myKeyThread aThread1('c');
1922 aThread1.create();
1923 myKeyThread aThread2('d');
1924 aThread2.create();
1926 aThread1.join();
1927 aThread2.join();
1929 char cData1 = aThread1.m_Char_Test;
1930 char cData2 = aThread2.m_Char_Test;
1932 char* pChar = static_cast<char*>(myThreadData.getData());
1933 char aChar = *pChar;
1935 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1936 "ThreadData setData: ",
1937 'c', cData1
1939 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1940 "ThreadData setData: ",
1941 'd', cData2
1943 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1944 "ThreadData setData: ",
1945 'i', aChar
1949 // setData then change the value in the address data pointer points,
1950 // and then getData, should get the new value
1951 void getData_002()
1953 char* pc = new char[2];
1954 char nData = 'i';
1955 memcpy(pc, &nData, 1);
1956 pc[1] = '\0';
1958 myThreadData.setData(pc);
1960 myKeyThread aThread1('a');
1961 aThread1.create();
1962 myKeyThread aThread2('b');
1963 aThread2.create();
1965 // change the value which pc points
1966 char nData2 = 'j';
1967 memcpy(pc, &nData2, 1);
1968 pc[1] = '\0';
1970 void* pChar = myThreadData.getData();
1971 char aChar = *static_cast<char*>(pChar);
1973 aThread1.join();
1974 aThread2.join();
1976 char cData1 = aThread1.m_Char_Test;
1977 char cData2 = aThread2.m_Char_Test;
1979 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1980 "ThreadData setData: ",
1981 'a', cData1
1983 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1984 "ThreadData setData: ",
1985 'b', cData2
1987 CPPUNIT_ASSERT_EQUAL_MESSAGE(
1988 "ThreadData setData: ",
1989 'j', aChar
1994 CPPUNIT_TEST_SUITE(getData);
1995 CPPUNIT_TEST(getData_001);
1996 CPPUNIT_TEST(getData_002);
1997 CPPUNIT_TEST_SUITE_END();
1998 }; // class getData
2000 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::ctors);
2001 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::setData);
2002 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ThreadData::getData);
2003 } // namespace osl_ThreadData
2005 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */