xref: /trunk/main/sal/qa/osl/thread/test_thread.cxx (revision f7e98e71)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sal.hxx"
26 
27 #include "sal/config.h"
28 
29 #include "osl/conditn.hxx"
30 #include "osl/thread.hxx"
31 #include "osl/time.h"
32 #include "sal/types.h"
33 #include "gtest/gtest.h"
34 
35 namespace {
36 
37 osl::Condition global;
38 
39 class Thread: public osl::Thread {
40 public:
Thread(osl::Condition & cond)41     explicit Thread(osl::Condition & cond): m_cond(cond) {}
42 
43 private:
run()44     virtual void SAL_CALL run() {}
45 
onTerminated()46     virtual void SAL_CALL onTerminated() {
47         m_cond.set();
48         ASSERT_EQ(osl::Condition::result_ok, global.wait());
49     }
50 
51     osl::Condition & m_cond;
52 };
53 
54 class Test: public ::testing::Test {
55 public:
56 };
57 
58 // Nondeterministic, best effort test that an osl::Thread can be destroyed
59 // (and in particular osl_destroyThread---indirectly---be called) before the
60 // corresponding thread has terminated:
TEST_F(Test,test)61 TEST_F(Test, test) {
62     for (int i = 0; i < 50; ++i) {
63         osl::Condition c;
64         Thread t(c);
65         ASSERT_TRUE(t.create());
66         // Make sure virtual Thread::run/onTerminated are called before
67         // Thread::~Thread:
68         ASSERT_EQ(osl::Condition::result_ok, c.wait());
69     }
70     // Make sure Thread::~Thread is called before each spawned thread
71     // terminates:
72     global.set();
73     // Give the spawned threads enough time to terminate:
74     TimeValue const twentySeconds = { 20, 0 };
75     osl::Thread::wait(twentySeconds);
76 }
77 
78 
79 }
80 
main(int argc,char ** argv)81 int main(int argc, char **argv)
82 {
83     ::testing::InitGoogleTest(&argc, argv);
84     return RUN_ALL_TESTS();
85 }
86 
87