Skip to content

Commit 02ce663

Browse files
committed
prevent expiration in pause
1 parent eb39c5f commit 02ce663

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

src/Timeout.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ void Timeout::expire(void)
4949

5050
bool Timeout::time_over(void)
5151
{
52+
if (paused_) {
53+
return false;
54+
}
5255
bool result = (time_over_forced_ || (uint32_t)(millis() - reset_time_ms_) >= time_ms_);
5356
if (result) {
5457
// make sure to stay expired and not roll over again
@@ -64,12 +67,12 @@ bool Timeout::is_paused(void)
6467

6568
uint32_t Timeout::time_left_ms(void)
6669
{
67-
if (time_over_forced_) {
68-
return 0;
69-
}
7070
if (paused_) {
7171
return time_ms_;
7272
}
73+
if (time_over()) {
74+
return 0;
75+
}
7376
int32_t ms_left = time_ms_ - (millis() - reset_time_ms_);
7477
return ms_left > 0 ? ms_left : 0;
7578
}

test/test_timeout.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,77 @@ void pause_resume()
144144
TEST_ASSERT_EQUAL(t.is_paused(), false);
145145
}
146146

147+
void no_expiration_in_pause()
148+
{
149+
Timeout t;
150+
TEST_ASSERT_EQUAL(t.time_left_ms(), 0);
151+
TEST_ASSERT_EQUAL(t.time_over(), true);
152+
TEST_ASSERT_EQUAL(t.is_paused(), false);
153+
154+
set_millis(500);
155+
t.start(500);
156+
TEST_ASSERT_EQUAL(t.time_left_ms(), 500);
157+
TEST_ASSERT_EQUAL(t.time_over(), false);
158+
TEST_ASSERT_EQUAL(t.is_paused(), false);
159+
160+
set_millis(600);
161+
TEST_ASSERT_EQUAL(t.time_left_ms(), 400);
162+
TEST_ASSERT_EQUAL(t.time_over(), false);
163+
TEST_ASSERT_EQUAL(t.is_paused(), false);
164+
165+
set_millis(700);
166+
t.pause();
167+
TEST_ASSERT_EQUAL(t.time_left_ms(), 300);
168+
TEST_ASSERT_EQUAL(t.time_over(), false);
169+
TEST_ASSERT_EQUAL(t.is_paused(), true);
170+
171+
set_millis(800);
172+
t.pause();
173+
TEST_ASSERT_EQUAL(t.time_left_ms(), 300);
174+
TEST_ASSERT_EQUAL(t.time_over(), false);
175+
TEST_ASSERT_EQUAL(t.is_paused(), true);
176+
177+
set_millis(900);
178+
t.pause();
179+
TEST_ASSERT_EQUAL(t.time_left_ms(), 300);
180+
TEST_ASSERT_EQUAL(t.time_over(), false);
181+
TEST_ASSERT_EQUAL(t.is_paused(), true);
182+
183+
set_millis(1000);
184+
TEST_ASSERT_EQUAL(t.time_left_ms(), 300);
185+
TEST_ASSERT_EQUAL(t.time_over(), false);
186+
TEST_ASSERT_EQUAL(t.is_paused(), true);
187+
188+
set_millis(1100);
189+
TEST_ASSERT_EQUAL(t.time_left_ms(), 300);
190+
TEST_ASSERT_EQUAL(t.time_over(), false);
191+
TEST_ASSERT_EQUAL(t.is_paused(), true);
192+
193+
set_millis(1200);
194+
t.resume();
195+
TEST_ASSERT_EQUAL(t.time_left_ms(), 300);
196+
TEST_ASSERT_EQUAL(t.time_over(), false);
197+
TEST_ASSERT_EQUAL(t.is_paused(), false);
198+
199+
set_millis(1300);
200+
t.resume();
201+
TEST_ASSERT_EQUAL(t.time_left_ms(), 200);
202+
TEST_ASSERT_EQUAL(t.time_over(), false);
203+
TEST_ASSERT_EQUAL(t.is_paused(), false);
204+
205+
set_millis(1400);
206+
t.resume();
207+
TEST_ASSERT_EQUAL(t.time_left_ms(), 100);
208+
TEST_ASSERT_EQUAL(t.time_over(), false);
209+
TEST_ASSERT_EQUAL(t.is_paused(), false);
210+
211+
set_millis(1500);
212+
t.resume();
213+
TEST_ASSERT_EQUAL(t.time_left_ms(), 0);
214+
TEST_ASSERT_EQUAL(t.time_over(), true);
215+
TEST_ASSERT_EQUAL(t.is_paused(), false);
216+
}
217+
147218
void periodic()
148219
{
149220
Timeout t;
@@ -199,6 +270,7 @@ int main(int argc, char** argv)
199270
RUN_TEST(heartbeat);
200271
RUN_TEST(expiration);
201272
RUN_TEST(pause_resume);
273+
RUN_TEST(no_expiration_in_pause);
202274
RUN_TEST(periodic);
203275
RUN_TEST(rollover);
204276
return UNITY_END();

0 commit comments

Comments
 (0)