ChimeraTK-cppext 01.05.02
Loading...
Searching...
No Matches
testSemaphore.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#define BOOST_TEST_MODULE test_semaphore
4#include <boost/test/included/unit_test.hpp>
5using namespace boost::unit_test_framework;
6
7#include "semaphore.hpp"
9
10#include <thread>
11
12BOOST_AUTO_TEST_SUITE(TestLatch)
13
14/*********************************************************************************************************************/
15
16BOOST_AUTO_TEST_CASE(singleThreaded) {
17 std::cout << "singleThreaded" << std::endl;
18
20
21 // unlock the semaphore
22 BOOST_CHECK(sem.is_ready() == false);
23 sem.unlock();
24 // now semaphore should be ready and wait() should not block
25 BOOST_CHECK(sem.is_ready() == true);
26 sem.wait_and_reset();
27 BOOST_CHECK(sem.is_ready() == false);
28 // test reusing the semaphore
29 sem.unlock();
30 BOOST_CHECK(sem.is_ready() == true);
31 sem.wait_and_reset();
32 BOOST_CHECK(sem.is_ready() == false);
33 sem.unlock();
34 BOOST_CHECK(sem.is_ready() == true);
35}
36
37/*********************************************************************************************************************/
38
39// one thread waiting and another counting
40BOOST_AUTO_TEST_CASE(multiThreaded) {
41 std::cout << "multiThreaded" << std::endl;
42
44
45 std::thread waiting([&sem] {
46 BOOST_CHECK_TS(sem.is_ready() == false);
47 sem.wait_and_reset();
48 }); // end waiting thread
49
50 std::thread down_counting([&sem] {
51 usleep(100000);
52 sem.unlock();
53 }); // end down_counting thread
54
55 down_counting.join();
56 waiting.join();
57 BOOST_CHECK_TS(sem.is_ready() == false);
58}
59
60/*********************************************************************************************************************/
61
62BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(singleThreaded)
#define BOOST_CHECK_TS(condition)