Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
thread-wrapper-autonomous-test.cpp
Go to the documentation of this file.
1/*
2 ThreadWrapperAutonomous(Test) - launching a self-contained and completely detached thread
3
4 Copyright (C)
5 2023, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
19#include "lib/test/run.hpp"
21#include "lib/thread.hpp"
22
23#include <atomic>
24#include <chrono>
25
26using test::Test;
27using std::atomic_bool;
28using std::this_thread::sleep_for;
29using namespace std::chrono_literals;
30
31
32namespace lib {
33namespace test{
34
35
36 /*******************************************************************/
43 {
44
45 virtual void
51
52
54 void
56 {
57 atomic_bool didRun{false};
58 launchDetached ("anarchy", [&]{ didRun = true; });
59
60 sleep_for(1ms);
61 CHECK (didRun); // verify the effect has taken place
62 }
63
64
68 void
70 {
71 struct TestThread
73 {
74 using ThreadHookable::ThreadHookable;
75
76 Dummy watcher;
77
78 void
79 doIt (int extra)
80 {
81 watcher.setVal (extra);
82 sleep_for (5ms);
83 }
84 };
85 // Note the Dummy member allows to watch instance lifecycle
86 CHECK (0 == Dummy::checksum());
87
88 launchDetached<TestThread> (&TestThread::doIt, 55);
89
90 CHECK (0 < Dummy::checksum());
91 sleep_for (1ms);
92 CHECK (55 == Dummy::checksum());
93 sleep_for (10ms);
94 CHECK (0 == Dummy::checksum());
95 }
96 };
97
98
99
102
103
104
105}} // namespace lib::test
Extended variant of the standard case, allowing to install callbacks (hook functions) to be invoked d...
Definition thread.hpp:718
A Dummy object for tests.
static long & checksum()
void setVal(int newVal)
Abstract Base Class for all testcases.
Definition run.hpp:54
const_LList llist_cmpfn void * extra
Definition llist.h:580
Implementation namespace for support and library code.
void launchDetached(ThreadHookable::Launch &&launchBuilder)
Launch an autonomous self-managing thread (and forget about it).
Definition thread.hpp:742
Test runner and basic definitions for tests.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Convenience front-end to simplify and codify basic thread handling.
unittest helper code: test dummy objects to track instances.