jaulib v1.3.0
Jau Support Library (C++, Java, ..)
test_simple_timer01.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <cassert>
25#include <cinttypes>
26#include <cstring>
27#include <memory>
28#include <thread>
29#include <pthread.h>
30
32
33#include <jau/simple_timer.hpp>
34
35using namespace jau;
36using namespace jau::fractions_i64_literals;
37using namespace jau::int_literals;
38
40 public:
41 // install it once ..
43
44 private:
45 int dog_count = 0;
46 const jau::fraction_i64 dog_period = 10_ms;
47 const jau::fraction_i64 test_period = 100_ms;
48
49 jau::sc_atomic_int ping_count = 0;
50 jau::simple_timer periodic_dog = jau::simple_timer("dog-"+std::to_string(dog_count), 100_ms /* shutdown timeout */);
51
52 jau::fraction_i64 dog_watch_func(jau::simple_timer& timer) {
53 static jau::fraction_timespec t0;
54
55 if( timer.shall_stop() ) {
56 return 0_s;
57 }
59 jau::fraction_i64 td = ( now - t0 ).to_fraction_i64();
60 t0 = now;
61 fprintf(stderr, "%3.3d dog is watching: Since last ping %" PRIi64 " us\n", ping_count++, td.to_num_of(1_us));
62
63 return timer.shall_stop() ? 0_s : dog_period;
64 }
65
66 public:
67
68 void test01_dog1() {
69 INFO_STR("\n\ntest01\n");
70
71 fprintf(stderr, "test01_dog1: start\n");
72 REQUIRE( 0 == ping_count );
73 REQUIRE( false == periodic_dog.is_running() );
74 REQUIRE( true == periodic_dog.shall_stop() );
75 const bool r = periodic_dog.start(dog_period, jau::bind_member(this, &TestSimpleTimer01::dog_watch_func));
76 REQUIRE( true == r );
77 REQUIRE( true == periodic_dog.is_running() );
78 REQUIRE( false == periodic_dog.shall_stop() );
79
80 {
82 jau::sleep_for( test_period );
84 REQUIRE( td <= test_period + 50_ms ); // allow some fuzzy
85 }
86 REQUIRE( true == periodic_dog.is_running() );
87 REQUIRE( false == periodic_dog.shall_stop() );
88
89 {
91 REQUIRE( true == periodic_dog.stop() );
93 REQUIRE( td <= 100_ms );
94 }
95 fprintf(stderr, "test01_dog1: stopped\n");
96 REQUIRE( false == periodic_dog.is_running() );
97 REQUIRE( true == periodic_dog.shall_stop() );
98 REQUIRE( 0 < ping_count );
99 ping_count = 0;
100 }
101
102 void test01_dog2() {
103 INFO_STR("\n\ntest01\n");
104
105 fprintf(stderr, "test01_dog2: start\n");
106 REQUIRE( 0 == ping_count );
107 REQUIRE( false == periodic_dog.is_running() );
108 REQUIRE( true == periodic_dog.shall_stop() );
109 const bool r = periodic_dog.start(dog_period, jau::bind_member(this, &TestSimpleTimer01::dog_watch_func));
110 REQUIRE( true == r );
111 REQUIRE( true == periodic_dog.is_running() );
112 REQUIRE( false == periodic_dog.shall_stop() );
113
114 {
116 jau::sleep_for( test_period );
118 REQUIRE( td <= test_period + 50_ms ); // allow some fuzzy
119 }
120 REQUIRE( true == periodic_dog.is_running() );
121 REQUIRE( false == periodic_dog.shall_stop() );
122
123 {
125 REQUIRE( true == periodic_dog.stop() );
127 REQUIRE( td <= 100_ms );
128 }
129 fprintf(stderr, "test01_dog2: stopped\n");
130 REQUIRE( false == periodic_dog.is_running() );
131 REQUIRE( true == periodic_dog.shall_stop() );
132 REQUIRE( 0 < ping_count );
133 }
134
135};
136
137METHOD_AS_TEST_CASE( TestSimpleTimer01::test01_dog1, "Test TestSimpleTimer01 - test01_dog1");
138METHOD_AS_TEST_CASE( TestSimpleTimer01::test01_dog2, "Test TestSimpleTimer01 - test01_dog2");
139
#define INFO_STR(msg)
Definition: catch2_ext.hpp:75
constexpr int_type to_num_of(const fraction< int_type > &new_base, bool *overflow_ptr=nullptr) const noexcept
Converts this this fraction to a numerator for the given new base fraction.
static bool singleton_sighandler() noexcept
Install the singleton SIGALRM sighandler instance.
A simple timer for timeout and interval applications, using one dedicated service_runner thread per i...
bool start(const fraction_i64 &duration_, Timer_func tofunc) noexcept
Start the timer with given user Timer_func function and initial duration.
bool stop() noexcept
Stop timer, see service_runner::stop()
bool shall_stop() const noexcept
Returns true if timer shall stop.
bool is_running() const noexcept
Returns true if timer is running.
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
bool to_fraction_i64(fraction_i64 &result, const std::string &value, const fraction_i64 &min_allowed, const fraction_i64 &max_allowed) noexcept
Stores the fraction_i64 value of the given string value in format <num>/<denom>, which may contain wh...
fraction_timespec getMonotonicTime() noexcept
Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01.
Definition: basic_types.cpp:52
jau::function< R(A...)> bind_member(C1 *base, R(C0::*mfunc)(A...)) noexcept
Bind given class instance and non-void member function to an anonymous function using func_member_tar...
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition: backtrace.hpp:32
bool sleep_for(const fraction_timespec &relative_time, const bool monotonic=true, const bool ignore_irq=true) noexcept
sleep_for causes the current thread to block until a specific amount of time has passed.
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...
METHOD_AS_TEST_CASE(TestSimpleTimer01::test01_dog1, "Test TestSimpleTimer01 - test01_dog1")