Skip to content

Commit 6b2a0fe

Browse files
committed
Update events period method to check for invalid values
The period method currently allows any ms value positive, negative and zero. A negative value means dispatch a non periodic event ie just once. 0 is unspecified behaviour. This commit forces the user to use either positive periods or a new constant non_periodic and should any other value be provided will default to non_periodic. A Greentea test case is also provided to check this works as expected.
1 parent e917282 commit 6b2a0fe

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

events/include/events/Event.h

100644100755
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ template <typename... ArgTs>
4747
class Event<void(ArgTs...)> {
4848
public:
4949
using duration = std::chrono::duration<int, std::milli>;
50+
static constexpr duration non_periodic{-1};
5051

5152
/** Create an event
5253
*
@@ -67,7 +68,7 @@ class Event<void(ArgTs...)> {
6768
_event->equeue = &q->_equeue;
6869
_event->id = 0;
6970
_event->delay = duration(0);
70-
_event->period = duration(-1);
71+
_event->period = non_periodic;
7172

7273
_event->post = &Event::event_post<F>;
7374
_event->dtor = &Event::event_dtor<F>;
@@ -140,12 +141,20 @@ class Event<void(ArgTs...)> {
140141
/** Configure the period of an event
141142
*
142143
* @param p Period (in milliseconds) for repeatedly dispatching an event, expressed as a Chrono duration.
144+
* Period must be either 'non_periodic' or > 0ms. If an invalid period is supplied then a
145+
* default non_periodic value is used.
143146
* E.g. period(200ms)
144147
*/
145148
void period(duration p)
146149
{
150+
MBED_ASSERT(p > duration(0) || p == non_periodic);
147151
if (_event) {
148-
_event->period = p;
152+
if (p > duration(0)) {
153+
_event->period = p;
154+
}
155+
else {
156+
_event->period = non_periodic;
157+
}
149158
}
150159
}
151160

events/tests/TESTS/events/queue/main.cpp

100644100755
Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,82 @@ void static_events_queue_test()
508508
TEST_ASSERT_EQUAL(15, test4.counter);
509509
}
510510

511+
static EventTest event_period;
512+
513+
void event_period_tests()
514+
{
515+
EventQueue queue;
516+
517+
// Test a non periodic event ie dispatched only once
518+
519+
Event<void(int)> event1(&queue, &event_period::f0);
520+
event1.delay(100ms);
521+
event1.period(Event::non_periodic);
522+
event1.post()
523+
524+
queue.dispatch(800);
525+
526+
// Wait 1000ms and check the event execution status
527+
wait_us(1000 * 1000)
528+
529+
// Event should only have been dispatched once and thus counter
530+
// should be 1
531+
TEST_ASSERT_EQUAL(1, event_period.counter);
532+
533+
// Test an event with an invalid -ve period.
534+
535+
event_period.counter = 0;
536+
Event<void(int)> event2(&queue, &event_period::f0);
537+
event2.delay(100ms);
538+
event2.period(-10ms);
539+
event2.post()
540+
541+
queue.dispatch(800);
542+
543+
// Wait 1000ms and check the event execution status
544+
wait_us(1000 * 1000)
545+
546+
// Event should default to non_periodic and thus only have been
547+
// dispatched once. Counter should be 1.
548+
TEST_ASSERT_EQUAL(1, event_period.counter);
549+
550+
// Test an event with a zero period.
551+
552+
event_period.counter = 0;
553+
Event<void(int)> event3(&queue, &event_period::f0);
554+
event3.delay(100ms);
555+
event3.period(0ms);
556+
event3.post()
557+
558+
queue.dispatch(800);
559+
560+
// Wait 1000ms and check the event execution status
561+
wait_us(1000 * 1000)
562+
563+
// Event should default to non_periodic and thus only have been
564+
// dispatched once. Counter should be 1.
565+
TEST_ASSERT_EQUAL(1, event_period.counter);
566+
567+
// Test a periodic event ie dispatched a number of times
568+
event_period.counter = 0;
569+
Event<void(int)> event4(&queue, &event_period::f0);
570+
event4.delay(100ms);
571+
event4.period(200ms);
572+
event4.post()
573+
574+
queue.dispatch(800);
575+
576+
// Wait 1000ms and check the event execution status
577+
wait_us(1000 * 1000)
578+
579+
// The event should be first dispatched after 100ms and then
580+
// every subsequent 200ms until the dispatcher has completed.
581+
// Thus the counter should be incremented after :
582+
// 100ms, 300ms, 500ms and 700ms
583+
TEST_ASSERT_EQUAL(4, event_period.counter);
584+
585+
}
586+
511587
// Test setup
512588
utest::v1::status_t test_setup(const size_t number_of_cases)
513589
{
@@ -536,8 +612,8 @@ const Case cases[] = {
536612
Case("Testing time_left", time_left_test),
537613
Case("Testing mixed dynamic & static events queue", mixed_dynamic_static_events_queue_test),
538614
Case("Testing static events queue", static_events_queue_test)
539-
540-
};
615+
Case("Testing event period values", event_period_tests)
616+
};
541617

542618
Specification specification(test_setup, cases);
543619

0 commit comments

Comments
 (0)