jaulib v1.4.1-10-ga2c96e0
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
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 <pthread.h>
28
29#include <jau/test/catch2_ext.hpp>
30
31#include <jau/simple_timer.hpp>
32#include <jau/basic_types.hpp>
33
34using namespace jau;
35using namespace jau::fractions_i64_literals;
36using namespace jau::int_literals;
37
39 public:
40 // install it once ..
42
43 private:
44 int dog_count = 0;
45 const jau::fraction_i64 dog_period = 10_ms;
46 const jau::fraction_i64 test_period = 100_ms;
47
48 jau::sc_atomic_int ping_count = 0;
49 jau::simple_timer periodic_dog = jau::simple_timer("dog-"+std::to_string(dog_count), 100_ms /* shutdown timeout */);
50
51 jau::fraction_i64 dog_watch_func(jau::simple_timer& timer) {
52 static jau::fraction_timespec t0;
53
54 if( timer.shall_stop() ) {
55 return 0_s;
56 }
58 jau::fraction_i64 td = ( now - t0 ).to_fraction_i64();
59 t0 = now;
60 fprintf(stderr, "%3.3d dog is watching: Since last ping %" PRIi64 " us\n", ping_count++, td.to_num_of(1_us));
61
62 return timer.shall_stop() ? 0_s : dog_period;
63 }
64
65 public:
66
67 void test01_dog1() {
68 INFO_STR("\n\ntest01\n");
69
70 fprintf(stderr, "test01_dog1: start\n");
71 REQUIRE( 0 == ping_count );
72 REQUIRE( false == periodic_dog.is_running() );
73 REQUIRE( true == periodic_dog.shall_stop() );
74 const bool r = periodic_dog.start(dog_period, jau::bind_member(this, &TestSimpleTimer01::dog_watch_func));
75 REQUIRE( true == r );
76 REQUIRE( true == periodic_dog.is_running() );
77 REQUIRE( false == periodic_dog.shall_stop() );
78
79 {
81 jau::sleep_for( test_period );
83 REQUIRE( td <= test_period + 50_ms ); // allow some fuzzy
84 }
85 REQUIRE( true == periodic_dog.is_running() );
86 REQUIRE( false == periodic_dog.shall_stop() );
87
88 {
90 REQUIRE( true == periodic_dog.stop() );
92 REQUIRE( td <= 100_ms );
93 }
94 fprintf(stderr, "test01_dog1: stopped\n");
95 REQUIRE( false == periodic_dog.is_running() );
96 REQUIRE( true == periodic_dog.shall_stop() );
97 REQUIRE( 0 < ping_count );
98 ping_count = 0;
99 }
100
101 void test01_dog2() {
102 INFO_STR("\n\ntest01\n");
103
104 fprintf(stderr, "test01_dog2: start\n");
105 REQUIRE( 0 == ping_count );
106 REQUIRE( false == periodic_dog.is_running() );
107 REQUIRE( true == periodic_dog.shall_stop() );
108 const bool r = periodic_dog.start(dog_period, jau::bind_member(this, &TestSimpleTimer01::dog_watch_func));
109 REQUIRE( true == r );
110 REQUIRE( true == periodic_dog.is_running() );
111 REQUIRE( false == periodic_dog.shall_stop() );
112
113 {
115 jau::sleep_for( test_period );
117 REQUIRE( td <= test_period + 50_ms ); // allow some fuzzy
118 }
119 REQUIRE( true == periodic_dog.is_running() );
120 REQUIRE( false == periodic_dog.shall_stop() );
121
122 {
124 REQUIRE( true == periodic_dog.stop() );
126 REQUIRE( td <= 100_ms );
127 }
128 fprintf(stderr, "test01_dog2: stopped\n");
129 REQUIRE( false == periodic_dog.is_running() );
130 REQUIRE( true == periodic_dog.shall_stop() );
131 REQUIRE( 0 < ping_count );
132 }
133
134};
135
136METHOD_AS_TEST_CASE( TestSimpleTimer01::test01_dog1, "Test TestSimpleTimer01 - test01_dog1");
137METHOD_AS_TEST_CASE( TestSimpleTimer01::test01_dog2, "Test TestSimpleTimer01 - test01_dog2");
138
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 shall_stop() const noexcept
Returns true if timer shall stop.
ordered_atomic< int, std::memory_order_seq_cst > sc_atomic_int
SC atomic integral scalar integer.
fraction_timespec getMonotonicTime() noexcept
Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01.
FracI64SizeBoolTuple to_fraction_i64(const std::string &value, const fraction_i64 &min_allowed, const fraction_i64 &max_allowed) noexcept
Returns the fraction_i64 of the given string in format <num>/<denom>, which may contain whitespace.
fraction< int64_t > fraction_i64
fraction using int64_t as integral type
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")