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
pull/14087/head
adbridge 2021-01-06 15:43:14 +00:00
parent 6b2a0fe1d4
commit c0a57ba3b9
2 changed files with 79 additions and 61 deletions

View File

@ -20,6 +20,7 @@
#include <utility>
#include "events/EventQueue.h"
#include "platform/mbed_assert.h"
#include "platform/mbed_error.h"
namespace events {
/** \defgroup events-public-api Events
@ -27,6 +28,8 @@ namespace events {
* @{
*/
static constexpr std::chrono::duration<int, std::milli> non_periodic{-1};
/** Event
*
* Representation of an event for fine-grain dispatch control
@ -47,7 +50,6 @@ template <typename... ArgTs>
class Event<void(ArgTs...)> {
public:
using duration = std::chrono::duration<int, std::milli>;
static constexpr duration non_periodic{-1};
/** Create an event
*
@ -68,7 +70,7 @@ public:
_event->equeue = &q->_equeue;
_event->id = 0;
_event->delay = duration(0);
_event->period = non_periodic;
_event->period = events::non_periodic;
_event->post = &Event::event_post<F>;
_event->dtor = &Event::event_dtor<F>;
@ -115,6 +117,8 @@ public:
}
}
/** Configure the delay of an event
*
* @param d Delay (in milliseconds) before dispatching the event, expressed as a Chrono duration.
@ -147,13 +151,16 @@ public:
*/
void period(duration p)
{
MBED_ASSERT(p > duration(0) || p == non_periodic);
if (_event) {
if (p > duration(0)) {
_event->period = p;
}
else {
_event->period = non_periodic;
if (p != events::non_periodic) {
MBED_WARNING(MBED_ERROR_INVALID_ARGUMENT,
"Invalid period specified, defaulting to non_periodic.");
}
_event->period = events::non_periodic;
}
}
}

View File

@ -93,12 +93,12 @@ void simple_posts_test##i() { \
TEST_ASSERT(touched); \
\
touched = false; \
queue.call_in(1, func##i,##__VA_ARGS__); \
queue.call_in(1ms, func##i,##__VA_ARGS__); \
queue.dispatch(2); \
TEST_ASSERT(touched); \
\
touched = false; \
queue.call_every(1, func##i,##__VA_ARGS__); \
queue.call_every(1ms, func##i,##__VA_ARGS__); \
queue.dispatch(2); \
TEST_ASSERT(touched); \
}
@ -126,7 +126,7 @@ void call_in_test()
for (int i = 0; i < N; i++) {
tickers[i].start();
queue.call_in((i + 1) * 100, time_func, &tickers[i], (i + 1) * 100);
queue.call_in((i + 1) * 100ms, time_func, &tickers[i], (i + 1) * 100);
}
queue.dispatch(N * 100);
@ -141,7 +141,7 @@ void call_every_test()
for (int i = 0; i < N; i++) {
tickers[i].start();
queue.call_every((i + 1) * 100, time_func, &tickers[i], (i + 1) * 100);
queue.call_every((i + 1) * 100ms, time_func, &tickers[i], (i + 1) * 100);
}
queue.dispatch(N * 100);
@ -172,7 +172,7 @@ void cancel_test1()
int ids[N];
for (int i = 0; i < N; i++) {
ids[i] = queue.call_in(1000, no);
ids[i] = queue.call_in(1000ms, no);
}
for (int i = N - 1; i >= 0; i--) {
@ -308,12 +308,12 @@ void time_left_test()
EventQueue queue(TEST_EQUEUE_SIZE);
// Enque check events
TEST_ASSERT(queue.call_in(50, check_time_left, &queue, 0, 100 - 50));
TEST_ASSERT(queue.call_in(200, check_time_left, &queue, 1, 200 - 200));
TEST_ASSERT(queue.call_in(50ms, check_time_left, &queue, 0, 100 - 50));
TEST_ASSERT(queue.call_in(200ms, check_time_left, &queue, 1, 200 - 200));
// Enque events to be checked
timeleft_events[0] = queue.call_in(100, time_left, &queue, 0);
timeleft_events[1] = queue.call_in(200, time_left, &queue, 1);
timeleft_events[0] = queue.call_in(100ms, time_left, &queue, 0);
timeleft_events[1] = queue.call_in(200ms, time_left, &queue, 1);
TEST_ASSERT(timeleft_events[0]);
TEST_ASSERT(timeleft_events[1]);
@ -373,18 +373,18 @@ void mixed_dynamic_static_events_queue_test()
EventTest e1_test;
Event<void()> e1 = queue.event(&e1_test, &EventTest::f0);
e1.delay(10);
e1.period(10);
e1.delay(10ms);
e1.period(10ms);
int id1 = e1.post();
TEST_ASSERT_NOT_EQUAL(0, id1);
EventTest e2_test;
Event<void()> e2 = queue.event(&e2_test, &EventTest::f1, 3);
e2.period(10);
e2.period(10ms);
int id2 = e2.post();
TEST_ASSERT_NOT_EQUAL(0, id2);
EventTest e3_test;
Event<void()> e3 = queue.event(&e3_test, &EventTest::f5, 1, 2, 3, 4, 5);
e3.period(10);
e3.period(10ms);
int id3 = e3.post();
TEST_ASSERT_NOT_EQUAL(0, id3);
@ -508,79 +508,90 @@ void static_events_queue_test()
TEST_ASSERT_EQUAL(15, test4.counter);
}
static EventTest event_period;
static EventQueue period_tests_queue;
static long update_counter = 0;
void handler()
{
update_counter ++;
}
void event_period_tests()
{
EventQueue queue;
// Test a non periodic event ie dispatched only once
// Test a non periodic event ie dispatched only once
Event<void()> event1(&period_tests_queue, handler);
Event<void(int)> event1(&queue, &event_period::f0);
event1.delay(100ms);
event1.period(Event::non_periodic);
event1.post()
event1.delay(10ms);
event1.period(events::non_periodic);
event1.post();
queue.dispatch(800);
period_tests_queue.dispatch(80);
// Wait 1000ms and check the event execution status
wait_us(1000 * 1000)
// Wait 100ms and check the event execution status
wait_us(100 * 1000);
// Event should only have been dispatched once and thus counter
// should be 1
TEST_ASSERT_EQUAL(1, event_period.counter);
TEST_ASSERT_EQUAL(1, update_counter);
// Test an event with an invalid -ve period.
// Test an event with an invalid negative period value.
event_period.counter = 0;
Event<void(int)> event2(&queue, &event_period::f0);
event2.delay(100ms);
update_counter = 0;
Event<void()> event2(&period_tests_queue, handler);
event2.delay(10ms);
event2.period(-10ms);
event2.post()
event2.post();
queue.dispatch(800);
period_tests_queue.dispatch(80);
// Wait 1000ms and check the event execution status
wait_us(1000 * 1000)
// Wait 100ms and check the event execution status
wait_us(100 * 1000);
// Event should default to non_periodic and thus only have been
// dispatched once. Counter should be 1.
TEST_ASSERT_EQUAL(1, event_period.counter);
TEST_ASSERT_EQUAL(1, update_counter);
// Test an event with a zero period.
event_period.counter = 0;
Event<void(int)> event3(&queue, &event_period::f0);
event3.delay(100ms);
event3.period(0ms);
event3.post()
queue.dispatch(800);
update_counter = 0;
// Wait 1000ms and check the event execution status
wait_us(1000 * 1000)
Event<void()> event3(&period_tests_queue, handler);
event3.delay(10ms);
event3.period(0ms);
event3.post();
period_tests_queue.dispatch(80);
// Wait 100ms and check the event execution status
wait_us(100 * 1000);
// Event should default to non_periodic and thus only have been
// dispatched once. Counter should be 1.
TEST_ASSERT_EQUAL(1, event_period.counter);
TEST_ASSERT_EQUAL(1, update_counter);
// Test a periodic event ie dispatched a number of times
event_period.counter = 0;
Event<void(int)> event4(&queue, &event_period::f0);
event4.delay(100ms);
event4.period(200ms);
event4.post()
queue.dispatch(800);
update_counter = 0;
// Wait 1000ms and check the event execution status
wait_us(1000 * 1000)
Event<void()> event4(&period_tests_queue, handler);
event4.delay(10ms);
event4.period(20ms);
event4.post();
period_tests_queue.dispatch(80);
// Wait 100ms and check the event execution status
wait_us(100 * 1000);
// The event should be first dispatched after 100ms and then
// every subsequent 200ms until the dispatcher has completed.
// The event should be first dispatched after 10ms and then
// every subsequent 20ms until the dispatcher has completed.
// Thus the counter should be incremented after :
// 100ms, 300ms, 500ms and 700ms
TEST_ASSERT_EQUAL(4, event_period.counter);
// 10ms, 30ms, 50ms and 70ms
TEST_ASSERT_EQUAL(4, update_counter);
}
@ -611,7 +622,7 @@ const Case cases[] = {
Case("Testing time_left", time_left_test),
Case("Testing mixed dynamic & static events queue", mixed_dynamic_static_events_queue_test),
Case("Testing static events queue", static_events_queue_test)
Case("Testing static events queue", static_events_queue_test),
Case("Testing event period values", event_period_tests)
};