| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <sal/types.h> |
| 21 |
#include <cppunit/TestFixture.h> |
| 22 |
#include <cppunit/extensions/HelperMacros.h> |
| 23 |
#include <cppunit/plugin/TestPlugIn.h> |
| 24 |
#include "osl/conditn.hxx" |
| 25 |
#include "osl/thread.hxx" |
| 26 |
#include "osl/time.h" |
| 27 |
#include <chrono> |
| 28 |
|
| 29 |
namespace { |
| 30 |
|
| 31 |
osl::Condition global; |
| 32 |
|
| 33 |
class Thread: public osl::Thread { |
| 34 |
public: |
| 35 |
explicit Thread(osl::Condition & cond): m_cond(cond) {} |
| 36 |
|
| 37 |
private: |
| 38 |
virtual void SAL_CALL run() {} |
| 39 |
|
| 40 |
virtual void SAL_CALL onTerminated() { |
| 41 |
m_cond.set(); |
| 42 |
CPPUNIT_ASSERT_EQUAL(osl::Condition::result_ok, global.wait()); |
| 43 |
} |
| 44 |
|
| 45 |
osl::Condition & m_cond; |
| 46 |
}; |
| 47 |
|
| 48 |
class Test: public CppUnit::TestFixture { |
| 49 |
public: |
| 50 |
// Nondeterministic, best effort test that an osl::Thread can be destroyed |
| 51 |
// (and in particular osl_destroyThread---indirectly---be called) before the |
| 52 |
// corresponding thread has terminated: |
| 53 |
void test() { |
| 54 |
for (int i = 0; i < 50; ++i) { |
| 55 |
osl::Condition c; |
| 56 |
Thread t(c); |
| 57 |
CPPUNIT_ASSERT(t.create()); |
| 58 |
// Make sure virtual Thread::run/onTerminated are called before |
| 59 |
// Thread::~Thread: |
| 60 |
CPPUNIT_ASSERT_EQUAL(osl::Condition::result_ok, c.wait()); |
| 61 |
} |
| 62 |
// Make sure Thread::~Thread is called before each spawned thread |
| 63 |
// terminates: |
| 64 |
global.set(); |
| 65 |
// Give the spawned threads enough time to terminate: |
| 66 |
osl::Thread::wait(std::chrono::seconds(20)); |
| 67 |
} |
| 68 |
|
| 69 |
CPPUNIT_TEST_SUITE(Test); |
| 70 |
CPPUNIT_TEST(test); |
| 71 |
CPPUNIT_TEST_SUITE_END(); |
| 72 |
}; |
| 73 |
|
| 74 |
CPPUNIT_TEST_SUITE_REGISTRATION(Test); |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
CPPUNIT_PLUGIN_IMPLEMENT(); |
| 79 |
|
| 80 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |