Skip to content

Commit c0a57ba

Browse files
committed
Updated event period handling and greentea test
non_periodic constant has been moved out of the Event class and made static within the events namespace so that it is available both internally within the class and externally. The MBED_ASSERT has been changed to MBED_WARNING. The greentea test has been updated: 1) timings reduced to make the test cases run faster 2) The call handler simplified
1 parent 6b2a0fe commit c0a57ba

File tree

2 files changed

+76
-58
lines changed

2 files changed

+76
-58
lines changed

events/include/events/Event.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
#include <utility>
2121
#include "events/EventQueue.h"
2222
#include "platform/mbed_assert.h"
23+
#include "platform/mbed_error.h"
2324

2425
namespace events {
2526
/** \defgroup events-public-api Events
2627
* \ingroup mbed-os-public
2728
* @{
2829
*/
2930

31+
static constexpr std::chrono::duration<int, std::milli> non_periodic{-1};
32+
3033
/** Event
3134
*
3235
* Representation of an event for fine-grain dispatch control
@@ -47,7 +50,6 @@ template <typename... ArgTs>
4750
class Event<void(ArgTs...)> {
4851
public:
4952
using duration = std::chrono::duration<int, std::milli>;
50-
static constexpr duration non_periodic{-1};
5153

5254
/** Create an event
5355
*
@@ -68,7 +70,7 @@ class Event<void(ArgTs...)> {
6870
_event->equeue = &q->_equeue;
6971
_event->id = 0;
7072
_event->delay = duration(0);
71-
_event->period = non_periodic;
73+
_event->period = events::non_periodic;
7274

7375
_event->post = &Event::event_post<F>;
7476
_event->dtor = &Event::event_dtor<F>;
@@ -115,6 +117,8 @@ class Event<void(ArgTs...)> {
115117
}
116118
}
117119

120+
121+
118122
/** Configure the delay of an event
119123
*
120124
* @param d Delay (in milliseconds) before dispatching the event, expressed as a Chrono duration.
@@ -147,13 +151,16 @@ class Event<void(ArgTs...)> {
147151
*/
148152
void period(duration p)
149153
{
150-
MBED_ASSERT(p > duration(0) || p == non_periodic);
151154
if (_event) {
152155
if (p > duration(0)) {
153156
_event->period = p;
154157
}
155158
else {
156-
_event->period = non_periodic;
159+
if (p != events::non_periodic) {
160+
MBED_WARNING(MBED_ERROR_INVALID_ARGUMENT,
161+
"Invalid period specified, defaulting to non_periodic.");
162+
}
163+
_event->period = events::non_periodic;
157164
}
158165
}
159166
}

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

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ void simple_posts_test##i() { \
9393
TEST_ASSERT(touched); \
9494
\
9595
touched = false; \
96-
queue.call_in(1, func##i,##__VA_ARGS__); \
96+
queue.call_in(1ms, func##i,##__VA_ARGS__); \
9797
queue.dispatch(2); \
9898
TEST_ASSERT(touched); \
9999
\
100100
touched = false; \
101-
queue.call_every(1, func##i,##__VA_ARGS__); \
101+
queue.call_every(1ms, func##i,##__VA_ARGS__); \
102102
queue.dispatch(2); \
103103
TEST_ASSERT(touched); \
104104
}
@@ -126,7 +126,7 @@ void call_in_test()
126126

127127
for (int i = 0; i < N; i++) {
128128
tickers[i].start();
129-
queue.call_in((i + 1) * 100, time_func, &tickers[i], (i + 1) * 100);
129+
queue.call_in((i + 1) * 100ms, time_func, &tickers[i], (i + 1) * 100);
130130
}
131131

132132
queue.dispatch(N * 100);
@@ -141,7 +141,7 @@ void call_every_test()
141141

142142
for (int i = 0; i < N; i++) {
143143
tickers[i].start();
144-
queue.call_every((i + 1) * 100, time_func, &tickers[i], (i + 1) * 100);
144+
queue.call_every((i + 1) * 100ms, time_func, &tickers[i], (i + 1) * 100);
145145
}
146146

147147
queue.dispatch(N * 100);
@@ -172,7 +172,7 @@ void cancel_test1()
172172
int ids[N];
173173

174174
for (int i = 0; i < N; i++) {
175-
ids[i] = queue.call_in(1000, no);
175+
ids[i] = queue.call_in(1000ms, no);
176176
}
177177

178178
for (int i = N - 1; i >= 0; i--) {
@@ -308,12 +308,12 @@ void time_left_test()
308308
EventQueue queue(TEST_EQUEUE_SIZE);
309309

310310
// Enque check events
311-
TEST_ASSERT(queue.call_in(50, check_time_left, &queue, 0, 100 - 50));
312-
TEST_ASSERT(queue.call_in(200, check_time_left, &queue, 1, 200 - 200));
311+
TEST_ASSERT(queue.call_in(50ms, check_time_left, &queue, 0, 100 - 50));
312+
TEST_ASSERT(queue.call_in(200ms, check_time_left, &queue, 1, 200 - 200));
313313

314314
// Enque events to be checked
315-
timeleft_events[0] = queue.call_in(100, time_left, &queue, 0);
316-
timeleft_events[1] = queue.call_in(200, time_left, &queue, 1);
315+
timeleft_events[0] = queue.call_in(100ms, time_left, &queue, 0);
316+
timeleft_events[1] = queue.call_in(200ms, time_left, &queue, 1);
317317
TEST_ASSERT(timeleft_events[0]);
318318
TEST_ASSERT(timeleft_events[1]);
319319

@@ -373,18 +373,18 @@ void mixed_dynamic_static_events_queue_test()
373373

374374
EventTest e1_test;
375375
Event<void()> e1 = queue.event(&e1_test, &EventTest::f0);
376-
e1.delay(10);
377-
e1.period(10);
376+
e1.delay(10ms);
377+
e1.period(10ms);
378378
int id1 = e1.post();
379379
TEST_ASSERT_NOT_EQUAL(0, id1);
380380
EventTest e2_test;
381381
Event<void()> e2 = queue.event(&e2_test, &EventTest::f1, 3);
382-
e2.period(10);
382+
e2.period(10ms);
383383
int id2 = e2.post();
384384
TEST_ASSERT_NOT_EQUAL(0, id2);
385385
EventTest e3_test;
386386
Event<void()> e3 = queue.event(&e3_test, &EventTest::f5, 1, 2, 3, 4, 5);
387-
e3.period(10);
387+
e3.period(10ms);
388388
int id3 = e3.post();
389389
TEST_ASSERT_NOT_EQUAL(0, id3);
390390

@@ -508,79 +508,90 @@ void static_events_queue_test()
508508
TEST_ASSERT_EQUAL(15, test4.counter);
509509
}
510510

511-
static EventTest event_period;
511+
static EventQueue period_tests_queue;
512+
static long update_counter = 0;
513+
514+
void handler()
515+
{
516+
update_counter ++;
517+
}
512518

513519
void event_period_tests()
514520
{
515-
EventQueue queue;
521+
// Test a non periodic event ie dispatched only once
516522

517-
// Test a non periodic event ie dispatched only once
523+
Event<void()> event1(&period_tests_queue, handler);
518524

519-
Event<void(int)> event1(&queue, &event_period::f0);
520-
event1.delay(100ms);
521-
event1.period(Event::non_periodic);
522-
event1.post()
525+
event1.delay(10ms);
526+
event1.period(events::non_periodic);
527+
event1.post();
523528

524-
queue.dispatch(800);
529+
period_tests_queue.dispatch(80);
525530

526-
// Wait 1000ms and check the event execution status
527-
wait_us(1000 * 1000)
531+
// Wait 100ms and check the event execution status
532+
wait_us(100 * 1000);
528533

529534
// Event should only have been dispatched once and thus counter
530535
// should be 1
531-
TEST_ASSERT_EQUAL(1, event_period.counter);
536+
TEST_ASSERT_EQUAL(1, update_counter);
532537

533-
// Test an event with an invalid -ve period.
538+
// Test an event with an invalid negative period value.
539+
540+
update_counter = 0;
541+
542+
Event<void()> event2(&period_tests_queue, handler);
534543

535-
event_period.counter = 0;
536-
Event<void(int)> event2(&queue, &event_period::f0);
537-
event2.delay(100ms);
544+
event2.delay(10ms);
538545
event2.period(-10ms);
539-
event2.post()
546+
event2.post();
540547

541-
queue.dispatch(800);
548+
period_tests_queue.dispatch(80);
542549

543-
// Wait 1000ms and check the event execution status
544-
wait_us(1000 * 1000)
550+
// Wait 100ms and check the event execution status
551+
wait_us(100 * 1000);
545552

546553
// Event should default to non_periodic and thus only have been
547554
// dispatched once. Counter should be 1.
548-
TEST_ASSERT_EQUAL(1, event_period.counter);
555+
TEST_ASSERT_EQUAL(1, update_counter);
549556

550557
// Test an event with a zero period.
551558

552-
event_period.counter = 0;
553-
Event<void(int)> event3(&queue, &event_period::f0);
554-
event3.delay(100ms);
559+
update_counter = 0;
560+
561+
Event<void()> event3(&period_tests_queue, handler);
562+
563+
event3.delay(10ms);
555564
event3.period(0ms);
556-
event3.post()
565+
event3.post();
557566

558-
queue.dispatch(800);
567+
period_tests_queue.dispatch(80);
559568

560-
// Wait 1000ms and check the event execution status
561-
wait_us(1000 * 1000)
569+
// Wait 100ms and check the event execution status
570+
wait_us(100 * 1000);
562571

563572
// Event should default to non_periodic and thus only have been
564573
// dispatched once. Counter should be 1.
565-
TEST_ASSERT_EQUAL(1, event_period.counter);
574+
TEST_ASSERT_EQUAL(1, update_counter);
566575

567576
// 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()
577+
update_counter = 0;
578+
579+
Event<void()> event4(&period_tests_queue, handler);
580+
581+
event4.delay(10ms);
582+
event4.period(20ms);
583+
event4.post();
573584

574-
queue.dispatch(800);
585+
period_tests_queue.dispatch(80);
575586

576-
// Wait 1000ms and check the event execution status
577-
wait_us(1000 * 1000)
587+
// Wait 100ms and check the event execution status
588+
wait_us(100 * 1000);
578589

579-
// The event should be first dispatched after 100ms and then
580-
// every subsequent 200ms until the dispatcher has completed.
590+
// The event should be first dispatched after 10ms and then
591+
// every subsequent 20ms until the dispatcher has completed.
581592
// Thus the counter should be incremented after :
582-
// 100ms, 300ms, 500ms and 700ms
583-
TEST_ASSERT_EQUAL(4, event_period.counter);
593+
// 10ms, 30ms, 50ms and 70ms
594+
TEST_ASSERT_EQUAL(4, update_counter);
584595

585596
}
586597

@@ -611,7 +622,7 @@ const Case cases[] = {
611622

612623
Case("Testing time_left", time_left_test),
613624
Case("Testing mixed dynamic & static events queue", mixed_dynamic_static_events_queue_test),
614-
Case("Testing static events queue", static_events_queue_test)
625+
Case("Testing static events queue", static_events_queue_test),
615626
Case("Testing event period values", event_period_tests)
616627
};
617628

0 commit comments

Comments
 (0)