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