xref: /trunk/main/sal/qa/osl/thread/test_thread.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
30 
31 #include "sal/config.h"
32 
33 #include "testshl/simpleheader.hxx"
34 #include "osl/conditn.hxx"
35 #include "osl/thread.hxx"
36 #include "osl/time.h"
37 #include "sal/types.h"
38 
39 namespace {
40 
41 osl::Condition global;
42 
43 class Thread: public osl::Thread {
44 public:
45     explicit Thread(osl::Condition & cond): m_cond(cond) {}
46 
47 private:
48     virtual void SAL_CALL run() {}
49 
50     virtual void SAL_CALL onTerminated() {
51         m_cond.set();
52         CPPUNIT_ASSERT_EQUAL(osl::Condition::result_ok, global.wait());
53     }
54 
55     osl::Condition & m_cond;
56 };
57 
58 class Test: public CppUnit::TestFixture {
59 public:
60     // Nondeterministic, best effort test that an osl::Thread can be destroyed
61     // (and in particular osl_destroyThread---indirectly---be called) before the
62     // corresponding thread has terminated:
63     void test() {
64         for (int i = 0; i < 50; ++i) {
65             osl::Condition c;
66             Thread t(c);
67             CPPUNIT_ASSERT(t.create());
68             // Make sure virtual Thread::run/onTerminated are called before
69             // Thread::~Thread:
70             CPPUNIT_ASSERT_EQUAL(osl::Condition::result_ok, c.wait());
71         }
72         // Make sure Thread::~Thread is called before each spawned thread
73         // terminates:
74         global.set();
75         // Give the spawned threads enough time to terminate:
76         TimeValue const twentySeconds = { 20, 0 };
77         osl::Thread::wait(twentySeconds);
78     }
79 
80     CPPUNIT_TEST_SUITE(Test);
81     CPPUNIT_TEST(test);
82     CPPUNIT_TEST_SUITE_END();
83 };
84 
85 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test, "alltests");
86 
87 }
88 
89 NOADDITIONAL;
90