• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mavlink / MAVSDK / 9106360009

16 May 2024 04:12AM UTC coverage: 36.895% (+0.04%) from 36.858%
9106360009

push

github

web-flow
Merge pull request #2301 from mavlink/pr-timeout-fixes

TimeoutHandler fixes

40 of 47 new or added lines in 4 files covered. (85.11%)

3 existing lines in 2 files now uncovered.

10890 of 29516 relevant lines covered (36.9%)

118.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.15
/src/mavsdk/core/timeout_handler_test.cpp
1
#include "timeout_handler.h"
2
#include "unused.h"
3
#include <gtest/gtest.h>
4

5
#ifdef FAKE_TIME
6
#define Time FakeTime
7
#endif
8

9
using namespace mavsdk;
10

11
TEST(TimeoutHandler, Timeout)
1✔
12
{
13
    Time time;
2✔
14
    TimeoutHandler th(time);
2✔
15

16
    bool timeout_happened = false;
1✔
17

18
    void* cookie = nullptr;
1✔
19
    th.add([&timeout_happened]() { timeout_happened = true; }, 0.5, &cookie);
1✔
20

21
    time.sleep_for(std::chrono::milliseconds(250));
1✔
22
    th.run_once();
1✔
23
    EXPECT_FALSE(timeout_happened);
1✔
24
    time.sleep_for(std::chrono::milliseconds(500));
1✔
25
    th.run_once();
1✔
26
    EXPECT_TRUE(timeout_happened);
1✔
27

28
    UNUSED(cookie);
1✔
29
}
1✔
30

31
TEST(TimeoutHandler, TimeoutNoCookie)
1✔
32
{
33
    Time time;
2✔
34
    TimeoutHandler th(time);
2✔
35

36
    bool timeout_happened = false;
1✔
37

38
    // This time we supply nullptr and don't want a cookie.
39
    th.add([&timeout_happened]() { timeout_happened = true; }, 0.5, nullptr);
1✔
40

41
    time.sleep_for(std::chrono::milliseconds(250));
1✔
42
    th.run_once();
1✔
43
    EXPECT_FALSE(timeout_happened);
1✔
44
    time.sleep_for(std::chrono::milliseconds(500));
1✔
45
    th.run_once();
1✔
46
    EXPECT_TRUE(timeout_happened);
1✔
47
}
1✔
48

49
TEST(TimeoutHandler, CallTimeoutInTimeoutCallback)
1✔
50
{
51
    Time time;
2✔
52
    TimeoutHandler th(time);
2✔
53

54
    bool timeout_happened = false;
1✔
55

56
    void* cookie1 = nullptr;
1✔
57
    void* cookie2 = nullptr;
1✔
58
    th.add(
1✔
59
        [&th, &timeout_happened, &cookie2]() {
1✔
60
            timeout_happened = true;
1✔
61
            // This tests the case where we want to set yet another timeout when we
62
            // are called because of a timeout. This tests if there are no locking
63
            // issues.
64
            th.add([]() {}, 5.0, &cookie2);
1✔
65
        },
1✔
66
        0.5,
67
        &cookie1);
68

69
    time.sleep_for(std::chrono::milliseconds(250));
1✔
70
    th.run_once();
1✔
71
    EXPECT_FALSE(timeout_happened);
1✔
72
    time.sleep_for(std::chrono::milliseconds(500));
1✔
73
    th.run_once();
1✔
74
    EXPECT_TRUE(timeout_happened);
1✔
75

76
    UNUSED(cookie1);
1✔
77
    UNUSED(cookie2);
1✔
78
}
1✔
79

80
TEST(TimeoutHandler, TimeoutRefreshed)
1✔
81
{
82
    Time time{};
2✔
83
    TimeoutHandler th(time);
2✔
84

85
    bool timeout_happened = false;
1✔
86

87
    void* cookie = nullptr;
1✔
88
    th.add([&timeout_happened]() { timeout_happened = true; }, 0.5, &cookie);
1✔
89

90
    time.sleep_for(std::chrono::milliseconds(400));
1✔
91
    th.run_once();
1✔
92
    EXPECT_FALSE(timeout_happened);
1✔
93
    th.refresh(cookie);
1✔
94
    time.sleep_for(std::chrono::milliseconds(300));
1✔
95
    th.run_once();
1✔
96
    EXPECT_FALSE(timeout_happened);
1✔
97
    time.sleep_for(std::chrono::milliseconds(300));
1✔
98
    th.run_once();
1✔
99
    EXPECT_TRUE(timeout_happened);
1✔
100

101
    UNUSED(cookie);
1✔
102
}
1✔
103

104
TEST(TimeoutHandler, TimeoutRemoved)
1✔
105
{
106
    Time time{};
2✔
107
    TimeoutHandler th(time);
2✔
108

109
    bool timeout_happened = false;
1✔
110

111
    void* cookie = nullptr;
1✔
112
    th.add([&timeout_happened]() { timeout_happened = true; }, 0.5, &cookie);
×
113

114
    time.sleep_for(std::chrono::milliseconds(250));
1✔
115
    th.run_once();
1✔
116
    EXPECT_FALSE(timeout_happened);
1✔
117
    th.remove(cookie);
1✔
118
    time.sleep_for(std::chrono::milliseconds(500));
1✔
119
    th.run_once();
1✔
120
    EXPECT_FALSE(timeout_happened);
1✔
121
}
1✔
122

123
TEST(TimeoutHandler, TimeoutRemovedDuringCallback)
1✔
124
{
125
    Time time{};
2✔
126
    TimeoutHandler th(time);
2✔
127

128
    bool timeout_happened = false;
1✔
129

130
    void* cookie = nullptr;
1✔
131
    th.add(
1✔
132
        [&th, &cookie, &timeout_happened]() {
2✔
133
            // This is evil but can potentially happen. We remove our own timeout while
134
            // being called.
135
            th.remove(cookie);
1✔
136
            timeout_happened = true;
1✔
137
        },
1✔
138
        0.5,
139
        &cookie);
140

141
    time.sleep_for(std::chrono::milliseconds(250));
1✔
142
    th.run_once();
1✔
143
    EXPECT_FALSE(timeout_happened);
1✔
144
    time.sleep_for(std::chrono::milliseconds(500));
1✔
145
    th.run_once();
1✔
146
    EXPECT_TRUE(timeout_happened);
1✔
147
}
1✔
148

149
TEST(TimeoutHandler, NextTimeoutRemovedDuringCallback)
1✔
150
{
151
    Time time{};
2✔
152
    TimeoutHandler th(time);
2✔
153

154
    void* cookie1 = nullptr;
1✔
155
    void* cookie2 = nullptr;
1✔
156

157
    th.add(
1✔
158
        [&th, &cookie2]() {
1✔
159
            // This is evil but can potentially happen. We remove the other timer while
160
            // being called. This triggers that the iterator is invalid and causes a segfault
161
            // if not handled properly.
162
            th.remove(cookie2);
1✔
163
        },
1✔
164
        0.5,
165
        &cookie1);
166

UNCOV
167
    th.add([]() {}, 0.5, &cookie2);
×
168

169
    time.sleep_for(std::chrono::milliseconds(1000));
1✔
170
    th.run_once();
1✔
171
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc