Lumiera  0.pre.03
»edit your freedom«
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) Lumiera.org
5  2023, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/testdummy.hpp"
30 #include "lib/thread.hpp"
31 
32 #include <atomic>
33 #include <chrono>
34 
35 using test::Test;
36 using std::atomic_bool;
37 using std::this_thread::sleep_for;
38 using namespace std::chrono_literals;
39 
40 
41 namespace lib {
42 namespace test{
43 
44 
45  /*******************************************************************/
52  {
53 
54  virtual void
55  run (Arg)
56  {
57  demonstrateSimpleUsage();
58  verifyMemoryManagement();
59  }
60 
61 
63  void
65  {
66  atomic_bool didRun{false};
67  launchDetached ("anarchy", [&]{ didRun = true; });
68 
69  sleep_for(1ms);
70  CHECK (didRun); // verify the effect has taken place
71  }
72 
73 
77  void
79  {
80  struct TestThread
82  {
83  using ThreadHookable::ThreadHookable;
84 
85  Dummy watcher;
86 
87  void
88  doIt (int extra)
89  {
90  watcher.setVal (extra);
91  sleep_for (5ms);
92  }
93  };
94  // Note the Dummy member allows to watch instance lifecycle
95  CHECK (0 == Dummy::checksum());
96 
97  launchDetached<TestThread> (&TestThread::doIt, 55);
98 
99  CHECK (0 < Dummy::checksum());
100  sleep_for (1ms);
101  CHECK (55 == Dummy::checksum());
102  sleep_for (10ms);
103  CHECK (0 == Dummy::checksum());
104  }
105  };
106 
107 
108 
110  LAUNCHER (ThreadWrapperAutonomous_test, "function common");
111 
112 
113 
114 }} // namespace lib::test
Definition: run.hpp:49
Implementation namespace for support and library code.
unittest helper code: test dummy objects to track instances.
Abstract Base Class for all testcases.
Definition: run.hpp:62
Simple test class runner.
void launchDetached(ThreadHookable::Launch &&launchBuilder)
Launch an autonomous self-managing thread (and forget about it).
Definition: thread.hpp:750
Convenience front-end to simplify and codify basic thread handling.
A Dummy object for tests.
Definition: testdummy.hpp:50
Extended variant of the standard case, allowing to install callbacks (hook functions) to be invoked d...
Definition: thread.hpp:724