xref: /trunk/main/sal/qa/osl/thread/test_thread.cxx (revision f7e98e71)
187d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
387d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
487d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
587d2adbcSAndrew Rist  * distributed with this work for additional information
687d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
787d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
887d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
987d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
1087d2adbcSAndrew Rist  *
1187d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1287d2adbcSAndrew Rist  *
1387d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1487d2adbcSAndrew Rist  * software distributed under the License is distributed on an
1587d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1687d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
1787d2adbcSAndrew Rist  * specific language governing permissions and limitations
1887d2adbcSAndrew Rist  * under the License.
1987d2adbcSAndrew Rist  *
2087d2adbcSAndrew Rist  *************************************************************/
2187d2adbcSAndrew Rist 
2287d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "sal/config.h"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "osl/conditn.hxx"
30cdf0e10cSrcweir #include "osl/thread.hxx"
31cdf0e10cSrcweir #include "osl/time.h"
32cdf0e10cSrcweir #include "sal/types.h"
334fda3f8eSDamjan Jovanovic #include "gtest/gtest.h"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir osl::Condition global;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir class Thread: public osl::Thread {
40cdf0e10cSrcweir public:
Thread(osl::Condition & cond)41cdf0e10cSrcweir     explicit Thread(osl::Condition & cond): m_cond(cond) {}
42cdf0e10cSrcweir 
43cdf0e10cSrcweir private:
run()44cdf0e10cSrcweir     virtual void SAL_CALL run() {}
45cdf0e10cSrcweir 
onTerminated()46cdf0e10cSrcweir     virtual void SAL_CALL onTerminated() {
47cdf0e10cSrcweir         m_cond.set();
484fda3f8eSDamjan Jovanovic         ASSERT_EQ(osl::Condition::result_ok, global.wait());
49cdf0e10cSrcweir     }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     osl::Condition & m_cond;
52cdf0e10cSrcweir };
53cdf0e10cSrcweir 
544fda3f8eSDamjan Jovanovic class Test: public ::testing::Test {
55cdf0e10cSrcweir public:
56cdf0e10cSrcweir };
57cdf0e10cSrcweir 
584fda3f8eSDamjan Jovanovic // Nondeterministic, best effort test that an osl::Thread can be destroyed
594fda3f8eSDamjan Jovanovic // (and in particular osl_destroyThread---indirectly---be called) before the
604fda3f8eSDamjan Jovanovic // corresponding thread has terminated:
TEST_F(Test,test)614fda3f8eSDamjan Jovanovic TEST_F(Test, test) {
624fda3f8eSDamjan Jovanovic     for (int i = 0; i < 50; ++i) {
634fda3f8eSDamjan Jovanovic         osl::Condition c;
644fda3f8eSDamjan Jovanovic         Thread t(c);
654fda3f8eSDamjan Jovanovic         ASSERT_TRUE(t.create());
664fda3f8eSDamjan Jovanovic         // Make sure virtual Thread::run/onTerminated are called before
674fda3f8eSDamjan Jovanovic         // Thread::~Thread:
684fda3f8eSDamjan Jovanovic         ASSERT_EQ(osl::Condition::result_ok, c.wait());
694fda3f8eSDamjan Jovanovic     }
704fda3f8eSDamjan Jovanovic     // Make sure Thread::~Thread is called before each spawned thread
714fda3f8eSDamjan Jovanovic     // terminates:
724fda3f8eSDamjan Jovanovic     global.set();
734fda3f8eSDamjan Jovanovic     // Give the spawned threads enough time to terminate:
744fda3f8eSDamjan Jovanovic     TimeValue const twentySeconds = { 20, 0 };
754fda3f8eSDamjan Jovanovic     osl::Thread::wait(twentySeconds);
764fda3f8eSDamjan Jovanovic }
774fda3f8eSDamjan Jovanovic 
78cdf0e10cSrcweir 
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
main(int argc,char ** argv)81*f7e98e71SDamjan Jovanovic int main(int argc, char **argv)
82*f7e98e71SDamjan Jovanovic {
83*f7e98e71SDamjan Jovanovic     ::testing::InitGoogleTest(&argc, argv);
84*f7e98e71SDamjan Jovanovic     return RUN_ALL_TESTS();
85*f7e98e71SDamjan Jovanovic }
86*f7e98e71SDamjan Jovanovic 
87