lumiera_documentation/tests/lib/thread-wrapper-autonomous-test.cpp
Ichthyostega 3caa5cfb03 Tests: reorganise test folders
This resolves a long-standing issue:
we could not use proper include paths for test-helper code that was defined alongside the tests.

Since some test-helper code must now be relocated into the »Vault«-Layer,
I took the opportunity to finally resolve this annoyance. The solution is two-fold
 * introduce a dedicated test-support library
 * remove the quirks that were necessary to support some plain-C tests
Together, this allows to raise the include-directory by one level,
and then to adjust all folder names so that they match the namespace
2026-03-07 00:56:19 +01:00

106 lines
2.9 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
ThreadWrapperAutonomous(Test) - launching a self-contained and completely detached thread
Copyright (C)
2023, Hermann Vosseler <Ichthyostega@web.de>
  **Lumiera** is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version. See the file COPYING for further details.
* *****************************************************************/
/** @file thread-wrapper-autonomous-test.cpp
** unit test \ref ThreadWrapperAutonomous_test
*/
#include "test/run.hpp"
#include "test/tracking-dummy.hpp"
#include "lib/thread.hpp"
#include <atomic>
#include <chrono>
using test::Test;
using test::Dummy;
using std::atomic_bool;
using std::this_thread::sleep_for;
using namespace std::chrono_literals;
namespace lib {
namespace test{
/*******************************************************************//**
* @test a variation of the Thread wrapper to launch a detached thread,
* with automatic memory management for the _thread-object._.
* @see thread.hpp
* @see ThreadWrapper_test
*/
class ThreadWrapperAutonomous_test : public Test
{
virtual void
run (Arg)
{
demonstrateSimpleUsage();
verifyMemoryManagement();
}
/** @test demonstrate simply launching a λ-function into background */
void
demonstrateSimpleUsage()
{
atomic_bool didRun{false};
launchDetached ("anarchy", [&]{ didRun = true; });
sleep_for(1ms);
CHECK (didRun); // verify the effect has taken place
}
/**
* @test verify the detached thread autonomously manages its memory.
*/
void
verifyMemoryManagement()
{
struct TestThread
: ThreadHookable
{
using ThreadHookable::ThreadHookable;
Dummy watcher;
void
doIt (int extra)
{
watcher.setVal (extra);
sleep_for (5ms);
}
};
// Note the Dummy member allows to watch instance lifecycle
CHECK (0 == Dummy::checksum());
launchDetached<TestThread> (&TestThread::doIt, 55);
CHECK (0 < Dummy::checksum());
sleep_for (1ms);
CHECK (55 == Dummy::checksum());
sleep_for (10ms);
CHECK (0 == Dummy::checksum());
}
};
/** Register this test class... */
LAUNCHER (ThreadWrapperAutonomous_test, "function common");
}} // namespace lib::test