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)
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 
26 using test::Test;
27 using std::atomic_bool;
28 using std::this_thread::sleep_for;
29 using namespace std::chrono_literals;
30 
31 
32 namespace lib {
33 namespace test{
34 
35 
36  /*******************************************************************/
43  {
44 
45  virtual void
46  run (Arg)
47  {
48  demonstrateSimpleUsage();
49  verifyMemoryManagement();
50  }
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 
101  LAUNCHER (ThreadWrapperAutonomous_test, "function common");
102 
103 
104 
105 }} // namespace lib::test
Definition: run.hpp:40
Implementation namespace for support and library code.
Abstract Base Class for all testcases.
Definition: run.hpp:53
Simplistic test class runner.
unittest helper code: test dummy objects to track instances.
void launchDetached(ThreadHookable::Launch &&launchBuilder)
Launch an autonomous self-managing thread (and forget about it).
Definition: thread.hpp:742
Convenience front-end to simplify and codify basic thread handling.
A Dummy object for tests.
Extended variant of the standard case, allowing to install callbacks (hook functions) to be invoked d...
Definition: thread.hpp:716